From f3be9325fa45d5dec1e716a4a9ffeb6e0cf56b22 Mon Sep 17 00:00:00 2001 From: Luis Metzger <148913056+luisgmetzger@users.noreply.github.com> Date: Wed, 2 Apr 2025 10:19:20 -0600 Subject: [PATCH] SRCH-5787: Replace existing searchgov style gem with rubocop gem (#369) * Replace existing searchgov style gem with rubocop gem. Add configurations for rubocop. Linter. --------- Co-authored-by: Luis Metzger --- .codeclimate.yml | 18 ++---- .rubocop.yml | 28 +++------ .rubocop_todo.yml | 19 ++++++ Gemfile | 24 +++---- Gemfile.lock | 157 +++++++++++++++++++++++++++------------------- README.md | 33 ++++++++++ 6 files changed, 172 insertions(+), 107 deletions(-) create mode 100644 .rubocop_todo.yml diff --git a/.codeclimate.yml b/.codeclimate.yml index 40d6ef06..48bf7cdd 100644 --- a/.codeclimate.yml +++ b/.codeclimate.yml @@ -1,10 +1,4 @@ version: '2' -# Work around CodeClimate's inability to use Rubocop's inherit_gem directive: -# https://github.com/codeclimate/codeclimate-rubocop/issues/71#issuecomment-635055223 -prepare: - fetch: - - url: https://raw.githubusercontent.com/GSA/searchgov_style/main/.default.yml - path: .rubocop-https---raw-githubusercontent-com-GSA-searchgov-style-main--default-yml plugins: brakeman: enabled: true @@ -16,17 +10,17 @@ plugins: enabled: true config: languages: - - ruby + - ruby exclude_patterns: - - "spec/" + - "spec/" eslint: enabled: true fixme: enabled: true rubocop: enabled: true - channel: rubocop-1-48-1 + channel: rubocop-1-65-0 exclude_patterns: -- bin/ -- vendor/ -- coverage/ + - bin/ + - vendor/ + - coverage/ \ No newline at end of file diff --git a/.rubocop.yml b/.rubocop.yml index 4bd418a0..eb4ebc86 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,22 +1,14 @@ -# Work around CodeClimate's inability to use Rubocop's inherit_gem directive: -# https://github.com/codeclimate/codeclimate-rubocop/issues/71#issuecomment-635055223 -inherit_from: - - https://raw.githubusercontent.com/GSA/searchgov_style/main/.default.yml +inherit_from: .rubocop_todo.yml + +AllCops: + NewCops: enable + +require: + - rubocop-performance + - rubocop-rails + - rubocop-rake + - rubocop-rspec inherit_mode: merge: - Exclude - -Metrics/ClassLength: - Exclude: - # Much of this class is made up of a long #body method which mimics the structure - # of a complex Elasticsearch query using the Elasticsearch DSL. (Method/Block ength cops are already - # disabled inline for that specific method.) - - app/classes/document_query.rb - -Style/MethodCallWithArgsParentheses: - Exclude: - # Methods in this file uses the the Elasticsearch DSL and, as such, they fall under the rule "Always omit - # parentheses for methods that are part of an internal DSL": - # https://rubystyle.guide/#methods-that-are-part-of-an-internal-dsl - - app/classes/document_query.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 00000000..7839d190 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,19 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2025-04-01 21:22:59 UTC using RuboCop version 1.75.1. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 1 +RSpec/BeforeAfterAll: + Exclude: + - 'spec/requests/api/v1/documents_spec.rb' + +# Offense count: 2 +# Configuration parameters: Prefixes, AllowedPatterns. +# Prefixes: when, with, without +RSpec/ContextWording: + Exclude: + - 'spec/classes/query_parser_spec.rb' diff --git a/Gemfile b/Gemfile index eb668cf2..2ea207d1 100644 --- a/Gemfile +++ b/Gemfile @@ -1,16 +1,18 @@ +# frozen_string_literal: true source 'https://rubygems.org' -gem "elasticsearch-persistence", '~> 6.0' -gem "rails_semantic_logger", "~> 4.14" gem 'dotenv', '~> 3.1' gem 'elasticsearch', '~> 6.0' gem 'elasticsearch-dsl', '~> 0.1.9' +gem 'elasticsearch-persistence', '~> 6.0' gem 'grape', '~> 1.7.0' gem 'jbuilder', '~> 2.7' gem 'newrelic_rpm', '~> 9.10' gem 'puma', '~> 5.6' +gem 'rack', '~> 2.2.8' gem 'rack-cors', '~> 1.0.5' gem 'rails', '~> 7.1.0' +gem 'rails_semantic_logger', '~> 4.14' gem 'rake', '~> 13.0.0' gem 'typhoeus', '~> 1.4.0' gem 'virtus', '~> 1.0' # Virtus is no longer supported. Consider replacing with ActiveModel::Attributes @@ -18,26 +20,24 @@ gem 'virtus', '~> 1.0' # Virtus is no longer supported. Consi group :development, :test do gem 'awesome_print', '~> 1.8' #To enable in Pry: https://github.com/awesome-print/awesome_print#pry-integration gem 'capistrano', require: false + gem 'capistrano3-puma', require: false gem 'capistrano-newrelic', require: false gem 'capistrano-rails', require: false gem 'capistrano-rbenv', require: false - gem 'capistrano3-puma', '~> 5.2.0', require: false gem 'debug' gem 'listen' gem 'pry-byebug', '~> 3.4' gem 'pry-rails', '~> 0.3' gem 'rspec-rails', '~> 3.7' -end - -group :development do - # Bumping searchgov_style? Be sure to update the Rubocop channel - # in .codeclimate.yml to match the channel in searchgov_style - # https://github.com/GSA/searchgov_style/blob/main/.codeclimate.yml - gem 'searchgov_style', '~> 0.1', require: false + gem 'rubocop', require: false + gem 'rubocop-performance', require: false + gem 'rubocop-rails', require: false + gem 'rubocop-rake', require: false + gem 'rubocop-rspec', require: false end group :test do - gem 'simplecov', '~> 0.13.0', require: false - gem "codeclimate-test-reporter", '~> 1.0.8', require: nil + gem 'codeclimate-test-reporter', '~> 1.0.8', require: nil gem 'shoulda', '~> 4.0' + gem 'simplecov', '~> 0.13.0', require: false end diff --git a/Gemfile.lock b/Gemfile.lock index 67235670..48e2f76c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -80,7 +80,7 @@ GEM tzinfo (~> 2.0) airbrussh (1.5.3) sshkit (>= 1.6.1, != 1.7.0) - ast (2.4.2) + ast (2.4.3) awesome_print (1.9.2) axiom-types (0.1.1) descendants_tracker (~> 0.0.4) @@ -90,7 +90,7 @@ GEM benchmark (0.4.0) bigdecimal (3.1.9) builder (3.3.0) - byebug (11.1.3) + byebug (12.0.0) capistrano (3.19.2) airbrussh (>= 1.0.0) i18n @@ -125,7 +125,7 @@ GEM reline (>= 0.3.8) descendants_tracker (0.0.4) thread_safe (~> 0.3, >= 0.3.1) - diff-lcs (1.5.1) + diff-lcs (1.6.1) docile (1.1.5) dotenv (3.1.7) drb (2.2.1) @@ -139,7 +139,7 @@ GEM concurrent-ruby (~> 1.0) dry-core (~> 1.1) zeitwerk (~> 2.6) - dry-types (1.8.1) + dry-types (1.8.2) bigdecimal (~> 3.0) concurrent-ruby (~> 1.0) dry-core (~> 1.0) @@ -192,7 +192,14 @@ GEM faraday-patron (1.0.0) faraday-rack (1.0.0) faraday-retry (1.0.3) - ffi (1.17.1) + ffi (1.17.1-aarch64-linux-gnu) + ffi (1.17.1-aarch64-linux-musl) + ffi (1.17.1-arm-linux-gnu) + ffi (1.17.1-arm-linux-musl) + ffi (1.17.1-arm64-darwin) + ffi (1.17.1-x86_64-darwin) + ffi (1.17.1-x86_64-linux-gnu) + ffi (1.17.1-x86_64-linux-musl) globalid (1.2.1) activesupport (>= 6.1) grape (1.7.1) @@ -207,18 +214,20 @@ GEM concurrent-ruby (~> 1.0) ice_nine (0.11.2) io-console (0.8.0) - irb (1.15.0) + irb (1.15.1) pp (>= 0.6.0) rdoc (>= 4.0.0) reline (>= 0.4.2) jbuilder (2.13.0) actionview (>= 5.0.0) activesupport (>= 5.0.0) - json (2.9.1) + json (2.10.2) + language_server-protocol (3.17.0.4) + lint_roller (1.1.0) listen (3.9.0) rb-fsevent (~> 0.10, >= 0.10.3) rb-inotify (~> 0.9, >= 0.9.10) - logger (1.6.5) + logger (1.7.0) loofah (2.24.0) crass (~> 1.0.2) nokogiri (>= 1.12.0) @@ -230,8 +239,7 @@ GEM marcel (1.0.4) method_source (1.1.0) mini_mime (1.1.5) - mini_portile2 (2.8.8) - minitest (5.25.4) + minitest (5.25.5) multi_json (1.15.0) multipart-post (2.4.1) mustermann (3.0.3) @@ -239,39 +247,53 @@ GEM mustermann-grape (1.0.2) mustermann (>= 1.0.0) mutex_m (0.3.0) - net-imap (0.5.5) + net-imap (0.5.6) date net-protocol net-pop (0.1.2) net-protocol net-protocol (0.2.2) timeout - net-scp (4.0.0) + net-scp (4.1.0) net-ssh (>= 2.6.5, < 8.0.0) net-sftp (4.0.0) net-ssh (>= 5.0.0, < 8.0.0) - net-smtp (0.5.0) + net-smtp (0.5.1) net-protocol net-ssh (7.3.0) - newrelic_rpm (9.16.1) + newrelic_rpm (9.17.0) nio4r (2.7.4) - nokogiri (1.18.2) - mini_portile2 (~> 2.8.2) + nokogiri (1.18.7-aarch64-linux-gnu) + racc (~> 1.4) + nokogiri (1.18.7-aarch64-linux-musl) + racc (~> 1.4) + nokogiri (1.18.7-arm-linux-gnu) + racc (~> 1.4) + nokogiri (1.18.7-arm-linux-musl) + racc (~> 1.4) + nokogiri (1.18.7-arm64-darwin) + racc (~> 1.4) + nokogiri (1.18.7-x86_64-darwin) + racc (~> 1.4) + nokogiri (1.18.7-x86_64-linux-gnu) + racc (~> 1.4) + nokogiri (1.18.7-x86_64-linux-musl) racc (~> 1.4) ostruct (0.6.1) parallel (1.26.3) - parser (3.3.7.0) + parser (3.3.7.4) ast (~> 2.4.1) racc pp (0.6.2) prettyprint prettyprint (0.2.0) - pry (0.14.2) + prism (1.4.0) + pry (0.15.2) coderay (~> 1.1) method_source (~> 1.0) - pry-byebug (3.10.1) - byebug (~> 11.0) - pry (>= 0.13, < 0.15) + pry-byebug (3.11.0) + byebug (~> 12.0) + pry (>= 0.13, < 0.16) pry-rails (0.3.11) pry (>= 0.13.0) psych (5.2.3) @@ -280,7 +302,7 @@ GEM puma (5.6.9) nio4r (~> 2.0) racc (1.8.1) - rack (2.2.10) + rack (2.2.13) rack-accept (0.4.5) rack (>= 0.4) rack-cors (1.0.6) @@ -330,12 +352,11 @@ GEM rb-fsevent (0.11.2) rb-inotify (0.11.1) ffi (~> 1.0) - rdoc (6.11.0) + rdoc (6.13.1) psych (>= 4.0.0) regexp_parser (2.10.0) reline (0.6.0) io-console (~> 0.5) - rexml (3.4.0) rspec-core (3.9.3) rspec-support (~> 3.9.3) rspec-expectations (3.9.4) @@ -353,47 +374,38 @@ GEM rspec-mocks (~> 3.9.0) rspec-support (~> 3.9.0) rspec-support (3.9.4) - rubocop (1.48.1) + rubocop (1.75.1) json (~> 2.3) + language_server-protocol (~> 3.17.0.2) + lint_roller (~> 1.1.0) parallel (~> 1.10) - parser (>= 3.2.0.0) + parser (>= 3.3.0.2) rainbow (>= 2.2.2, < 4.0) - regexp_parser (>= 1.8, < 3.0) - rexml (>= 3.2.5, < 4.0) - rubocop-ast (>= 1.26.0, < 2.0) + regexp_parser (>= 2.9.3, < 3.0) + rubocop-ast (>= 1.43.0, < 2.0) ruby-progressbar (~> 1.7) - unicode-display_width (>= 2.4.0, < 3.0) - rubocop-ast (1.37.0) - parser (>= 3.3.1.0) - rubocop-capybara (2.21.0) - rubocop (~> 1.41) - rubocop-factory_bot (2.26.0) - rubocop (~> 1.41) - rubocop-performance (1.23.1) - rubocop (>= 1.48.1, < 2.0) - rubocop-ast (>= 1.31.1, < 2.0) - rubocop-rails (2.25.1) + unicode-display_width (>= 2.4.0, < 4.0) + rubocop-ast (1.43.0) + parser (>= 3.3.7.2) + prism (~> 1.4) + rubocop-performance (1.25.0) + lint_roller (~> 1.1) + rubocop (>= 1.75.0, < 2.0) + rubocop-ast (>= 1.38.0, < 2.0) + rubocop-rails (2.31.0) activesupport (>= 4.2.0) + lint_roller (~> 1.1) rack (>= 1.1) - rubocop (>= 1.33.0, < 2.0) - rubocop-ast (>= 1.31.1, < 2.0) - rubocop-rake (0.6.0) - rubocop (~> 1.0) - rubocop-rspec (2.31.0) - rubocop (~> 1.40) - rubocop-capybara (~> 2.17) - rubocop-factory_bot (~> 2.22) - rubocop-rspec_rails (~> 2.28) - rubocop-rspec_rails (2.29.0) - rubocop (~> 1.40) + rubocop (>= 1.75.0, < 2.0) + rubocop-ast (>= 1.38.0, < 2.0) + rubocop-rake (0.7.1) + lint_roller (~> 1.1) + rubocop (>= 1.72.1) + rubocop-rspec (3.5.0) + lint_roller (~> 1.1) + rubocop (~> 1.72, >= 1.72.1) ruby-progressbar (1.13.0) ruby2_keywords (0.0.5) - searchgov_style (0.1.25) - rubocop (= 1.48.1) - rubocop-performance (~> 1.9) - rubocop-rails (~> 2.9) - rubocop-rake (~> 0.5) - rubocop-rspec (~> 2.5) securerandom (0.4.1) semantic_logger (4.16.1) concurrent-ruby (~> 1.0) @@ -408,13 +420,14 @@ GEM json (>= 1.8, < 3) simplecov-html (~> 0.10.0) simplecov-html (0.10.2) - sshkit (1.23.2) + sshkit (1.24.0) base64 + logger net-scp (>= 1.1.2) net-sftp (>= 2.1.2) net-ssh (>= 2.8.0) ostruct - stringio (3.1.2) + stringio (3.1.6) thor (1.3.2) thread_safe (0.3.6) timeout (0.4.3) @@ -422,7 +435,9 @@ GEM ethon (>= 0.9.0) tzinfo (2.0.6) concurrent-ruby (~> 1.0) - unicode-display_width (2.6.0) + unicode-display_width (3.1.4) + unicode-emoji (~> 4.0, >= 4.0.4) + unicode-emoji (4.0.4) virtus (1.0.5) axiom-types (~> 0.1) coercible (~> 1.0) @@ -433,10 +448,17 @@ GEM base64 websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) - zeitwerk (2.6.18) + zeitwerk (2.7.2) PLATFORMS - ruby + aarch64-linux-gnu + aarch64-linux-musl + arm-linux-gnu + arm-linux-musl + arm64-darwin + x86_64-darwin + x86_64-linux-gnu + x86_64-linux-musl DEPENDENCIES awesome_print (~> 1.8) @@ -444,7 +466,7 @@ DEPENDENCIES capistrano-newrelic capistrano-rails capistrano-rbenv - capistrano3-puma (~> 5.2.0) + capistrano3-puma codeclimate-test-reporter (~> 1.0.8) debug dotenv (~> 3.1) @@ -458,16 +480,21 @@ DEPENDENCIES pry-byebug (~> 3.4) pry-rails (~> 0.3) puma (~> 5.6) + rack (~> 2.2.8) rack-cors (~> 1.0.5) rails (~> 7.1.0) rails_semantic_logger (~> 4.14) rake (~> 13.0.0) rspec-rails (~> 3.7) - searchgov_style (~> 0.1) + rubocop + rubocop-performance + rubocop-rails + rubocop-rake + rubocop-rspec shoulda (~> 4.0) simplecov (~> 0.13.0) typhoeus (~> 1.4.0) virtus (~> 1.0) BUNDLED WITH - 2.5.9 + 2.6.3 diff --git a/README.md b/README.md index 75c2894c..07bf197f 100644 --- a/README.md +++ b/README.md @@ -117,3 +117,36 @@ $ docker-compose up elasticsearch7 $ bundle exec rake i14y:setup $ rake ``` + +## Code Quality + +We use [Rubocop](https://rubocop.org/) for static code analysis. Settings specific to i14y are configured via [.rubocop.yml](.rubocop.yml). + +### Running RuboCop Locally + +Basic commands you should frequently use: + +- **Generate or update the RuboCop TODO file**. Use this when RuboCop identifies many issues: +```bash +bundle exec rubocop --auto-gen-config +``` +- **Autocorrect easy-to-fix offenses** (safe corrections only): +```bash +bundle exec rubocop -a +``` +- **Autocorrect all possible offenses, including some more complex cases** (use with caution, review changes carefully): +```bash +bundle exec rubocop -A +``` +- **Disable offenses that cannot be automatically corrected** when running autocorrections. Useful if you'd like to quickly apply auto-fixes without manually addressing harder issues immediately: +```bash +bundle exec rubocop -a --disable-uncorrectable +``` + +or + +```bash +bundle exec rubocop -A --disable-uncorrectable +``` + +It is recommended to always review diff changes after running autocorrection commands to ensure code correctness and maintainability. \ No newline at end of file