Skip to content

Commit e25b653

Browse files
committed
merge dev into main for the v2.34.0 release
Promotes the dev integration line onto main. The resulting tree is byte-identical to origin/dev, including package.json at 2.34.0. The package.json conflict is resolved to dev's side, NOT to main's stale 2.33.0. Earlier promotions (#2553, #2507) kept the target's version so the release bump would land on its own "release: vX.Y.Z" commit. That is no longer legal: this very delta adds tests/release-version-line.test.ts, which fails when the in-tree version sits behind the highest release tag. With v2.34.0-preview.20260827 now published, 2.33.0 orders behind it, so a promotion carrying the stale line turns CI red on every shard that runs the suite. The consequence for the release step is that scripts/release.ts skips the bump (release.ts:568, currentVersion === version), so v2.34.0 gets tagged on this merge commit rather than on a separate release commit. The workflow creates the tag itself after publishing and validates expected-sha against the checked-out commit, so the tag still names exactly the audited tree.
2 parents ec51e42 + 7ca954f commit e25b653

601 files changed

Lines changed: 47707 additions & 1979 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
"use strict";
2+
3+
/**
4+
* Deletion planning for branches left behind by closed-without-merge pull
5+
* requests.
6+
*
7+
* GitHub's repository-level `delete_branch_on_merge` only fires on merge, so a
8+
* PR that is closed unmerged leaves its head branch in the repository forever.
9+
* This module decides which of those branches may be deleted; the workflow
10+
* performs the deletion.
11+
*
12+
* Kept as a pure module so the safety rules can be unit-tested without Actions
13+
* and without a live repository.
14+
*/
15+
16+
/** Branches that may never be deleted regardless of pull-request state. */
17+
const PROTECTED_BRANCHES = Object.freeze(["main", "dev", "preview", "gh-pages"]);
18+
19+
/** Default grace period before a closed PR's head branch becomes eligible. */
20+
const DEFAULT_GRACE_DAYS = 14;
21+
22+
function normalizeBranchName(value) {
23+
return String(value || "").trim();
24+
}
25+
26+
/**
27+
* A commit id, lowercased for comparison.
28+
*
29+
* The REST and GraphQL APIs are not consistent about case, and a full 40-character
30+
* sha compared case-sensitively against an abbreviated or upper-case one silently
31+
* reads as "different" - which here would mean "keep", so the failure direction is
32+
* safe, but it would make the guard useless rather than protective. Anything that
33+
* is not a plausible hex object id becomes null, i.e. unknown.
34+
*/
35+
function normalizeOid(value) {
36+
const text = String(value || "").trim().toLowerCase();
37+
return /^[0-9a-f]{7,64}$/.test(text) ? text : null;
38+
}
39+
40+
function isProtectedBranch(name) {
41+
return PROTECTED_BRANCHES.includes(normalizeBranchName(name));
42+
}
43+
44+
function toTimestamp(value) {
45+
if (!value) return null;
46+
const ms = Date.parse(String(value));
47+
return Number.isFinite(ms) ? ms : null;
48+
}
49+
50+
/**
51+
* Reasons a candidate branch is kept. Exported so the workflow can log a
52+
* stable, greppable verdict per branch instead of a free-form sentence.
53+
*/
54+
const KEEP_REASONS = Object.freeze({
55+
PROTECTED: "protected-branch",
56+
MERGED: "pull-request-merged",
57+
OPEN: "open-pull-request",
58+
BASE_OF_OPEN: "base-of-open-pull-request",
59+
CROSS_REPOSITORY: "cross-repository-head",
60+
MISSING_CLOSED_AT: "missing-closed-at",
61+
WITHIN_GRACE: "within-grace-period",
62+
MOVED_SINCE_CLOSE: "branch-moved-since-close",
63+
UNKNOWN_HEAD_SHA: "unknown-head-sha",
64+
});
65+
66+
/**
67+
* Plan deletions for head branches of closed-unmerged pull requests.
68+
*
69+
* Every rule here is a safety rule, and each one exists because the opposite
70+
* behavior destroys work that is still referenced:
71+
*
72+
* - A branch is a candidate only when *every* pull request that ever used it as
73+
* a head is closed and unmerged. One open or merged PR on the same branch
74+
* keeps it, because reopening a PR whose head branch is gone cannot restore
75+
* the commits.
76+
* - A branch that is the base of an open pull request is kept. Deleting it
77+
* closes the stacked child PR that targets it.
78+
* - Cross-repository (fork) heads are never touched: they live in the
79+
* contributor's repository and this token has no business there.
80+
* - A grace period after `closed_at` leaves room to reopen a PR that was
81+
* closed by mistake.
82+
* - The branch must still POINT AT a commit one of those closed pull requests
83+
* had as its head. Matching by NAME alone deletes reused work: `codex/`-style
84+
* names get picked up again all the time, and a branch recreated for new work
85+
* inherits the closed history of every PR that ever used that name. The tip
86+
* moved, so the branch is not the closed PR's branch any more - it only shares
87+
* its label.
88+
* - A branch whose current tip cannot be determined is kept. An unknown tip is
89+
* not evidence of an abandoned branch, and this job's mistakes are not
90+
* recoverable.
91+
*
92+
* @param {object} input
93+
* @param {Array<object>} input.pullRequests Pull requests with
94+
* `headRefName`, `headRefOid`, `baseRefName`, `state`, `merged`, `closedAt`,
95+
* and `isCrossRepository`.
96+
* @param {Array<string|{name: string, oid?: string}>} input.branches Branches
97+
* that currently exist. A bare string carries no tip, which is treated as an
98+
* unknown tip and kept.
99+
* @param {number} [input.now] Current time in milliseconds.
100+
* @param {number} [input.graceDays] Days to wait after `closedAt`.
101+
* @returns {{ deletions: Array<{branch: string, pullRequests: number[]}>,
102+
* keeps: Array<{branch: string, reason: string}> }}
103+
*/
104+
function planClosedPrBranchDeletions({
105+
pullRequests = [],
106+
branches = [],
107+
now = Date.now(),
108+
graceDays = DEFAULT_GRACE_DAYS,
109+
}) {
110+
// Accepts both shapes so an older caller passing bare names still works - it
111+
// just gets the conservative answer, because a name without a tip cannot be
112+
// proven safe to delete.
113+
/** @type {Map<string, string|null>} */
114+
const existing = new Map();
115+
for (const entry of branches) {
116+
const name = normalizeBranchName(typeof entry === "string" ? entry : entry && entry.name);
117+
if (!name) continue;
118+
const oid = typeof entry === "string" ? null : normalizeOid(entry && entry.oid);
119+
existing.set(name, oid);
120+
}
121+
const graceMs = Math.max(0, Number(graceDays) || 0) * 24 * 60 * 60 * 1000;
122+
123+
/** @type {Map<string, object[]>} */
124+
const byHead = new Map();
125+
const openBases = new Set();
126+
127+
for (const pr of pullRequests) {
128+
const head = normalizeBranchName(pr && pr.headRefName);
129+
if (head) {
130+
const list = byHead.get(head) || [];
131+
list.push(pr);
132+
byHead.set(head, list);
133+
}
134+
const isOpen = String(pr && pr.state).toUpperCase() === "OPEN";
135+
if (isOpen) {
136+
const base = normalizeBranchName(pr && pr.baseRefName);
137+
if (base) openBases.add(base);
138+
}
139+
}
140+
141+
const deletions = [];
142+
const keeps = [];
143+
144+
for (const branch of [...existing.keys()].sort()) {
145+
if (isProtectedBranch(branch)) {
146+
keeps.push({ branch, reason: KEEP_REASONS.PROTECTED });
147+
continue;
148+
}
149+
150+
const related = byHead.get(branch) || [];
151+
if (related.length === 0) continue; // No PR ever used it; out of scope.
152+
153+
if (related.some((pr) => pr && pr.isCrossRepository === true)) {
154+
keeps.push({ branch, reason: KEEP_REASONS.CROSS_REPOSITORY });
155+
continue;
156+
}
157+
if (related.some((pr) => pr && pr.merged === true)) {
158+
keeps.push({ branch, reason: KEEP_REASONS.MERGED });
159+
continue;
160+
}
161+
if (related.some((pr) => String(pr && pr.state).toUpperCase() === "OPEN")) {
162+
keeps.push({ branch, reason: KEEP_REASONS.OPEN });
163+
continue;
164+
}
165+
if (openBases.has(branch)) {
166+
keeps.push({ branch, reason: KEEP_REASONS.BASE_OF_OPEN });
167+
continue;
168+
}
169+
170+
const closedTimestamps = related.map((pr) => toTimestamp(pr && pr.closedAt));
171+
if (closedTimestamps.some((ts) => ts === null)) {
172+
keeps.push({ branch, reason: KEEP_REASONS.MISSING_CLOSED_AT });
173+
continue;
174+
}
175+
const newestClosedAt = Math.max(...closedTimestamps);
176+
if (now - newestClosedAt < graceMs) {
177+
keeps.push({ branch, reason: KEEP_REASONS.WITHIN_GRACE });
178+
continue;
179+
}
180+
181+
// The tip check, last because it is the most expensive claim to satisfy and
182+
// the cheaper rules above have already excluded most branches.
183+
//
184+
// A closed PR's head branch is only THIS branch if the branch still points at
185+
// a commit that PR had as its head. Without this, a name reused for new work
186+
// is deleted on the strength of an unrelated PR that happened to share the
187+
// label months earlier - and a deleted branch whose commits were never pushed
188+
// anywhere else is gone.
189+
const currentOid = existing.get(branch) || null;
190+
if (!currentOid) {
191+
keeps.push({ branch, reason: KEEP_REASONS.UNKNOWN_HEAD_SHA });
192+
continue;
193+
}
194+
const closedOids = new Set(
195+
related.map((pr) => normalizeOid(pr && pr.headRefOid)).filter(Boolean),
196+
);
197+
// An empty set means the API gave us no head SHA for any of them, which is the
198+
// unknown case again rather than a licence to delete.
199+
if (closedOids.size === 0) {
200+
keeps.push({ branch, reason: KEEP_REASONS.UNKNOWN_HEAD_SHA });
201+
continue;
202+
}
203+
if (!closedOids.has(currentOid)) {
204+
keeps.push({ branch, reason: KEEP_REASONS.MOVED_SINCE_CLOSE });
205+
continue;
206+
}
207+
208+
deletions.push({
209+
branch,
210+
pullRequests: related
211+
.map((pr) => Number(pr && pr.number))
212+
.filter((n) => Number.isFinite(n))
213+
.sort((a, b) => a - b),
214+
});
215+
}
216+
217+
return { deletions, keeps };
218+
}
219+
220+
module.exports = {
221+
DEFAULT_GRACE_DAYS,
222+
KEEP_REASONS,
223+
PROTECTED_BRANCHES,
224+
isProtectedBranch,
225+
planClosedPrBranchDeletions,
226+
};
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
"use strict";
2+
3+
const { describe, it } = require("node:test");
4+
const assert = require("node:assert/strict");
5+
const {
6+
DEFAULT_GRACE_DAYS,
7+
KEEP_REASONS,
8+
isProtectedBranch,
9+
planClosedPrBranchDeletions,
10+
} = require("./closed-pr-branch-cleanup.cjs");
11+
12+
const NOW = Date.parse("2026-08-26T00:00:00Z");
13+
const DAY = 24 * 60 * 60 * 1000;
14+
const longAgo = new Date(NOW - 60 * DAY).toISOString();
15+
16+
function closedPr(overrides) {
17+
return {
18+
number: 1,
19+
state: "CLOSED",
20+
merged: false,
21+
isCrossRepository: false,
22+
headRefName: "codex/example",
23+
baseRefName: "dev",
24+
closedAt: longAgo,
25+
...overrides,
26+
};
27+
}
28+
29+
function keepReason(result, branch) {
30+
const hit = result.keeps.find((entry) => entry.branch === branch);
31+
return hit ? hit.reason : null;
32+
}
33+
34+
function deletedBranches(result) {
35+
return result.deletions.map((entry) => entry.branch);
36+
}
37+
38+
describe("isProtectedBranch", () => {
39+
it("protects the integration, release, and prerelease lines", () => {
40+
for (const name of ["main", "dev", "preview", "gh-pages"]) {
41+
assert.equal(isProtectedBranch(name), true, name);
42+
}
43+
assert.equal(isProtectedBranch("codex/dev"), false);
44+
});
45+
});
46+
47+
describe("planClosedPrBranchDeletions", () => {
48+
it("deletes a branch whose only pull request closed unmerged past the grace period", () => {
49+
const result = planClosedPrBranchDeletions({
50+
pullRequests: [closedPr({ number: 42, headRefName: "codex/stale" })],
51+
branches: ["codex/stale", "dev"],
52+
now: NOW,
53+
});
54+
assert.deepEqual(deletedBranches(result), ["codex/stale"]);
55+
assert.deepEqual(result.deletions[0].pullRequests, [42]);
56+
});
57+
58+
it("keeps a branch that any merged pull request used as a head", () => {
59+
const result = planClosedPrBranchDeletions({
60+
pullRequests: [
61+
closedPr({ number: 10, headRefName: "codex/reused" }),
62+
closedPr({ number: 11, headRefName: "codex/reused", state: "MERGED", merged: true }),
63+
],
64+
branches: ["codex/reused"],
65+
now: NOW,
66+
});
67+
assert.deepEqual(deletedBranches(result), []);
68+
assert.equal(keepReason(result, "codex/reused"), KEEP_REASONS.MERGED);
69+
});
70+
71+
it("keeps a branch that still has an open pull request", () => {
72+
const result = planClosedPrBranchDeletions({
73+
pullRequests: [
74+
closedPr({ number: 20, headRefName: "codex/active" }),
75+
closedPr({ number: 21, headRefName: "codex/active", state: "OPEN", closedAt: null }),
76+
],
77+
branches: ["codex/active"],
78+
now: NOW,
79+
});
80+
assert.deepEqual(deletedBranches(result), []);
81+
assert.equal(keepReason(result, "codex/active"), KEEP_REASONS.OPEN);
82+
});
83+
84+
it("keeps a closed stack parent while an open child still targets it", () => {
85+
const result = planClosedPrBranchDeletions({
86+
pullRequests: [
87+
closedPr({ number: 30, headRefName: "codex/stack-1" }),
88+
closedPr({
89+
number: 31,
90+
state: "OPEN",
91+
closedAt: null,
92+
headRefName: "codex/stack-2",
93+
baseRefName: "codex/stack-1",
94+
}),
95+
],
96+
branches: ["codex/stack-1", "codex/stack-2"],
97+
now: NOW,
98+
});
99+
assert.deepEqual(deletedBranches(result), []);
100+
assert.equal(keepReason(result, "codex/stack-1"), KEEP_REASONS.BASE_OF_OPEN);
101+
});
102+
103+
it("never touches a fork head branch", () => {
104+
const result = planClosedPrBranchDeletions({
105+
pullRequests: [
106+
closedPr({ number: 40, headRefName: "patch-1", isCrossRepository: true }),
107+
],
108+
branches: ["patch-1"],
109+
now: NOW,
110+
});
111+
assert.deepEqual(deletedBranches(result), []);
112+
assert.equal(keepReason(result, "patch-1"), KEEP_REASONS.CROSS_REPOSITORY);
113+
});
114+
115+
it("waits out the grace period so a mistaken close can be reopened", () => {
116+
const recent = new Date(NOW - 3 * DAY).toISOString();
117+
const result = planClosedPrBranchDeletions({
118+
pullRequests: [closedPr({ number: 50, headRefName: "codex/recent", closedAt: recent })],
119+
branches: ["codex/recent"],
120+
now: NOW,
121+
graceDays: DEFAULT_GRACE_DAYS,
122+
});
123+
assert.deepEqual(deletedBranches(result), []);
124+
assert.equal(keepReason(result, "codex/recent"), KEEP_REASONS.WITHIN_GRACE);
125+
});
126+
127+
it("keeps a branch when a closed pull request has no closed_at timestamp", () => {
128+
const result = planClosedPrBranchDeletions({
129+
pullRequests: [closedPr({ number: 60, headRefName: "codex/unknown", closedAt: null })],
130+
branches: ["codex/unknown"],
131+
now: NOW,
132+
});
133+
assert.deepEqual(deletedBranches(result), []);
134+
assert.equal(keepReason(result, "codex/unknown"), KEEP_REASONS.MISSING_CLOSED_AT);
135+
});
136+
137+
it("refuses to delete a protected branch even if a closed pull request used it", () => {
138+
const result = planClosedPrBranchDeletions({
139+
pullRequests: [closedPr({ number: 70, headRefName: "dev" })],
140+
branches: ["dev", "main", "preview"],
141+
now: NOW,
142+
});
143+
assert.deepEqual(deletedBranches(result), []);
144+
assert.equal(keepReason(result, "dev"), KEEP_REASONS.PROTECTED);
145+
});
146+
147+
it("ignores branches that no pull request ever used", () => {
148+
const result = planClosedPrBranchDeletions({
149+
pullRequests: [closedPr({ number: 80, headRefName: "codex/known" })],
150+
branches: ["codex/known", "codex/never-a-pr"],
151+
now: NOW,
152+
});
153+
assert.deepEqual(deletedBranches(result), ["codex/known"]);
154+
assert.equal(keepReason(result, "codex/never-a-pr"), null);
155+
});
156+
157+
it("only plans deletions for branches that still exist", () => {
158+
const result = planClosedPrBranchDeletions({
159+
pullRequests: [closedPr({ number: 90, headRefName: "codex/already-gone" })],
160+
branches: [],
161+
now: NOW,
162+
});
163+
assert.deepEqual(deletedBranches(result), []);
164+
});
165+
});

0 commit comments

Comments
 (0)