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
2 changes: 1 addition & 1 deletion plugins/stewards/carries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export async function evaluateCarries(target: DeploymentTarget, context: Carries
// only settle a watch whose merge is already on the default branch. Require
// the deployment's ref to contain the merge, exactly as ref-contains-merge
// does: a stacked pull request's squash merge is not on main, so it stays
// not-yet until the BB-215 override re-points the watch. The record path
// not-yet until it reaches the default branch. The record path
// (`liveAtMergeVerdict`) settles a normal merge at merge time, so this read
// only runs for a watch left awaiting a deployment.
if (rule.strategy.kind === "live-at-merge") {
Expand Down
49 changes: 4 additions & 45 deletions plugins/stewards/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,6 @@ describe("recording merged pull requests", () => {
expect(recordMerged(first, observed(), [target], NOW + 1000)).toBe(first);
});

it("carries a stacked pull request by the base pull request's merge on the default branch (BB-215)", () => {
const stacked = pull({ key: "acme/web#20" }, { merged: true, mergeCommit: "stacked1", files: ["plugins/stewards/a.ts"] });
const first = recordMerged({}, { entity: stacked, pulls: [] }, [target], NOW);
expect(first["acme/web#20"]).toMatchObject({ mergeSha: "stacked1", prRepo: "acme/web", files: ["plugins/stewards/a.ts"] });
// The base pull request's merge lands on the default branch: the override
// re-points the watch to it, so a carries check finds it on main.
const base = { mergeSha: "base0001", prRepo: "acme/web", files: ["plugins/stewards/a.ts", "plugins/stewards/b.ts"] };
const after = recordMerged(first, { entity: stacked, pulls: [] }, [target], NOW + 1000, () => base);
expect(after["acme/web#20"]).toMatchObject({ mergeSha: "base0001", prRepo: "acme/web", files: ["plugins/stewards/a.ts", "plugins/stewards/b.ts"] });
});

it("applies the landedIn override only to the pull request that merged into the base (BB-215)", () => {
const stacked = pull({ key: "acme/web#20" }, { merged: true, mergeCommit: "stacked1", base: "feature-base", head: "feature-stacked" });
const direct = pull({ key: "acme/web#21" }, { merged: true, mergeCommit: "direct1", base: "main", head: "feature-direct" });
const first = recordMerged({}, { entity: stacked, pulls: [direct] }, [target], NOW);
const base = { mergeSha: "base0001", prRepo: "acme/web", files: ["plugins/stewards/a.ts"] };
const after = recordMerged(first, { entity: stacked, pulls: [direct] }, [target], NOW + 1000, (p) => (p.pull?.base === "feature-base" ? base : null));
// The stacked pull request follows the base; the direct one keeps its own merge.
expect(after["acme/web#20"]).toMatchObject({ mergeSha: "base0001" });
expect(after["acme/web#21"]).toMatchObject({ mergeSha: "direct1" });
});

it("stores the merge commit, not the head, and notes an unknown oid", () => {
const merged = pull({ key: "acme/web#9" }, { merged: true, headSha: "headsha1", mergeCommit: "mergesha" });
const deployments = recordMerged({}, { entity: merged, pulls: [] }, [target], NOW);
Expand Down Expand Up @@ -267,35 +245,16 @@ describe("recording merged pull requests", () => {
});

// BB-260: a stacked pull request merged into its base branch is not on main,
// so it is not live at merge. The BB-215 override re-points it once the base
// lands, and the live check is re-run against the base's files.
// so it is not live at merge. Under bottom-up (BB-264) it is retargeted and
// merged to main rather than carried by the base's merge, so the live check
// reads its own base.
it("does not mark a stacked .bb-only merge deployed before it reaches the default branch", () => {
const stacked = pull({ key: "bitcomplete/bb-gate#20", unit: "bitcomplete/bb-gate" }, { merged: true, mergeCommit: "stacked1", base: "feature-base", head: "feature-stacked", files: [".bb/AGENTS.md"] });
const isDefaultBase = (_repo: string, base: string | null) => base === "main";
const recorded = recordMerged({}, { entity: stacked, pulls: [] }, [liveAtMergeTarget], NOW, undefined, isDefaultBase);
const recorded = recordMerged({}, { entity: stacked, pulls: [] }, [liveAtMergeTarget], NOW, isDefaultBase);
expect(recorded["bitcomplete/bb-gate#20::bb-gate"]).toMatchObject({ state: "merged", mergeSha: "stacked1", deadlineAt: NOW + liveAtMergeTarget.timeout });
});

it("keeps a stacked watch merged when the override's base is not live at merge", () => {
const stacked = pull({ key: "bitcomplete/bb-gate#21", unit: "bitcomplete/bb-gate" }, { merged: true, mergeCommit: "stacked2", base: "feature-base", head: "feature-stacked", files: [".bb/AGENTS.md"] });
const isDefaultBase = (_repo: string, base: string | null) => base === "main";
const first = recordMerged({}, { entity: stacked, pulls: [] }, [liveAtMergeTarget], NOW, undefined, isDefaultBase);
expect(first["bitcomplete/bb-gate#21::bb-gate"]).toMatchObject({ state: "merged" });
const base = { mergeSha: "base0001", prRepo: "bitcomplete/bb-gate", files: [".bb/AGENTS.md", "cmd/router/main.go"] };
const after = recordMerged(first, { entity: stacked, pulls: [] }, [liveAtMergeTarget], NOW + 1000, () => base, isDefaultBase);
expect(after["bitcomplete/bb-gate#21::bb-gate"]).toMatchObject({ state: "merged", mergeSha: "base0001", files: [".bb/AGENTS.md", "cmd/router/main.go"] });
});

it("marks a stacked watch deployed when the override's base is live at merge", () => {
const stacked = pull({ key: "bitcomplete/bb-gate#22", unit: "bitcomplete/bb-gate" }, { merged: true, mergeCommit: "stacked3", base: "feature-base", head: "feature-stacked", files: ["cmd/router/main.go"] });
const isDefaultBase = (_repo: string, base: string | null) => base === "main";
const first = recordMerged({}, { entity: stacked, pulls: [] }, [liveAtMergeTarget], NOW, undefined, isDefaultBase);
expect(first["bitcomplete/bb-gate#22::bb-gate"]).toMatchObject({ state: "merged" });
const base = { mergeSha: "base0002", prRepo: "bitcomplete/bb-gate", files: [".bb/AGENTS.md"] };
const after = recordMerged(first, { entity: stacked, pulls: [] }, [liveAtMergeTarget], NOW + 1000, () => base, isDefaultBase);
expect(after["bitcomplete/bb-gate#22::bb-gate"]).toMatchObject({ state: "deployed", mergeSha: "base0002", deadlineAt: null, note: "live-at-merge: nothing deploys .bb/**, docs/**, **.md" });
});

it("does not record an open pull request, and reads a ticket's linked merged ones", () => {
expect(recordMerged({}, observed({ entity: pull({ key: "acme/web#7" }, { merged: false }) }), [target], NOW)).toEqual({});
const linked = recordMerged({}, observed({ entity: pull({ key: "acme/web#7", kind: "pull" }, { merged: false }), pulls: [pull({ key: "acme/web#8" }, { merged: true })] }), [target], NOW);
Expand Down
46 changes: 10 additions & 36 deletions plugins/stewards/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,11 @@ export function recordMerged(
observed: Observed,
targets: readonly DeploymentTarget[],
now: number,
overrideFor?: (p: EntitySnapshot) => { mergeSha: string | null; prRepo: string; files: string[] } | null,
/**
* Whether a merged pull request's base is its repo's default branch, so its
* merge is already on `main`. A stacked pull request merged into its base
* branch is not live yet; the BB-215 override re-points it once the base
* lands. Omitted, every base is treated as the default (the pre-BB-260
* behaviour, for callers with no default-branch read).
* branch is not live yet. Omitted, every base is treated as the default
* (the pre-BB-260 behaviour, for callers with no default-branch read).
*/
isDefaultBase?: (prRepo: string, base: string | null) => boolean,
): Deployments {
Expand All @@ -140,26 +138,20 @@ export function recordMerged(
const next = { ...deployments };
let changed = false;
for (const p of pulls) {
// A stacked pull request that landed in a base pull request (BB-215) is
// carried by the base's merge on the default branch, not by the stacked
// merge a squash rewrote: the override supplies that merge's commit and
// the base's repo and files, so the carries rule matches what is on main.
const over = overrideFor?.(p) ?? null;
const merge = over === null ? p.pull?.mergeCommit ?? null : over.mergeSha;
const files = over === null ? p.pull?.files ?? [] : over.files;
const prRepo = over === null ? p.unit : over.prRepo;
const merge = p.pull?.mergeCommit ?? null;
const files = p.pull?.files ?? [];
const prRepo = p.unit;
// BB-260: a live-at-merge merge is only live once it is on the default
// branch. A stacked pull request merged into its base branch is not live
// yet; the override, when it lands, re-points the watch to the base's
// merge on main. A base the read could not name is not treated as default.
const onDefault = over !== null || (isDefaultBase?.(p.unit, p.pull?.base ?? null) ?? true);
// yet. A base the read could not name is not treated as default.
const onDefault = isDefaultBase?.(p.unit, p.pull?.base ?? null) ?? true;
// An existing watch's own recorded repo and files decide applicability
// when it has them: a snapshot whose files read came back empty must not
// drop an in-flight watch. The fresh snapshot is the fallback.
const contextFor = (target: DeploymentTarget): { prRepo: string; files: readonly string[] } => {
const id = watchId(p.key, target, multiple);
const existing = next[id] ?? (id === p.key ? undefined : next[p.key]);
if (over === null && existing !== undefined && existing.prRepo !== "" && matchesTarget(target, existing.repo, existing.environment)) return { prRepo: existing.prRepo, files: existing.files };
if (existing !== undefined && existing.prRepo !== "" && matchesTarget(target, existing.repo, existing.environment)) return { prRepo: existing.prRepo, files: existing.files };
return { prRepo, files };
};
const applicable = targets.filter((t) => {
Expand Down Expand Up @@ -193,32 +185,14 @@ export function recordMerged(
if (merge !== null && existing.mergeSha !== merge) {
const resolved = `Merge commit re-resolved to ${merge.slice(0, 7)}`;
// A `merged` watch that already holds a success deployment was judged
// against the old sha (BB-215): moving it back to `deploying` lets the
// against the old sha (BB-259): moving it back to `deploying` lets the
// reconcile's carries re-evaluation loop settle it against the
// re-resolved sha in the same pass, rather than waiting for a new
// deployment or timing out. Its stale not-yet note is replaced, so it
// never says a pin does not contain the sha it was re-resolved to.
const reevaluate = existing.state === "merged" && existing.deploymentId !== null && existing.deploymentState === "success";
const note = reevaluate ? resolved : existing.note === null || existing.note === undefined || existing.note === "" ? resolved : `${existing.note} (${resolved})`;
// BB-260: when the BB-215 override re-points the watch to the base
// merge on the default branch, re-run the live check against the
// base's files. A live rule marks it deployed; a watch live-marked
// on a stacked merge whose base is not live goes back to `merged`
// and awaits a deployment.
const live = over === null ? null : liveVerdict(target, prRepo, files);
let state = reevaluate ? "deploying" : existing.state;
let deadlineAt = existing.deadlineAt;
let finalNote = note;
if (live !== null) {
state = "deployed";
deadlineAt = null;
finalNote = live.note;
} else if (over !== null && existing.state === "deployed" && existing.deploymentId === null) {
state = "merged";
deadlineAt = now + target.timeout;
finalNote = resolved;
}
next[id] = { ...existing, state, mergeSha: merge, deadlineAt, ...(over === null ? {} : { prRepo, files }), note: finalNote };
next[id] = { ...existing, state: reevaluate ? "deploying" : existing.state, mergeSha: merge, note };
changed = true;
}
continue;
Expand Down
25 changes: 25 additions & 0 deletions plugins/stewards/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ export interface GithubWriter {
dispatchWorkflow(repo: string, workflow: string, ref: string, inputs: Record<string, string>): Promise<void>;
/** A repository's default branch, for the base of a lesson's pull request (BB-108). */
defaultBranch(repo: string): Promise<string | null>;
/**
* The newest pull request in `repo` whose head branch is `head`, across
* every state, or null when none is visible. A stacked pull request's base
* names the branch of the pull request it is stacked on; the plugin reads
* this to tell an open base (a merge hold) from a merged one (a retarget).
*/
pullByHead(repo: string, head: string): Promise<{ number: number; key: string; state: string; merged: boolean } | null>;
/** Open a pull request and return its URL and number (BB-108). */
createPull(repo: string, args: { head: string; base: string; title: string; body: string }): Promise<{ url: string; number: number }>;
/** Close a pull request without merging it (BB-108 Drop). */
Expand Down Expand Up @@ -185,6 +192,24 @@ export function createGhWriter(token: TokenReader = createGhTokenReader(), doFet
const body = (await response.json().catch(() => null)) as { default_branch?: unknown } | null;
return typeof body?.default_branch === "string" && body.default_branch !== "" ? body.default_branch : null;
},
async pullByHead(repo, head) {
// GitHub's `head` filter is `user:ref-name` and only matches same-repo
// pull requests when `user` is the repository owner. The list is newest
// first; the first match is the pull request the base branch belongs to.
const owner = repo.split("/")[0] ?? "";
const query = `head=${encodeURIComponent(`${owner}:${head}`)}&state=all&sort=created&direction=desc&per_page=100`;
const response = await request("GET", `/repos/${repo}/pulls?${query}`, undefined);
const body = (await response.json().catch(() => null)) as unknown;
if (!Array.isArray(body)) return null;
for (const item of body) {
if (item === null || typeof item !== "object") continue;
const pr = item as { number?: unknown; state?: unknown; merged_at?: unknown; head?: { ref?: unknown } };
if (typeof pr.number !== "number") continue;
if (typeof pr.head?.ref === "string" && pr.head.ref !== head) continue;
return { number: pr.number, key: `${repo}#${pr.number}`, state: typeof pr.state === "string" ? pr.state : "", merged: pr.merged_at !== null && pr.merged_at !== undefined };
}
return null;
},
async dispatchWorkflow(repo, workflow, ref, inputs) {
await request("POST", `/repos/${repo}/actions/workflows/${encodeURIComponent(workflow)}/dispatches`, { ref, inputs });
},
Expand Down
3 changes: 2 additions & 1 deletion plugins/stewards/migrations.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,6 @@
"a4c47117eadbd16a6521f838e277fe6b56dd463a81bcf64e26907ea56ae68361",
"dab0f2a29dd1b4d8d65a58fb712c4d6689105d6c5bbe90e95dc3accff924c3f1",
"699df5c091ae8f12adf16a45faad54c866cd28c58963b3a9132ab6708617d35b",
"0cb5723b6d354beb4c2e6e60fabb5b5e3ddf0245bc755b3b4bcd3cbce6b6fd56"
"0cb5723b6d354beb4c2e6e60fabb5b5e3ddf0245bc755b3b4bcd3cbce6b6fd56",
"a8f6be062abc53e51ea5b4acd27627cf915242aa1b0a60e7e32dc3184ab65b13"
]
Loading
Loading