Skip to content

Commit 867494b

Browse files
authored
Merge pull request #11 from reclaim-the-stack/new-gemfile-approach
Use bundle install when running via gemfile
2 parents 2e2bd13 + f8f919a commit 867494b

File tree

3 files changed

+41
-25
lines changed

3 files changed

+41
-25
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: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,35 @@
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+
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+
2438
puts "::endgroup::"
2539

2640
# Script
@@ -38,7 +52,7 @@
3852
# JSON reference: https://docs.rubocop.org/rubocop/formatters.html#json-formatter
3953
files_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")
@@ -52,7 +66,7 @@
5266

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

5771
comments_made_by_rubocop = existing_comments.select do |comment|
5872
comment.fetch("body").include?("rubocop-comment-id")
@@ -76,7 +90,7 @@
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}")
8094
end
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")
151165
existing_separate_comment = separate_comments.find do |comment|
152166
comment.fetch("body").include?("rubocop-comment-id: outside-diff")
153167
end
@@ -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
183197
elsif existing_separate_comment
184198
existing_comment_id = existing_separate_comment.fetch("id")

0 commit comments

Comments
 (0)