From 6f02d9d5f2569d0ffb2e63bd8178c40066ef2c92 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Mon, 9 Nov 2026 13:45:16 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(automerge):=20group=20the=20?= =?UTF-8?q?one-PR-per-issue=20rule=20by=20base=20branch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Release-line siblings (the same fix against v5 and v6, both Fixes #N) were read as duplicates: the later PR was parked with merge-train:duplicate-of# and the dashboard asked for a human decision while CI was still running. Group duplicate candidates by base branch and skip cross-line claimants in the claim-conflict pass. Fixes #17436 Signed-off-by: Andrew Anderson --- .../fixed-merge-train-duplicate-per-base.md | 1 + .../automerge_merge_train_duplicate.go | 31 +++++++++++-- .../automerge_merge_train_duplicate_test.go | 43 +++++++++++++++++++ 3 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 changelog.d/fixed-merge-train-duplicate-per-base.md diff --git a/changelog.d/fixed-merge-train-duplicate-per-base.md b/changelog.d/fixed-merge-train-duplicate-per-base.md new file mode 100644 index 0000000000..d6bb9e2916 --- /dev/null +++ b/changelog.d/fixed-merge-train-duplicate-per-base.md @@ -0,0 +1 @@ +- The merge train's one-PR-per-issue rule now groups candidates by base branch, so the same fix opened against two release lines (v5 and v6, both `Fixes #N`) is no longer held as a duplicate and shown as "needs human decision"; the same-issue claim-conflict pass likewise ignores claimants on another base branch. Two PRs for one issue on the same line still compete as before ([#17436](https://github.com/hivecommons/hive/issues/17436)). diff --git a/src/pkg/github/automerge/automerge_merge_train_duplicate.go b/src/pkg/github/automerge/automerge_merge_train_duplicate.go index fa69496bd4..9d24578e2b 100644 --- a/src/pkg/github/automerge/automerge_merge_train_duplicate.go +++ b/src/pkg/github/automerge/automerge_merge_train_duplicate.go @@ -116,12 +116,25 @@ func mergeTrainDuplicateReason(h mergeTrainDuplicateHold) string { return prstage.MergeTrain{State: prstage.MergeTrainStateDuplicateOf, Front: h.Winner, Position: h.Issue}.Reason() } +// mergeTrainSameLine reports whether two PRs target the same base branch. +// The one-PR-per-issue rule only compares PRs on the same line: the same +// fix opened against v5 and v6 both says "Fixes #N", and that is the +// release-line process, not a duplicate (hivecommons/hive#17436). A PR +// with no base recorded compares as the same line so partial fixtures +// keep their old behaviour. +func mergeTrainSameLine(a, b *gh.PullRequest) bool { + ab, bb := a.GetBase().GetRef(), b.GetBase().GetRef() + return ab == "" || bb == "" || ab == bb +} + // planDuplicateHolds runs the one-PR-per-issue rule over the lane's // candidates before the trains are grouped: the held PRs leave the -// candidate list and the plan remembers who each one yields to. +// candidate list and the plan remembers who each one yields to. PRs are +// grouped by base branch first, so siblings on different release lines +// never compete. func (c *Engine) planDuplicateHolds(ctx context.Context, plan *mergeTrainPlan, lane, owner, repo, displayRepo string, prs []*gh.PullRequest, prefilter func(*gh.PullRequest) string) []*gh.PullRequest { prs = c.planClaimConflicts(ctx, plan, owner, repo, displayRepo, prs, prefilter) - byIssue := map[int][]mergeTrainDuplicateCandidate{} + byLine := map[string]map[int][]mergeTrainDuplicateCandidate{} for _, pr := range prs { if pr == nil || pr.GetNumber() <= 0 || !mergeTrainCandidate(prefilter(pr)) { continue @@ -135,11 +148,21 @@ func (c *Engine) planDuplicateHolds(ctx context.Context, plan *mergeTrainPlan, l CreatedAt: pr.GetCreatedAt().Time, Green: !mergeTrainDirty(pr.GetMergeableState()) && !c.mergeTrainHeadBlocked(lane, owner, repo, pr.GetNumber(), pr.GetHead().GetSHA()), } + line := pr.GetBase().GetRef() + byIssue := byLine[line] + if byIssue == nil { + byIssue = map[int][]mergeTrainDuplicateCandidate{} + byLine[line] = byIssue + } for _, issue := range issues { byIssue[issue] = append(byIssue[issue], cand) } } - holds := mergeTrainDuplicateHolds(byIssue) + var holds []mergeTrainDuplicateHold + for _, byIssue := range byLine { + holds = append(holds, mergeTrainDuplicateHolds(byIssue)...) + } + sort.Slice(holds, func(i, j int) bool { return holds[i].Number < holds[j].Number }) if len(holds) == 0 { return prs } @@ -287,7 +310,7 @@ func (c *Engine) planClaimConflicts(ctx context.Context, plan *mergeTrainPlan, o for _, issue := range mergeTrainClosedIssues(pr, displayRepo) { for _, other := range byIssue[issue] { o := other.GetNumber() - if o == n || candidate[o] || mergeTrainAdoptionPair(pr, other) { + if o == n || candidate[o] || mergeTrainAdoptionPair(pr, other) || !mergeTrainSameLine(pr, other) { continue } if _, ok := c.recordedDecisionFor(ctx, owner, repo, n, pr.GetBase().GetRef(), pr.GetHead().GetSHA()); ok { diff --git a/src/pkg/github/automerge/automerge_merge_train_duplicate_test.go b/src/pkg/github/automerge/automerge_merge_train_duplicate_test.go index f141a5e23f..35f2ed1770 100644 --- a/src/pkg/github/automerge/automerge_merge_train_duplicate_test.go +++ b/src/pkg/github/automerge/automerge_merge_train_duplicate_test.go @@ -153,3 +153,46 @@ func TestPlanMergeTrainsHoldsSameIssueDuplicate(t *testing.T) { t.Fatalf("skip reason parsed %+v ok=%v", mt, ok) } } + +// The same fix opened against two release lines says "Fixes #N" twice and +// is not a duplicate: neither PR is held and neither is a claim conflict +// (hivecommons/hive#17436). Two PRs on the SAME line still compete. +func TestPlanDuplicateHoldsGroupsByBaseBranch(t *testing.T) { + c := &Engine{} + t0 := time.Date(2026, 11, 9, 17, 0, 0, 0, time.UTC) + pr := func(n int, base string, at time.Time) *gh.PullRequest { + return &gh.PullRequest{Number: gh.Int(n), Title: gh.String("t"), Body: gh.String("Fixes #17392"), CreatedAt: &gh.Timestamp{Time: at}, MergeableState: gh.String("clean"), State: gh.String("open"), Base: &gh.PullRequestBranch{Ref: gh.String(base)}, Head: &gh.PullRequestBranch{SHA: gh.String("sha")}} + } + prs := []*gh.PullRequest{ + pr(17414, "v5", t0), + pr(17415, "v6", t0.Add(time.Minute)), + pr(17420, "v6", t0.Add(2*time.Minute)), + } + plan := &mergeTrainPlan{lane: "self", displayRepo: "o/r"} + kept := c.planDuplicateHolds(context.Background(), plan, stageLaneSelf, "o", "r", "o/r", prs, func(*gh.PullRequest) string { return "" }) + if len(kept) != 2 || kept[0].GetNumber() != 17414 || kept[1].GetNumber() != 17415 { + t.Fatalf("kept = %v", kept) + } + if _, held := plan.duplicateOf(17415); held { + t.Fatal("a sibling on another base branch is not a duplicate") + } + if h, held := plan.duplicateOf(17420); !held || h.Winner != 17415 { + t.Fatalf("same-line duplicate hold = %+v held=%v", h, held) + } + if _, held := plan.claimConflictOf(17415); held { + t.Fatal("a sibling on another base branch is not a claim conflict") + } + + // A prefilter refusal on the v5 sibling leaves it an open claimant, but + // it still does not conflict with the v6 PR on another line. + plan2 := &mergeTrainPlan{} + c.planDuplicateHolds(context.Background(), plan2, stageLaneSelf, "o", "r", "o/r", prs[:2], func(p *gh.PullRequest) string { + if p.GetNumber() == 17414 { + return "label:hold" + } + return "" + }) + if _, held := plan2.claimConflictOf(17415); held { + t.Fatal("cross-line claimant must not hold the candidate") + } +}