66# Setup
77
88puts "::group::Installing Rubocop gems"
9- versioned_rubocop_gems =
10- if ENV . fetch ( "RUBOCOP_GEM_VERSIONS" ) . downcase == "gemfile"
11- require "bundler"
129
13- rubocop_config_gems_without_prefix = %w[ syntax_tree ] . to_set
10+ if ENV . fetch ( "RUBOCOP_GEM_VERSIONS" ) . downcase == "gemfile"
11+ require "bundler"
1412
15- Bundler ::LockfileParser . new ( Bundler . read_file ( "Gemfile.lock" ) ) . specs
16- . select { |spec | spec . name . start_with? ( "rubocop" ) || rubocop_config_gems_without_prefix . include? ( spec . name ) }
17- . map { |spec | "#{ spec . name } :#{ spec . version } " }
18- else
19- ENV . fetch ( "RUBOCOP_GEM_VERSIONS" ) . split
13+ gemfile = Bundler ::LockfileParser . new ( Bundler . read_file ( "Gemfile.lock" ) )
14+ to_remove = gemfile . dependencies . keys . reject do |dependency |
15+ dependency . include? "rubocop" || dependency == "syntax_tree"
2016 end
21- gem_install_command = "gem install #{ versioned_rubocop_gems . join ( ' ' ) } --no-document --conservative"
22- puts "Installing gems with:" , gem_install_command
23- system "time #{ gem_install_command } "
17+
18+ puts "Removing non rubocop gems from Gemfile"
19+ system ( "bundle remove #{ to_remove . join ( " " ) } " ) or abort ( "ERROR: Failed to remove non rubocop gems from Gemfile" )
20+ puts
21+
22+ puts "Resulting Gemfile:"
23+ puts Bundler . read_file ( "Gemfile" )
24+
25+ puts "Installing gems with: bundle install"
26+ system ( "time bundle install" ) or abort ( "ERROR: Failed to install gems" )
27+
28+ rubocop_command = "bundle exec rubocop"
29+ else
30+ versioned_rubocop_gems = ENV . fetch ( "RUBOCOP_GEM_VERSIONS" ) . split
31+ gem_install_command = "gem install #{ versioned_rubocop_gems . join ( ' ' ) } --no-document --conservative"
32+ puts "Installing gems with:" , gem_install_command
33+ system "time #{ gem_install_command } "
34+
35+ rubocop_command = "rubocop"
36+ end
37+
2438puts "::endgroup::"
2539
2640# Script
3852# JSON reference: https://docs.rubocop.org/rubocop/formatters.html#json-formatter
3953files_with_offenses =
4054 if changed_ruby_files . any?
41- command = "rubocop #{ changed_ruby_files . map ( &:path ) . join ( ' ' ) } --format json --force-exclusion #{ ARGV . join ( ' ' ) } "
55+ command = "#{ rubocop_command } #{ changed_ruby_files . map ( &:path ) . join ( ' ' ) } --format json --force-exclusion #{ ARGV . join ( ' ' ) } "
4256
4357 puts "Running rubocop with: #{ command } "
4458 JSON . parse ( `#{ command } ` ) . fetch ( "files" )
5266
5367puts "Fetching PR comments from https://api.github.com/repos/#{ owner_and_repository } /pulls/#{ pr_number } /comments"
5468
55- existing_comments = Github . get! ( "/repos/#{ owner_and_repository } /pulls/#{ pr_number } /comments" )
69+ existing_comments = Github . get ( "/repos/#{ owner_and_repository } /pulls/#{ pr_number } /comments" )
5670
5771comments_made_by_rubocop = existing_comments . select do |comment |
5872 comment . fetch ( "body" ) . include? ( "rubocop-comment-id" )
7690
7791 puts "Deleting resolved comment #{ comment_id } on #{ path } line #{ line } "
7892
79- Github . delete! ( "/repos/#{ owner_and_repository } /pulls/comments/#{ comment_id } " )
93+ Github . delete ( "/repos/#{ owner_and_repository } /pulls/comments/#{ comment_id } " )
8094end
8195
8296# Comment on the pull request with the offenses found
@@ -132,7 +146,7 @@ def in_diff?(changed_files, path, line)
132146 # Somehow the commit_id should not be just the HEAD SHA: https://stackoverflow.com/a/71431370/1075108
133147 commit_id = github_event . fetch ( "pull_request" ) . fetch ( "head" ) . fetch ( "sha" )
134148
135- Github . post! (
149+ Github . post (
136150 "/repos/#{ owner_and_repository } /pulls/#{ pr_number } /comments" ,
137151 body : body ,
138152 path : path ,
@@ -147,7 +161,7 @@ def in_diff?(changed_files, path, line)
147161
148162# If there are any offenses outside the diff, make a separate comment for them
149163
150- separate_comments = Github . get! ( "/repos/#{ owner_and_repository } /issues/#{ pr_number } /comments" )
164+ separate_comments = Github . get ( "/repos/#{ owner_and_repository } /issues/#{ pr_number } /comments" )
151165existing_separate_comment = separate_comments . find do |comment |
152166 comment . fetch ( "body" ) . include? ( "rubocop-comment-id: outside-diff" )
153167end
@@ -173,12 +187,12 @@ def in_diff?(changed_files, path, line)
173187 puts "Skipping unchanged separate comment #{ existing_comment_id } "
174188 else
175189 puts "Updating separate comment #{ existing_comment_id } "
176- Github . patch! ( "/repos/#{ owner_and_repository } /issues/comments/#{ existing_comment_id } " , body : body )
190+ Github . patch ( "/repos/#{ owner_and_repository } /issues/comments/#{ existing_comment_id } " , body : body )
177191 end
178192 else
179193 puts "Commenting on pull request with offenses found outside the diff"
180194
181- Github . post! ( "/repos/#{ owner_and_repository } /issues/#{ pr_number } /comments" , body : body )
195+ Github . post ( "/repos/#{ owner_and_repository } /issues/#{ pr_number } /comments" , body : body )
182196 end
183197elsif existing_separate_comment
184198 existing_comment_id = existing_separate_comment . fetch ( "id" )
0 commit comments