From 368e8a739d0a8867f577dc1bc2d3f62bb6f18ae4 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Thu, 19 Nov 2026 11:43:50 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Treat=20changelog-only=20base=20?= =?UTF-8?q?moves=20as=20current=20in=20the=20current-base=20gate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A head behind its base only by commits touching the root CHANGELOG.md or changelog.d/ is reported current instead of being sent to base preparation. Fails closed on a missing merge base, API error, truncated compare or any other path. Fixes #19269 Signed-off-by: Andy Anderson --- ...fixed-19269-changelog-only-base-current.md | 1 + src/docs/hive-merge.md | 15 ++ .../automerge/automerge_current_base.go | 80 +++++++++++ .../automerge/automerge_current_base_test.go | 131 ++++++++++++++++++ 4 files changed, 227 insertions(+) create mode 100644 changelog.d/fixed-19269-changelog-only-base-current.md diff --git a/changelog.d/fixed-19269-changelog-only-base-current.md b/changelog.d/fixed-19269-changelog-only-base-current.md new file mode 100644 index 0000000000..3625a4e984 --- /dev/null +++ b/changelog.d/fixed-19269-changelog-only-base-current.md @@ -0,0 +1 @@ +- A green PR whose base moved only by changelog bookkeeping (the root `CHANGELOG.md` or files under `changelog.d/`, such as the release gate's merges) is no longer treated as stale by `auto_merge.require_current_base`. Before, each of those merges sent the head back to base preparation, so it was refreshed and every workflow re-ran for a change CI never tests; about one base move in five was of this kind. The gate now compares the PR's merge base with the base tip, treats the head as current when every changed file is on the changelog allowlist and none of them is a file the PR also changes, and logs `reason="behind only by changelog-only commits"` (#19269). Any other change, an API error or a possibly truncated comparison still refreshes the head as before. diff --git a/src/docs/hive-merge.md b/src/docs/hive-merge.md index 72d71874cd..971d62c10c 100644 --- a/src/docs/hive-merge.md +++ b/src/docs/hive-merge.md @@ -552,6 +552,21 @@ write-ahead identity. Its conflict-repair allowance lasts only through the existing mutation-reconciliation TTL; a failed/conflicting result returns to the normal repair path, never a ready seat. +**A changelog-only base move does not make a head stale.** When the head is +behind, the current-base gate (`currentBaseGate` in +`src/pkg/github/automerge/automerge_current_base.go`) also compares the PR's +merge base with the base tip. If every file changed there is the root +`CHANGELOG.md` or lies under the root `changelog.d/` (the +`changelogOnlyFile` and `changelogOnlyFragmentDir` allowlist), and the PR +changes none of those same files, the head counts as current: it is not +refreshed and does not enter preparation, and the INFO line `automerge head +treated as current` carries `reason="behind only by changelog-only commits"`. +The release gate's CHANGELOG-only merges therefore no longer re-run every +workflow on every green head. The rule fails closed: a missing merge base, an +API error, a compare that is not `ahead`, an empty or possibly truncated file +list (`compareFilesResponseCap`, GitHub's 300-file limit), or any other path +leaves the head stale and refreshed exactly as before. + **Ownership serializes refreshes, not ready merges.** A successor that is already current-base, CI-green and reviewed passes every normal merge gate without waiting for a preparing owner. A behind successor cannot skip testing. diff --git a/src/pkg/github/automerge/automerge_current_base.go b/src/pkg/github/automerge/automerge_current_base.go index f8f8de833e..87623e8c53 100644 --- a/src/pkg/github/automerge/automerge_current_base.go +++ b/src/pkg/github/automerge/automerge_current_base.go @@ -23,8 +23,80 @@ const ( // gated only by auto_merge.require_current_base_default has a behind head // the App cannot update, so the head merges as it was tested. currentBaseFallbackMessage = "automerge current-base gate: head is behind the base but the branch cannot be updated; merging as tested (list the repo in auto_merge.require_current_base to park it instead)" + // currentBaseChangelogOnlyReason is the reason logged when a behind head + // is treated as current because every file the base changed since the + // PR's merge base is release-note bookkeeping (see changelogOnlyFile and + // changelogOnlyFragmentDir). + currentBaseChangelogOnlyReason = "behind only by changelog-only commits" + // changelogOnlyFile is the repo-root changelog the release gate rewrites. + // It is one half of the changelog-only allowlist: a base move that + // touches only this file and changelogOnlyFragmentDir cannot change what + // the PR's CI tested, so it does not make a green head stale. + changelogOnlyFile = "CHANGELOG.md" + // changelogOnlyFragmentDir is the repo-root directory of per-PR + // changelog fragments (trailing slash included), the other half of the + // changelog-only allowlist. + changelogOnlyFragmentDir = "changelog.d/" + // compareFilesResponseCap is the most files GitHub returns in one + // compare response. A list this long may be truncated, so it can never + // prove a base move changelog-only. + compareFilesResponseCap = 300 ) +// isChangelogOnlyPath reports whether path is on the changelog-only +// allowlist: the repo-root changelogOnlyFile, or a file under the repo-root +// changelogOnlyFragmentDir. +func isChangelogOnlyPath(path string) bool { + if path == changelogOnlyFile { + return true + } + rest, ok := strings.CutPrefix(path, changelogOnlyFragmentDir) + return ok && rest != "" && !strings.Contains(rest, "..") +} + +// behindOnlyByChangelog reports whether a head that is behind its base is +// behind only by release-note bookkeeping: every file the base changed +// between the PR's merge base and the base tip is on the changelog-only +// allowlist and none of them is a file the PR itself changes. Such a base +// move cannot change what the head's CI tested, so refreshing the branch +// would only re-run CI for nothing. headSide is the base...head comparison +// the gate already read. Fails closed: a missing merge base, a possibly +// truncated file list on either side, an API error, an unexpected compare +// status or any other path reads as stale (false). +func (c *Engine) behindOnlyByChangelog(ctx context.Context, owner, repo, baseBranch string, headSide *gh.CommitsComparison) bool { + if c == nil || c.gh == nil || headSide == nil { + return false + } + mergeBase := strings.TrimSpace(headSide.GetMergeBaseCommit().GetSHA()) + if mergeBase == "" || len(headSide.Files) >= compareFilesResponseCap { + return false + } + baseSide, _, err := c.gh.Repositories.CompareCommits(hgithub.WithRESTCaller(ctx, "hive:automerge_current_base"), owner, repo, mergeBase, baseBranch, nil) + if err != nil || baseSide == nil || !strings.EqualFold(baseSide.GetStatus(), "ahead") || baseSide.GetTotalCommits() <= 0 { + return false + } + if len(baseSide.Files) == 0 || len(baseSide.Files) >= compareFilesResponseCap { + return false + } + ours := make(map[string]bool, len(headSide.Files)) + for _, f := range headSide.Files { + ours[f.GetFilename()] = true + if prev := f.GetPreviousFilename(); prev != "" { + ours[prev] = true + } + } + for _, f := range baseSide.Files { + name := f.GetFilename() + if !isChangelogOnlyPath(name) || ours[name] { + return false + } + if prev := f.GetPreviousFilename(); prev != "" && (!isChangelogOnlyPath(prev) || ours[prev]) { + return false + } + } + return true +} + func (c *Engine) requireCurrentBaseFor(repo string) bool { if c == nil || c.requireCurrentBase == nil { return false @@ -114,6 +186,14 @@ func (c *Engine) currentBaseGate(ctx context.Context, displayRepo, owner, repo s // A fully gated successor may land while another head prepares. return AutoMergeSweepEvent{}, "", false, nil } + if c.behindOnlyByChangelog(ctx, owner, repo, baseBranch, cmp) { + // The release gate's CHANGELOG-only merges move the base without + // changing anything CI tests. Refreshing for them re-runs every + // workflow on the head and starves the preparation front, so the + // head counts as current. Anything else still refreshes. + c.info("automerge head treated as current", "repo", displayRepo, "pr", number, "base", baseBranch, "behind_by", cmp.GetBehindBy(), "reason", currentBaseChangelogOnlyReason) + return AutoMergeSweepEvent{}, "", false, nil + } if !branchUpdateAllowed { return AutoMergeSweepEvent{}, holdReason, true, nil } diff --git a/src/pkg/github/automerge/automerge_current_base_test.go b/src/pkg/github/automerge/automerge_current_base_test.go index db046a91d3..8e31149488 100644 --- a/src/pkg/github/automerge/automerge_current_base_test.go +++ b/src/pkg/github/automerge/automerge_current_base_test.go @@ -538,3 +538,134 @@ func TestContributorLaneCurrentBaseGateAfterGreen(t *testing.T) { }) } } + +// TestCurrentBaseGateChangelogOnlyBaseMove pins the changelog-only rule: a +// head behind its base only by commits that touch CHANGELOG.md or +// changelog.d/ (the release gate's bookkeeping merges) counts as current and +// is not refreshed, while any other base move, an unreadable or possibly +// truncated comparison, or a changelog path the PR itself changes still +// sends the head to base preparation as before. +func TestCurrentBaseGateChangelogOnlyBaseMove(t *testing.T) { + const head = "sha-head" + const mergeBase = "sha-mergebase" + fileList := func(names ...string) []map[string]any { + out := make([]map[string]any, 0, len(names)) + for _, n := range names { + out = append(out, map[string]any{"filename": n, "status": "modified"}) + } + return out + } + manyFragments := func(n int) []map[string]any { + out := make([]map[string]any, 0, n) + for range n { + out = append(out, map[string]any{"filename": "changelog.d/fragment.md", "status": "removed"}) + } + return out + } + cases := []struct { + name string + behindBy int + noMergeBase bool + headFiles []map[string]any + baseFiles []map[string]any + baseStatus string + baseCompareFails bool + wantStop bool + wantReason string + wantCompares int + wantUpdates int + }{ + {name: "changelog-only behind is current", behindBy: 3, headFiles: fileList("pkg/a.go", "changelog.d/19000.md"), baseFiles: fileList("CHANGELOG.md", "changelog.d/18990.md"), wantCompares: 2}, + {name: "fragments-only behind is current", behindBy: 1, headFiles: fileList("pkg/a.go"), baseFiles: fileList("changelog.d/18990.md"), wantCompares: 2}, + {name: "mixed base move is stale", behindBy: 2, headFiles: fileList("pkg/a.go"), baseFiles: fileList("CHANGELOG.md", "pkg/b.go"), wantStop: true, wantReason: basePreparationReady, wantCompares: 2, wantUpdates: 1}, + {name: "nested CHANGELOG.md is not allowlisted", behindBy: 1, headFiles: fileList("pkg/a.go"), baseFiles: fileList("docs/CHANGELOG.md"), wantStop: true, wantReason: basePreparationReady, wantCompares: 2, wantUpdates: 1}, + {name: "base compare error is stale", behindBy: 2, headFiles: fileList("pkg/a.go"), baseCompareFails: true, wantStop: true, wantReason: basePreparationReady, wantCompares: 2, wantUpdates: 1}, + {name: "possibly truncated base file list is stale", behindBy: 40, headFiles: fileList("pkg/a.go"), baseFiles: manyFragments(compareFilesResponseCap), wantStop: true, wantReason: basePreparationReady, wantCompares: 2, wantUpdates: 1}, + {name: "empty base file list is stale", behindBy: 1, headFiles: fileList("pkg/a.go"), baseFiles: []map[string]any{}, wantStop: true, wantReason: basePreparationReady, wantCompares: 2, wantUpdates: 1}, + {name: "unexpected base compare status is stale", behindBy: 1, headFiles: fileList("pkg/a.go"), baseFiles: fileList("CHANGELOG.md"), baseStatus: "diverged", wantStop: true, wantReason: basePreparationReady, wantCompares: 2, wantUpdates: 1}, + {name: "changelog path the PR also changes is stale", behindBy: 1, headFiles: fileList("CHANGELOG.md"), baseFiles: fileList("CHANGELOG.md"), wantStop: true, wantReason: basePreparationReady, wantCompares: 2, wantUpdates: 1}, + {name: "missing merge base is stale without a second compare", behindBy: 1, noMergeBase: true, headFiles: fileList("pkg/a.go"), wantStop: true, wantReason: basePreparationReady, wantCompares: 1, wantUpdates: 1}, + {name: "zero behind is unchanged", behindBy: 0, headFiles: fileList("pkg/a.go"), baseFiles: fileList("pkg/b.go"), wantCompares: 1}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + var mu sync.Mutex + compares, updates := 0, 0 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if servePRCommitsSigned(w, r) { + return + } + mu.Lock() + defer mu.Unlock() + switch { + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/pulls/7": + _ = json.NewEncoder(w).Encode(map[string]any{"number": 7, "state": "open", "head": map[string]string{"sha": head}, "base": map[string]string{"ref": "main", "sha": "base1"}}) + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/compare/main..."+head: + compares++ + cmp := map[string]any{"status": "diverged", "behind_by": tc.behindBy, "ahead_by": 1, "files": tc.headFiles} + if !tc.noMergeBase { + cmp["merge_base_commit"] = map[string]string{"sha": mergeBase} + } + _ = json.NewEncoder(w).Encode(cmp) + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/compare/"+mergeBase+"...main": + compares++ + if tc.baseCompareFails { + w.WriteHeader(http.StatusInternalServerError) + return + } + status := tc.baseStatus + if status == "" { + status = "ahead" + } + _ = json.NewEncoder(w).Encode(map[string]any{"status": status, "ahead_by": tc.behindBy, "total_commits": tc.behindBy, "files": tc.baseFiles}) + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/branches/main": + _ = json.NewEncoder(w).Encode(map[string]any{"name": "main", "commit": map[string]string{"sha": "base1"}}) + case r.Method == http.MethodPut && r.URL.Path == "/repos/acme/widget/pulls/7/update-branch": + updates++ + w.WriteHeader(http.StatusAccepted) + _, _ = w.Write([]byte(`{"message":"Updating pull request branch."}`)) + default: + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + w.WriteHeader(http.StatusNotFound) + } + })) + defer srv.Close() + + c := newAutoMergeSweepClient(srv.URL) + c.requireCurrentBase = func(repo string) bool { return strings.EqualFold(repo, "acme/widget") } + _, reason, stop, err := c.currentBaseGate(context.Background(), "acme/widget", "acme", "widget", 7, "main", head, true, "behind", nil) + if err != nil { + t.Fatalf("err = %v", err) + } + if stop != tc.wantStop || reason != tc.wantReason { + t.Fatalf("stop, reason = %v, %q; want %v, %q", stop, reason, tc.wantStop, tc.wantReason) + } + mu.Lock() + defer mu.Unlock() + if compares != tc.wantCompares || updates != tc.wantUpdates { + t.Fatalf("compares, updates = %d, %d; want %d, %d", compares, updates, tc.wantCompares, tc.wantUpdates) + } + }) + } +} + +func TestIsChangelogOnlyPath(t *testing.T) { + for path, want := range map[string]bool{ + "CHANGELOG.md": true, + "changelog.d/19000.md": true, + "changelog.d/sub/fragment": true, + "changelog.d/": false, + "changelog.d": false, + "changelog.d/../go.mod": false, + "docs/CHANGELOG.md": false, + "src/changelog.d/19000.md": false, + "changelog.dx/19000.md": false, + "changelog.md": false, + "CHANGELOG.md.bak": false, + "src/pkg/github/merge.go": false, + } { + if got := isChangelogOnlyPath(path); got != want { + t.Errorf("isChangelogOnlyPath(%q) = %v, want %v", path, got, want) + } + } +}