diff --git a/common/lib/dependabot/pull_request_updater/github.rb b/common/lib/dependabot/pull_request_updater/github.rb index d8e79b55065..cc835320955 100644 --- a/common/lib/dependabot/pull_request_updater/github.rb +++ b/common/lib/dependabot/pull_request_updater/github.rb @@ -182,28 +182,31 @@ def update_branch(commit) end def commit_message - # Take the commit message from the old commit - commit_being_updated.message + fallback_message = + "#{pull_request.title}" \ + "\n\n" \ + "Dependabot couldn't find the original pull request head commit, " \ + "#{old_commit}." + + # Take the commit message from the old commit. If the old commit can't + # be found, use the PR title as the commit message. + commit_being_updated&.message || fallback_message end def commit_being_updated - @commit_being_updated ||= + return @commit_being_updated if defined?(@commit_being_updated) + + @commit_being_updated = if pull_request.commits == 1 github_client_for_source. git_commit(source.repo, pull_request.head.sha) else - author_name = author_details&.fetch(:name, nil) || "dependabot" commits = github_client_for_source. - pull_request_commits(source.repo, pull_request_number). - reverse - - commit = - commits.find { |c| c.sha == old_commit } || - commits.find { |c| c.commit.author.name.include?(author_name) } || - commits.first + pull_request_commits(source.repo, pull_request_number) - commit.commit + commit = commits.find { |c| c.sha == old_commit } + commit&.commit end end diff --git a/common/spec/dependabot/pull_request_updater/github_spec.rb b/common/spec/dependabot/pull_request_updater/github_spec.rb index d247add0540..f0a2003004c 100644 --- a/common/spec/dependabot/pull_request_updater/github_spec.rb +++ b/common/spec/dependabot/pull_request_updater/github_spec.rb @@ -292,6 +292,8 @@ end context "with multiple commits on the branch" do + let(:old_commit) { "0b7144dca992829a894671e275dec5bd66ebb16d" } + before do stub_request(:get, pull_request_url). to_return(status: 200, @@ -338,7 +340,6 @@ headers: json_header ) end - let(:old_commit) { "0b7144dca992829a894671e275dec5bd66ebb16d" } it "has the right commit message" do updater.update @@ -361,6 +362,28 @@ ) end end + + context "the original PR head commit cannot be found" do + let(:old_commit) { "oldcommitsha" } + + it "generates a reasonable fallback commit message" do + updater.update + + expect(WebMock). + to have_requested(:post, "#{watched_repo_url}/git/commits"). + with( + body: { + parents: ["basecommitsha"], + tree: "cd8274d15fa3ae2ab983129fb037999f264ba9a7", + message: + "Bump business from 1.4.0 to 1.5.0" \ + "\n\n" \ + "Dependabot couldn't find the original pull request " \ + "head commit, oldcommitsha." + } + ) + end + end end context "when the default branch has changed" do