Skip to content

Commit 51e4acf

Browse files
committed
Use bundle install when running via gemfile
1 parent 2e2bd13 commit 51e4acf

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

.github/workflows/rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
rubocop:
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: actions/checkout@v3
13+
- uses: actions/checkout@v4
1414
- uses: ruby/setup-ruby@v1
1515
- uses: ./
1616
with:

lib/github.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
module Github
55
HttpError = Class.new(StandardError)
66

7+
# The File class wraps the Github API response for files in a pull requests. Its main purpose
8+
# is to provide the #changed_lines method which returns the line numbers of the lines which
9+
# have changed in a file by parsing its patch attribute.
710
class File
811
RANGE_INFORMATION_LINE = /^@@ .+\+(?<line_number>\d+),/.freeze
912
MODIFIED_LINE = /^\+(?!\+|\+)/.freeze
@@ -42,23 +45,22 @@ def changed_lines
4245
delete: Net::HTTP::Delete,
4346
}.freeze
4447

48+
# Defines .get, .patch, .post, and .delete methods for making requests to the GitHub API.
49+
# For successful requests, The JSON parsed body is returned, otherwise HttpError is raised.
4550
REQUEST_METHOD_TO_CLASS.each do |method, klass|
4651
define_singleton_method(method) do |path, params = nil|
47-
request(klass, path, params)
48-
end
49-
50-
define_singleton_method("#{method}!") do |path, params = nil|
5152
response = request(klass, path, params)
5253
raise HttpError, "status: #{response.code}, body: #{response.body}" unless response.is_a?(Net::HTTPSuccess)
5354

5455
JSON.parse(response.body) if response.body
5556
end
5657
end
5758

59+
# Returns Array of File objects for all files in a pull request which have the .rb extension.
5860
def self.pull_request_ruby_files(owner_and_repository, pr_number)
5961
changed_files = []
6062
1.step do |page|
61-
files = Github.get!("/repos/#{owner_and_repository}/pulls/#{pr_number}/files?per_page=100&page=#{page}")
63+
files = Github.get("/repos/#{owner_and_repository}/pulls/#{pr_number}/files?per_page=100&page=#{page}")
6264
changed_files.concat(files)
6365
break if files.length < 100
6466
end

rubocop.rb

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,31 @@
66
# Setup
77

88
puts "::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+
2434
puts "::endgroup::"
2535

2636
# Script
@@ -52,7 +62,7 @@
5262

5363
puts "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

5767
comments_made_by_rubocop = existing_comments.select do |comment|
5868
comment.fetch("body").include?("rubocop-comment-id")
@@ -76,7 +86,7 @@
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}")
8090
end
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")
151161
existing_separate_comment = separate_comments.find do |comment|
152162
comment.fetch("body").include?("rubocop-comment-id: outside-diff")
153163
end
@@ -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
183193
elsif existing_separate_comment
184194
existing_comment_id = existing_separate_comment.fetch("id")

0 commit comments

Comments
 (0)