Skip to content

Commit

Permalink
Merge pull request danger#1479 from manicmaniac/compatibility-with-oc…
Browse files Browse the repository at this point in the history
…tokit-v7

Fix compatibility issue with Octokit >= 8
  • Loading branch information
manicmaniac authored Feb 4, 2024
2 parents 96d86e3 + bf46822 commit 02ceb1f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<!-- Your comment below here -->
* Add keyword arguments to MessageAggregator.aggregate for Ruby 3.X compatibility - [@dirtyhabits97](https://github.com/dirtyhabits97) [#1466](https://github.com/danger/danger/pull/1466)
* Fix: remove stale violations when there are no new violations to report - [@iangmaia](https://github.com/iangmaia) [#1477](https://github.com/danger/danger/pull/1477)
* Fix compatibility issue with Octokit >= 8 - [@manicmaniac](https://github.com/manicmaniac) [#1479](https://github.com/danger/danger/pull/1479)
<!-- Your comment above here -->

## 9.4.1
Expand Down
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 02ceb1f

Please sign in to comment.