From 65c3b7034cbac57557718436ebd72d011a8908eb Mon Sep 17 00:00:00 2001 From: David Backeus Date: Wed, 5 Jul 2023 13:10:24 +0200 Subject: [PATCH] Ensure we clean up separate comment when violations are fixed --- rubocop.rb | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/rubocop.rb b/rubocop.rb index ecd327c..2ed6945 100644 --- a/rubocop.rb +++ b/rubocop.rb @@ -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") @@ -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 @@ -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" @@ -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