Skip to content
Merged
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
27 changes: 15 additions & 12 deletions common/lib/dependabot/pull_request_updater/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 24 additions & 1 deletion common/spec/dependabot/pull_request_updater/github_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -338,7 +340,6 @@
headers: json_header
)
end
let(:old_commit) { "0b7144dca992829a894671e275dec5bd66ebb16d" }

it "has the right commit message" do
updater.update
Expand All @@ -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
Expand Down