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+ else
28+ versioned_rubocop_gems = ENV . fetch ( "RUBOCOP_GEM_VERSIONS" ) . split
29+ gem_install_command = "gem install #{ versioned_rubocop_gems . join ( ' ' ) } --no-document --conservative"
30+ puts "Installing gems with:" , gem_install_command
31+ system "time #{ gem_install_command } "
32+ end
33+
2434puts "::endgroup::"
2535
2636# Script
5262
5363puts "Fetching PR comments from https://api.github.com/repos/#{ owner_and_repository } /pulls/#{ pr_number } /comments"
5464
55- existing_comments = Github . get! ( "/repos/#{ owner_and_repository } /pulls/#{ pr_number } /comments" )
65+ existing_comments = Github . get ( "/repos/#{ owner_and_repository } /pulls/#{ pr_number } /comments" )
5666
5767comments_made_by_rubocop = existing_comments . select do |comment |
5868 comment . fetch ( "body" ) . include? ( "rubocop-comment-id" )
7686
7787 puts "Deleting resolved comment #{ comment_id } on #{ path } line #{ line } "
7888
79- Github . delete! ( "/repos/#{ owner_and_repository } /pulls/comments/#{ comment_id } " )
89+ Github . delete ( "/repos/#{ owner_and_repository } /pulls/comments/#{ comment_id } " )
8090end
8191
8292# Comment on the pull request with the offenses found
@@ -132,7 +142,7 @@ def in_diff?(changed_files, path, line)
132142 # Somehow the commit_id should not be just the HEAD SHA: https://stackoverflow.com/a/71431370/1075108
133143 commit_id = github_event . fetch ( "pull_request" ) . fetch ( "head" ) . fetch ( "sha" )
134144
135- Github . post! (
145+ Github . post (
136146 "/repos/#{ owner_and_repository } /pulls/#{ pr_number } /comments" ,
137147 body : body ,
138148 path : path ,
@@ -147,7 +157,7 @@ def in_diff?(changed_files, path, line)
147157
148158# If there are any offenses outside the diff, make a separate comment for them
149159
150- separate_comments = Github . get! ( "/repos/#{ owner_and_repository } /issues/#{ pr_number } /comments" )
160+ separate_comments = Github . get ( "/repos/#{ owner_and_repository } /issues/#{ pr_number } /comments" )
151161existing_separate_comment = separate_comments . find do |comment |
152162 comment . fetch ( "body" ) . include? ( "rubocop-comment-id: outside-diff" )
153163end
@@ -173,12 +183,12 @@ def in_diff?(changed_files, path, line)
173183 puts "Skipping unchanged separate comment #{ existing_comment_id } "
174184 else
175185 puts "Updating separate comment #{ existing_comment_id } "
176- Github . patch! ( "/repos/#{ owner_and_repository } /issues/comments/#{ existing_comment_id } " , body : body )
186+ Github . patch ( "/repos/#{ owner_and_repository } /issues/comments/#{ existing_comment_id } " , body : body )
177187 end
178188 else
179189 puts "Commenting on pull request with offenses found outside the diff"
180190
181- Github . post! ( "/repos/#{ owner_and_repository } /issues/#{ pr_number } /comments" , body : body )
191+ Github . post ( "/repos/#{ owner_and_repository } /issues/#{ pr_number } /comments" , body : body )
182192 end
183193elsif existing_separate_comment
184194 existing_comment_id = existing_separate_comment . fetch ( "id" )
0 commit comments