@intentius/chant 0.124.89 → 0.124.90
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ci-comment-commands.d.ts +18 -2
- package/dist/ci-comment-commands.d.ts.map +1 -1
- package/dist/ci-comment-commands.js +30 -2
- package/dist/ci-job-guard.d.ts +42 -45
- package/dist/ci-job-guard.d.ts.map +1 -1
- package/dist/ci-job-guard.js +206 -3
- package/dist/ci-note-commands.d.ts +41 -1
- package/dist/ci-note-commands.d.ts.map +1 -1
- package/dist/ci-note-commands.js +64 -5
- package/dist/ci-product.d.ts +41 -1
- package/dist/ci-product.d.ts.map +1 -1
- package/dist/ci-product.js +44 -7
- package/dist/cli/handlers/pr-comment.d.ts +13 -1
- package/dist/cli/handlers/pr-comment.d.ts.map +1 -1
- package/dist/cli/handlers/pr-comment.js +90 -1
- package/dist/cli/handlers/pr-notes.d.ts.map +1 -1
- package/dist/cli/handlers/pr-notes.js +23 -1
- package/dist/cli/handlers/pr.js +16 -14
- package/dist/cli/handlers/run-wave.d.ts.map +1 -1
- package/dist/cli/handlers/run-wave.js +154 -4
- package/dist/cli/main.d.ts.map +1 -1
- package/dist/cli/main.js +17 -0
- package/dist/cli/parse-args.d.ts.map +1 -1
- package/dist/cli/parse-args.js +15 -0
- package/dist/cli/registry.d.ts +8 -1
- package/dist/cli/registry.d.ts.map +1 -1
- package/dist/components/pr-checks.d.ts +28 -1
- package/dist/components/pr-checks.d.ts.map +1 -1
- package/dist/components/pr-checks.js +35 -2
- package/dist/components/pr-pipeline.d.ts +3 -2
- package/dist/components/pr-pipeline.d.ts.map +1 -1
- package/dist/components/pr-pipeline.js +4 -2
- package/dist/components/product-pipeline.d.ts +54 -2
- package/dist/components/product-pipeline.d.ts.map +1 -1
- package/dist/components/product-pipeline.js +59 -5
- package/dist/components/product-plan.d.ts +13 -1
- package/dist/components/product-plan.d.ts.map +1 -1
- package/dist/components/product-plan.js +36 -10
- package/dist/floor-table.data.d.ts.map +1 -1
- package/dist/floor-table.data.js +32 -23
- package/dist/lexicon.d.ts +82 -4
- package/dist/lexicon.d.ts.map +1 -1
- package/dist/lifecycle/gate-applied.d.ts +9 -0
- package/dist/lifecycle/gate-applied.d.ts.map +1 -1
- package/dist/op/apply-status.d.ts +84 -0
- package/dist/op/apply-status.d.ts.map +1 -0
- package/dist/op/apply-status.js +106 -0
- package/dist/op/before-merge-decide.d.ts +102 -0
- package/dist/op/before-merge-decide.d.ts.map +1 -0
- package/dist/op/before-merge-decide.js +142 -0
- package/dist/op/before-merge-done.d.ts +56 -0
- package/dist/op/before-merge-done.d.ts.map +1 -0
- package/dist/op/before-merge-done.js +69 -0
- package/dist/op/before-merge-store.d.ts +20 -0
- package/dist/op/before-merge-store.d.ts.map +1 -0
- package/dist/op/before-merge-store.js +48 -0
- package/dist/op/before-merge.d.ts +150 -0
- package/dist/op/before-merge.d.ts.map +1 -0
- package/dist/op/before-merge.js +207 -0
- package/dist/op/follow-on-run.d.ts +117 -0
- package/dist/op/follow-on-run.d.ts.map +1 -0
- package/dist/op/follow-on-run.js +154 -0
- package/dist/op/follow-on-store.d.ts +17 -0
- package/dist/op/follow-on-store.d.ts.map +1 -0
- package/dist/op/follow-on-store.js +54 -0
- package/dist/op/gate.d.ts +6 -0
- package/dist/op/gate.d.ts.map +1 -1
- package/dist/op/gate.js +10 -1
- package/dist/op/index.d.ts +3 -1
- package/dist/op/index.d.ts.map +1 -1
- package/dist/op/index.js +1 -0
- package/dist/op/op-waves-after-plan.d.ts +13 -0
- package/dist/op/op-waves-after-plan.d.ts.map +1 -1
- package/dist/op/op-waves-evidence-jobs.d.ts +4 -1
- package/dist/op/op-waves-evidence-jobs.d.ts.map +1 -1
- package/dist/op/op-waves-evidence-jobs.js +10 -2
- package/dist/op/op-waves-follow-on.d.ts +162 -0
- package/dist/op/op-waves-follow-on.d.ts.map +1 -0
- package/dist/op/op-waves-follow-on.js +355 -0
- package/dist/op/op-waves-run.d.ts +146 -6
- package/dist/op/op-waves-run.d.ts.map +1 -1
- package/dist/op/op-waves-run.js +871 -99
- package/dist/op/op-waves-split.d.ts +122 -0
- package/dist/op/op-waves-split.d.ts.map +1 -0
- package/dist/op/op-waves-split.js +166 -0
- package/dist/op/op-waves.d.ts +20 -0
- package/dist/op/op-waves.d.ts.map +1 -1
- package/dist/op/op-waves.js +15 -1
- package/dist/op/schedule-overdue.d.ts +15 -1
- package/dist/op/schedule-overdue.d.ts.map +1 -1
- package/dist/op/schedule-overdue.js +18 -2
- package/dist/op/stage-hooks.d.ts +36 -1
- package/dist/op/stage-hooks.d.ts.map +1 -1
- package/dist/op/stage-hooks.js +17 -3
- package/dist/op/wave-stop.d.ts +4 -1
- package/dist/op/wave-stop.d.ts.map +1 -1
- package/dist/op/wave-stop.js +4 -2
- package/dist/pr-post.d.ts.map +1 -1
- package/dist/pr-post.js +2 -1
- package/dist/release-floors.d.ts +3 -1
- package/dist/release-floors.d.ts.map +1 -1
- package/dist/release-floors.js +21 -19
- package/dist/wave-blocked.d.ts +7 -1
- package/dist/wave-blocked.d.ts.map +1 -1
- package/dist/wave-blocked.js +7 -2
- package/dist/wave-runtime.mjs +364 -351
- package/package.json +1 -1
- package/src/ci-comment-commands.test.ts +63 -0
- package/src/ci-comment-commands.ts +43 -4
- package/src/ci-image.test.ts +1 -2
- package/src/ci-job-guard.before-merge.test.ts +148 -0
- package/src/ci-job-guard.follow-on-record.test.ts +137 -0
- package/src/ci-job-guard.follow-on.test.ts +144 -0
- package/src/ci-job-guard.ts +221 -3
- package/src/ci-note-commands.test.ts +76 -1
- package/src/ci-note-commands.ts +96 -7
- package/src/ci-product.ts +55 -7
- package/src/cli/handlers/pr-comment.ts +85 -2
- package/src/cli/handlers/pr-notes.ts +30 -6
- package/src/cli/handlers/pr.ts +14 -11
- package/src/cli/handlers/run-wave.ts +144 -4
- package/src/cli/main.ts +17 -0
- package/src/cli/parse-args.ts +11 -0
- package/src/cli/registry.ts +8 -1
- package/src/components/pr-checks.ts +55 -2
- package/src/components/pr-pipeline.ts +5 -3
- package/src/components/product-pipeline.apply-status.test.ts +50 -0
- package/src/components/product-pipeline.ts +113 -8
- package/src/components/product-plan.test.ts +23 -5
- package/src/components/product-plan.ts +43 -12
- package/src/floor-table.data.ts +32 -23
- package/src/lexicon.ts +78 -1
- package/src/lifecycle/gate-applied.ts +7 -0
- package/src/op/apply-status.test.ts +83 -0
- package/src/op/apply-status.ts +132 -0
- package/src/op/before-merge-decide.ts +190 -0
- package/src/op/before-merge-done.ts +99 -0
- package/src/op/before-merge-store.ts +44 -0
- package/src/op/before-merge.test.ts +201 -0
- package/src/op/before-merge.ts +266 -0
- package/src/op/follow-on-run.ts +206 -0
- package/src/op/follow-on-store.ts +54 -0
- package/src/op/gate.ts +16 -1
- package/src/op/index.ts +5 -0
- package/src/op/op-waves-after-plan.ts +13 -0
- package/src/op/op-waves-evidence-jobs.ts +11 -3
- package/src/op/op-waves-follow-on-record.test.ts +152 -0
- package/src/op/op-waves-follow-on.test.ts +247 -0
- package/src/op/op-waves-follow-on.ts +385 -0
- package/src/op/op-waves-render-spec.test.ts +1 -2
- package/src/op/op-waves-run.ts +971 -93
- package/src/op/op-waves-split-product.test.ts +288 -0
- package/src/op/op-waves-split-step.test.ts +298 -0
- package/src/op/op-waves-split.test.ts +196 -0
- package/src/op/op-waves-split.ts +189 -0
- package/src/op/op-waves.ts +28 -3
- package/src/op/schedule-overdue.test.ts +29 -1
- package/src/op/schedule-overdue.ts +27 -3
- package/src/op/stage-hooks.ts +52 -4
- package/src/op/wave-stop.ts +5 -3
- package/src/pr-post.ts +2 -1
- package/src/release-floors.ts +22 -19
- package/src/wave-blocked.ts +9 -4
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The decision behind `/<product> apply [wave-<n>]` on an open pull request
|
|
3
|
+
* whose waves run before merge (#4984), moved from terragucci's
|
|
4
|
+
* `decideOpen` and `openChecks` (comment-apply.ts) and `decideGitLabApply`
|
|
5
|
+
* (comment-apply-gitlab.ts). The comment job runs it on GitHub and Forgejo
|
|
6
|
+
* (../ci-comment-commands.ts), the note poll on GitLab
|
|
7
|
+
* (../ci-note-commands.ts), each at the default branch's commit with the
|
|
8
|
+
* job's token, so none of the change's code runs here.
|
|
9
|
+
*
|
|
10
|
+
* In order, each refused with a reply that says why:
|
|
11
|
+
*
|
|
12
|
+
* - the pull request is from this repository, into the default branch, and
|
|
13
|
+
* the wave it names is one the waves have;
|
|
14
|
+
* - its head, fetched from the forge's pull request ref, is still the head
|
|
15
|
+
* the forge named while the comment was read;
|
|
16
|
+
* - the requires the product lists (`apply.requires`, chant's requires
|
|
17
|
+
* checks: approved, mergeable, undiverged, checks) pass at that head;
|
|
18
|
+
* - every plan status of the product plan (`<product>/plan`, or one per
|
|
19
|
+
* target) passed on that head: a policy denial fails it, and nothing
|
|
20
|
+
* waives one;
|
|
21
|
+
* - the change touches none of the pipeline's files (the forge's workflow
|
|
22
|
+
* directory and the waves spec by default): the run is the default
|
|
23
|
+
* branch's pipeline, so a change to it applies after it merges;
|
|
24
|
+
* - with the product's reach line, it takes the locks of what it reaches at
|
|
25
|
+
* stage `apply`, and no other open pull request holds one of them.
|
|
26
|
+
*
|
|
27
|
+
* Then it records the decision on `chant/lifecycle` (./before-merge.ts),
|
|
28
|
+
* writes a wave stop for the head when the comment named a wave, or clears
|
|
29
|
+
* one (./wave-stop.ts), and starts the waves for that head: GitHub and
|
|
30
|
+
* Forgejo dispatch the waves workflow on the default branch with the
|
|
31
|
+
* inputs `pr`, `head` and `digest`; GitLab starts a pipeline of the default
|
|
32
|
+
* branch with the same three as variables.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
import { commentTerms, forkReply } from "../ci-comment-replies";
|
|
36
|
+
import type { ApplyRetried, StartedRun } from "../ci-note-commands";
|
|
37
|
+
import type { CiProductWords } from "../ci-product-words";
|
|
38
|
+
import type { ForgeKind } from "../pr-forge";
|
|
39
|
+
import type { PullRequestDetail } from "../pr-forge-reads";
|
|
40
|
+
import type { PrApplyRequire } from "../pr-requires";
|
|
41
|
+
import { pullRequestHeadRef, type ReachRun } from "../lifecycle/reach-locks";
|
|
42
|
+
import type { BeforeMergeDecision, BeforeMergeRecord } from "./before-merge";
|
|
43
|
+
|
|
44
|
+
/** The waves before merge, as the pipeline tells the comment job and the note poll (#4984), in `CHANT_COMMENT_JOBS`. */
|
|
45
|
+
export interface WavesBeforeMergeJobs {
|
|
46
|
+
/** GitHub and Forgejo: the waves workflow's file, which the comment job dispatches. */
|
|
47
|
+
workflow?: string;
|
|
48
|
+
/** The requires the forge is asked (`apply.requires`). Default every one. */
|
|
49
|
+
requires?: PrApplyRequire[];
|
|
50
|
+
/** The statuses that must have passed on the head: the product plan's `<product>/plan`, or one per target. */
|
|
51
|
+
plan: string[];
|
|
52
|
+
/** Paths the change must not touch; one that ends in `/` is a directory. */
|
|
53
|
+
pipelineFiles: string[];
|
|
54
|
+
/** With the product plan's reach line: the lock file the locks at stage apply go in. */
|
|
55
|
+
locks?: { gate?: string };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The pipeline files a before-merge change may not touch by default, by forge: the workflows and the waves specs. */
|
|
59
|
+
export function defaultPipelineFiles(forge: ForgeKind): string[] {
|
|
60
|
+
const workflows = forge === "gitlab" ? [".gitlab-ci.yml", ".gitlab/"] : forge === "forgejo" ? [".forgejo/workflows/"] : [".github/workflows/"];
|
|
61
|
+
return [...workflows, ".chant/op-waves/"];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** What {@link decideWavesBeforeMerge} takes: the comment, and what the job reads and writes with. */
|
|
65
|
+
export interface DecideBeforeMergeInput {
|
|
66
|
+
forge: ForgeKind;
|
|
67
|
+
pr: PullRequestDetail;
|
|
68
|
+
user: string;
|
|
69
|
+
/** The default branch. */
|
|
70
|
+
base: string;
|
|
71
|
+
/** The last wave, when the comment named one. */
|
|
72
|
+
wave?: number;
|
|
73
|
+
op: string;
|
|
74
|
+
/** How many waves there are. */
|
|
75
|
+
count: number;
|
|
76
|
+
jobs: WavesBeforeMergeJobs;
|
|
77
|
+
words: CiProductWords;
|
|
78
|
+
/** The requires that are not met at `head`, as one sentence, or null when they pass. */
|
|
79
|
+
requires(pr: number, head: string): Promise<string | null>;
|
|
80
|
+
/** The head's statuses, newest per context. */
|
|
81
|
+
statuses(sha: string): Promise<ReadonlyArray<{ context: string; state: string }>>;
|
|
82
|
+
/** Git in the job's checkout of the default branch. */
|
|
83
|
+
git: ReachRun;
|
|
84
|
+
cwd?: string;
|
|
85
|
+
/** Take the locks of what the change reaches at stage apply, for `by`; a refusal is the reply. Absent: the pipeline has no reach line, so nothing is locked. */
|
|
86
|
+
lock?(pr: number, head: string, base: string, by: string): Promise<{ ok: true; targets: string[] } | { ok: false; text: string }>;
|
|
87
|
+
/** Write the decision on chant/lifecycle. */
|
|
88
|
+
record(d: BeforeMergeDecision): Promise<BeforeMergeRecord>;
|
|
89
|
+
/** Write the head's wave stop, or clear it (null). */
|
|
90
|
+
stop(head: string, through: number | null): Promise<void>;
|
|
91
|
+
/** Start the waves for the recorded decision. */
|
|
92
|
+
start(r: BeforeMergeRecord): Promise<StartedRun>;
|
|
93
|
+
now?: () => string;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const SHA = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/;
|
|
97
|
+
const BRANCH = /^[A-Za-z0-9][A-Za-z0-9._/-]{0,254}$/;
|
|
98
|
+
const short = (sha: string): string => sha.slice(0, 8);
|
|
99
|
+
const message = (e: unknown): string => (e instanceof Error ? e.message : String(e)).split("\n")[0]!;
|
|
100
|
+
|
|
101
|
+
/** Whether `file` is one of `paths`: equal to one, or under one that ends in `/`. */
|
|
102
|
+
export function touchesPipeline(file: string, paths: readonly string[]): boolean {
|
|
103
|
+
return paths.some((p) => (p.endsWith("/") ? file.startsWith(p) : file === p));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Decide the apply, record it and start the waves (#4984). */
|
|
107
|
+
export async function decideWavesBeforeMerge(i: DecideBeforeMergeInput): Promise<ApplyRetried> {
|
|
108
|
+
const t = commentTerms(i.forge);
|
|
109
|
+
const at = t.at(i.pr.number);
|
|
110
|
+
const verb = i.words.verbs.apply;
|
|
111
|
+
const typed = `/${i.words.product} ${Object.entries(i.words.commands).find(([, c]) => c === "apply")?.[0] ?? "apply"}`;
|
|
112
|
+
const reply = (text: string): ApplyRetried => ({ kind: "reply", text });
|
|
113
|
+
const fail = (reason: string): ApplyRetried => ({ kind: "fail", reason });
|
|
114
|
+
const run = (args: string[]) => i.git(["git", ...args], { ...(i.cwd ? { cwd: i.cwd } : {}) });
|
|
115
|
+
|
|
116
|
+
if (!i.pr.sameRepo) return reply(forkReply(i.words, i.forge));
|
|
117
|
+
if (i.pr.base !== undefined && i.pr.base !== i.base) return reply(`${at} targets ${i.pr.base}, not the default branch ${i.base}, so it is not ${verb[1]} before merge`);
|
|
118
|
+
if (i.wave !== undefined && i.wave > i.count) return reply(`the ${verb[0]} of this ${t.place} runs in ${i.count} wave${i.count === 1 ? "" : "s"}, so there is no \`wave-${i.wave}\``);
|
|
119
|
+
const head = i.pr.headSha;
|
|
120
|
+
if (!head || !SHA.test(head)) return fail(`${at} names no head commit`);
|
|
121
|
+
if (!BRANCH.test(i.base) || i.base.split("/").some((s) => s === ".." || s === "")) return fail(`the default branch ${JSON.stringify(i.base)} is not a branch name this job passes on`);
|
|
122
|
+
|
|
123
|
+
// The head as the forge's pull request ref has it now, and the default branch's commit the change is measured from.
|
|
124
|
+
const headRef = "refs/remotes/chant/before-merge-head";
|
|
125
|
+
const baseRef = `refs/remotes/origin/${i.base}`;
|
|
126
|
+
const fetched = run(["fetch", "-q", "--no-tags", "origin", `+refs/heads/${i.base}:${baseRef}`, `+${pullRequestHeadRef(i.forge, i.pr.number)}:${headRef}`]);
|
|
127
|
+
if (fetched.status !== 0) return fail(`could not fetch ${i.base} and the head of ${at} (${fetched.stderr.trim() || `git exited ${fetched.status}`})`);
|
|
128
|
+
const now = run(["rev-parse", headRef]).stdout.trim();
|
|
129
|
+
if (now !== head) return reply(`${at} moved while this comment was read (its head is now ${short(now) || "unknown"}); comment again once its checks pass`);
|
|
130
|
+
const baseSha = run(["rev-parse", baseRef]).stdout.trim();
|
|
131
|
+
if (!SHA.test(baseSha)) return fail(`could not read the commit of ${i.base}`);
|
|
132
|
+
|
|
133
|
+
let unmet: string | null;
|
|
134
|
+
try {
|
|
135
|
+
unmet = await i.requires(i.pr.number, head);
|
|
136
|
+
} catch (e) {
|
|
137
|
+
return fail(`could not ask whether ${at} is ready (${message(e)})`);
|
|
138
|
+
}
|
|
139
|
+
if (unmet) return reply(`${at} is not ready at ${short(head)}, so nothing is ${verb[1]}: ${unmet}. Comment again once that is fixed and its plan passes`);
|
|
140
|
+
|
|
141
|
+
let statuses: ReadonlyArray<{ context: string; state: string }>;
|
|
142
|
+
try {
|
|
143
|
+
statuses = await i.statuses(head);
|
|
144
|
+
} catch (e) {
|
|
145
|
+
return fail(`could not read the statuses of ${short(head)} (${message(e)})`);
|
|
146
|
+
}
|
|
147
|
+
const unplanned = i.jobs.plan.filter((c) => statuses.find((s) => s.context === c)?.state !== "success");
|
|
148
|
+
if (unplanned.length > 0) {
|
|
149
|
+
return reply(`the plan of ${at} has not passed on its head ${short(head)} (${unplanned.map((c) => `\`${c}\``).join(", ")}), so nothing is ${verb[1]}; push to it or comment the plan again, then comment \`${typed}\` again`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const diff = run(["diff", "--name-only", "--no-renames", `${baseSha}...${head}`]);
|
|
153
|
+
if (diff.status !== 0) return fail(`could not list the files ${at} changes (${diff.stderr.trim()})`);
|
|
154
|
+
const touched = diff.stdout.split("\n").map((l) => l.trim()).filter((f) => f && touchesPipeline(f, i.jobs.pipelineFiles));
|
|
155
|
+
if (touched.length > 0) {
|
|
156
|
+
return reply(`${at} changes \`${touched[0]}\`, and the ${verb[0]} runs the pipeline of ${i.base}, so it is not ${verb[1]} before merge. Merge it, then comment \`${typed}\` on it to ${verb[0]} from the merge commit`);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
let locked: string[] = [];
|
|
160
|
+
if (i.lock) {
|
|
161
|
+
let l;
|
|
162
|
+
try {
|
|
163
|
+
l = await i.lock(i.pr.number, head, baseSha, i.user);
|
|
164
|
+
} catch (e) {
|
|
165
|
+
return fail(`could not take the locks of ${at} (${message(e)})`);
|
|
166
|
+
}
|
|
167
|
+
if (!l.ok) return reply(l.text);
|
|
168
|
+
locked = l.targets;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const decision: BeforeMergeDecision = { pr: i.pr.number, head, base: baseSha, ...(i.wave !== undefined ? { through: i.wave } : {}), at: (i.now ?? (() => new Date().toISOString()))(), by: i.user };
|
|
172
|
+
let record: BeforeMergeRecord;
|
|
173
|
+
try {
|
|
174
|
+
record = await i.record(decision);
|
|
175
|
+
// A wave-<n> stops the waves after it at this head; an apply without one clears an earlier stop.
|
|
176
|
+
await i.stop(head, i.wave !== undefined && i.wave < i.count ? i.wave : null);
|
|
177
|
+
} catch (e) {
|
|
178
|
+
return fail(`could not record the decision for ${short(head)}, so nothing was started (${message(e)})`);
|
|
179
|
+
}
|
|
180
|
+
let started: StartedRun;
|
|
181
|
+
try {
|
|
182
|
+
started = await i.start(record);
|
|
183
|
+
} catch (e) {
|
|
184
|
+
return fail(`recorded the decision for ${short(head)}, and could not start the waves (${message(e)})`);
|
|
185
|
+
}
|
|
186
|
+
const through = i.wave !== undefined && i.wave < i.count ? ` through wave ${i.wave}` : "";
|
|
187
|
+
const locks = locked.length > 0 ? `, holding ${locked.map((x) => `\`${x}\``).join(", ")}` : "";
|
|
188
|
+
const link = started.url ? ` (${started.url})` : "";
|
|
189
|
+
return { kind: "ran", text: `started the waves of ${i.op}${link} on ${at}'s head ${short(head)}${through} for ${i.user}${locks}; each wave's gate decides, and the run says when the head applied`, ...(started.url ? { url: started.url } : {}) };
|
|
190
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The job after a before-merge run's last wave (#4984): `chant run wave --op
|
|
3
|
+
* <op> --wave <last> --merge auto|manual --forge <kind>`, at the default
|
|
4
|
+
* branch's commit, with every wave job's outcome file.
|
|
5
|
+
*
|
|
6
|
+
* It reads the decision the run names again (./before-merge.ts) and checks
|
|
7
|
+
* every wave applied: the outcomes add up to `applied` or `unchanged`, and
|
|
8
|
+
* the decision ran every wave. Then, with `auto`, it asks the forge whether
|
|
9
|
+
* the pull request is still approved at the decided head, merges it only
|
|
10
|
+
* at that head (`mergeAfterApply`, ../pr-merge.ts: open, head unmoved),
|
|
11
|
+
* releases the change's locks and replies. With `manual` it replies that
|
|
12
|
+
* the head applied, and the locks hold until the pull request merges or
|
|
13
|
+
* closes, when the unlock job releases them. A refusal is posted on the
|
|
14
|
+
* pull request and the job exits `waiting` (3), so the run shows it.
|
|
15
|
+
*
|
|
16
|
+
* A run whose `rest` wave started a follow-on run for the next wave the
|
|
17
|
+
* plan found (#4992, ./op-waves-follow-on.ts) is not the last: the job says
|
|
18
|
+
* so and does nothing more, and the follow-on's done job merges.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { CHANT_EXIT } from "../exit-codes";
|
|
22
|
+
import type { CiProductWords } from "../ci-product-words";
|
|
23
|
+
import type { ApplyOutcomeSummary } from "../control/applied";
|
|
24
|
+
import type { PrForge } from "../pr-forge";
|
|
25
|
+
import { mergeAfterApply } from "../pr-merge";
|
|
26
|
+
import { beforeMergeProblems, type BeforeMergeRecord, type BeforeMergeRun } from "./before-merge";
|
|
27
|
+
|
|
28
|
+
/** What {@link beforeMergeDone} takes. */
|
|
29
|
+
export interface BeforeMergeDoneInput {
|
|
30
|
+
op: string;
|
|
31
|
+
/** How many waves the spec has. */
|
|
32
|
+
waves: number;
|
|
33
|
+
run: BeforeMergeRun;
|
|
34
|
+
record: BeforeMergeRecord | undefined;
|
|
35
|
+
/** The waves' outcome, from every job's outcome file. */
|
|
36
|
+
outcome: ApplyOutcomeSummary;
|
|
37
|
+
/** The rest wave of this run started a follow-on run for wave `next` (#4992): that run merges, so this one does nothing more. */
|
|
38
|
+
followOn?: { wave: number; next: number; found: number };
|
|
39
|
+
merge: "auto" | "manual";
|
|
40
|
+
/** The job's client: it replies and reads the approval. */
|
|
41
|
+
forge: Pick<PrForge, "kind" | "addComment"> & Partial<Pick<PrForge, "prApproved">>;
|
|
42
|
+
/** The client holding the merge token (`auto`); default `forge`. */
|
|
43
|
+
merger?: Pick<PrForge, "kind" | "mergePullRequest" | "pullRequestHead" | "upsertComment">;
|
|
44
|
+
/** Release every lock the pull request holds; the targets released. */
|
|
45
|
+
release(pr: number): Promise<string[]>;
|
|
46
|
+
words: CiProductWords;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** What the job did: its exit and its line. */
|
|
50
|
+
export interface BeforeMergeDone {
|
|
51
|
+
code: number;
|
|
52
|
+
text: string;
|
|
53
|
+
merged?: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const short = (sha: string): string => sha.slice(0, 12);
|
|
57
|
+
|
|
58
|
+
/** Check the run and its waves, then merge or say it is ready (#4984). Throws only when the reply cannot be posted. */
|
|
59
|
+
export async function beforeMergeDone(i: BeforeMergeDoneInput): Promise<BeforeMergeDone> {
|
|
60
|
+
const problems = beforeMergeProblems(i.op, i.run, i.record);
|
|
61
|
+
if (problems.length > 0) return { code: CHANT_EXIT.misplaced, text: problems.join("; ") };
|
|
62
|
+
const r = i.record!;
|
|
63
|
+
const at = i.forge.kind === "gitlab" ? `!${r.pr}` : `pull request ${r.pr}`;
|
|
64
|
+
const verb = i.words.verbs.apply;
|
|
65
|
+
const say = async (text: string, code: number, merged?: boolean): Promise<BeforeMergeDone> => {
|
|
66
|
+
await i.forge.addComment(r.pr, `${i.words.product}: ${text}`);
|
|
67
|
+
return { code, text, ...(merged !== undefined ? { merged } : {}) };
|
|
68
|
+
};
|
|
69
|
+
// #4992: not the last run of the head's waves; the follow-on's done job merges and replies.
|
|
70
|
+
if (i.followOn) {
|
|
71
|
+
return { code: CHANT_EXIT.done, text: `wave ${i.followOn.wave} of ${i.followOn.found} ${verb[1]} ${at}'s head ${short(r.head)}, and a follow-on run goes on with wave ${i.followOn.next}; its done job merges, so nothing is merged here` };
|
|
72
|
+
}
|
|
73
|
+
if (r.through !== undefined && r.through < i.waves) {
|
|
74
|
+
return say(`${verb[1]} ${i.op} through wave ${r.through} of ${i.waves} at ${at}'s head ${short(r.head)}; the waves after it wait until the ${verb[0]} is asked for again without a wave, so nothing is merged`, CHANT_EXIT.done);
|
|
75
|
+
}
|
|
76
|
+
if (i.outcome.outcome !== "applied" && i.outcome.outcome !== "unchanged") {
|
|
77
|
+
return say(`the waves of ${i.op} at ${at}'s head ${short(r.head)} ended ${i.outcome.outcome}, so nothing is merged; each wave's job says why`, CHANT_EXIT.waiting);
|
|
78
|
+
}
|
|
79
|
+
if (i.merge === "manual") {
|
|
80
|
+
return say(`every wave of ${i.op} ${verb[1]} ${at}'s head ${short(r.head)}. Merge it when you are ready; its locks hold until it merges or closes`, CHANT_EXIT.done);
|
|
81
|
+
}
|
|
82
|
+
// The approval is read again for the head that applied: one taken back since does not merge.
|
|
83
|
+
if (i.forge.prApproved) {
|
|
84
|
+
const approval = await i.forge.prApproved(r.pr, r.head);
|
|
85
|
+
if (!approval.passed) return say(`every wave of ${i.op} ${verb[1]} ${at}'s head ${short(r.head)}, and it was not merged: it is not approved at that head (${approval.reason}). Merge it by hand once it is`, CHANT_EXIT.waiting, false);
|
|
86
|
+
}
|
|
87
|
+
const merger = i.merger ?? (i.forge as unknown as NonNullable<BeforeMergeDoneInput["merger"]>);
|
|
88
|
+
const merged = await mergeAfterApply({ forge: merger, pr: r.pr, head: r.head, recorded: { pushed: true }, words: i.words });
|
|
89
|
+
if (!merged.merged) return { code: CHANT_EXIT.waiting, text: `${at} was not merged: ${merged.reason ?? "the forge refused"}${merged.posted ? "; the reason is posted on it" : ""}`, merged: false };
|
|
90
|
+
let released: string[] = [];
|
|
91
|
+
let kept = "";
|
|
92
|
+
try {
|
|
93
|
+
released = await i.release(r.pr);
|
|
94
|
+
} catch (e) {
|
|
95
|
+
kept = `; its locks could not be released (${e instanceof Error ? e.message : String(e)}), and the unlock job releases them on close`;
|
|
96
|
+
}
|
|
97
|
+
const locks = released.length > 0 ? ` and released its locks on ${released.join(", ")}` : "";
|
|
98
|
+
return say(`every wave of ${i.op} ${verb[1]} ${at}'s head ${short(r.head)}: merged it${merged.sha ? ` as ${short(merged.sha)}` : ""}${locks}${kept}`, CHANT_EXIT.done, true);
|
|
99
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The before-merge decisions on `chant/lifecycle` (#4984): read and written
|
|
3
|
+
* through the ledger's git, at `_waves/<op>.before-merge.json` from the top
|
|
4
|
+
* of the branch (./before-merge.ts says what a decision is). Kept apart from
|
|
5
|
+
* ./before-merge.ts so the generators and the guard load no git code.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { fetchLifecycleStatus, pushLifecycleStatus, readBlobBySha, readPathSha, writeBlobToPath } from "../lifecycle/git";
|
|
9
|
+
import { BEFORE_MERGE_DIR, beforeMergeDigest, beforeMergeFile, beforeMergeKeyProblem, parseBeforeMerge, renderBeforeMerge, type BeforeMergeDecision, type BeforeMergeFile, type BeforeMergeRecord } from "./before-merge";
|
|
10
|
+
|
|
11
|
+
/** The decision of op `op` for `head`, from `chant/lifecycle` as the remote has it. Undefined when there is none. */
|
|
12
|
+
export async function beforeMergeRecordAt(op: string, head: string, opts: { cwd?: string } = {}): Promise<BeforeMergeRecord | undefined> {
|
|
13
|
+
if (beforeMergeKeyProblem(op, head)) return undefined;
|
|
14
|
+
const fetched = await fetchLifecycleStatus(opts);
|
|
15
|
+
if (fetched.status === "unreachable") throw new Error("chant/lifecycle could not be fetched, so the decision cannot be read");
|
|
16
|
+
const blob = await readPathSha(BEFORE_MERGE_DIR, beforeMergeFile(op), { ...opts, ledgerPrefix: "" });
|
|
17
|
+
if (!blob) return undefined;
|
|
18
|
+
return parseBeforeMerge(await readBlobBySha(blob, opts)).heads[head];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Write a decision for op `op` and push `chant/lifecycle`: the record the
|
|
23
|
+
* run's guard reads. Read, written and pushed again when another writer
|
|
24
|
+
* pushed first, up to three times. Returns the record, digest included.
|
|
25
|
+
*/
|
|
26
|
+
export async function recordBeforeMerge(op: string, decision: BeforeMergeDecision, opts: { cwd?: string } = {}): Promise<BeforeMergeRecord> {
|
|
27
|
+
const problem = beforeMergeKeyProblem(op, decision.head) ?? beforeMergeKeyProblem(op, decision.base);
|
|
28
|
+
if (problem) throw new Error(problem);
|
|
29
|
+
const record: BeforeMergeRecord = { ...decision, digest: beforeMergeDigest(op, decision) };
|
|
30
|
+
const where = { ...opts, ledgerPrefix: "" };
|
|
31
|
+
for (let attempt = 1; attempt <= 3; attempt++) {
|
|
32
|
+
const fetched = await fetchLifecycleStatus(opts);
|
|
33
|
+
if (fetched.status === "unreachable") throw new Error("chant/lifecycle could not be fetched, so the decision was not written");
|
|
34
|
+
const blob = await readPathSha(BEFORE_MERGE_DIR, beforeMergeFile(op), where);
|
|
35
|
+
const file = parseBeforeMerge(blob ? await readBlobBySha(blob, opts) : null);
|
|
36
|
+
const next: BeforeMergeFile = { version: 1, heads: { ...file.heads, [decision.head]: record } };
|
|
37
|
+
await writeBlobToPath(BEFORE_MERGE_DIR, beforeMergeFile(op), renderBeforeMerge(next), `Decide ${op} before merge: pull request ${decision.pr} at ${decision.head.slice(0, 12)}`, { ...where, expectPriorPathSha: blob });
|
|
38
|
+
const pushed = await pushLifecycleStatus(opts);
|
|
39
|
+
if (pushed.status === "stale") continue;
|
|
40
|
+
if (pushed.status === "failed") throw new Error(`could not push the decision to chant/lifecycle (${pushed.stderr.trim()})`);
|
|
41
|
+
return record;
|
|
42
|
+
}
|
|
43
|
+
throw new Error("chant/lifecycle kept moving, so the decision was not written; comment again");
|
|
44
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Waves before merge (#4984): the decision on an open pull request, against
|
|
3
|
+
* a real repository with the forge's pull request ref, the record it writes,
|
|
4
|
+
* and the job after the last wave.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { spawnSync } from "node:child_process";
|
|
8
|
+
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { describe, expect, test } from "vitest";
|
|
12
|
+
import { CHANT_WORDS } from "../ci-product-words";
|
|
13
|
+
import type { PullRequestDetail } from "../pr-forge-reads";
|
|
14
|
+
import { beforeMergeDigest, beforeMergeProblems, parseBeforeMerge, renderBeforeMerge, type BeforeMergeDecision, type BeforeMergeRecord } from "./before-merge";
|
|
15
|
+
import { decideWavesBeforeMerge, defaultPipelineFiles, touchesPipeline, type DecideBeforeMergeInput } from "./before-merge-decide";
|
|
16
|
+
import { beforeMergeDone } from "./before-merge-done";
|
|
17
|
+
|
|
18
|
+
const words = { ...CHANT_WORDS, product: "terragucci" };
|
|
19
|
+
|
|
20
|
+
const git = (cwd: string, ...args: string[]): string => {
|
|
21
|
+
const r = spawnSync("git", args, { cwd, encoding: "utf-8", env: { ...process.env, GIT_AUTHOR_NAME: "t", GIT_AUTHOR_EMAIL: "t@t", GIT_COMMITTER_NAME: "t", GIT_COMMITTER_EMAIL: "t@t" } });
|
|
22
|
+
if (r.status !== 0) throw new Error(r.stderr);
|
|
23
|
+
return r.stdout.trim();
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/** An origin with main, a pull request head on refs/pull/7/head, and a clone of main. */
|
|
27
|
+
function repos(change: Record<string, string> = { "live/app/main.tf": "2" }) {
|
|
28
|
+
const origin = mkdtempSync(join(tmpdir(), "chant-bmd-origin-"));
|
|
29
|
+
git(origin, "init", "-q", "-b", "main");
|
|
30
|
+
mkdirSync(join(origin, "live/app"), { recursive: true });
|
|
31
|
+
writeFileSync(join(origin, "live/app/main.tf"), "1");
|
|
32
|
+
git(origin, "add", ".");
|
|
33
|
+
git(origin, "commit", "-q", "-m", "base");
|
|
34
|
+
const base = git(origin, "rev-parse", "HEAD");
|
|
35
|
+
git(origin, "checkout", "-q", "-b", "feature");
|
|
36
|
+
for (const [f, text] of Object.entries(change)) {
|
|
37
|
+
mkdirSync(join(origin, f, ".."), { recursive: true });
|
|
38
|
+
writeFileSync(join(origin, f), text);
|
|
39
|
+
}
|
|
40
|
+
git(origin, "add", ".");
|
|
41
|
+
git(origin, "commit", "-q", "-m", "head");
|
|
42
|
+
const head = git(origin, "rev-parse", "HEAD");
|
|
43
|
+
git(origin, "update-ref", "refs/pull/7/head", head);
|
|
44
|
+
git(origin, "checkout", "-q", "main");
|
|
45
|
+
const work = mkdtempSync(join(tmpdir(), "chant-bmd-work-"));
|
|
46
|
+
git(work, "clone", "-q", origin, ".");
|
|
47
|
+
return { origin, work, base, head };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const runGit: DecideBeforeMergeInput["git"] = (argv, o) => {
|
|
51
|
+
const r = spawnSync(argv[0]!, argv.slice(1), { encoding: "utf8", ...(o.cwd ? { cwd: o.cwd } : {}) });
|
|
52
|
+
return { status: r.status, stdout: r.stdout ?? "", stderr: r.stderr ?? "" };
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
function input(cwd: string, head: string, over: Partial<DecideBeforeMergeInput> = {}) {
|
|
56
|
+
const log: string[] = [];
|
|
57
|
+
const pr: PullRequestDetail = { number: 7, title: "t", body: "", state: "open", head: "feature", headSha: head, base: "main", sameRepo: true };
|
|
58
|
+
const i: DecideBeforeMergeInput = {
|
|
59
|
+
forge: "github",
|
|
60
|
+
pr,
|
|
61
|
+
user: "alice",
|
|
62
|
+
base: "main",
|
|
63
|
+
op: "tf-apply",
|
|
64
|
+
count: 3,
|
|
65
|
+
jobs: { workflow: "tf-apply.yml", plan: ["terragucci/plan"], pipelineFiles: defaultPipelineFiles("github") },
|
|
66
|
+
words,
|
|
67
|
+
requires: async () => null,
|
|
68
|
+
statuses: async () => [{ context: "terragucci/plan", state: "success" }],
|
|
69
|
+
git: runGit,
|
|
70
|
+
cwd,
|
|
71
|
+
record: async (d: BeforeMergeDecision) => {
|
|
72
|
+
log.push(`record ${d.pr} ${d.head.slice(0, 8)} ${d.through ?? "-"}`);
|
|
73
|
+
return { ...d, digest: beforeMergeDigest("tf-apply", d) };
|
|
74
|
+
},
|
|
75
|
+
stop: async (h, through) => void log.push(`stop ${h.slice(0, 8)} ${through}`),
|
|
76
|
+
start: async (r: BeforeMergeRecord) => {
|
|
77
|
+
log.push(`start ${r.pr} ${r.head.slice(0, 8)} ${r.digest.slice(0, 13)}`);
|
|
78
|
+
return { url: "https://forge/run/1" };
|
|
79
|
+
},
|
|
80
|
+
now: () => "2026-11-10T12:00:00.000Z",
|
|
81
|
+
...over,
|
|
82
|
+
};
|
|
83
|
+
return { i, log };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
describe("the decision before merge (#4984)", () => {
|
|
87
|
+
test("a ready pull request is recorded, its stop set, and the waves started for its head", async () => {
|
|
88
|
+
const { work, base, head } = repos();
|
|
89
|
+
const { i, log } = input(work, head, { wave: 2, lock: async (pr, h, b, by) => ({ ok: true, targets: [`${pr}:${h === head}:${b === base}:${by}`] }) });
|
|
90
|
+
const done = await decideWavesBeforeMerge(i);
|
|
91
|
+
expect(done).toEqual({ kind: "ran", text: `started the waves of tf-apply (https://forge/run/1) on pull request 7's head ${head.slice(0, 8)} through wave 2 for alice, holding \`7:true:true:alice\`; each wave's gate decides, and the run says when the head applied`, url: "https://forge/run/1" });
|
|
92
|
+
expect(log).toEqual([`record 7 ${head.slice(0, 8)} 2`, `stop ${head.slice(0, 8)} 2`, `start 7 ${head.slice(0, 8)} sha256:${beforeMergeDigest("tf-apply", { pr: 7, head, base, through: 2, at: "2026-11-10T12:00:00.000Z", by: "alice" }).slice(7, 13)}`]);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("an apply of every wave clears the head's stop", async () => {
|
|
96
|
+
const { work, head } = repos();
|
|
97
|
+
const { i, log } = input(work, head);
|
|
98
|
+
await decideWavesBeforeMerge(i);
|
|
99
|
+
expect(log[1]).toBe(`stop ${head.slice(0, 8)} null`);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("each refusal says why, and nothing is recorded", async () => {
|
|
103
|
+
const { work, base, head } = repos();
|
|
104
|
+
const cases: Array<[Partial<DecideBeforeMergeInput>, string]> = [
|
|
105
|
+
[{ pr: { number: 7, title: "", body: "", state: "open", head: "f", headSha: head, base: "main", sameRepo: false } }, "a pull request from a fork runs no command"],
|
|
106
|
+
[{ pr: { number: 7, title: "", body: "", state: "open", head: "f", headSha: head, base: "release", sameRepo: true } }, "pull request 7 targets release, not the default branch main"],
|
|
107
|
+
[{ wave: 4 }, "runs in 3 waves, so there is no `wave-4`"],
|
|
108
|
+
[{ pr: { number: 7, title: "", body: "", state: "open", head: "f", headSha: base, base: "main", sameRepo: true } }, `pull request 7 moved while this comment was read (its head is now ${head.slice(0, 8)})`],
|
|
109
|
+
[{ requires: async () => "approved: no approval at this head" }, `pull request 7 is not ready at ${head.slice(0, 8)}, so nothing is applied: approved: no approval at this head`],
|
|
110
|
+
[{ statuses: async () => [{ context: "terragucci/plan", state: "failure" }] }, "the plan of pull request 7 has not passed on its head"],
|
|
111
|
+
[{ lock: async () => ({ ok: false, text: "`live/app` is locked by pull request 9" }) }, "`live/app` is locked by pull request 9"],
|
|
112
|
+
];
|
|
113
|
+
for (const [over, text] of cases) {
|
|
114
|
+
const { i, log } = input(work, head, over);
|
|
115
|
+
const done = await decideWavesBeforeMerge(i);
|
|
116
|
+
expect(done.kind).toBe("reply");
|
|
117
|
+
expect((done as { text: string }).text).toContain(text);
|
|
118
|
+
expect(log).toEqual([]);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test("a change to the pipeline's files applies after it merges", async () => {
|
|
123
|
+
const { work, head } = repos({ ".github/workflows/tf-apply.yml": "x" });
|
|
124
|
+
const { i, log } = input(work, head);
|
|
125
|
+
const done = await decideWavesBeforeMerge(i);
|
|
126
|
+
expect(done).toMatchObject({ kind: "reply" });
|
|
127
|
+
expect((done as { text: string }).text).toBe("pull request 7 changes `.github/workflows/tf-apply.yml`, and the apply runs the pipeline of main, so it is not applied before merge. Merge it, then comment `/terragucci apply` on it to apply from the merge commit");
|
|
128
|
+
expect(log).toEqual([]);
|
|
129
|
+
expect(touchesPipeline(".chant/op-waves/tf-apply.spec.json", defaultPipelineFiles("gitlab"))).toBe(true);
|
|
130
|
+
expect(touchesPipeline(".gitlab-ci.yml", defaultPipelineFiles("gitlab"))).toBe(true);
|
|
131
|
+
expect(touchesPipeline("live/app/main.tf", defaultPipelineFiles("gitlab"))).toBe(false);
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
describe("the record (#4984)", () => {
|
|
136
|
+
const d: BeforeMergeDecision = { pr: 7, head: "a".repeat(40), base: "b".repeat(40), at: "2026-11-10T12:00:00.000Z", by: "alice" };
|
|
137
|
+
const r: BeforeMergeRecord = { ...d, digest: beforeMergeDigest("tf-apply", d) };
|
|
138
|
+
|
|
139
|
+
test("round-trips, keeps one decision per head, and drops what is not one", () => {
|
|
140
|
+
const text = renderBeforeMerge({ version: 1, heads: { [d.head]: r } });
|
|
141
|
+
expect(parseBeforeMerge(text).heads[d.head]).toEqual(r);
|
|
142
|
+
expect(parseBeforeMerge(JSON.stringify({ version: 1, heads: { [d.head]: { ...r, head: "c".repeat(40) }, nope: r } })).heads).toEqual({});
|
|
143
|
+
expect(parseBeforeMerge("not json").heads).toEqual({});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test("the digest covers every field, so an edited decision no longer matches", () => {
|
|
147
|
+
expect(beforeMergeProblems("tf-apply", { pr: "7", head: d.head, digest: r.digest }, r)).toEqual([]);
|
|
148
|
+
expect(beforeMergeProblems("other-op", { pr: "7", head: d.head, digest: r.digest }, r)).toHaveLength(1);
|
|
149
|
+
expect(beforeMergeProblems("tf-apply", { pr: "7", head: d.head, digest: r.digest }, { ...r, by: "mallory" })).toEqual([`before merge: the decision for head ${d.head.slice(0, 12)} was changed after it was made: its fields give another digest`]);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe("the job after the last wave (#4984)", () => {
|
|
154
|
+
const d: BeforeMergeDecision = { pr: 7, head: "a".repeat(40), base: "b".repeat(40), at: "2026-11-10T12:00:00.000Z", by: "alice" };
|
|
155
|
+
const record: BeforeMergeRecord = { ...d, digest: beforeMergeDigest("tf-apply", d) };
|
|
156
|
+
const run = { pr: "7", head: d.head, digest: record.digest };
|
|
157
|
+
const forge = (over: Record<string, unknown> = {}) => {
|
|
158
|
+
const said: string[] = [];
|
|
159
|
+
const merges: Array<{ pr: number; sha?: string }> = [];
|
|
160
|
+
const f = {
|
|
161
|
+
kind: "github" as const,
|
|
162
|
+
addComment: async (_pr: number, body: string) => void said.push(body),
|
|
163
|
+
upsertComment: async (_pr: number, _m: string, body: string) => void said.push(body),
|
|
164
|
+
prApproved: async () => ({ passed: true, reason: "approved by bob" }),
|
|
165
|
+
pullRequestHead: async () => ({ open: true, sha: d.head }),
|
|
166
|
+
mergePullRequest: async (pr: number, m: { sha?: string }) => {
|
|
167
|
+
merges.push({ pr, sha: m.sha });
|
|
168
|
+
return { merged: true, sha: "c".repeat(40) };
|
|
169
|
+
},
|
|
170
|
+
...over,
|
|
171
|
+
};
|
|
172
|
+
return { f, said, merges };
|
|
173
|
+
};
|
|
174
|
+
const applied = { outcome: "applied" as const, applied: ["dev", "prod"], counts: { applied: 2 } };
|
|
175
|
+
|
|
176
|
+
test("auto: merges at the decided head and releases the locks", async () => {
|
|
177
|
+
const { f, said, merges } = forge();
|
|
178
|
+
const done = await beforeMergeDone({ op: "tf-apply", waves: 2, run, record, outcome: applied, merge: "auto", forge: f as never, release: async () => ["live/app"], words });
|
|
179
|
+
expect(done).toMatchObject({ code: 0, merged: true });
|
|
180
|
+
expect(merges).toEqual([{ pr: 7, sha: d.head }]);
|
|
181
|
+
expect(said).toEqual([`terragucci: every wave of tf-apply applied pull request 7's head ${d.head.slice(0, 12)}: merged it as ${"c".repeat(12)} and released its locks on live/app`]);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test("manual says it is ready, a run another head names stops misplaced, and a wave that did not apply merges nothing", async () => {
|
|
185
|
+
const { f, said, merges } = forge();
|
|
186
|
+
expect((await beforeMergeDone({ op: "tf-apply", waves: 2, run, record, outcome: applied, merge: "manual", forge: f as never, release: async () => [], words })).code).toBe(0);
|
|
187
|
+
expect(said.at(-1)).toContain("Merge it when you are ready; its locks hold until it merges or closes");
|
|
188
|
+
expect((await beforeMergeDone({ op: "tf-apply", waves: 2, run: { ...run, head: "e".repeat(40) }, record: undefined, outcome: applied, merge: "auto", forge: f as never, release: async () => [], words })).code).toBe(9);
|
|
189
|
+
const partial = await beforeMergeDone({ op: "tf-apply", waves: 2, run, record, outcome: { outcome: "partial", applied: ["dev"], counts: {} }, merge: "auto", forge: f as never, release: async () => [], words });
|
|
190
|
+
expect(partial.code).toBe(3);
|
|
191
|
+
expect(merges).toEqual([]);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test("an approval taken back since the decision leaves it open", async () => {
|
|
195
|
+
const { f, merges, said } = forge({ prApproved: async () => ({ passed: false, reason: "the approval was dismissed" }) });
|
|
196
|
+
const done = await beforeMergeDone({ op: "tf-apply", waves: 2, run, record, outcome: applied, merge: "auto", forge: f as never, release: async () => [], words });
|
|
197
|
+
expect(done).toMatchObject({ code: 3, merged: false });
|
|
198
|
+
expect(merges).toEqual([]);
|
|
199
|
+
expect(said.at(-1)).toContain("it is not approved at that head (the approval was dismissed)");
|
|
200
|
+
});
|
|
201
|
+
});
|