Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub - Feature delete outdated comments #467

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/pronto/comment.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Pronto
Comment = Struct.new(:sha, :body, :path, :position) do
Comment = Struct.new(:sha, :body, :path, :position, :id) do
def ==(other)
position == other.position &&
path == other.path &&
Expand Down
4 changes: 3 additions & 1 deletion lib/pronto/formatter/git_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ def format(messages, repo, patches)
existing = existing_comments(messages, client, repo)
comments = new_comments(messages, patches)
additions = remove_duplicate_comments(existing, comments)
outdated_comments = existing.reject { |key, _| comments.key?(key) }.values.flatten
remove_outdated_comments(client, outdated_comments) if respond_to?(:remove_outdated_comments)
submit_comments(client, additions)

approve_pull_request(comments.count, additions.count, client) if defined?(self.approve_pull_request)

"#{additions.count} Pronto messages posted to #{pretty_name} (#{existing.count} existing)"
Expand Down
8 changes: 8 additions & 0 deletions lib/pronto/formatter/github_pull_request_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ def pretty_name
def line_number(message, _)
message.line&.new_lineno
end

def remove_outdated_comments(client, outdated_comments)
outdated_comments.each do |comment|
client.delete_comment(comment)
end
rescue Octokit::UnprocessableEntity, HTTParty::Error => e
warn "Failed to delete: #{e.message}"
end
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/pronto/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def pull_comments(sha)
@comment_cache["#{pull_id}/#{sha}"] ||= begin
client.pull_comments(slug, pull_id).map do |comment|
Comment.new(
sha, comment.body, comment.path, comment.line || comment.original_line
sha, comment.body, comment.path, comment.line || comment.original_line, comment.id
)
end
end
Expand Down Expand Up @@ -66,6 +66,10 @@ def create_commit_status(status)
description: status.description)
end

def delete_comment(comment)
client.delete_pull_comment(slug, comment.id)
end

private

def create_pull_request_review(comments)
Expand Down
10 changes: 7 additions & 3 deletions spec/pronto/formatter/github_pull_request_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ module Formatter
octokit_client
.stub(:pull_comments)
.once
.and_return([double(body: 'a comment', path: 'a/path', line: 5)])
.and_return([double(body: 'a comment', path: 'a/path', line: 5, id: 1_881_471_822)])
end

specify do
octokit_client
.should_receive(:create_pull_comment)
.once

octokit_client.should_receive(:delete_pull_comment).with(nil, 1_881_471_822).and_return(true)

subject
end

Expand All @@ -43,7 +45,7 @@ module Formatter

specify do
octokit_client.should_receive(:pull_comments).and_return(
[double(body: 'existed', path: 'path/to', line: line.new_lineno)]
[double(body: 'existed', path: 'path/to', line: line.new_lineno, id: 1_881_471_822)]
)

octokit_client.should_not_receive(:create_pull_comment)
Expand All @@ -59,7 +61,7 @@ module Formatter

specify do
octokit_client.should_receive(:pull_comments).and_return(
[double(body: 'existed', path: 'path/to', line: line.new_lineno)]
[double(body: 'existed', path: 'path/to', line: line.new_lineno, id: 1_881_471_822)]
)

octokit_client.should_receive(:create_pull_comment).once
Expand Down Expand Up @@ -92,6 +94,8 @@ module Formatter
.should_receive(:create_pull_comment)
.and_raise(error)

octokit_client.should_receive(:delete_pull_comment).with(nil, 1_881_471_822).and_return(true)

$stderr.should_receive(:puts) do |line|
line.should =~ /Failed to post/
line.should =~ /Validation Failed/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module Formatter

specify do
octokit_client.should_receive(:pull_comments).and_return(
[double(body: 'existed', path: 'path/to', line: line.new_lineno)]
[double(body: 'existed', path: 'path/to', line: line.new_lineno, id: 1_881_471_822)]
)

octokit_client.should_not_receive(:create_pull_request_review)
Expand All @@ -49,7 +49,7 @@ module Formatter

specify do
octokit_client.should_receive(:pull_comments).and_return(
[double(body: 'existed', path: 'path/to', line: line.new_lineno)]
[double(body: 'existed', path: 'path/to', line: line.new_lineno, id: 1_881_471_822)]
)

octokit_client.should_receive(:create_pull_request_review).once
Expand Down
2 changes: 1 addition & 1 deletion spec/pronto/github_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Pronto
let(:ssh_remote_urls) { ["[email protected]:#{github_slug}.git"] }
let(:github_slug) { 'prontolabs/pronto' }
let(:sha) { '61e4bef' }
let(:comment) { double(body: 'note', path: 'path', line: 1, position: 1) }
let(:comment) { double(body: 'note', path: 'path', line: 1, position: 1, id: 1_881_471_822) }
let(:empty_client_options) do
{
event: 'COMMENT'
Expand Down