fix: merge autodetect can't close other PRs but only the last one when multiple PRs are pushed at once (#37512) (#37516)

Backport #37512

Fixes #37510.

Co-authored-by: Jason Learst <jason@jasonlearst.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Giteabot
2026-05-03 01:15:56 -07:00
committed by GitHub
parent e2b211f291
commit dd78d87dcd
4 changed files with 74 additions and 22 deletions

View File

@@ -313,18 +313,25 @@ func getMergeCommit(ctx context.Context, pr *issues_model.PullRequest) (*git.Com
objectFormat := git.ObjectFormatFromName(pr.BaseRepo.ObjectFormatName)
// Get the commit from BaseBranch where the pull request got merged
// Get the commit from BaseBranch where the pull request got merged.
// When several PRs targeting the same base are merged in a single push,
// rev-list returns one line per merge commit on the ancestry path; we
// only want the first one (the oldest, with --reverse, i.e. the merge
// commit that actually introduced this PR).
mergeCommit, _, err := gitrepo.RunCmdString(ctx, pr.BaseRepo,
gitcmd.NewCommand("rev-list", "--ancestry-path", "--merges", "--reverse").
AddDynamicArguments(prHeadCommitID+".."+pr.BaseBranch))
if err != nil {
return nil, fmt.Errorf("git rev-list --ancestry-path --merges --reverse: %w", err)
} else if len(mergeCommit) < objectFormat.FullLength() {
}
// only use the latest commit as merge commit if the output contains multiple commits
mergeCommit = strings.TrimSpace(mergeCommit)
mergeCommit, _, _ = strings.Cut(mergeCommit, "\n")
if len(mergeCommit) < objectFormat.FullLength() {
// PR was maybe fast-forwarded, so just use last commit of PR
mergeCommit = prHeadCommitID
}
mergeCommit = strings.TrimSpace(mergeCommit)
commit, err := gitRepo.GetCommit(mergeCommit)
if err != nil {
return nil, fmt.Errorf("GetMergeCommit[%s]: %w", mergeCommit, err)