Skip to content

Commit

Permalink
Ensure we clean up separate comment when violations are fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
dbackeus committed Jul 5, 2023
1 parent 6c4d197 commit 65c3b70
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def self.request(request_class, path, params = nil)

# Fetch existing pull request comments

puts "Fetching comments from https://api.github.com/repos/#{owner_and_repository}/pulls/#{pr_number}/comments"
puts "Fetching PR comments from https://api.github.com/repos/#{owner_and_repository}/pulls/#{pr_number}/comments"

existing_comments = Github.get!("/repos/#{owner_and_repository}/pulls/#{pr_number}/comments")

Expand Down Expand Up @@ -192,10 +192,11 @@ def self.request(request_class, path, params = nil)

# If there are any offenses outside the diff, make a separate comment for them

separate_comments = Github.get!("/repos/#{owner_and_repository}/issues/#{pr_number}/comments")
existing_separate_comment = separate_comments.find do |comment|
comment.fetch("body").include?("rubocop-comment-id: outside-diff")
end
if offences_outside_diff.any?
existing_comment = comments_made_by_rubocop.find do |comment|
comment.fetch("body").include?("rubocop-comment-id: outside-diff")
end

body = <<~BODY
<!-- rubocop-comment-id: outside-diff -->
Expand All @@ -207,11 +208,11 @@ def self.request(request_class, path, params = nil)
"**#{offense.fetch(:path)}:#{offense.fetch(:line)}**\n#{offense.fetch(:message)}"
end.join("\n\n")

if existing_comment
existing_comment_id = existing_comment.fetch("id")
if existing_separate_comment
existing_comment_id = existing_separate_comment.fetch("id")

# No need to do anything if the offense already exists and hasn't changed
if existing_comment.fetch("body") == body
if existing_separate_comment.fetch("body") == body
puts "Skipping unchanged separate comment #{existing_comment_id}"
else
puts "Updating comment #{existing_comment_id} on pull request"
Expand All @@ -222,6 +223,10 @@ def self.request(request_class, path, params = nil)

Github.post!("/repos/#{owner_and_repository}/issues/#{pr_number}/comments", body: body)
end
elsif existing_separate_comment
existing_comment_id = existing_separate_comment.fetch("id")
puts "Deleting resolved separate comment #{existing_comment_id}"
Github.delete("/repos/#{owner_and_repository}/issues/comments/#{existing_comment_id}")
end

# Fail the build if there were any offenses
Expand Down

0 comments on commit 65c3b70

Please sign in to comment.