Skip to content

Commit

Permalink
Handle Rubocop failures better (#9)
Browse files Browse the repository at this point in the history
* Make sure we don't leave the action running

* Lets see if it breaks the right way

* Revert "Lets see if it breaks the right way"

This reverts commit c239d77.

* Remove debug for success case
  • Loading branch information
sheck authored Oct 2, 2020
1 parent 7c57baa commit 4670030
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 26 deletions.
61 changes: 35 additions & 26 deletions action/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@
raise "Couldn't create check run #{check_run_create.inspect}"
end

compare_sha = event.pull_request.base.sha

rubocop_json = Bundler.with_original_env do
`git diff --name-only #{compare_sha} --diff-filter AM --relative | xargs rubocop --force-exclusion --format json`
end

rubocop_output = JSON.parse(rubocop_json, object_class: OpenStruct)

RUBOCOP_TO_GITHUB_SEVERITY = {
"refactor" => "failure",
"convention" => "failure",
Expand All @@ -49,8 +41,6 @@
"fatal" => "failure"
}.freeze

annotations = []

def git_root
@git_root ||= Pathname.new(GitUtils.root)
end
Expand All @@ -67,25 +57,44 @@ def file_fullpath(relative_path)
end
end

rubocop_output.files.each do |file|
path = file_fullpath(file.path)
def generate_annotations(compare_sha:)
annotations = []

rubocop_json = Bundler.with_original_env do
`git diff --name-only #{compare_sha} --diff-filter AM --relative | xargs rubocop --force-exclusion --format json`
end

rubocop_output = JSON.parse(rubocop_json, object_class: OpenStruct)

rubocop_output.files.each do |file|
path = file_fullpath(file.path)

change_ranges = GitUtils.generate_change_ranges(path, compare_sha: compare_sha)
change_ranges = GitUtils.generate_change_ranges(path, compare_sha: compare_sha)

file.offenses.each do |offense|
next unless change_ranges.any? { |range| range.include?(offense.location.start_line) }
file.offenses.each do |offense|
next unless change_ranges.any? { |range| range.include?(offense.location.start_line) }

annotations.push(
path: path,
start_line: offense.location.start_line,
end_line: offense.location.last_line,
annotation_level: RUBOCOP_TO_GITHUB_SEVERITY[offense.severity],
message: offense.message
)
annotations.push(
path: path,
start_line: offense.location.start_line,
end_line: offense.location.last_line,
annotation_level: RUBOCOP_TO_GITHUB_SEVERITY[offense.severity],
message: offense.message
)
end
end
end

resp = check_run.update(annotations: annotations)
annotations
end

p resp
p resp.json
begin
annotations = generate_annotations(compare_sha: event.pull_request.base.sha)
rescue Exception => e
puts e.message
puts e.backtrace.inspect
resp = check_run.error(message: e.message)
p resp
p resp.json
else
check_run.update(annotations: annotations)
end
17 changes: 17 additions & 0 deletions action/check_run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ def update(annotations:)
patch("/repos/#{owner}/#{repo}/check-runs/#{id}", body)
end

def error(message:)
output = {
title: name,
summary: "Error during linting process",
text: message
}

body = {
status: "completed",
completed_at: Time.now.iso8601,
conclusion: "failure",
output: output,
}

patch("/repos/#{owner}/#{repo}/check-runs/#{id}", body)
end

private

attr_reader :owner, :repo, :headers, :id, :name
Expand Down
6 changes: 6 additions & 0 deletions action/fake_check_run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ def update(annotations:)
puts annotations.to_yaml
OpenStruct.new(json: annotations.to_json)
end

def error(message:)
puts "FAKE CHECK RUN: received error message \n"
puts message.to_yaml
OpenStruct.new(json: message.to_json)
end
end

0 comments on commit 4670030

Please sign in to comment.