@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
package/src/ci-job-guard.ts
CHANGED
|
@@ -41,9 +41,41 @@
|
|
|
41
41
|
* label. Someone who can edit the workflow can delete it too: the forge's
|
|
42
42
|
* environment and runner settings and the cloud role's trust are what keep
|
|
43
43
|
* a job out.
|
|
44
|
+
*
|
|
45
|
+
* Before merge (#4984): a waves workflow with a before-merge mode carries
|
|
46
|
+
* `beforeMerge: { op }`. A run started for an open pull request's head
|
|
47
|
+
* names it in `CHANT_BEFORE_MERGE_PR`, `CHANT_BEFORE_MERGE_HEAD` and
|
|
48
|
+
* `CHANT_BEFORE_MERGE_DIGEST` (./op/before-merge.ts), and still runs on one
|
|
49
|
+
* of the spec's branches. The guard then fetches `chant/lifecycle` into a
|
|
50
|
+
* directory of its own, with the job's token (GitHub and Forgejo
|
|
51
|
+
* `GITHUB_TOKEN`, GitLab `CI_JOB_TOKEN`), reads the decision for that head,
|
|
52
|
+
* and admits the run only when the pull request and the digest are the
|
|
53
|
+
* decision's and the decision's fields still give its digest. A job of a
|
|
54
|
+
* workflow without the mode refuses any run that names a head.
|
|
55
|
+
*
|
|
56
|
+
* A follow-on run (#4992, ./op/follow-on-run.ts): a job of a `rest` wave
|
|
57
|
+
* carries `rest: <its wave number>`. A run the rest wave's record job
|
|
58
|
+
* started names a later wave in `CHANT_WAVES_WAVE`, with the same decision
|
|
59
|
+
* (admitted as above, so a follow-on for another head or digest is refused)
|
|
60
|
+
* or, for a push, the pushed commit in `CHANT_WAVES_COMMIT`. The guard
|
|
61
|
+
* admits it only in a job of the rest wave and only for a wave after it; any
|
|
62
|
+
* other job refuses a run that names either variable.
|
|
63
|
+
*
|
|
64
|
+
* #4998: and only for the wave the newest record job of its chain started.
|
|
65
|
+
* The guard reads the follow-on records of the op (`_waves/<op>.follow-on.json`
|
|
66
|
+
* on `chant/lifecycle`, as it reads the decisions) and admits the run when
|
|
67
|
+
* the entry for its decided head, or a push's commit, names the run's wave
|
|
68
|
+
* and, before merge, the run's decision digest. A wave started by hand past
|
|
69
|
+
* the one the chain reached is refused, though each wave's gate would ask.
|
|
44
70
|
*/
|
|
71
|
+
import { spawnSync } from "node:child_process";
|
|
72
|
+
import { mkdtempSync } from "node:fs";
|
|
73
|
+
import { tmpdir } from "node:os";
|
|
74
|
+
import { join } from "node:path";
|
|
45
75
|
import { isRunnerGroup, type CiRunner } from "./ci-jobs";
|
|
46
76
|
import { CHANT_EXIT } from "./exit-codes";
|
|
77
|
+
import { BEFORE_MERGE_DIGEST_ENV, BEFORE_MERGE_HEAD_ENV, BEFORE_MERGE_PR_ENV, beforeMergePath, beforeMergeProblems, beforeMergeRunFromEnv, isBeforeMergeRun, parseBeforeMerge, type BeforeMergeRecord, type BeforeMergeRun } from "./op/before-merge";
|
|
78
|
+
import { followOnPath, followOnProblems, followOnRecordProblems, followOnRunFromEnv, OP_WAVES_COMMIT_ENV, OP_WAVES_WAVE_ENV, parseFollowOn, type FollowOnRecord, type FollowOnRun } from "./op/follow-on-run";
|
|
47
79
|
|
|
48
80
|
/** The variable the generated step carries its expectation in. */
|
|
49
81
|
export const JOB_GUARD_ENV = "CHANT_JOB_GUARD";
|
|
@@ -68,6 +100,10 @@ export interface JobGuardSpec {
|
|
|
68
100
|
runnerTags?: string[];
|
|
69
101
|
/** GitHub and Forgejo: the runner must be self-hosted. */
|
|
70
102
|
selfHosted?: boolean;
|
|
103
|
+
/** The waves have a before-merge mode (#4984): a run that names a head runs only as op `op`'s recorded decision. */
|
|
104
|
+
beforeMerge?: { op: string };
|
|
105
|
+
/** The job is one of the `rest` wave's (#4992), rendered as this wave: a follow-on run may name a later one. */
|
|
106
|
+
rest?: number;
|
|
71
107
|
}
|
|
72
108
|
|
|
73
109
|
/** What the job sees. Undefined: the forge did not say. */
|
|
@@ -80,6 +116,18 @@ export interface JobSeen {
|
|
|
80
116
|
refProtected?: string;
|
|
81
117
|
runnerTags?: string[];
|
|
82
118
|
runnerEnvironment?: string;
|
|
119
|
+
/** The decision the run names (#4984), from its variables. */
|
|
120
|
+
beforeMerge?: BeforeMergeRun;
|
|
121
|
+
/** The recorded decision for that head; null when there is none. Read only when the spec has a before-merge mode. */
|
|
122
|
+
beforeMergeRecord?: BeforeMergeRecord | null;
|
|
123
|
+
/** Why the decisions could not be read, when they could not. */
|
|
124
|
+
beforeMergeError?: string;
|
|
125
|
+
/** The follow-on variables the run names (#4992). */
|
|
126
|
+
followOn?: FollowOnRun;
|
|
127
|
+
/** The follow-on record of the run's chain (#4998); null when there is none. Read only for a follow-on the rest wave's job would admit. */
|
|
128
|
+
followOnRecord?: FollowOnRecord | null;
|
|
129
|
+
/** Why the follow-on records could not be read, when they could not. */
|
|
130
|
+
followOnError?: string;
|
|
83
131
|
}
|
|
84
132
|
|
|
85
133
|
export interface JobGuardResult {
|
|
@@ -104,6 +152,10 @@ export interface JobGuardInput {
|
|
|
104
152
|
runner?: CiRunner;
|
|
105
153
|
/** GitHub: the job may request an OIDC token (`id-token: write`), so the guard can read its environment. */
|
|
106
154
|
idToken?: boolean;
|
|
155
|
+
/** The waves' before-merge mode (#4984), with the op its decisions are recorded under. */
|
|
156
|
+
beforeMerge?: { op: string };
|
|
157
|
+
/** The job is one of the `rest` wave's (#4992), whose number this is. */
|
|
158
|
+
rest?: number;
|
|
107
159
|
}
|
|
108
160
|
|
|
109
161
|
/**
|
|
@@ -121,6 +173,8 @@ export function jobGuardSpec(input: JobGuardInput): JobGuardSpec {
|
|
|
121
173
|
branches: [...input.branches],
|
|
122
174
|
...(input.forge === "gitlab" && labels.length > 0 ? { runnerTags: labels } : {}),
|
|
123
175
|
...(input.forge !== "gitlab" && labels.includes("self-hosted") ? { selfHosted: true } : {}),
|
|
176
|
+
...(input.beforeMerge ? { beforeMerge: { op: input.beforeMerge.op } } : {}),
|
|
177
|
+
...(input.rest !== undefined ? { rest: input.rest } : {}),
|
|
124
178
|
};
|
|
125
179
|
}
|
|
126
180
|
|
|
@@ -140,6 +194,8 @@ export function parseJobGuardSpec(raw: string | undefined): JobGuardSpec {
|
|
|
140
194
|
if (!Array.isArray(o.branches) || o.branches.some((b) => typeof b !== "string")) throw new Error(`${JOB_GUARD_ENV}.branches is not a list of branches`);
|
|
141
195
|
if (o.forgeEnvironment !== undefined && typeof o.forgeEnvironment !== "string") throw new Error(`${JOB_GUARD_ENV}.forgeEnvironment is not a name`);
|
|
142
196
|
if (o.runnerTags !== undefined && (!Array.isArray(o.runnerTags) || o.runnerTags.some((t) => typeof t !== "string"))) throw new Error(`${JOB_GUARD_ENV}.runnerTags is not a list of tags`);
|
|
197
|
+
if (o.beforeMerge !== undefined && (typeof o.beforeMerge !== "object" || o.beforeMerge === null || typeof o.beforeMerge.op !== "string")) throw new Error(`${JOB_GUARD_ENV}.beforeMerge is { op }`);
|
|
198
|
+
if (o.rest !== undefined && (!Number.isInteger(o.rest) || o.rest < 1)) throw new Error(`${JOB_GUARD_ENV}.rest is the rest wave's number`);
|
|
143
199
|
return o as JobGuardSpec;
|
|
144
200
|
}
|
|
145
201
|
|
|
@@ -194,6 +250,34 @@ export function checkJob(expect: JobGuardSpec, seen: JobSeen): JobGuardResult {
|
|
|
194
250
|
else out.checked.push(`ref ${seen.ref}`);
|
|
195
251
|
}
|
|
196
252
|
|
|
253
|
+
// #4984: a run started for an open pull request's head runs only as the decision recorded for it.
|
|
254
|
+
const run = seen.beforeMerge;
|
|
255
|
+
if (run && isBeforeMergeRun(run)) {
|
|
256
|
+
if (!expect.beforeMerge) out.problems.push(`before merge: the run names pull request ${run.pr ?? "none"}'s head ${run.head ?? "none"}, and this job applies only the pushes to its branches`);
|
|
257
|
+
else if (seen.beforeMergeError !== undefined) out.problems.push(`before merge: the decisions on chant/lifecycle could not be read: ${seen.beforeMergeError}`);
|
|
258
|
+
else {
|
|
259
|
+
const problems = beforeMergeProblems(expect.beforeMerge.op, run, seen.beforeMergeRecord ?? undefined);
|
|
260
|
+
if (problems.length > 0) out.problems.push(...problems);
|
|
261
|
+
else out.checked.push(`before merge: pull request ${run.pr}'s decided head ${run.head!.slice(0, 12)}`);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// #4992: a follow-on run of the rest wave, admitted in the rest wave's jobs alone and for a later wave.
|
|
266
|
+
const bmRun = run !== undefined && isBeforeMergeRun(run);
|
|
267
|
+
const follow = followOnProblems(expect.rest, seen.followOn ?? {}, bmRun);
|
|
268
|
+
out.problems.push(...follow.problems);
|
|
269
|
+
if (follow.checked) {
|
|
270
|
+
// #4998: and only for the wave the newest record job of its chain started.
|
|
271
|
+
const key = (bmRun ? run!.head : seen.followOn?.commit) ?? "";
|
|
272
|
+
const rec = !expect.beforeMerge
|
|
273
|
+
? ["follow-on: this job names no op whose follow-on records it reads, so it admits no follow-on"]
|
|
274
|
+
: seen.followOnError !== undefined
|
|
275
|
+
? [`follow-on: the follow-on records on chant/lifecycle could not be read: ${seen.followOnError}`]
|
|
276
|
+
: followOnRecordProblems(expect.beforeMerge.op, seen.followOn!.wave!, key, bmRun ? (run!.digest ?? "") : undefined, seen.followOnRecord);
|
|
277
|
+
if (rec.length > 0) out.problems.push(...rec);
|
|
278
|
+
else out.checked.push(follow.checked);
|
|
279
|
+
}
|
|
280
|
+
|
|
197
281
|
if (expect.forge === "gitlab") {
|
|
198
282
|
const want = expect.runnerTags ?? [];
|
|
199
283
|
if (want.length > 0) {
|
|
@@ -224,7 +308,71 @@ async function githubClaims(env: NodeJS.ProcessEnv, fetchFn: typeof fetch): Prom
|
|
|
224
308
|
}
|
|
225
309
|
|
|
226
310
|
/** What the job sees, from the forge's variables, and on GitHub from its token's claims when the forge environment is to be checked. */
|
|
227
|
-
export async function jobSeen(expect: JobGuardSpec, env: NodeJS.ProcessEnv = process.env, fetchFn: typeof fetch = fetch): Promise<JobSeen> {
|
|
311
|
+
export async function jobSeen(expect: JobGuardSpec, env: NodeJS.ProcessEnv = process.env, fetchFn: typeof fetch = fetch, readLifecycle: LifecycleFileReader = lifecycleFileByGit): Promise<JobSeen> {
|
|
312
|
+
const seen = await forgeSeen(expect, env, fetchFn);
|
|
313
|
+
// #4992: the follow-on variables, whatever else the run names.
|
|
314
|
+
const follow = followOnRunFromEnv(env);
|
|
315
|
+
if (follow.wave !== undefined || follow.commit !== undefined) seen.followOn = follow;
|
|
316
|
+
// #4984: the decision a run names, read only when the spec has a before-merge mode.
|
|
317
|
+
const run = beforeMergeRunFromEnv(env);
|
|
318
|
+
const bm = isBeforeMergeRun(run);
|
|
319
|
+
if (bm) {
|
|
320
|
+
seen.beforeMerge = run;
|
|
321
|
+
if (expect.beforeMerge && run.head) {
|
|
322
|
+
try {
|
|
323
|
+
const text = readLifecycle(expect.forge, env, beforeMergePath(expect.beforeMerge.op));
|
|
324
|
+
seen.beforeMergeRecord = parseBeforeMerge(text).heads[run.head] ?? null;
|
|
325
|
+
} catch (e) {
|
|
326
|
+
seen.beforeMergeError = (e as Error).message;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
// #4998: the record of the run's chain, read only for a follow-on the rest wave's job would admit.
|
|
331
|
+
const key = bm ? run.head : follow.commit;
|
|
332
|
+
if (expect.beforeMerge && key && followOnProblems(expect.rest, follow, bm).checked !== undefined) {
|
|
333
|
+
try {
|
|
334
|
+
seen.followOnRecord = parseFollowOn(readLifecycle(expect.forge, env, followOnPath(expect.beforeMerge.op))).chains[key] ?? null;
|
|
335
|
+
} catch (e) {
|
|
336
|
+
seen.followOnError = (e as Error).message;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return seen;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** Reads one file of `chant/lifecycle` as the remote has it; null when the branch has no such file (#4984). */
|
|
343
|
+
export type LifecycleFileReader = (forge: JobGuardForge, env: NodeJS.ProcessEnv, path: string) => string | null;
|
|
344
|
+
|
|
345
|
+
/** The repository's git URL and the job token's basic credentials, as the guard fetches with them (#4984). */
|
|
346
|
+
export function guardRemote(forge: JobGuardForge, env: NodeJS.ProcessEnv): { url: string; user: string; token: string } {
|
|
347
|
+
if (forge === "gitlab") {
|
|
348
|
+
if (!env.CI_SERVER_URL || !env.CI_PROJECT_PATH) throw new Error("the job names no repository (no CI_SERVER_URL or CI_PROJECT_PATH)");
|
|
349
|
+
return { url: `${env.CI_SERVER_URL.replace(/\/+$/, "")}/${env.CI_PROJECT_PATH}.git`, user: "gitlab-ci-token", token: env.CI_JOB_TOKEN ?? "" };
|
|
350
|
+
}
|
|
351
|
+
if (!env.GITHUB_SERVER_URL || !env.GITHUB_REPOSITORY) throw new Error("the job names no repository (no GITHUB_SERVER_URL or GITHUB_REPOSITORY)");
|
|
352
|
+
return { url: `${env.GITHUB_SERVER_URL.replace(/\/+$/, "")}/${env.GITHUB_REPOSITORY}.git`, user: "x-access-token", token: env.GITHUB_TOKEN ?? "" };
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* {@link LifecycleFileReader} through git: `chant/lifecycle` fetched at depth
|
|
357
|
+
* one into a directory of the guard's own, outside the checkout, with the
|
|
358
|
+
* token as a basic credential header, and the file shown from it. The
|
|
359
|
+
* `node -e` program does the same.
|
|
360
|
+
*/
|
|
361
|
+
export const lifecycleFileByGit: LifecycleFileReader = (forge, env, path) => {
|
|
362
|
+
const remote = guardRemote(forge, env);
|
|
363
|
+
const dir = mkdtempSync(join(tmpdir(), "chant-guard-"));
|
|
364
|
+
const git = (args: string[]) => spawnSync("git", args, { cwd: dir, encoding: "utf-8", env: { ...env, GIT_TERMINAL_PROMPT: "0" } });
|
|
365
|
+
const init = git(["init", "-q"]);
|
|
366
|
+
if (init.status !== 0) throw new Error(`git init failed: ${(init.stderr || "").trim()}`);
|
|
367
|
+
const auth = `Authorization: Basic ${Buffer.from(`${remote.user}:${remote.token}`).toString("base64")}`;
|
|
368
|
+
const fetched = git(["-c", `http.extraHeader=${auth}`, "fetch", "-q", "--depth=1", remote.url, "refs/heads/chant/lifecycle"]);
|
|
369
|
+
if (fetched.status !== 0) throw new Error(`could not fetch chant/lifecycle: ${(fetched.stderr || "").trim().split("\n").pop()}`);
|
|
370
|
+
const shown = git(["show", `FETCH_HEAD:${path}`]);
|
|
371
|
+
return shown.status === 0 ? shown.stdout : null;
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
/** What the forge's own variables and token say, before the decision is read. */
|
|
375
|
+
async function forgeSeen(expect: JobGuardSpec, env: NodeJS.ProcessEnv, fetchFn: typeof fetch): Promise<JobSeen> {
|
|
228
376
|
if (expect.forge === "gitlab") {
|
|
229
377
|
return {
|
|
230
378
|
forgeEnvironment: env.CI_ENVIRONMENT_NAME || null,
|
|
@@ -262,6 +410,68 @@ export function describeJobGuard(expect: JobGuardSpec, result: JobGuardResult):
|
|
|
262
410
|
return { ok: true, out: [`${at}: ${result.checked.length ? result.checked.join(", ") : "nothing to check"}${skipped}`], err: [] };
|
|
263
411
|
}
|
|
264
412
|
|
|
413
|
+
/**
|
|
414
|
+
* The before-merge part of the `node -e` program (#4984): the run's three
|
|
415
|
+
* variables, the read of `chant/lifecycle` as {@link lifecycleFileByGit}
|
|
416
|
+
* does it, the decision entry as {@link parseBeforeMerge} keeps it, and the
|
|
417
|
+
* lines of {@link beforeMergeProblems}, digest included. No single quote,
|
|
418
|
+
* since the program is one shell word in single quotes.
|
|
419
|
+
*/
|
|
420
|
+
const BM_PATH = beforeMergePath("\u0000").split("\u0000") as [string, string];
|
|
421
|
+
/** Each apostrophe in the program's strings as `'`, since the whole program is one single-quoted shell word. */
|
|
422
|
+
const noQuote = (s: string): string => s.replace(/'/g, "\\u0027");
|
|
423
|
+
const BEFORE_MERGE_JS = [
|
|
424
|
+
`const bmV=[${JSON.stringify(BEFORE_MERGE_PR_ENV)},${JSON.stringify(BEFORE_MERGE_HEAD_ENV)},${JSON.stringify(BEFORE_MERGE_DIGEST_ENV)}].map((k)=>String(E[k]||"").trim());`,
|
|
425
|
+
'const bmRun={};if(bmV[0])bmRun.pr=bmV[0];if(bmV[1])bmRun.head=bmV[1];if(bmV[2])bmRun.digest=bmV[2];const bmOn=Boolean(bmV[0]||bmV[1]||bmV[2]);',
|
|
426
|
+
"const SHA=/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/;",
|
|
427
|
+
String.raw`const bmRead=(file)=>{const cp=require("child_process"),fs=require("fs"),os=require("os"),pa=require("path");let url,user,tok;`,
|
|
428
|
+
String.raw`if(x.forge==="gitlab"){if(!E.CI_SERVER_URL||!E.CI_PROJECT_PATH)throw new Error("the job names no repository (no CI_SERVER_URL or CI_PROJECT_PATH)");url=E.CI_SERVER_URL.replace(/\/+$/,"")+"/"+E.CI_PROJECT_PATH+".git";user="gitlab-ci-token";tok=E.CI_JOB_TOKEN||""}`,
|
|
429
|
+
String.raw`else{if(!E.GITHUB_SERVER_URL||!E.GITHUB_REPOSITORY)throw new Error("the job names no repository (no GITHUB_SERVER_URL or GITHUB_REPOSITORY)");url=E.GITHUB_SERVER_URL.replace(/\/+$/,"")+"/"+E.GITHUB_REPOSITORY+".git";user="x-access-token";tok=E.GITHUB_TOKEN||""}`,
|
|
430
|
+
String.raw`const d=fs.mkdtempSync(pa.join(os.tmpdir(),"chant-guard-"));const g=(a)=>cp.spawnSync("git",a,{cwd:d,encoding:"utf-8",env:Object.assign({},E,{GIT_TERMINAL_PROMPT:"0"})});`,
|
|
431
|
+
String.raw`const i=g(["init","-q"]);if(i.status!==0)throw new Error("git init failed: "+String(i.stderr||"").trim());`,
|
|
432
|
+
String.raw`const f=g(["-c","http.extraHeader=Authorization: Basic "+Buffer.from(user+":"+tok).toString("base64"),"fetch","-q","--depth=1",url,"refs/heads/chant/lifecycle"]);`,
|
|
433
|
+
String.raw`if(f.status!==0)throw new Error("could not fetch chant/lifecycle: "+String(f.stderr||"").trim().split("\n").pop());`,
|
|
434
|
+
String.raw`const o=g(["show","FETCH_HEAD:"+file]);return o.status===0?o.stdout:null};`,
|
|
435
|
+
String.raw`const bmEntry=(t,h)=>{let v;try{v=JSON.parse(t===null?"null":t)}catch(e){return null}const hs=v&&typeof v==="object"&&v.version===1?v.heads:undefined;if(!hs||typeof hs!=="object"||!SHA.test(h))return null;const e=hs[h];`,
|
|
436
|
+
String.raw`if(!e||typeof e!=="object"||e.head!==h||!Number.isInteger(e.pr)||e.pr<1||typeof e.base!=="string"||!SHA.test(e.base)||typeof e.at!=="string"||typeof e.by!=="string"||typeof e.digest!=="string")return null;`,
|
|
437
|
+
String.raw`if(e.through!==undefined&&(!Number.isInteger(e.through)||e.through<1))return null;const r={pr:e.pr,head:h,base:e.base,at:e.at,by:e.by,digest:e.digest};if(e.through!==undefined)r.through=e.through;return r};`,
|
|
438
|
+
`const bmSeen=(s)=>{if(!bmOn)return s;s.beforeMerge=bmRun;if(!x.beforeMerge||!bmRun.head)return s;try{s.rec=bmEntry(bmRead(${JSON.stringify(BM_PATH[0])}+x.beforeMerge.op+${JSON.stringify(BM_PATH[1])}),bmRun.head)}catch(e){s.bmErr=e&&e.message?e.message:String(e)}return s};`,
|
|
439
|
+
'const shownJ=(v)=>v===undefined||v===""?"none":JSON.stringify(v);',
|
|
440
|
+
String.raw`const bmProblems=(op,r,rec)=>{const p=[];if(!r.pr||!/^[1-9][0-9]{0,9}$/.test(r.pr))p.push("before merge: the run's pull request "+shownJ(r.pr)+" is not a number");`,
|
|
441
|
+
String.raw`if(!r.head||!SHA.test(r.head))p.push("before merge: the run's head "+shownJ(r.head)+" is not a commit sha");`,
|
|
442
|
+
String.raw`if(!r.digest||!/^sha256:[0-9a-f]{64}$/.test(r.digest))p.push("before merge: the run's digest "+shownJ(r.digest)+" is not a sha256 digest");`,
|
|
443
|
+
String.raw`if(p.length)return p;const h=r.head.slice(0,12);if(!rec)return["before merge: no decision on chant/lifecycle names head "+h+" of "+op+", so this run applies nothing"];`,
|
|
444
|
+
String.raw`if(rec.pr!==Number(r.pr))p.push("before merge: the decision for head "+h+" is pull request "+rec.pr+"'s, not "+r.pr+"'s");`,
|
|
445
|
+
String.raw`if(rec.digest!==r.digest)p.push("before merge: the run carries digest "+r.digest+", and the decision for head "+h+" has "+rec.digest);`,
|
|
446
|
+
String.raw`const dg="sha256:"+require("crypto").createHash("sha256").update(JSON.stringify(["chant-before-merge/1",op,rec.pr,rec.head,rec.base,rec.through===undefined?0:rec.through,rec.at,rec.by])).digest("hex");`,
|
|
447
|
+
String.raw`if(dg!==rec.digest)p.push("before merge: the decision for head "+h+" was changed after it was made: its fields give another digest");return p};`,
|
|
448
|
+
].map(noQuote);
|
|
449
|
+
|
|
450
|
+
const FO_PATH = followOnPath("\u0000").split("\u0000") as [string, string];
|
|
451
|
+
/**
|
|
452
|
+
* The follow-on part of the program (#4992): the run's two variables and the lines of {@link followOnProblems}, and
|
|
453
|
+
* (#4998) the read of the chain's record and the lines of {@link followOnRecordProblems}. After {@link BEFORE_MERGE_JS},
|
|
454
|
+
* whose `SHA`, `bmRead` and decision variables it reads.
|
|
455
|
+
*/
|
|
456
|
+
const FOLLOW_ON_JS = [
|
|
457
|
+
`const foV=[${JSON.stringify(OP_WAVES_WAVE_ENV)},${JSON.stringify(OP_WAVES_COMMIT_ENV)}].map((k)=>String(E[k]||"").trim());const foRun={};if(foV[0])foRun.wave=foV[0];if(foV[1])foRun.commit=foV[1];`,
|
|
458
|
+
String.raw`const foProblems=(rest,r,bm)=>{const w=r.wave,c=r.commit;`,
|
|
459
|
+
String.raw`if(rest===undefined){if(w===undefined&&c===undefined)return{p:[]};return{p:["follow-on: the run names wave "+(w===undefined?"none":w)+" and commit "+(c===undefined?"none":c)+", and this job is none of the rest wave's, which alone runs in a follow-on"]}}`,
|
|
460
|
+
String.raw`if(w===undefined||w===String(rest)){if(c===undefined)return{p:[]};return{p:["follow-on: the run names the commit "+c+" and no wave after wave "+rest+", so it is no follow-on"]}}`,
|
|
461
|
+
String.raw`if(!/^[1-9][0-9]{0,5}$/.test(w)||Number(w)<=rest)return{p:["follow-on: the run's wave "+JSON.stringify(w)+" is no wave after wave "+rest]};`,
|
|
462
|
+
String.raw`if(bm){if(c!==undefined)return{p:["follow-on: a follow-on of a decided head checks out that head, and the run names the commit "+c+" too"]};return{p:[],c:"follow-on wave "+w}}`,
|
|
463
|
+
String.raw`if(c===undefined||!SHA.test(c))return{p:["follow-on: a push's follow-on names the commit it goes on with, and the run's "+(c===undefined?"none":JSON.stringify(c))+" is not a commit sha"]};`,
|
|
464
|
+
String.raw`return{p:[],c:"follow-on wave "+w+" at "+c.slice(0,12)}};`,
|
|
465
|
+
// #4998: the record of the run's chain, as parseFollowOn keeps an entry, and the lines of followOnRecordProblems.
|
|
466
|
+
String.raw`const foEntry=(t,k)=>{let v;try{v=JSON.parse(t===null?"null":t)}catch(e){return null}const cs=v&&typeof v==="object"&&v.version===1?v.chains:undefined;if(!cs||typeof cs!=="object"||!SHA.test(k))return null;const e=cs[k];const W=(n,m)=>Number.isInteger(n)&&n>=m;`,
|
|
467
|
+
String.raw`if(!e||typeof e!=="object"||e.key!==k||!W(e.wave,2)||!W(e.from,1)||typeof e.run!=="string"||typeof e.at!=="string")return null;`,
|
|
468
|
+
String.raw`if((e.digest!==undefined&&typeof e.digest!=="string")||(e.pr!==undefined&&!W(e.pr,1))||(e.commit!==undefined&&(typeof e.commit!=="string"||!SHA.test(e.commit)))||!e.outcomes||typeof e.outcomes!=="object")return null;return e};`,
|
|
469
|
+
`const foSeen=(s)=>{const k=bmOn?bmRun.head:foRun.commit;if(!x.beforeMerge||!k||foProblems(x.rest,foRun,bmOn).c===undefined)return s;try{s.foRec=foEntry(bmRead(${JSON.stringify(FO_PATH[0])}+x.beforeMerge.op+${JSON.stringify(FO_PATH[1])}),k)}catch(e){s.foErr=e&&e.message?e.message:String(e)}return s};`,
|
|
470
|
+
String.raw`const foRecProblems=(op,w,k,dg,rec)=>{const what=dg!==undefined?"head":"commit";const sh=k.slice(0,12);if(!rec)return["follow-on: no record job of "+op+" started a follow-on for "+what+" "+sh+", so this run applies nothing"];const p=[];`,
|
|
471
|
+
String.raw`if(String(rec.wave)!==w)p.push("follow-on: the newest record job of "+op+" for "+what+" "+sh+" started wave "+rec.wave+", and this run names wave "+w);`,
|
|
472
|
+
String.raw`if(dg!==undefined&&rec.digest!==dg)p.push("follow-on: wave "+rec.wave+" of "+what+" "+sh+" was started for the decision with digest "+(rec.digest===undefined?"none":rec.digest)+", and the run carries "+dg);return p};`,
|
|
473
|
+
].map(noQuote);
|
|
474
|
+
|
|
265
475
|
/**
|
|
266
476
|
* The guard as one `node -e` program: the same checks as {@link checkJob},
|
|
267
477
|
* {@link jobSeen} and {@link describeJobGuard}, reading {@link JOB_GUARD_ENV}.
|
|
@@ -278,15 +488,17 @@ export const JOB_GUARD_JS = [
|
|
|
278
488
|
"if(!x||![\"github\",\"gitlab\",\"forgejo\"].includes(x.forge)||!Array.isArray(x.branches))fail(\"" + JOB_GUARD_ENV + " is not a guard spec\");",
|
|
279
489
|
'const tags=(r)=>{if(r===undefined)return undefined;const t=r.trim();if(t.startsWith("[")){try{const v=JSON.parse(t);if(Array.isArray(v))return v.map(String)}catch(e){}}return t===""?[]:t.split(",").map((s)=>s.trim()).filter((s)=>s!=="")};',
|
|
280
490
|
'const claimsOf=(tok)=>{const p=tok.split(".")[1];if(!p)throw new Error("the forge\\u0027s OIDC token is not a JWT");const c=JSON.parse(Buffer.from(p.replace(/-/g,"+").replace(/_/g,"/"),"base64").toString("utf-8"));if(!c||typeof c!=="object")throw new Error("the forge\\u0027s OIDC token has no claims");return c};',
|
|
491
|
+
...BEFORE_MERGE_JS,
|
|
492
|
+
...FOLLOW_ON_JS,
|
|
281
493
|
"async function seenOf(){",
|
|
282
|
-
'if(x.forge==="gitlab")return{forgeEnvironment:E.CI_ENVIRONMENT_NAME||null,ref:E.CI_COMMIT_REF_NAME,refProtected:E.CI_COMMIT_REF_PROTECTED,runnerTags:tags(E.CI_RUNNER_TAGS)};',
|
|
494
|
+
'if(x.forge==="gitlab")return foSeen(bmSeen({forgeEnvironment:E.CI_ENVIRONMENT_NAME||null,ref:E.CI_COMMIT_REF_NAME,refProtected:E.CI_COMMIT_REF_PROTECTED,runnerTags:tags(E.CI_RUNNER_TAGS)}));',
|
|
283
495
|
"const s={ref:E.GITHUB_REF};let c;",
|
|
284
496
|
'if(x.forge==="github"&&x.forgeEnvironment!==undefined){try{',
|
|
285
497
|
'if(!E.ACTIONS_ID_TOKEN_REQUEST_URL||!E.ACTIONS_ID_TOKEN_REQUEST_TOKEN)throw new Error("the job may request no OIDC token (no ACTIONS_ID_TOKEN_REQUEST_URL); it needs the id-token: write permission");',
|
|
286
498
|
'const r=await fetch(E.ACTIONS_ID_TOKEN_REQUEST_URL+"&audience="+encodeURIComponent(A),{headers:{authorization:"bearer "+E.ACTIONS_ID_TOKEN_REQUEST_TOKEN}});',
|
|
287
499
|
'if(!r.ok)throw new Error("the OIDC token request answered "+r.status);const v=(await r.json()).value;if(typeof v!=="string")throw new Error("the OIDC token request answered no token");',
|
|
288
500
|
'c=claimsOf(v);s.forgeEnvironment=typeof c.environment==="string"&&c.environment!==""?c.environment:null}catch(e){s.forgeEnvironmentError=e&&e.message?e.message:String(e)}}',
|
|
289
|
-
'const run=c&&typeof c.runner_environment==="string"?c.runner_environment:E.RUNNER_ENVIRONMENT;if(run)s.runnerEnvironment=run;return s}',
|
|
501
|
+
'const run=c&&typeof c.runner_environment==="string"?c.runner_environment:E.RUNNER_ENVIRONMENT;if(run)s.runnerEnvironment=run;return foSeen(bmSeen(s))}',
|
|
290
502
|
"seenOf().then((s)=>{const P=[],C=[],S=[];const fe=x.forgeEnvironment;",
|
|
291
503
|
'if(x.forge==="forgejo")S.push("forge environment (Forgejo has none)");',
|
|
292
504
|
'else if(fe!==undefined){if(s.forgeEnvironmentError!==undefined)P.push("forge environment: expected "+fe+", and it could not be read: "+s.forgeEnvironmentError);else if(s.forgeEnvironment!==fe)P.push("forge environment: expected "+fe+", found "+shown(s.forgeEnvironment));else C.push("forge environment "+fe)}',
|
|
@@ -295,6 +507,12 @@ export const JOB_GUARD_JS = [
|
|
|
295
507
|
'else if(x.forge==="gitlab"){if(!s.ref||!b.includes(s.ref))P.push("ref: expected "+b.join(" or ")+", found "+shown(s.ref));else C.push("ref "+s.ref);',
|
|
296
508
|
'if(s.refProtected!=="true")P.push("ref: expected a protected ref (CI_COMMIT_REF_PROTECTED true), found "+shown(s.refProtected));else C.push("protected ref")}',
|
|
297
509
|
'else{const refs=b.map((v)=>"refs/heads/"+v);if(!s.ref||!refs.includes(s.ref))P.push("ref: expected "+refs.join(" or ")+", found "+shown(s.ref));else C.push("ref "+s.ref)}',
|
|
510
|
+
...[
|
|
511
|
+
String.raw`if(s.beforeMerge){const r=s.beforeMerge;if(!x.beforeMerge)P.push("before merge: the run names pull request "+(r.pr||"none")+"'s head "+(r.head||"none")+", and this job applies only the pushes to its branches");`,
|
|
512
|
+
String.raw`else if(s.bmErr!==undefined)P.push("before merge: the decisions on chant/lifecycle could not be read: "+s.bmErr);`,
|
|
513
|
+
String.raw`else{const Q=bmProblems(x.beforeMerge.op,r,s.rec||undefined);if(Q.length)P.push(...Q);else C.push("before merge: pull request "+r.pr+"'s decided head "+r.head.slice(0,12))}}`,
|
|
514
|
+
].map(noQuote),
|
|
515
|
+
"{const F=foProblems(x.rest,foRun,bmOn);P.push(...F.p);if(F.c){const G=!x.beforeMerge?[\"follow-on: this job names no op whose follow-on records it reads, so it admits no follow-on\"]:s.foErr!==undefined?[\"follow-on: the follow-on records on chant/lifecycle could not be read: \"+s.foErr]:foRecProblems(x.beforeMerge.op,foRun.wave,(bmOn?bmRun.head:foRun.commit)||\"\",bmOn?(bmRun.digest||\"\"):undefined,s.foRec);if(G.length)P.push(...G);else C.push(F.c)}}",
|
|
298
516
|
'if(x.forge==="gitlab"){const w=x.runnerTags||[];if(w.length>0){const h=s.runnerTags;const miss=w.filter((t)=>!(h||[]).includes(t));',
|
|
299
517
|
'if(h===undefined)P.push("runner: expected tags "+w.join(", ")+", and the job has no CI_RUNNER_TAGS");else if(miss.length>0)P.push("runner: expected tags "+w.join(", ")+", found "+(h.length?h.join(", "):"none"));else C.push("runner tags "+w.join(", "))}}',
|
|
300
518
|
'else if(x.selfHosted){if(s.runnerEnvironment===undefined)S.push("runner (the runner says nothing about itself: no RUNNER_ENVIRONMENT)");else if(s.runnerEnvironment!=="self-hosted")P.push("runner: expected self-hosted, found "+s.runnerEnvironment);else C.push("self-hosted runner")}',
|
|
@@ -26,7 +26,9 @@ import {
|
|
|
26
26
|
pollNoteCommands,
|
|
27
27
|
type ChantNoteJobs,
|
|
28
28
|
type NoteCommandRuns,
|
|
29
|
+
type NoteCommandsOptions,
|
|
29
30
|
} from "./ci-note-commands";
|
|
31
|
+
import { beforeMergeDigest } from "./op/before-merge";
|
|
30
32
|
|
|
31
33
|
const NOW = new Date("2026-10-07T12:00:00Z");
|
|
32
34
|
const BOT = 900;
|
|
@@ -125,7 +127,7 @@ beforeEach(() => {
|
|
|
125
127
|
const mr = (iid: number, over: Record<string, unknown> = {}) => ({ iid, state: "opened", sha: HEAD, source_branch: `feat-${iid}`, target_branch: "main", source_project_id: 7, target_project_id: 7, updated_at: recent(5), ...over });
|
|
126
128
|
const note = (id: number, body: string, by = "alice", over: Record<string, unknown> = {}) => ({ id, body, system: false, author: user(by), created_at: recent(10), ...over });
|
|
127
129
|
|
|
128
|
-
async function poll(w: World, o: { jobs?: ChantNoteJobs; runs?: NoteCommandRuns; log?: ReturnType<typeof memoryNoteLog>; minRole?: "developer" | "maintainer" } = {}) {
|
|
130
|
+
async function poll(w: World, o: { jobs?: ChantNoteJobs; runs?: NoteCommandRuns; log?: ReturnType<typeof memoryNoteLog>; minRole?: "developer" | "maintainer"; stops?: NoteCommandsOptions["stops"] } = {}) {
|
|
129
131
|
world = w;
|
|
130
132
|
const opts = { apiBase: base, project: "7", token: "t", host: "gitlab" };
|
|
131
133
|
const log = o.log ?? memoryNoteLog();
|
|
@@ -139,6 +141,7 @@ async function poll(w: World, o: { jobs?: ChantNoteJobs; runs?: NoteCommandRuns;
|
|
|
139
141
|
log,
|
|
140
142
|
now: NOW,
|
|
141
143
|
...(o.minRole ? { minRole: o.minRole } : {}),
|
|
144
|
+
...(o.stops ? { stops: o.stops } : {}),
|
|
142
145
|
});
|
|
143
146
|
return { ...got, log };
|
|
144
147
|
}
|
|
@@ -351,6 +354,78 @@ describe("pollNoteCommands: apply on a merged merge request", () => {
|
|
|
351
354
|
});
|
|
352
355
|
});
|
|
353
356
|
|
|
357
|
+
describe("pollNoteCommands: the waves' stop and the waves before merge (#4984)", () => {
|
|
358
|
+
const members = { [uid("alice")]: 30 };
|
|
359
|
+
const pipelines = [{ id: 10, sha: MERGE, ref: "main", source: "push", status: "failed" }];
|
|
360
|
+
const applyJobs = { 10: [{ id: 2, name: "apply-wave-1", status: "success" }, { id: 3, name: "apply-wave-2", status: "failed" }] };
|
|
361
|
+
const waves = { op: "deploy", count: 3 };
|
|
362
|
+
|
|
363
|
+
test("a merged merge request's wave-<n> writes the stop for the merge commit before the retry, and an apply without one clears it", async () => {
|
|
364
|
+
const stops: unknown[] = [];
|
|
365
|
+
const stopsFn: NoteCommandsOptions["stops"] = async (op, sha, stop) => void stops.push([op, sha, stop && { through: stop.through, by: stop.by, pr: stop.pr }]);
|
|
366
|
+
const w: World = { mrs: [mr(3, { state: "merged", merge_commit_sha: MERGE })], notes: { 3: [note(41, "/plywood up wave-2")] }, members, pipelines, jobs: applyJobs };
|
|
367
|
+
const got = await poll(w, { jobs: { applyOnMerge: true, waves }, stops: stopsFn });
|
|
368
|
+
expect(stops).toEqual([["deploy", MERGE, { through: 2, by: "alice", pr: 3 }]]);
|
|
369
|
+
expect(posts(/\/retry$/)).toHaveLength(1);
|
|
370
|
+
expect(got.outcomes[0]!.reason).toMatch(/; it stops after `wave-2`, and the waves after it wait until `\/plywood up` runs it again$/);
|
|
371
|
+
await poll({ ...w, notes: { 3: [note(42, "/plywood up")] } }, { jobs: { applyOnMerge: true, waves }, stops: stopsFn });
|
|
372
|
+
expect(stops.at(-1)).toEqual(["deploy", MERGE, null]);
|
|
373
|
+
// A wave the waves do not have is refused before anything is written.
|
|
374
|
+
const over = await poll({ ...w, notes: { 3: [note(43, "/plywood up wave-4")] } }, { jobs: { applyOnMerge: true, waves }, stops: stopsFn });
|
|
375
|
+
expect(over.outcomes[0]!.reason).toBe("the update of this project runs in 3 waves, so there is no `wave-4`");
|
|
376
|
+
expect(stops).toHaveLength(2);
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
test("an open merge request's apply is decided, recorded, and started as a pipeline of the default branch with the decision's variables", async () => {
|
|
380
|
+
await withTestDir(async (dir) => {
|
|
381
|
+
const git = (...args: string[]) => {
|
|
382
|
+
const r = spawnSync("git", args, { cwd: dir, encoding: "utf-8", env: { ...process.env, GIT_AUTHOR_NAME: "t", GIT_AUTHOR_EMAIL: "t@t", GIT_COMMITTER_NAME: "t", GIT_COMMITTER_EMAIL: "t@t" } });
|
|
383
|
+
if (r.status !== 0) throw new Error(r.stderr);
|
|
384
|
+
return r.stdout.trim();
|
|
385
|
+
};
|
|
386
|
+
// The default branch and the merge request's head ref, in one repository that is its own origin.
|
|
387
|
+
git("init", "-q", "-b", "main");
|
|
388
|
+
writeFileSync(join(dir, "main.tf"), "1");
|
|
389
|
+
git("add", ".");
|
|
390
|
+
git("commit", "-q", "-m", "base");
|
|
391
|
+
git("checkout", "-q", "-b", "feat-3");
|
|
392
|
+
writeFileSync(join(dir, "main.tf"), "2");
|
|
393
|
+
git("commit", "-q", "-am", "head");
|
|
394
|
+
const head = git("rev-parse", "HEAD");
|
|
395
|
+
git("update-ref", "refs/merge-requests/3/head", head);
|
|
396
|
+
git("checkout", "-q", "main");
|
|
397
|
+
git("remote", "add", "origin", dir);
|
|
398
|
+
const recorded: unknown[] = [];
|
|
399
|
+
const runs = chantNoteRuns(
|
|
400
|
+
{ applyOnMerge: true, waves, wavesBeforeMerge: { plan: ["plywood/preview"], pipelineFiles: [".gitlab-ci.yml"] } },
|
|
401
|
+
{
|
|
402
|
+
beforeMerge: {
|
|
403
|
+
requires: async () => null,
|
|
404
|
+
statuses: async () => [{ context: "plywood/preview", state: "success" }],
|
|
405
|
+
git: (argv, o) => {
|
|
406
|
+
const r = spawnSync(argv[0]!, argv.slice(1), { encoding: "utf8", ...(o.cwd ? { cwd: o.cwd } : {}) });
|
|
407
|
+
return { status: r.status, stdout: r.stdout ?? "", stderr: r.stderr ?? "" };
|
|
408
|
+
},
|
|
409
|
+
cwd: dir,
|
|
410
|
+
record: async (d) => (recorded.push(d), { ...d, digest: beforeMergeDigest("deploy", d) }),
|
|
411
|
+
stop: async () => {},
|
|
412
|
+
now: () => NOW.toISOString(),
|
|
413
|
+
},
|
|
414
|
+
},
|
|
415
|
+
);
|
|
416
|
+
const w: World = { mrs: [mr(3, { sha: head })], notes: { 3: [note(41, "/plywood up")] }, members };
|
|
417
|
+
const got = await poll(w, { runs });
|
|
418
|
+
const started = posts(/^\/projects\/7\/pipeline$/);
|
|
419
|
+
expect(started).toHaveLength(1);
|
|
420
|
+
const vars = Object.fromEntries((started[0]!.body as { variables: Array<{ key: string; value: string }> }).variables.map((v) => [v.key, v.value]));
|
|
421
|
+
const decision = { pr: 3, head, base: git("rev-parse", "main"), at: NOW.toISOString(), by: "alice" };
|
|
422
|
+
expect(recorded).toEqual([decision]);
|
|
423
|
+
expect(vars).toEqual({ CHANT_BEFORE_MERGE_PR: "3", CHANT_BEFORE_MERGE_HEAD: head, CHANT_BEFORE_MERGE_DIGEST: beforeMergeDigest("deploy", decision) });
|
|
424
|
+
expect(got.outcomes[0]!.reason).toBe(`started pipeline http://gitlab/acme/infra/-/pipelines/78 on main to update !3's head ${head.slice(0, 8)} before it merges for alice`);
|
|
425
|
+
});
|
|
426
|
+
});
|
|
427
|
+
});
|
|
428
|
+
|
|
354
429
|
describe("ledgerNoteLog", () => {
|
|
355
430
|
const git = (dir: string, ...args: string[]): void => {
|
|
356
431
|
const r = spawnSync("git", args, { cwd: dir, encoding: "utf-8" });
|
package/src/ci-note-commands.ts
CHANGED
|
@@ -34,6 +34,7 @@ import { commandLine, commentTerms, forkReply, memberReply, notRunReply } from "
|
|
|
34
34
|
import { GITLAB_APPLY_PR_VAR, GITLAB_LOCK_PR_VAR, GITLAB_PREVIEW_PR_VAR, GITLAB_UNLOCK_PR_VAR, PREVIEW_COMMENT_ENV } from "./components/pr-pipeline";
|
|
35
35
|
import { ForgeApiError, type ForgeFetch, type ForgeKind, type GitlabForgeOptions, type PrForge } from "./pr-forge";
|
|
36
36
|
import type { ForgeReads, PullRequestDetail, PullRequestNote, RunJob, WorkflowRun } from "./pr-forge-reads";
|
|
37
|
+
import type { DecideBeforeMergeInput, WavesBeforeMergeJobs } from "./op/before-merge-decide";
|
|
37
38
|
|
|
38
39
|
export { GITLAB_NOTE_COMMANDS_VAR } from "./components/pr-pipeline";
|
|
39
40
|
|
|
@@ -119,7 +120,17 @@ export type NoteRun =
|
|
|
119
120
|
* that has such a job. Refused when a later commit of the default branch
|
|
120
121
|
* already carries the product's apply status.
|
|
121
122
|
*/
|
|
122
|
-
| {
|
|
123
|
+
| {
|
|
124
|
+
kind: "retry";
|
|
125
|
+
sha: string;
|
|
126
|
+
jobs?: RegExp;
|
|
127
|
+
/**
|
|
128
|
+
* With the waves' op (#4984, the GitLab half of #4979's stop): the
|
|
129
|
+
* stop written for `sha` on `chant/lifecycle` just before the retry,
|
|
130
|
+
* the last wave that runs, or null to clear an earlier one.
|
|
131
|
+
*/
|
|
132
|
+
stop?: { op: string; through: number | null };
|
|
133
|
+
}
|
|
123
134
|
/** A reply, and nothing runs. */
|
|
124
135
|
| { kind: "reply"; text: string }
|
|
125
136
|
/** Something broke: the note gets no reply and is read again next poll. */
|
|
@@ -184,6 +195,8 @@ export interface NoteCommandsOptions {
|
|
|
184
195
|
env?: Record<string, string | undefined>;
|
|
185
196
|
/** The commit words that take an argument, as `parseProductComment` takes them. */
|
|
186
197
|
commitArgs?: Readonly<Record<string, RegExp>>;
|
|
198
|
+
/** Writes a retry's wave stop for op `op` at `sha`, or clears it (#4984). Absent: a retry that carries a stop is refused. */
|
|
199
|
+
stops?: (op: string, sha: string, stop: { through: number; at: string; by?: string; pr?: number } | null) => Promise<void>;
|
|
187
200
|
}
|
|
188
201
|
|
|
189
202
|
export interface NotePoll {
|
|
@@ -306,7 +319,25 @@ async function answer(o: NoteCommandsOptions, mr: PullRequestDetail, note: PullR
|
|
|
306
319
|
return reply(`started pipeline${link(started)} to ${run.doing} for ${note.author}`, true, started.url);
|
|
307
320
|
}
|
|
308
321
|
case "retry": {
|
|
309
|
-
|
|
322
|
+
// #4984: the stop goes on chant/lifecycle just before the retry, so every wave job after it reads it.
|
|
323
|
+
const stop = run.stop;
|
|
324
|
+
if (stop && !o.stops) return broke("the retry carries a wave stop, and this poll cannot write one");
|
|
325
|
+
const retry = gitlabRetry(o.pipelines);
|
|
326
|
+
const author = note.author;
|
|
327
|
+
const rerun: ApplyRerun = stop
|
|
328
|
+
? async (pipeline, jobs, ctx) => {
|
|
329
|
+
try {
|
|
330
|
+
await o.stops!(stop.op, run.sha, stop.through === null ? null : { through: stop.through, at, by: author, pr: mr.number });
|
|
331
|
+
} catch (e) {
|
|
332
|
+
return { kind: "fail", reason: `could not write the wave stop for ${short(run.sha)}, so nothing was retried (${message(e)})` };
|
|
333
|
+
}
|
|
334
|
+
return retry(pipeline, jobs, ctx);
|
|
335
|
+
}
|
|
336
|
+
: retry;
|
|
337
|
+
const done = await retryApply(o, "gitlab", mr, note.author, run, rerun);
|
|
338
|
+
if (done.kind === "ran" && stop?.through !== null && stop?.through !== undefined) {
|
|
339
|
+
return reply(`${done.text}; it stops after \`wave-${stop.through}\`, and the waves after it wait until ${commandLine(o.words, "apply")} runs it again`, true, done.url);
|
|
340
|
+
}
|
|
310
341
|
if (done.kind === "fail") return broke(done.reason);
|
|
311
342
|
return reply(done.text, done.kind === "ran", done.kind === "ran" ? done.url : undefined);
|
|
312
343
|
}
|
|
@@ -447,6 +478,27 @@ export interface ChantNoteJobs {
|
|
|
447
478
|
* reply, and nothing starts. Absent: any member a comment may name.
|
|
448
479
|
*/
|
|
449
480
|
members?: readonly string[];
|
|
481
|
+
/**
|
|
482
|
+
* The waves on merge (#4984, as the comment job's #4979): their op and
|
|
483
|
+
* count. With them a merged merge request's `wave-<n>` writes a stop for
|
|
484
|
+
* the merge commit before the retry, and an apply without a wave clears
|
|
485
|
+
* it; without them a `wave-<n>` is refused, as before.
|
|
486
|
+
*/
|
|
487
|
+
waves?: { op: string; count: number };
|
|
488
|
+
/**
|
|
489
|
+
* The waves before merge (#4984), with `waves`: an open merge request's
|
|
490
|
+
* apply is decided here (./op/before-merge-decide.ts), recorded on
|
|
491
|
+
* `chant/lifecycle`, and runs in a pipeline of the default branch started
|
|
492
|
+
* with `CHANT_BEFORE_MERGE_PR`, `CHANT_BEFORE_MERGE_HEAD` and
|
|
493
|
+
* `CHANT_BEFORE_MERGE_DIGEST`.
|
|
494
|
+
*/
|
|
495
|
+
wavesBeforeMerge?: WavesBeforeMergeJobs;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/** What {@link chantNoteRuns} reads and writes with besides the jobs (#4984). */
|
|
499
|
+
export interface ChantNoteRunsOptions {
|
|
500
|
+
/** With `jobs.wavesBeforeMerge`: what the decision before merge reads and writes with. */
|
|
501
|
+
beforeMerge?: Pick<DecideBeforeMergeInput, "requires" | "statuses" | "git" | "cwd" | "lock" | "record" | "stop" | "now">;
|
|
450
502
|
}
|
|
451
503
|
|
|
452
504
|
/**
|
|
@@ -454,8 +506,8 @@ export interface ChantNoteJobs {
|
|
|
454
506
|
* A command no job runs gets the words' reply for it (`words.replies`,
|
|
455
507
|
* #4946), else chant's.
|
|
456
508
|
*/
|
|
457
|
-
export function chantNoteRuns(jobs: ChantNoteJobs): NoteCommandRuns {
|
|
458
|
-
return ({ mr, note, parsed, base, words }): NoteRun => {
|
|
509
|
+
export function chantNoteRuns(jobs: ChantNoteJobs, opts: ChantNoteRunsOptions = {}): NoteCommandRuns {
|
|
510
|
+
return ({ mr, note, parsed, base, words }): NoteRun | Promise<NoteRun> => {
|
|
459
511
|
const iid = String(mr.number);
|
|
460
512
|
const at = `!${mr.number}`;
|
|
461
513
|
const line = (kind: CiProductCommand): string => commandLine(words, kind);
|
|
@@ -466,7 +518,7 @@ export function chantNoteRuns(jobs: ChantNoteJobs): NoteCommandRuns {
|
|
|
466
518
|
const open = mr.state === "open";
|
|
467
519
|
const intoBase = mr.base === undefined || mr.base === base;
|
|
468
520
|
// #4946: the product's sentence for an apply no job answers, in place of chant's, which say what the merge does.
|
|
469
|
-
if (parsed.kind === "apply" && !jobs.applyBeforeMerge && !jobs.applyOnMerge && words.replies?.apply !== undefined) return { kind: "reply", text: words.replies.apply };
|
|
521
|
+
if (parsed.kind === "apply" && !jobs.applyBeforeMerge && !jobs.applyOnMerge && !jobs.wavesBeforeMerge && words.replies?.apply !== undefined) return { kind: "reply", text: words.replies.apply };
|
|
470
522
|
switch (parsed.kind) {
|
|
471
523
|
case "plan":
|
|
472
524
|
if (!open) return { kind: "reply", text: `${at} is not open, so it is not ${words.verbs.plan[1]}` };
|
|
@@ -474,6 +526,8 @@ export function chantNoteRuns(jobs: ChantNoteJobs): NoteCommandRuns {
|
|
|
474
526
|
return { kind: "merge-request-pipeline", doing: `${words.verbs.plan[0]} ${at} again` };
|
|
475
527
|
case "apply":
|
|
476
528
|
if (open) {
|
|
529
|
+
// #4984: the waves before merge decide here, and the pipeline they start runs the decided head.
|
|
530
|
+
if (jobs.wavesBeforeMerge && jobs.waves && opts.beforeMerge && !jobs.applyBeforeMerge) return wavesBeforeMergeRun(jobs, opts.beforeMerge, { mr, note, parsed, base, words });
|
|
477
531
|
if (!jobs.applyBeforeMerge) return { kind: "reply", text: `${at} is not merged, and this project ${words.verbs.apply[0]}s after a merge request merges, never from its branch` };
|
|
478
532
|
if (!intoBase) return { kind: "reply", text: `${at} targets ${mr.base}, not the default branch ${base}, so it is not ${words.verbs.apply[1]}` };
|
|
479
533
|
return { kind: "pipeline", ref: base, variables: { [GITLAB_APPLY_PR_VAR]: iid }, doing: `${words.verbs.apply[0]} ${at}'s head${mr.headSha ? ` ${short(mr.headSha)}` : ""}` };
|
|
@@ -482,10 +536,12 @@ export function chantNoteRuns(jobs: ChantNoteJobs): NoteCommandRuns {
|
|
|
482
536
|
if (!jobs.applyOnMerge) {
|
|
483
537
|
return { kind: "reply", text: jobs.applyBeforeMerge ? `${at} is merged, and this project ${words.verbs.apply[0]}s a merge request before it merges, so there is nothing of it left to ${words.verbs.apply[0]}` : `${at} is merged, and a merge here records the plan: a release deploys it` };
|
|
484
538
|
}
|
|
485
|
-
|
|
539
|
+
// #4984: with the waves' op, a wave-<n> writes a stop for the merge commit before the retry, as on GitHub and Forgejo.
|
|
540
|
+
if (parsed.wave !== undefined && !jobs.waves) return { kind: "reply", text: `GitLab runs the waves after a retried apply job by itself, so \`wave-${parsed.wave}\` cannot stop them; write ${line("apply")}, and each wave still waits behind its own gate` };
|
|
541
|
+
if (parsed.wave !== undefined && jobs.waves && parsed.wave > jobs.waves.count) return { kind: "reply", text: `the ${words.verbs.apply[0]} of this project runs in ${jobs.waves.count} wave${jobs.waves.count === 1 ? "" : "s"}, so there is no \`wave-${parsed.wave}\`` };
|
|
486
542
|
if (!intoBase) return { kind: "reply", text: `${at} was merged into ${mr.base}, not the default branch ${base}, so nothing applies from it` };
|
|
487
543
|
if (!mr.mergeCommit) return { kind: "fail", reason: `${at} has no merge commit` };
|
|
488
|
-
return { kind: "retry", sha: mr.mergeCommit };
|
|
544
|
+
return { kind: "retry", sha: mr.mergeCommit, ...(jobs.waves ? { stop: { op: jobs.waves.op, through: parsed.wave !== undefined && parsed.wave < jobs.waves.count ? parsed.wave : null } } : {}) };
|
|
489
545
|
case "lock":
|
|
490
546
|
case "unlock":
|
|
491
547
|
if (!jobs.locks) return { kind: "reply", text: notRunReply(words, parsed.kind, "gitlab") };
|
|
@@ -506,6 +562,39 @@ export function chantNoteRuns(jobs: ChantNoteJobs): NoteCommandRuns {
|
|
|
506
562
|
};
|
|
507
563
|
}
|
|
508
564
|
|
|
565
|
+
/**
|
|
566
|
+
* An open merge request's apply under the waves before merge (#4984): the
|
|
567
|
+
* decision records the head, and the run is a pipeline of the default branch
|
|
568
|
+
* started with the decision's three variables, which the wave jobs' guard
|
|
569
|
+
* checks against the record.
|
|
570
|
+
*/
|
|
571
|
+
async function wavesBeforeMergeRun(jobs: ChantNoteJobs, deps: NonNullable<ChantNoteRunsOptions["beforeMerge"]>, c: Omit<NoteCommand, "role">): Promise<NoteRun> {
|
|
572
|
+
const parsed = c.parsed as Extract<NoteCommand["parsed"], { kind: "apply" }>;
|
|
573
|
+
const { decideWavesBeforeMerge } = await import("./op/before-merge-decide");
|
|
574
|
+
const { BEFORE_MERGE_DIGEST_ENV, BEFORE_MERGE_HEAD_ENV, BEFORE_MERGE_PR_ENV } = await import("./op/before-merge");
|
|
575
|
+
let variables: Record<string, string> | undefined;
|
|
576
|
+
const done = await decideWavesBeforeMerge({
|
|
577
|
+
...deps,
|
|
578
|
+
forge: "gitlab",
|
|
579
|
+
pr: c.mr,
|
|
580
|
+
user: c.note.author ?? "",
|
|
581
|
+
base: c.base,
|
|
582
|
+
...(parsed.wave !== undefined ? { wave: parsed.wave } : {}),
|
|
583
|
+
op: jobs.waves!.op,
|
|
584
|
+
count: jobs.waves!.count,
|
|
585
|
+
jobs: jobs.wavesBeforeMerge!,
|
|
586
|
+
words: c.words,
|
|
587
|
+
// The poll starts the pipeline once the decision is recorded, and its reply links it.
|
|
588
|
+
start: async (r) => {
|
|
589
|
+
variables = { [BEFORE_MERGE_PR_ENV]: String(r.pr), [BEFORE_MERGE_HEAD_ENV]: r.head, [BEFORE_MERGE_DIGEST_ENV]: r.digest };
|
|
590
|
+
return {};
|
|
591
|
+
},
|
|
592
|
+
});
|
|
593
|
+
if (done.kind !== "ran") return done.kind === "fail" ? { kind: "fail", reason: done.reason } : { kind: "reply", text: done.text };
|
|
594
|
+
const through = parsed.wave !== undefined && parsed.wave < jobs.waves!.count ? ` through wave ${parsed.wave}` : "";
|
|
595
|
+
return { kind: "pipeline", ref: c.base, variables: variables!, doing: `${c.words.verbs.apply[0]} !${c.mr.number}'s head ${short(c.mr.headSha ?? "")}${through} before it merges` };
|
|
596
|
+
}
|
|
597
|
+
|
|
509
598
|
/** The directory on `chant/lifecycle` that keeps the notes the poll answered. */
|
|
510
599
|
export const NOTES_DIR = "_notes";
|
|
511
600
|
|