Skip to content

Commit

Permalink
Fix compatibility issue with Octokit >= 8
Browse files Browse the repository at this point in the history
  • Loading branch information
manicmaniac committed Feb 3, 2024
1 parent 96d86e3 commit 952f229
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/danger/request_sources/github/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,10 @@ def submit_inline_comments_for_kind!(kind, messages, diff_lines, danger_comments

if matching_comments.empty?
begin
# Since Octokit v8, the signature of create_pull_request_comment has been changed.
# See https://github.com/danger/danger/issues/1475 for detailed information.
client.create_pull_request_comment(ci_source.repo_slug, ci_source.pull_request_id,
body, head_ref, m.file, position)
body, head_ref, m.file, (Octokit::MAJOR >= 8 ? m.line : position))
rescue Octokit::UnprocessableEntity => e
# Show more detail for UnprocessableEntity error
message = [e, "body: #{body}", "head_ref: #{head_ref}", "filename: #{m.file}", "position: #{position}"].join("\n")
Expand Down
36 changes: 36 additions & 0 deletions spec/lib/danger/request_sources/github_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,42 @@
allow(@g).to receive(:submit_pull_request_status!)
end

context "with Octokit v7" do
it "passes relative line number in the diff hunk to client.create_pull_request_comment" do
stub_const("Octokit::MAJOR", 7)

allow(@g.client).to receive(:pull_request_comments).with("artsy/eigen", "800").and_return([])

allow(@g.client).to receive(:create_pull_request_comment)

expect(@g.client).to receive(:delete_comment).with("artsy/eigen", inline_issue_id_1).and_return({})
expect(@g.client).to receive(:delete_comment).with("artsy/eigen", inline_issue_id_2).and_return({})
expect(@g.client).to receive(:delete_comment).with("artsy/eigen", main_issue_id).and_return({})

v = Danger::Violation.new("Sure thing", true, "Rakefile", 34)
@g.update_pull_request!(warnings: [], errors: [], messages: [v])
expect(@g.client).to have_received(:create_pull_request_comment).with("artsy/eigen", "800", anything, "561827e46167077b5e53515b4b7349b8ae04610b", "Rakefile", 7)
end
end

context "with Octokit v8" do
it "passes absolute line number in the file to client.create_pull_request_comment" do
stub_const("Octokit::MAJOR", 8)

allow(@g.client).to receive(:pull_request_comments).with("artsy/eigen", "800").and_return([])

allow(@g.client).to receive(:create_pull_request_comment)

expect(@g.client).to receive(:delete_comment).with("artsy/eigen", inline_issue_id_1).and_return({})
expect(@g.client).to receive(:delete_comment).with("artsy/eigen", inline_issue_id_2).and_return({})
expect(@g.client).to receive(:delete_comment).with("artsy/eigen", main_issue_id).and_return({})

v = Danger::Violation.new("Sure thing", true, "Rakefile", 34)
@g.update_pull_request!(warnings: [], errors: [], messages: [v])
expect(@g.client).to have_received(:create_pull_request_comment).with("artsy/eigen", "800", anything, "561827e46167077b5e53515b4b7349b8ae04610b", "Rakefile", 34)
end
end

it "deletes all inline comments if there are no violations at all" do
allow(@g.client).to receive(:pull_request_comments).with("artsy/eigen", "800").and_return([])

Expand Down

0 comments on commit 952f229

Please sign in to comment.