From 14b0259d67a215b20baa0b299c6eac77f44f2bea Mon Sep 17 00:00:00 2001 From: Damir Sultanbekov Date: Tue, 2 Apr 2024 15:24:50 +0500 Subject: [PATCH 1/7] added pronto, rubocop and haml_lint to project --- Gemfile | 8 ++++++- Gemfile.lock | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index d24cdb29..dda5a17e 100644 --- a/Gemfile +++ b/Gemfile @@ -121,13 +121,19 @@ group :development do gem 'capistrano3-puma' gem 'listen' gem 'derailed_benchmarks' -# gem 'bullet' # for suggestions to add/remove eager loading + # gem 'bullet' # for suggestions to add/remove eager loading gem 'stackprof' gem 'active_record_query_trace' gem 'immigrant' gem 'ruby-prof' # for profiling gem 'ed25519' gem 'bcrypt_pbkdf' + + gem 'haml_lint', '~> 0.57.0' + gem 'pronto', '~> 0.11.2' + gem 'pronto-haml', '~> 0.11.1' + gem 'pronto-rubocop', '~> 0.11.5' + gem 'rubocop', '~> 1.62.1' end group :test, :development do diff --git a/Gemfile.lock b/Gemfile.lock index 644c6fcd..09eac921 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -109,6 +109,8 @@ GEM minitest (>= 5.1) tzinfo (~> 2.0) zeitwerk (~> 2.3) + addressable (2.8.6) + public_suffix (>= 2.0.2, < 6.0) ahoy_matey (5.0.2) activesupport (>= 6.1) device_detector (>= 1) @@ -116,6 +118,7 @@ GEM airbrussh (1.5.1) sshkit (>= 1.6.1, != 1.7.0) ansi (1.5.0) + ast (2.4.2) aws-eventstream (1.3.0) aws-partitions (1.895.0) aws-sdk-core (3.191.3) @@ -285,6 +288,9 @@ GEM rubyzip (> 1.1.1, < 2.4) get_process_mem (0.2.7) ffi (~> 1.0) + gitlab (4.19.0) + httparty (~> 0.20) + terminal-table (>= 1.5.1) globalid (1.2.1) activesupport (>= 6.1) grape (1.6.0) @@ -311,6 +317,12 @@ GEM activesupport (>= 5.1) haml (>= 4.0.6) railties (>= 5.1) + haml_lint (0.57.0) + haml (>= 5.0) + parallel (~> 1.10) + rainbow + rubocop (>= 1.0) + sysexits (~> 1.1) hashie (5.0.0) heapy (0.2.0) thor @@ -320,6 +332,9 @@ GEM http-accept (1.7.0) http-cookie (1.0.5) domain_name (~> 0.5) + httparty (0.21.0) + mini_mime (>= 1.0.0) + multi_xml (>= 0.5.2) i18n (1.14.4) concurrent-ruby (~> 1.0) image_processing (1.12.2) @@ -360,6 +375,7 @@ GEM activerecord kaminari-core (= 1.2.2) kaminari-core (1.2.2) + language_server-protocol (3.17.0.3) libv8-node (18.16.0.0) libv8-node (18.16.0.0-x86_64-linux) listen (3.9.0) @@ -437,6 +453,9 @@ GEM snaky_hash (~> 2.0) version_gem (~> 1.1) observer (0.1.2) + octokit (7.2.0) + faraday (>= 1, < 3) + sawyer (~> 0.9) omniauth (2.1.2) hashie (>= 3.4.6) rack (>= 2.2.3) @@ -464,15 +483,34 @@ GEM paper_trail (15.1.0) activerecord (>= 6.1) request_store (~> 1.4) + parallel (1.24.0) + parser (3.3.0.5) + ast (~> 2.4.1) + racc pcaprub (0.13.1) pkg-config (1.5.6) project-honeypot2 (0.1.3) net-dns2 + pronto (0.11.2) + gitlab (>= 4.4.0, < 5.0) + httparty (>= 0.13.7, < 1.0) + octokit (>= 4.7.0, < 8.0) + rainbow (>= 2.2, < 4.0) + rexml (>= 3.2.5, < 4.0) + rugged (>= 0.23.0, < 2.0) + thor (>= 0.20.3, < 2.0) + pronto-haml (0.11.1) + haml_lint (~> 0.23) + pronto (~> 0.11.0) + pronto-rubocop (0.11.5) + pronto (~> 0.11.0) + rubocop (>= 0.63.1, < 2.0) property_sets (3.12.0) activerecord (>= 6.0, < 7.2) json psych (5.1.2) stringio + public_suffix (5.0.5) puma (6.4.2) nio4r (~> 2.0) puma-daemon (0.3.2) @@ -534,6 +572,7 @@ GEM method_source rake (>= 12.2) thor (~> 1.0) + rainbow (3.1.1) rake (13.1.0) rb-fsevent (0.11.2) rb-inotify (0.10.1) @@ -585,7 +624,21 @@ GEM rswag-ui (2.13.0) actionpack (>= 3.1, < 7.2) railties (>= 3.1, < 7.2) + rubocop (1.62.1) + json (~> 2.3) + language_server-protocol (>= 3.17.0) + parallel (~> 1.10) + 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.31.1, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.31.2) + parser (>= 3.3.0.4) ruby-prof (1.7.0) + ruby-progressbar (1.13.0) ruby-statistics (3.0.2) ruby-vips (2.2.1) ffi (~> 1.12) @@ -593,6 +646,7 @@ GEM rubyzip (2.3.2) rufus-scheduler (3.9.1) fugit (~> 1.1, >= 1.1.6) + rugged (1.7.2) rvm1-capistrano3 (1.4.0) capistrano (~> 3.0) sshkit (>= 1.2) @@ -607,6 +661,9 @@ GEM sprockets (> 3.0) sprockets-rails tilt + sawyer (0.9.2) + addressable (>= 2.3.5) + faraday (>= 0.17.3, < 3) selenium-webdriver (4.18.1) base64 (~> 0.2) rexml (~> 3.2, >= 3.2.5) @@ -646,7 +703,10 @@ GEM net-ssh (>= 2.8.0) stackprof (0.2.26) stringio (3.1.0) + sysexits (1.2.0) temple (0.10.3) + terminal-table (3.0.2) + unicode-display_width (>= 1.1.1, < 3) terrapin (1.0.1) climate_control thor (1.3.1) @@ -658,6 +718,7 @@ GEM concurrent-ruby (~> 1.0) uglifier (4.2.0) execjs (>= 0.3.0, < 3) + unicode-display_width (2.5.0) version_gem (1.1.3) watir (6.19.1) regexp_parser (>= 1.2, < 3) @@ -716,6 +777,7 @@ DEPENDENCIES grape-swagger-entity (~> 0.5.1) haml haml-rails + haml_lint (~> 0.57.0) hebrew (>= 0.2.1) hebruby htmlentities @@ -741,6 +803,9 @@ DEPENDENCIES pandoc-ruby paper_trail project-honeypot2 (>= 0.1.3) + pronto (~> 0.11.2) + pronto-haml (~> 0.11.1) + pronto-rubocop (~> 0.11.5) property_sets (>= 3.7.1) puma puma-daemon @@ -758,6 +823,7 @@ DEPENDENCIES rspec-rails (~> 5.0.2) rswag-api rswag-ui + rubocop (~> 1.62.1) ruby-prof rufus-scheduler rvm1-capistrano3 From 76065152b7d176116c8d9e6ebfa226c3e1eb0a3d Mon Sep 17 00:00:00 2001 From: Damir Sultanbekov Date: Tue, 2 Apr 2024 16:29:06 +0500 Subject: [PATCH 2/7] added rubocop-rails, rubocop-rspec and rubocop-factory_bot linters --- .rubocop.yml | 39 ++++++++++++++++++++++++++++++ Gemfile | 13 +++++++--- Gemfile.lock | 30 ++++++++++++++++++++--- config/environments/development.rb | 9 +++++++ 4 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 .rubocop.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 00000000..5fd40c57 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,39 @@ +AllCops: + TargetRubyVersion: 3.2 + NewCops: enable + Exclude: + - bin/* + - config/environments/* + - db/migrate/* + +require: + - rubocop-rails + - rubocop-rspec + - rubocop-factory_bot + +Metrics/BlockLength: + Max: 100 + +Metrics/ClassLength: + Max: 1000 + +Metrics/MethodLength: + Max: 100 + +Metrics/ModuleLength: + Max: 1000 + +Metrics/AbcSize: + Enabled: false + +Metrics/CyclomaticComplexity: + Enabled: false + +Metrics/PerceivedComplexity: + Enabled: false + +Layout/LineLength: + Max: 120 + +Style/HashSyntax: + EnforcedShorthandSyntax: never diff --git a/Gemfile b/Gemfile index dda5a17e..685dfa32 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +ruby '3.2.1' + source 'http://rubygems.org' gem 'rails', '~> 6' @@ -129,11 +131,14 @@ group :development do gem 'ed25519' gem 'bcrypt_pbkdf' - gem 'haml_lint', '~> 0.57.0' + gem 'haml_lint', '~> 0.57.0', require: false gem 'pronto', '~> 0.11.2' - gem 'pronto-haml', '~> 0.11.1' - gem 'pronto-rubocop', '~> 0.11.5' - gem 'rubocop', '~> 1.62.1' + gem 'pronto-haml', '~> 0.11.1', require: false + gem 'pronto-rubocop', '~> 0.11.5', require: false + gem 'rubocop', '~> 1.61.0', require: false + gem 'rubocop-factory_bot', '~> 2.25.1', require: false + gem 'rubocop-rails', '~> 2.24.1', require: false + gem 'rubocop-rspec', '~> 2.28.0', require: false end group :test, :development do diff --git a/Gemfile.lock b/Gemfile.lock index 09eac921..f19d7194 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -521,7 +521,7 @@ GEM puma (>= 2.7) raabro (1.4.0) racc (1.7.3) - rack (2.2.8.1) + rack (2.2.9) rack-accept (0.4.5) rack (>= 0.4) rack-attack (6.7.0) @@ -624,7 +624,7 @@ GEM rswag-ui (2.13.0) actionpack (>= 3.1, < 7.2) railties (>= 3.1, < 7.2) - rubocop (1.62.1) + rubocop (1.61.0) json (~> 2.3) language_server-protocol (>= 3.17.0) parallel (~> 1.10) @@ -632,11 +632,27 @@ GEM rainbow (>= 2.2.2, < 4.0) regexp_parser (>= 1.8, < 3.0) rexml (>= 3.2.5, < 4.0) - rubocop-ast (>= 1.31.1, < 2.0) + rubocop-ast (>= 1.30.0, < 2.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 2.4.0, < 3.0) rubocop-ast (1.31.2) parser (>= 3.3.0.4) + rubocop-capybara (2.20.0) + rubocop (~> 1.41) + rubocop-factory_bot (2.25.1) + rubocop (~> 1.41) + rubocop-rails (2.24.1) + activesupport (>= 4.2.0) + rack (>= 1.1) + rubocop (>= 1.33.0, < 2.0) + rubocop-ast (>= 1.31.1, < 2.0) + rubocop-rspec (2.28.0) + rubocop (~> 1.40) + rubocop-capybara (~> 2.17) + rubocop-factory_bot (~> 2.22) + rubocop-rspec_rails (~> 2.28) + rubocop-rspec_rails (2.28.2) + rubocop (~> 1.40) ruby-prof (1.7.0) ruby-progressbar (1.13.0) ruby-statistics (3.0.2) @@ -823,7 +839,10 @@ DEPENDENCIES rspec-rails (~> 5.0.2) rswag-api rswag-ui - rubocop (~> 1.62.1) + rubocop (~> 1.61.0) + rubocop-factory_bot (~> 2.25.1) + rubocop-rails (~> 2.24.1) + rubocop-rspec (~> 2.28.0) ruby-prof rufus-scheduler rvm1-capistrano3 @@ -840,5 +859,8 @@ DEPENDENCIES web-console yt +RUBY VERSION + ruby 3.2.1p31 + BUNDLED WITH 2.4.5 diff --git a/config/environments/development.rb b/config/environments/development.rb index 174eb690..79255c7c 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -122,4 +122,13 @@ config.active_record.verbose_query_logs = false config.action_view.cache_template_loading = true end + + # Applying rubocop-rails autocorrection to generated content + # see https://github.com/rubocop/rubocop-rails?tab=readme-ov-file#rails-configuration-tip + config.generators.after_generate do |files| + parsable_files = files.filter { |file| file.end_with?('.rb') } + unless parsable_files.empty? + system("bundle exec rubocop -A --fail-level=E #{parsable_files.shelljoin}", exception: true) + end + end end From 08336739656a06b2e4b53c8157454387738c576b Mon Sep 17 00:00:00 2001 From: Damir Sultanbekov Date: Tue, 2 Apr 2024 16:50:25 +0500 Subject: [PATCH 3/7] updated MySQL version used in CI --- .github/workflows/rspec.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/rspec.yml b/.github/workflows/rspec.yml index f530396b..5bb5bc7f 100644 --- a/.github/workflows/rspec.yml +++ b/.github/workflows/rspec.yml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-20.04 services: mysql: - image: mysql:5.7 + image: mysql:8.0 env: MYSQL_ROOT_PASSWORD: root ports: @@ -24,7 +24,7 @@ jobs: - name: Install dependencies run: | sudo apt-get update - sudo apt-get install -y mysql-client libmysqlclient-dev wkhtmltopdf pandoc yaz libyaz-dev libmagickwand-dev libpcap-dev memcached + sudo apt-get install -y mysql-client libmysqlclient-dev wkhtmltopdf pandoc yaz libyaz-dev libmagickwand-dev libpcap-dev memcached cmake - name: Configure sysctl limits run: | sudo swapoff -a @@ -59,4 +59,4 @@ jobs: MYSQL_PORT: ${{ job.services.mysql.ports[3306] }} ELASTICSEARCH_PORT: ${{ job.services.elasticsearch.ports[9200] }} run: | - bundle exec rspec \ No newline at end of file + bundle exec rspec From 681f9f089cc82d15b120fef3b517290217c4ea8b Mon Sep 17 00:00:00 2001 From: Damir Sultanbekov Date: Tue, 2 Apr 2024 18:09:57 +0500 Subject: [PATCH 4/7] added pronto config for Github actions. Updated some config for Rspec github action. Added intentional violation to trigger rubocop warning for test --- .github/workflows/pronto.yml | 31 +++++++++++++++++++++++++++++++ .github/workflows/rspec.yml | 6 +++--- spec/api/v1/people_api_spec.rb | 4 ++-- 3 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/pronto.yml diff --git a/.github/workflows/pronto.yml b/.github/workflows/pronto.yml new file mode 100644 index 00000000..2817ae81 --- /dev/null +++ b/.github/workflows/pronto.yml @@ -0,0 +1,31 @@ +name: Pronto + +on: + pull_request: + branches: [ master ] + +jobs: + pronto: + name: Linters + runs-on: ubuntu-22.04 + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y mysql-client libmysqlclient-dev wkhtmltopdf pandoc yaz libyaz-dev libmagickwand-dev libpcap-dev memcached cmake + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + bundler-cache: true + - name: Run pronto + env: + RAILS_ENV: "test" + PRONTO_PULL_REQUEST_ID: ${{ github.event.pull_request.number }} + PRONTO_GITHUB_ACCESS_TOKEN: "${{ github.token }}" + BUNDLE_PATH: "vendor/bundle" + run: + bundle exec pronto run -f github_pr github_status -c origin/${{ github.base_ref }} diff --git a/.github/workflows/rspec.yml b/.github/workflows/rspec.yml index 5bb5bc7f..8fb332d3 100644 --- a/.github/workflows/rspec.yml +++ b/.github/workflows/rspec.yml @@ -8,8 +8,8 @@ on: jobs: test: - name: CI - runs-on: ubuntu-20.04 + name: RSpec + runs-on: ubuntu-22.04 services: mysql: image: mysql:8.0 @@ -20,7 +20,7 @@ jobs: options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 steps: - name: Checkout - uses: actions/checkout@v1 + uses: actions/checkout@v4 - name: Install dependencies run: | sudo apt-get update diff --git a/spec/api/v1/people_api_spec.rb b/spec/api/v1/people_api_spec.rb index 42c0d164..01f94c69 100644 --- a/spec/api/v1/people_api_spec.rb +++ b/spec/api/v1/people_api_spec.rb @@ -4,7 +4,7 @@ include_context 'API Spec Helpers' describe 'GET api/v1/people/{id}' do - let(:detail) { 'metadata' } + let(:detail) { "metadata" } let(:person_id) { -1 } let(:path) { "/api/v1/people/#{person_id}?key=#{key}&author_detail=#{detail}" } let(:subject) { get path } @@ -120,4 +120,4 @@ def validate_person(json, person, detail) expect(json).to_not have_key('enrichment') end end -end \ No newline at end of file +end From 4a0c0fe82dcfc2ac0a7b7188d2c29a9938114aa8 Mon Sep 17 00:00:00 2001 From: Damir Sultanbekov Date: Tue, 2 Apr 2024 19:21:15 +0500 Subject: [PATCH 5/7] Added docs for rubocop/pronto --- README.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/README.md b/README.md index ed9b33d6..cd0d424c 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,50 @@ External (i.e. hosting system) dependencies: * redis as [backend for sidekiq](https://github.com/sidekiq/sidekiq/wiki/Using-Redis) * memcached for caching +Linters +------- +Since 2024 we've included some linters in project, namely: +- [rubocop](https://github.com/rubocop/rubocop) +- [haml_lint](https://github.com/sds/haml-lint) + +### Some quick rubocop tips +Most simple command: +```shell +rubocop +``` +Will check whole project. In most cases it is not required. Also it will produce tons of warnings, as many parts of +codebase does not follow style guidelines. + +In most cases you may want to check single file: +```shell +rubocop +``` + +Another useful feature is rubocop's autocorrection. In some cases rubocop can try to fix style violations on its own. + +There is "safe" autocorrection which should be OK in most cases: +```shell +rubocop -a +``` + +And more risky version of it, which can fix more issues, but known to produce errors more often: +```shell +rubocop -A +``` + +In any case you should be careful with autocorrection and always check result of autocorrection before commiting it +to git. + +### Pronto + +To run linters only on those parts of projects, affected by your PR you can use +[pronto](https://github.com/prontolabs/pronto) tool. For example following command: +```shell +pronto run -c origin/master +``` +will run linters only on lines of code which were changed compared to `origin/master` branch. Our CI pipeline uses +this approach for all PRs. + License ------- From fff8784971cb0b63430448f108f3078360df3bbb Mon Sep 17 00:00:00 2001 From: Damir Sultanbekov Date: Tue, 2 Apr 2024 19:23:01 +0500 Subject: [PATCH 6/7] Added frozen_string_literal to Gemfile --- Gemfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Gemfile b/Gemfile index 685dfa32..737b35d7 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + ruby '3.2.1' source 'http://rubygems.org' From b50428d29563d97b729370d2994f95ab3ecd212d Mon Sep 17 00:00:00 2001 From: Damir Sultanbekov Date: Tue, 2 Apr 2024 19:37:47 +0500 Subject: [PATCH 7/7] resolved rubocop's warnings --- spec/api/v1/people_api_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/api/v1/people_api_spec.rb b/spec/api/v1/people_api_spec.rb index 01f94c69..9cfe5c6d 100644 --- a/spec/api/v1/people_api_spec.rb +++ b/spec/api/v1/people_api_spec.rb @@ -4,7 +4,7 @@ include_context 'API Spec Helpers' describe 'GET api/v1/people/{id}' do - let(:detail) { "metadata" } + let(:detail) { 'metadata' } let(:person_id) { -1 } let(:path) { "/api/v1/people/#{person_id}?key=#{key}&author_detail=#{detail}" } let(:subject) { get path }