@intentius/chant 0.124.91 → 0.124.92

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.
Files changed (69) hide show
  1. package/dist/ci-comment-commands.d.ts +11 -1
  2. package/dist/ci-comment-commands.d.ts.map +1 -1
  3. package/dist/ci-comment-commands.js +10 -3
  4. package/dist/ci-product.d.ts +18 -1
  5. package/dist/ci-product.d.ts.map +1 -1
  6. package/dist/ci-product.js +20 -3
  7. package/dist/cli/handlers/pr-comment.d.ts.map +1 -1
  8. package/dist/cli/handlers/pr-comment.js +15 -2
  9. package/dist/cli/handlers/pr.d.ts.map +1 -1
  10. package/dist/cli/handlers/pr.js +4 -1
  11. package/dist/components/pr-pipeline.d.ts +4 -2
  12. package/dist/components/pr-pipeline.d.ts.map +1 -1
  13. package/dist/components/pr-pipeline.js +7 -4
  14. package/dist/components/product-pipeline.d.ts +41 -7
  15. package/dist/components/product-pipeline.d.ts.map +1 -1
  16. package/dist/components/product-pipeline.js +57 -20
  17. package/dist/components/product-plan.d.ts +7 -0
  18. package/dist/components/product-plan.d.ts.map +1 -1
  19. package/dist/components/product-plan.js +1 -0
  20. package/dist/floor-table.data.d.ts.map +1 -1
  21. package/dist/floor-table.data.js +9 -5
  22. package/dist/lexicon.d.ts +42 -1
  23. package/dist/lexicon.d.ts.map +1 -1
  24. package/dist/op/before-merge-decide.d.ts +2 -0
  25. package/dist/op/before-merge-decide.d.ts.map +1 -1
  26. package/dist/op/op-waves-evidence-jobs.d.ts +3 -2
  27. package/dist/op/op-waves-evidence-jobs.d.ts.map +1 -1
  28. package/dist/op/op-waves-evidence-jobs.js +4 -3
  29. package/dist/op/op-waves-follow-on.d.ts +2 -0
  30. package/dist/op/op-waves-follow-on.d.ts.map +1 -1
  31. package/dist/op/op-waves-follow-on.js +8 -2
  32. package/dist/op/op-waves-names.d.ts +122 -0
  33. package/dist/op/op-waves-names.d.ts.map +1 -0
  34. package/dist/op/op-waves-names.js +151 -0
  35. package/dist/pr-forge-job.d.ts +8 -0
  36. package/dist/pr-forge-job.d.ts.map +1 -1
  37. package/dist/pr-forge-job.js +13 -0
  38. package/dist/pr-plan-record.d.ts +3 -1
  39. package/dist/pr-plan-record.d.ts.map +1 -1
  40. package/dist/pr-plan-record.js +1 -1
  41. package/dist/pr-post.d.ts +9 -3
  42. package/dist/pr-post.d.ts.map +1 -1
  43. package/dist/pr-post.js +5 -2
  44. package/dist/release-floors.d.ts +1 -1
  45. package/dist/release-floors.d.ts.map +1 -1
  46. package/dist/release-floors.js +4 -4
  47. package/dist/wave-runtime.mjs +258 -258
  48. package/package.json +1 -1
  49. package/src/ci-comment-commands.test.ts +19 -5
  50. package/src/ci-comment-commands.ts +18 -3
  51. package/src/ci-product.ts +24 -3
  52. package/src/cli/handlers/pr-comment.ts +12 -2
  53. package/src/cli/handlers/pr.ts +4 -1
  54. package/src/components/pr-pipeline.ts +7 -4
  55. package/src/components/product-job-names.test.ts +176 -0
  56. package/src/components/product-pipeline.ts +95 -28
  57. package/src/components/product-plan.ts +8 -0
  58. package/src/floor-table.data.ts +9 -5
  59. package/src/lexicon.ts +42 -1
  60. package/src/op/before-merge-decide.ts +2 -0
  61. package/src/op/op-waves-evidence-jobs.ts +4 -3
  62. package/src/op/op-waves-follow-on.ts +10 -2
  63. package/src/op/op-waves-names.test.ts +131 -0
  64. package/src/op/op-waves-names.ts +241 -0
  65. package/src/pr-forge-job.ts +13 -0
  66. package/src/pr-plan-record.ts +4 -2
  67. package/src/pr-post-head.test.ts +116 -0
  68. package/src/pr-post.ts +15 -5
  69. package/src/release-floors.ts +4 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intentius/chant",
3
- "version": "0.124.91",
3
+ "version": "0.124.92",
4
4
  "description": "Declarative infrastructure-as-code toolkit — TypeScript on Node.js",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://intentius.io/chant",
@@ -380,6 +380,23 @@ describe("the comment job's apply before merge on the waves (#4984)", () => {
380
380
  const jobs: CommentJobs = { applyOnMerge: true, waves: { op: "tf-apply", count: 2 }, wavesBeforeMerge: bm };
381
381
 
382
382
  test("an open pull request's apply is decided, and the waves workflow dispatched on the default branch with the decided head", async () => {
383
+ const { sent, stops, got, posted, head, digest } = await decideAndDispatch(jobs);
384
+ expect(sent).toEqual([["tf-apply.yml", "main", { pr: "12", head, digest }]]);
385
+ expect(stops).toEqual([[head, 1]]);
386
+ expect(got).toMatchObject({ command: "apply", ran: true, replied: true });
387
+ expect(posted).toEqual([`terragucci: started the waves of tf-apply on pull request 12's head ${head.slice(0, 8)} through wave 1 for alice; each wave's gate decides, and the run says when the head applied`]);
388
+ });
389
+
390
+ test("#5022: a product's workflow is dispatched with the product's input names", async () => {
391
+ const named: CommentJobs = { ...jobs, wavesBeforeMerge: { ...bm, workflow: "terragucci.yml", inputs: { pr: "apply_pr", head: "apply_head", digest: "apply_digest" } } };
392
+ expect(commentJobsFromEnv(JSON.stringify(named))).toEqual(named);
393
+ expect(() => commentJobsFromEnv(JSON.stringify({ ...jobs, wavesBeforeMerge: { ...bm, inputs: { wave: "w" } } }))).toThrow("inputs names the inputs pr, head and digest");
394
+ expect(() => commentJobsFromEnv(JSON.stringify({ ...jobs, wavesBeforeMerge: { ...bm, inputs: { pr: "a b" } } }))).toThrow("inputs names the inputs pr, head and digest");
395
+ const { sent, head, digest } = await decideAndDispatch(named);
396
+ expect(sent).toEqual([["terragucci.yml", "main", { apply_pr: "12", apply_head: head, apply_digest: digest }]]);
397
+ });
398
+
399
+ async function decideAndDispatch(jobs: CommentJobs) {
383
400
  const { spawnSync } = await import("node:child_process");
384
401
  const { mkdtempSync, writeFileSync } = await import("node:fs");
385
402
  const { tmpdir } = await import("node:os");
@@ -424,11 +441,8 @@ describe("the comment job's apply before merge on the waves (#4984)", () => {
424
441
  },
425
442
  });
426
443
  const digest = beforeMergeDigest("tf-apply", { pr: 12, head, base: baseSha, through: 1, at, by: "alice" });
427
- expect(sent).toEqual([["tf-apply.yml", "main", { pr: "12", head, digest }]]);
428
- expect(stops).toEqual([[head, 1]]);
429
- expect(got).toMatchObject({ command: "apply", ran: true, replied: true });
430
- expect(s.posted).toEqual([`terragucci: started the waves of tf-apply on pull request 12's head ${head.slice(0, 8)} through wave 1 for alice; each wave's gate decides, and the run says when the head applied`]);
431
- });
444
+ return { sent, stops, got, posted: s.posted, head, digest };
445
+ }
432
446
 
433
447
  test("CHANT_COMMENT_JOBS carries the waves before merge, each field checked", () => {
434
448
  expect(commentJobsFromEnv(JSON.stringify(jobs))).toEqual(jobs);
@@ -57,7 +57,7 @@ import { ForgeApiError, type ForgeFetch, type PrForge } from "./pr-forge";
57
57
  import type { ForgeReads, PullRequestDetail } from "./pr-forge-reads";
58
58
  import type { DecideBeforeMergeInput, WavesBeforeMergeJobs } from "./op/before-merge-decide";
59
59
 
60
- /** The comment job's name in a generated workflow (#4946). */
60
+ /** The comment job's name in a generated workflow (#4946), unless the product names it (#5027: the product pipeline's `commentJob.jobName`, such as terragucci's `apply-comment`). */
61
61
  export const PR_COMMENT_JOB = "comment";
62
62
 
63
63
  /** The variable the comment job reads the pipeline's jobs from, as JSON ({@link CommentJobs}), written by the generator (#4946). */
@@ -102,6 +102,14 @@ export interface CommentJobs extends Omit<ChantNoteJobs, "members"> {
102
102
  * `chant/lifecycle`, and dispatches the waves workflow for it.
103
103
  */
104
104
  wavesBeforeMerge?: WavesBeforeMergeJobs;
105
+ /**
106
+ * The waves' apply jobs by the names the product gave them (#5027, the
107
+ * Op waves generator's `jobNames`, such as terragucci's `apply-wave-<k>`,
108
+ * `apply-wave-<k>-share-<n>`, its group jobs and `apply-rest`). A re-run
109
+ * of a merged pull request's apply looks for these. Absent:
110
+ * {@link RERUN_APPLY_JOBS}.
111
+ */
112
+ applyJobs?: readonly string[];
105
113
  }
106
114
 
107
115
  /** What the decision before merge reads and writes with (#4984), the comment job's handler's. */
@@ -117,6 +125,12 @@ export type CommentWaveStops = (op: string, sha: string, stop: { through: number
117
125
  */
118
126
  export const RERUN_APPLY_JOBS = /^(?:wave-([1-9][0-9]*)-[a-z0-9][a-z0-9_-]*|(?:[a-z0-9][a-z0-9-]*-)?apply(?:-wave-([1-9][0-9]*)(?:-share-[1-9][0-9]*)?)?)$/;
119
127
 
128
+ /** The apply jobs a re-run looks for (#5027): exactly `jobs.applyJobs` when the product named them, else {@link RERUN_APPLY_JOBS}. */
129
+ export function rerunApplyJobs(jobs: Pick<CommentJobs, "applyJobs">): RegExp {
130
+ if (!jobs.applyJobs?.length) return RERUN_APPLY_JOBS;
131
+ return new RegExp(`^(?:${jobs.applyJobs.map((n) => n.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|")})$`);
132
+ }
133
+
120
134
  /** The writes a re-run makes, with the job's token. */
121
135
  export interface CommentRuns {
122
136
  /** GitHub: `POST /repos/{repo}/actions/runs/{run}/rerun-failed-jobs`. */
@@ -327,7 +341,8 @@ export async function answerProductComment(o: AnswerCommentOptions): Promise<Com
327
341
  count: o.jobs.waves.count,
328
342
  jobs: bm,
329
343
  words: o.words,
330
- start: (r) => o.runs.dispatch(workflow, o.base, { pr: String(r.pr), head: r.head, digest: r.digest }),
344
+ // #5022: under the waves workflow's input names, the product's when it renamed them.
345
+ start: (r) => o.runs.dispatch(workflow, o.base, { [bm.inputs?.pr ?? "pr"]: String(r.pr), [bm.inputs?.head ?? "head"]: r.head, [bm.inputs?.digest ?? "digest"]: r.digest }),
331
346
  });
332
347
  if (done.kind === "fail") return broke(done.reason, command);
333
348
  return reply(done.text, done.kind === "ran", done.kind === "ran" ? done.url : undefined);
@@ -359,7 +374,7 @@ export async function answerProductComment(o: AnswerCommentOptions): Promise<Com
359
374
  return start(run, jobs, ctx);
360
375
  }
361
376
  : start;
362
- const done = await retryApply({ reads: o.reads, forge: o.poster, words: o.words, base: o.base }, o.forge, pr, user, { kind: "retry", sha: mergeCommit, jobs: RERUN_APPLY_JOBS }, rerun);
377
+ const done = await retryApply({ reads: o.reads, forge: o.poster, words: o.words, base: o.base }, o.forge, pr, user, { kind: "retry", sha: mergeCommit, jobs: rerunApplyJobs(o.jobs) }, rerun);
363
378
  if (done.kind === "fail") return broke(done.reason, command);
364
379
  const stopped = done.kind === "ran" && wave !== undefined && waves && wave < waves.count ? `; it stops after \`wave-${wave}\`, and the waves after it wait until \`/${o.words.product} ${typedApply(o.words)}\` runs it again` : "";
365
380
  return reply(`${done.text}${stopped}`, done.kind === "ran", done.kind === "ran" ? done.url : undefined);
package/src/ci-product.ts CHANGED
@@ -152,7 +152,7 @@ export const COMMENT_LEAVES_COMMIT_FLOOR = "0.124.88";
152
152
  * The first chant with the comment job on GitHub and Forgejo and the replies to commands no job runs (#4946): `commentJob` in the product pipeline (`chant components pr-comment`, `answerProductComment` in `@intentius/chant/ci-comment-commands`), which replies to a refusal and to a command the pipeline runs no job for, and with waves on merge starts a merged pull request's apply again; the words' `replies` and an alias's `refuse`, which the GitLab note poll reads too, as `chantNoteRuns`' `members`; `retryApply`; and `locks: "plan"` in GitHub's and Forgejo's product plan, with the lock, unlock and comment lock jobs.
153
153
  */
154
154
  export const COMMENT_COMMANDS_FLOOR = "0.124.89";
155
- export { answerProductComment, answeredElsewhere, COMMENT_JOBS_ENV, commentRuns, PR_COMMENT_JOB, RERUN_APPLY_JOBS, type CommentJobs, type CommentOutcome, type CommentRuns, type AnswerCommentOptions } from "./ci-comment-commands";
155
+ export { answerProductComment, answeredElsewhere, COMMENT_JOBS_ENV, commentRuns, PR_COMMENT_JOB, RERUN_APPLY_JOBS, rerunApplyJobs, type CommentJobs, type CommentOutcome, type CommentRuns, type AnswerCommentOptions } from "./ci-comment-commands";
156
156
  export { forkReply, memberReply, notRunReply } from "./ci-comment-replies";
157
157
  export type { CiProductCommentReplies } from "./ci-product-words";
158
158
  export { productCommentJob, type ProductCommentJob } from "./components/product-pipeline";
@@ -247,13 +247,34 @@ export { PLAN_SUBJECT_LIMIT, planSubjectProblem, sinceLastLine, subjectChanges,
247
247
  /**
248
248
  * The first chant whose product plan keeps a product's own names (#5003): `workflowFile` (the generators' `path`), `jobNames` (the plan, note, re-plan and re-plan note jobs, with `{target}`), `statusContext` (which `pr-post --context`, the pending step, the stale job and the apply before merge use) and `reportDir` (`{output}`, the hand-off, the artifact's path and `pr-post --output`), and whose `pr-post --open` finds a plan job of the product's name (`--plan-job`).
249
249
  */
250
- export const PRODUCT_PLAN_NAMES_FLOOR = NEXT_RELEASE;
250
+ export const PRODUCT_PLAN_NAMES_FLOOR = "0.124.91";
251
251
  export { productPlanContext, productPlanReportDir, productPlanWorkflowFile } from "./components/product-plan";
252
252
 
253
253
  /**
254
254
  * The first chant whose GitLab product plan job keeps a product's own artifacts beside the report directory (#5004): `productPlan.artifacts: { paths, reports }`, as `artifacts:paths` and `artifacts:reports`, such as terragucci's `terraform` report for the merge request's widget, with `{target}` and `{output}` filled. GitHub and Forgejo refuse it.
255
255
  */
256
- export const PRODUCT_PLAN_ARTIFACTS_FLOOR = NEXT_RELEASE;
256
+ export const PRODUCT_PLAN_ARTIFACTS_FLOOR = "0.124.91";
257
+
258
+ /**
259
+ * The first chant whose GitHub and Forgejo product plan, after a `login` or with `idTokens`, starts every step after the checkout but the login's without `ACTIONS_ID_TOKEN_REQUEST_URL` and `ACTIONS_ID_TOKEN_REQUEST_TOKEN` (#5021), as after a `trade`: the setup, the probe and the plan step, in the plan and the re-plan job, so the change's code can request no token. A product's own `idTokens` leave them in the plan step, whose verb requests them.
260
+ */
261
+ export const PRODUCT_PLAN_LOGIN_TOKENS_FLOOR = NEXT_RELEASE;
262
+
263
+ /**
264
+ * The first chant whose pull request jobs on GitHub and Forgejo name the pull request's head, `github.event.pull_request.head.sha`, where `github.sha` named the merge preview (#5020): the product plan's pending step (`CHANT_PENDING_SHA`), the note job's `HEAD_SHA` (the product plan's, the pull request loop's and the product pipeline's) and the `env-post` job's. `pr-post --head <head>` takes a report, a plan note hand-off or a run record for the head or for the run's merge preview the plan job checked out (`plannedForHead`), so a product's plan verb may write its hand-off for either.
265
+ */
266
+ export const PR_HEAD_SHA_FLOOR = NEXT_RELEASE;
267
+
268
+ /**
269
+ * The first chant whose Op waves generators keep a product's own names (#5022): `jobNames` (every job they write: a wave's, deciding, share and job group jobs, a split wave's plan, gate, apply and record jobs, the policy and evidence jobs, the apply status, stale, before-merge done and control jobs; `needs:`, `if:`, `{job}` and `--from-job` follow it), `stageNames` (GitLab), `artifactNames` (the outcome, decision, saved plan and check claims artifacts, and the pattern a wave job downloads the earlier outcomes with), `workflowFile` (the generators' file and the follow-on's dispatch), `runIf` (the jobs go in the product's own workflow, each running on a push it admits or a before-merge or follow-on dispatch, with its own permissions; on GitLab joined to every rule) and `dispatchInputs` (the dispatch inputs' names, which the comment job's dispatch and the follow-on use), with `buildForgejoOpWavesDoc` for the Forgejo jobs, and whose product pipeline's `wavesBeforeMerge` takes `workflowFile` and `dispatchInputs`.
270
+ */
271
+ export const OP_WAVES_NAMES_FLOOR = NEXT_RELEASE;
272
+
273
+ /**
274
+ * The first chant whose product pipeline names its comment job and its waves' jobs as the product does (#5027): `commentJob: { jobName }` (default `comment`, such as terragucci's `apply-comment`), refused when another job of the workflow has the name, and `wavesJobNames`, the Op waves generators' `jobNames`, which `waves.pipeline` carries to the generator and `waves.jobs` takes, and under which the comment job's re-run of a merged pull request's apply looks for the waves' jobs (`CHANT_COMMENT_JOBS` `applyJobs`, `rerunApplyJobs`), so terragucci's `apply-wave-<k>`, `-share-<n>`, group jobs and `apply-rest` run again. Without them the job is `comment` and the re-run looks for `RERUN_APPLY_JOBS`, as before.
275
+ */
276
+ export const PRODUCT_JOB_NAMES_FLOOR = NEXT_RELEASE;
277
+ export { opWavesArtifacts, opWavesDispatchInputs, opWavesNamer, opWavesWorkflowFile, type OpWavesArtifactNames, type OpWavesDispatchInput, type OpWavesJobNames, type OpWavesJobRef, type OpWavesJobRole, type OpWavesStageNames, type OpWavesStageRef } from "./op/op-waves-names";
257
278
 
258
279
  /**
259
280
  * Day-2 actions in the product pipeline (#4654): the `action` comment
@@ -34,7 +34,7 @@ export function commentJobsFromEnv(raw: string | undefined): CommentJobs {
34
34
  if (raw === undefined || raw.trim() === "") return {};
35
35
  const v: unknown = JSON.parse(raw);
36
36
  if (typeof v !== "object" || v === null || Array.isArray(v)) throw new Error(`${COMMENT_JOBS_ENV} is a JSON object of the jobs the pipeline has`);
37
- const known = new Set(["preview", "applyBeforeMerge", "applyOnMerge", "locks", "agent", "commits", "actions", "skipForks", "targets", "noPlanFlags", "waves", "wavesBeforeMerge"]);
37
+ const known = new Set(["preview", "applyBeforeMerge", "applyOnMerge", "locks", "agent", "commits", "actions", "skipForks", "targets", "noPlanFlags", "waves", "wavesBeforeMerge", "applyJobs"]);
38
38
  const jobs: Record<string, unknown> = {};
39
39
  for (const [k, x] of Object.entries(v)) {
40
40
  if (!known.has(k)) throw new Error(`${COMMENT_JOBS_ENV} names ${JSON.stringify(k)}, which is not one of ${[...known].join(", ")}`);
@@ -55,6 +55,12 @@ export function commentJobsFromEnv(raw: string | undefined): CommentJobs {
55
55
  if (x.length > 0) jobs.targets = x;
56
56
  continue;
57
57
  }
58
+ // #5027: the waves' apply jobs by the product's names, which a re-run looks for.
59
+ if (k === "applyJobs") {
60
+ if (!Array.isArray(x) || x.some((t) => typeof t !== "string" || !/^[A-Za-z0-9_][A-Za-z0-9_.-]*$/.test(t))) throw new Error(`${COMMENT_JOBS_ENV}.applyJobs is a list of job names`);
61
+ if (x.length > 0) jobs.applyJobs = x;
62
+ continue;
63
+ }
58
64
  if (typeof x !== "boolean") throw new Error(`${COMMENT_JOBS_ENV}.${k} is true or false`);
59
65
  if (x) jobs[k] = true;
60
66
  }
@@ -66,15 +72,19 @@ export function commentJobsFromEnv(raw: string | undefined): CommentJobs {
66
72
  export function wavesBeforeMergeOf(x: unknown): WavesBeforeMergeJobs {
67
73
  const what = `${COMMENT_JOBS_ENV}.wavesBeforeMerge`;
68
74
  const o = x as Partial<WavesBeforeMergeJobs> | null;
69
- if (!o || typeof o !== "object" || Array.isArray(o)) throw new Error(`${what} is { workflow?, requires?, plan, pipelineFiles, locks? }`);
75
+ if (!o || typeof o !== "object" || Array.isArray(o)) throw new Error(`${what} is { workflow?, inputs?, requires?, plan, pipelineFiles, locks? }`);
70
76
  const strings = (v: unknown): v is string[] => Array.isArray(v) && v.every((s) => typeof s === "string" && s.trim() !== "" && !/[\r\n]/.test(s));
71
77
  if (o.workflow !== undefined && (typeof o.workflow !== "string" || !/^[A-Za-z0-9][A-Za-z0-9._-]*\.ya?ml$/.test(o.workflow))) throw new Error(`${what}.workflow is a workflow file name, like deploy.yml`);
72
78
  if (o.requires !== undefined && (!Array.isArray(o.requires) || o.requires.some((r) => !(PR_APPLY_REQUIRES as readonly string[]).includes(r)))) throw new Error(`${what}.requires is a list of ${PR_APPLY_REQUIRES.join(", ")}`);
73
79
  if (!strings(o.plan)) throw new Error(`${what}.plan is the list of plan statuses that must pass on the head`);
74
80
  if (!strings(o.pipelineFiles)) throw new Error(`${what}.pipelineFiles is the list of paths a change may not touch`);
75
81
  if (o.locks !== undefined && (typeof o.locks !== "object" || o.locks === null || (o.locks.gate !== undefined && (typeof o.locks.gate !== "string" || !/^[A-Za-z0-9][A-Za-z0-9_.-]{0,127}$/.test(o.locks.gate))))) throw new Error(`${what}.locks is { gate? }`);
82
+ // #5022: the waves workflow's input names, the product's.
83
+ const inputs = o.inputs;
84
+ if (inputs !== undefined && (typeof inputs !== "object" || inputs === null || Object.entries(inputs).some(([k, n]) => !["pr", "head", "digest"].includes(k) || typeof n !== "string" || !/^[A-Za-z_][A-Za-z0-9_-]*$/.test(n)))) throw new Error(`${what}.inputs names the inputs pr, head and digest`);
76
85
  return {
77
86
  ...(o.workflow ? { workflow: o.workflow } : {}),
87
+ ...(inputs ? { inputs: { ...inputs } } : {}),
78
88
  ...(o.requires ? { requires: [...o.requires] } : {}),
79
89
  plan: [...o.plan],
80
90
  pipelineFiles: [...o.pipelineFiles],
@@ -120,7 +120,7 @@ import {
120
120
  import { prStatusDescription, prStatusState } from "../../pr-status";
121
121
  import { FORGE_KINDS, forgeFromEnv, forgePrincipalOf, type ForgeKind, type PrForge } from "../../pr-forge";
122
122
  // #4964: a status for a pull_request run's merge preview goes on the pull request's head.
123
- import { forgeFromJobEnv } from "../../pr-forge-job";
123
+ import { forgeFromJobEnv, plannedForHead } from "../../pr-forge-job";
124
124
  import { GITHUB_COMMENT_LIMIT, GITLAB_NOTE_LIMIT } from "../../plan-summary";
125
125
  import { upsertPlanNote } from "../../pr-post";
126
126
  import { activeProductWords, commandWord, parseProductComment, productContext } from "../../ci-product-words";
@@ -2849,11 +2849,14 @@ export async function runComponentsPrPost(ctx: CommandContext): Promise<number>
2849
2849
  const context = args.statusContexts?.[0];
2850
2850
  const description = await descriptionCheckOf(ctx, forge);
2851
2851
  if (typeof description === "number") return description;
2852
+ // #5020: --head is the pull request's head; a GitHub plan job checked out the run's merge preview, whose report is the head's.
2853
+ const planned = args.forge === "gitlab" ? undefined : plannedForHead(args.head);
2852
2854
  const posted = await postPlanReport({
2853
2855
  forge,
2854
2856
  dir: resolve(args.output ?? PR_REPORT_DIR),
2855
2857
  pr: args.pr,
2856
2858
  head: args.head,
2859
+ ...(planned ? { planned } : {}),
2857
2860
  env: args.env,
2858
2861
  ...(member ? { member } : {}),
2859
2862
  ...(context ? { context } : {}),
@@ -983,9 +983,11 @@ export const PR_PLAN_NOTE_JOB = "plan-note";
983
983
  * and Forgejo it runs in a fresh job after the plan, at the target branch's
984
984
  * commit, and posts the note and the `chant/plan` status from the report
985
985
  * the plan job handed on, with the forge token the plan job never held.
986
- * `HEAD_SHA` is the commit the plan job checked out, `PR_NUMBER` the pull
986
+ * `HEAD_SHA` is the pull request's head (#5020), `PR_NUMBER` the pull
987
987
  * request, and `PLAN_RESULT` the plan job's result, which fails the status
988
- * when the plan job failed.
988
+ * when the plan job failed. A GitHub plan job checks out the merge preview
989
+ * (`github.sha`), and `pr-post` takes a report for it as the head's
990
+ * (`pullRequestHeads` in ../pr-forge-job.ts).
989
991
  *
990
992
  * On GitLab (#4328) it runs `pr-post --open` in a pipeline of the target
991
993
  * branch, which GitLab builds from that branch's files and gives the
@@ -1026,7 +1028,8 @@ function prLoopPostJobBase(forge: ForgeKind, env: string, member: string | undef
1026
1028
  command: withProductBin(command, words?.bin),
1027
1029
  env: {
1028
1030
  PR_NUMBER: "${{ github.event.pull_request.number }}",
1029
- HEAD_SHA: "${{ github.sha }}",
1031
+ // #5020: the head, where the forge shows and requires the status; GitHub's github.sha is the merge preview.
1032
+ HEAD_SHA: "${{ github.event.pull_request.head.sha }}",
1030
1033
  PLAN_RESULT: "${{ needs.plan.result }}",
1031
1034
  ...(opts.staleNotes ? { TIP_SHA: "${{ github.event.pull_request.base.sha }}" } : {}),
1032
1035
  GITHUB_TOKEN: GITHUB_JOB_TOKEN,
@@ -1328,7 +1331,7 @@ export function prEnvJobs(forge: ForgeKind, options: PrLoopPipelineOptions = {},
1328
1331
  jobName: PR_ENV_POST_JOB,
1329
1332
  command: ["chant", "components", "pr-env", "post", "--pr", '"$PR_NUMBER"', "--head", '"$HEAD_SHA"', ...common, ...(github ? ["--result", '"$UP_RESULT"'] : [])].join(" "),
1330
1333
  env: github
1331
- ? { PR_NUMBER: prNumber, HEAD_SHA: "${{ github.sha }}", UP_RESULT: "${{ needs.env-up.result }}", ...token }
1334
+ ? { PR_NUMBER: prNumber, HEAD_SHA: "${{ github.event.pull_request.head.sha }}", UP_RESULT: "${{ needs.env-up.result }}", ...token }
1332
1335
  : { PR_NUMBER: prNumber, HEAD_SHA: "$CI_COMMIT_SHA", BASE_SHA: "$CI_MERGE_REQUEST_DIFF_BASE_SHA" },
1333
1336
  },
1334
1337
  {
@@ -0,0 +1,176 @@
1
+ /**
2
+ * #5027: the product pipeline names its comment job and its waves' jobs as
3
+ * the product does. The comment job takes `commentJob.jobName` (terragucci's
4
+ * `apply-comment`), and a merged pull request's re-run looks for the waves'
5
+ * jobs under the same `jobNames` the Op waves generator renders (#5022), so
6
+ * terragucci's `apply-wave-<k>`, its shares, its group jobs and `apply-rest`
7
+ * run again. Without them the job is `comment` and the re-run's pattern is
8
+ * `RERUN_APPLY_JOBS`, as before.
9
+ */
10
+ import { describe, expect, test } from "vitest";
11
+ import type { OpWavesSpec } from "../op/op-waves";
12
+ import type { OpWavesJobRef } from "../op/op-waves-names";
13
+ import { answerProductComment, RERUN_APPLY_JOBS, rerunApplyJobs, type AnswerCommentOptions, type CommentJobs } from "../ci-comment-commands";
14
+ import { assertProductWords } from "../ci-product-words";
15
+ import { commentJobsFromEnv } from "../cli/handlers/pr-comment";
16
+ import type { ForgeReads, PullRequestDetail, RunJob, WorkflowRun } from "../pr-forge-reads";
17
+ import { PR_COMMENT_JOB } from "../ci-comment-commands";
18
+ import { productPipelineJobs } from "./product-pipeline";
19
+
20
+ const words = { product: "terragucci", noun: ["root", "roots"] as [string, string], commands: { plan: "plan" as const, apply: "apply" as const }, approval: "per-wave" as const, op: "tf-apply", bin: "terragucci" };
21
+
22
+ /** The waves of PR 5026's names goldens: a canary, a wide wave with shares, a wave of job groups (one with shares) and a rest wave. */
23
+ const waves = {
24
+ name: "tf-apply",
25
+ op: "tf-apply",
26
+ planWave: ["terragucci", "plan-wave", "{runs}", "{plans}"],
27
+ apply: ["terragucci", "apply", "{target}", "--plan", "{plan}"],
28
+ waves: [
29
+ { name: "canary", runs: [], discover: true },
30
+ { name: "wide", runs: [], discover: true, shares: 2 },
31
+ { name: "regions", runs: [], discover: true, jobs: [{ name: "east", runs: ["east/*"] }, { name: "west", runs: ["west/*"], shares: 2 }] },
32
+ { name: "rest", runs: [], discover: true, rest: true },
33
+ ],
34
+ } as unknown as OpWavesSpec;
35
+
36
+ /** terragucci's names, as PR 5026's names goldens give them. */
37
+ const terragucciJob = (j: OpWavesJobRef): string | undefined => {
38
+ const w = j.rest ? "apply-rest" : `apply-wave-${j.wave}`;
39
+ const part = `${j.group !== undefined ? `-${j.group}` : ""}${j.share !== undefined ? `-share-${j.share}` : ""}`;
40
+ switch (j.role) {
41
+ case "wave":
42
+ case "decide":
43
+ return w;
44
+ case "share":
45
+ case "group":
46
+ case "apply":
47
+ return `${w}${part}`;
48
+ case "plan":
49
+ case "gate":
50
+ case "record":
51
+ return `${w}-${j.role}`;
52
+ case "apply-status":
53
+ return j.wave === waves.waves.length ? "apply-done" : `${w}-status`;
54
+ case "before-merge-done":
55
+ return "pr-merge";
56
+ case "control":
57
+ return "control";
58
+ default:
59
+ return undefined;
60
+ }
61
+ };
62
+
63
+ const PUSH_JOBS = ["apply-wave-1", "apply-wave-2", "apply-wave-2-share-1", "apply-wave-2-share-2", "apply-wave-3", "apply-wave-3-east", "apply-wave-3-west-share-1", "apply-wave-3-west-share-2", "apply-rest"];
64
+ const productPlan = { command: "terragucci plan --out {output}", replan: true };
65
+ const env = (jobs: { comment?: { env: Record<string, string> } }): CommentJobs => JSON.parse(jobs.comment!.env.CHANT_COMMENT_JOBS!);
66
+
67
+ describe("the comment job's name (#5027)", () => {
68
+ test("is the product's, and comment by default", () => {
69
+ const named = productPipelineJobs("github", "default", { words, waves, productPlan, commentJob: { jobName: "apply-comment" } });
70
+ expect(named.comment!.jobName).toBe("apply-comment");
71
+ for (const commentJob of [true, {}] as const) expect(productPipelineJobs("github", "default", { words, waves, productPlan, commentJob }).comment!.jobName).toBe(PR_COMMENT_JOB);
72
+ expect(PR_COMMENT_JOB).toBe("comment");
73
+ });
74
+
75
+ test("a name the forge cannot take is refused", () => {
76
+ expect(() => productPipelineJobs("github", "default", { words, waves, productPlan, commentJob: { jobName: "apply comment" } })).toThrow(/commentJob\.jobName "apply comment" is not a job name/);
77
+ expect(() => productPipelineJobs("forgejo", "default", { words, waves, productPlan, commentJob: { jobName: "1st" } })).toThrow(/not a job name/);
78
+ });
79
+ });
80
+
81
+ describe("the re-run's jobs follow the waves' names (#5027)", () => {
82
+ test("by default the comment job carries no list and the re-run keeps RERUN_APPLY_JOBS", () => {
83
+ const jobs = productPipelineJobs("github", "default", { words, waves, productPlan, commentJob: true });
84
+ expect(env(jobs).applyJobs).toBeUndefined();
85
+ expect(jobs.comment!.jobs.applyJobs).toBeUndefined();
86
+ expect(jobs.waves!.pipeline.jobNames).toBeUndefined();
87
+ expect(jobs.waves!.jobs.map((j) => j.jobName)).toEqual(["wave-1-canary", "wave-2-wide-decide", "wave-2-wide-share-1", "wave-2-wide-share-2", "wave-3-regions-decide", "wave-3-regions-east", "wave-3-regions-west-share-1", "wave-3-regions-west-share-2", "wave-4-rest"]);
88
+ expect(rerunApplyJobs({})).toBe(RERUN_APPLY_JOBS);
89
+ expect(rerunApplyJobs({ applyJobs: [] })).toBe(RERUN_APPLY_JOBS);
90
+ });
91
+
92
+ test("with terragucci's names the comment job looks for apply-wave-<k>, its shares, its groups and apply-rest", () => {
93
+ const jobs = productPipelineJobs("github", "default", { words, waves, productPlan, commentJob: { jobName: "apply-comment" }, wavesJobNames: terragucciJob });
94
+ expect(env(jobs).applyJobs).toEqual(PUSH_JOBS);
95
+ expect(jobs.waves!.jobs.map((j) => j.jobName)).toEqual(PUSH_JOBS);
96
+ expect(jobs.waves!.jobs.find((j) => j.jobName === "apply-wave-2-share-1")!.needs).toEqual(["apply-wave-2"]);
97
+ // The generator is handed the same names, so the jobs it renders are the ones the re-run looks for.
98
+ expect(jobs.waves!.pipeline.jobNames).toBe(terragucciJob);
99
+ const pattern = rerunApplyJobs(env(jobs));
100
+ for (const name of PUSH_JOBS) expect(pattern.test(name), name).toBe(true);
101
+ for (const name of ["apply-comment", "apply-wave-1-evidence", "apply-wave-1-status", "apply-done", "stale-notes", "control", "apply-wave-10", "xapply-rest", "wave-1-canary"]) expect(pattern.test(name), name).toBe(false);
102
+ // chant's pattern skips the group jobs and the rest wave, which is what #5027 is about.
103
+ expect(["apply-wave-3-east", "apply-wave-3-west-share-1", "apply-rest"].filter((n) => RERUN_APPLY_JOBS.test(n))).toEqual([]);
104
+ });
105
+
106
+ test("before merge the split jobs are looked for too, by the same names", () => {
107
+ const jobs = productPipelineJobs("github", "default", { words, waves, productPlan, commentJob: { jobName: "apply-comment" }, wavesJobNames: terragucciJob, wavesBeforeMerge: { workflowFile: "terragucci.yml", dispatchInputs: { pr: "apply_pr", head: "apply_head", digest: "apply_digest" } } });
108
+ const names = env(jobs).applyJobs!;
109
+ for (const name of [...PUSH_JOBS, "apply-wave-1-plan", "apply-wave-1-gate", "apply-wave-1-record", "apply-wave-2-share-2", "apply-wave-3-west-share-1", "apply-rest-plan", "apply-rest-record"]) expect(names, name).toContain(name);
110
+ expect(new Set(names).size).toBe(names.length);
111
+ expect(jobs.waves!.pipeline).toMatchObject({ workflowFile: "terragucci.yml", jobNames: terragucciJob });
112
+ });
113
+
114
+ test("is refused without waves, and the generator's checks hold", () => {
115
+ expect(() => productPipelineJobs("github", "default", { words, productPlan, commentJob: true, wavesJobNames: terragucciJob })).toThrow(/wavesJobNames names the waves' jobs, and there are none/);
116
+ expect(() => productPipelineJobs("github", "default", { words, waves, productPlan, commentJob: true, wavesJobNames: () => "apply" })).toThrow(/names two jobs apply/);
117
+ });
118
+
119
+ test("the job reads the list from CHANT_COMMENT_JOBS, and refuses one that is not job names", () => {
120
+ expect(commentJobsFromEnv(JSON.stringify({ applyOnMerge: true, applyJobs: PUSH_JOBS })).applyJobs).toEqual(PUSH_JOBS);
121
+ expect(commentJobsFromEnv(JSON.stringify({ applyJobs: [] })).applyJobs).toBeUndefined();
122
+ expect(() => commentJobsFromEnv(JSON.stringify({ applyJobs: ["apply-rest", "a b"] }))).toThrow(/applyJobs is a list of job names/);
123
+ expect(() => commentJobsFromEnv(JSON.stringify({ applyJobs: "apply-rest" }))).toThrow(/applyJobs is a list of job names/);
124
+ });
125
+ });
126
+
127
+ describe("a merged pull request's apply runs terragucci's jobs again (#5027)", () => {
128
+ const MERGE = "a".repeat(40);
129
+ const run: WorkflowRun = { id: 10, event: "push", headSha: MERGE, status: "completed", conclusion: "failure", done: true, pullRequests: [], workflow: "terragucci.yml", url: "https://github.com/acme/infra/actions/runs/10" };
130
+ const job = (id: number, name: string, conclusion: string): RunJob => ({ id, name, status: "completed", conclusion, done: true }) as RunJob;
131
+
132
+ const answer = async (jobs: CommentJobs, runJobs: RunJob[], forge: "github" | "forgejo" = "github") => {
133
+ const posted: string[] = [];
134
+ const reran: number[] = [];
135
+ const dispatched: string[] = [];
136
+ const reads = {
137
+ kind: forge,
138
+ permission: async () => "write",
139
+ pullRequest: async (n: number) => ({ number: n, title: "", body: "", state: "merged", head: "feature", headSha: "c".repeat(40), base: "main", sameRepo: true, mergeCommit: MERGE }) as PullRequestDetail,
140
+ headRuns: async () => [run],
141
+ runJobs: async () => runJobs,
142
+ branchRuns: async () => [run],
143
+ } as unknown as ForgeReads;
144
+ const opts: AnswerCommentOptions = {
145
+ forge,
146
+ event: { action: "created", comment: { id: 7, body: "/terragucci apply", user: { login: "alice" } }, issue: { number: 12, pull_request: {} }, repository: { full_name: "acme/infra", permissions: { push: true } }, sender: { login: "alice" } },
147
+ repo: "acme/infra",
148
+ reads,
149
+ poster: { addComment: async (_n: number, text: string) => (posted.push(text), { id: 1 } as never), commitStatuses: async () => [] as never },
150
+ runs: { rerunFailed: async (id) => (reran.push(id), {}), dispatch: async (wf) => (dispatched.push(wf), {}) },
151
+ words: assertProductWords(words),
152
+ base: "main",
153
+ jobs,
154
+ };
155
+ const got = await answerProductComment(opts);
156
+ return { got, posted, reran, dispatched };
157
+ };
158
+
159
+ test("a run whose only failure is apply-rest is started again, where chant's pattern would see the earlier waves' successes alone", async () => {
160
+ const jobs = productPipelineJobs("github", "default", { words, waves, productPlan, commentJob: { jobName: "apply-comment" }, wavesJobNames: terragucciJob });
161
+ const runJobs = [...PUSH_JOBS.slice(0, -1).map((n, i) => job(i + 1, n, "success")), job(99, "apply-rest", "failure"), job(100, "apply-comment", "failure")];
162
+ const named = await answer(commentJobsFromEnv(jobs.comment!.env.CHANT_COMMENT_JOBS), runJobs);
163
+ expect(named.reran).toEqual([10]);
164
+ expect(named.got.ran).toBe(true);
165
+ const chant = await answer({ applyOnMerge: true }, runJobs);
166
+ expect(chant.reran).toEqual([]);
167
+ expect(chant.posted[0]).toMatch(/already succeeded/);
168
+ });
169
+
170
+ test("on Forgejo a failed group job dispatches the workflow again", async () => {
171
+ const jobs = productPipelineJobs("forgejo", "default", { words, waves, productPlan, commentJob: { jobName: "apply-comment" }, wavesJobNames: terragucciJob });
172
+ const runJobs = [job(1, "apply-wave-1", "success"), job(2, "apply-wave-3-east", "failure")];
173
+ const got = await answer(commentJobsFromEnv(jobs.comment!.env.CHANT_COMMENT_JOBS), runJobs, "forgejo");
174
+ expect(got.dispatched).toEqual(["terragucci.yml"]);
175
+ });
176
+ });