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
1 change: 1 addition & 0 deletions changelog.d/fixed-merge-train-duplicate-per-base.md
Original file line number Diff line number Diff line change
@@ -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)).
31 changes: 27 additions & 4 deletions src/pkg/github/automerge/automerge_merge_train_duplicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
43 changes: 43 additions & 0 deletions src/pkg/github/automerge/automerge_merge_train_duplicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Loading