@intentius/sql-yodeler 0.12.1 → 0.13.1
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/README.md +2 -0
- package/dist/bin.js +3 -0
- package/dist/bin.js.map +1 -1
- package/dist/chant/build.d.ts +4 -2
- package/dist/chant/build.js +5 -3
- package/dist/chant/build.js.map +1 -1
- package/dist/chant/drift.d.ts +0 -9
- package/dist/chant/drift.js +2 -42
- package/dist/chant/drift.js.map +1 -1
- package/dist/chant/gated-request.d.ts +4 -8
- package/dist/chant/gated-request.js +5 -20
- package/dist/chant/gated-request.js.map +1 -1
- package/dist/chant/ledger-fetch.d.ts +46 -0
- package/dist/chant/ledger-fetch.js +42 -0
- package/dist/chant/ledger-fetch.js.map +1 -0
- package/dist/chant/process.d.ts +7 -19
- package/dist/chant/process.js +12 -30
- package/dist/chant/process.js.map +1 -1
- package/dist/chant/resolve-fallback.d.ts +35 -0
- package/dist/chant/resolve-fallback.js +89 -0
- package/dist/chant/resolve-fallback.js.map +1 -0
- package/dist/chant/sql-types.js +2 -1
- package/dist/chant/sql-types.js.map +1 -1
- package/dist/chant-floor-version.d.ts +4 -11
- package/dist/chant-floor-version.js +4 -11
- package/dist/chant-floor-version.js.map +1 -1
- package/dist/chant-floor.d.ts +5 -36
- package/dist/chant-floor.js +5 -65
- package/dist/chant-floor.js.map +1 -1
- package/dist/check/run.js +1 -1
- package/dist/check/run.js.map +1 -1
- package/dist/ci/check-hooks.d.ts +2 -2
- package/dist/ci/check-hooks.js +4 -17
- package/dist/ci/check-hooks.js.map +1 -1
- package/dist/ci/commit.d.ts +106 -0
- package/dist/ci/commit.js +115 -0
- package/dist/ci/commit.js.map +1 -0
- package/dist/ci/login.d.ts +17 -12
- package/dist/ci/login.js +32 -41
- package/dist/ci/login.js.map +1 -1
- package/dist/ci/render.d.ts +75 -47
- package/dist/ci/render.js +209 -284
- package/dist/ci/render.js.map +1 -1
- package/dist/ci-words.d.ts +18 -19
- package/dist/ci-words.js +16 -30
- package/dist/ci-words.js.map +1 -1
- package/dist/cli.js +1 -3
- package/dist/cli.js.map +1 -1
- package/dist/commands/chant.d.ts +6 -0
- package/dist/commands/chant.js +14 -1
- package/dist/commands/chant.js.map +1 -1
- package/dist/commands/ci.js +15 -4
- package/dist/commands/ci.js.map +1 -1
- package/dist/commands/drift.js +6 -16
- package/dist/commands/drift.js.map +1 -1
- package/dist/commands/init.js +3 -2
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/lint.js +29 -3
- package/dist/commands/lint.js.map +1 -1
- package/dist/commands/new.d.ts +7 -0
- package/dist/commands/new.js +156 -22
- package/dist/commands/new.js.map +1 -1
- package/dist/commands/plan.js +7 -4
- package/dist/commands/plan.js.map +1 -1
- package/dist/commands/upgrade.d.ts +2 -2
- package/dist/commands/upgrade.js +12 -36
- package/dist/commands/upgrade.js.map +1 -1
- package/dist/config/define.d.ts +37 -0
- package/dist/config/define.js +4 -0
- package/dist/config/define.js.map +1 -1
- package/dist/config/load.js +22 -4
- package/dist/config/load.js.map +1 -1
- package/dist/declarative/plan.d.ts +1 -0
- package/dist/declarative/plan.js +5 -3
- package/dist/declarative/plan.js.map +1 -1
- package/dist/exit.d.ts +15 -37
- package/dist/exit.js +23 -79
- package/dist/exit.js.map +1 -1
- package/dist/init/adopt.d.ts +1 -1
- package/dist/init/adopt.js +3 -2
- package/dist/init/adopt.js.map +1 -1
- package/dist/lint/rules.d.ts +3 -1
- package/dist/lint/rules.js +3 -1
- package/dist/lint/rules.js.map +1 -1
- package/dist/lint/unmigrated.d.ts +18 -0
- package/dist/lint/unmigrated.js +62 -0
- package/dist/lint/unmigrated.js.map +1 -0
- package/dist/mcp/chant-hook.d.ts +1 -2
- package/dist/mcp/chant-hook.js +4 -12
- package/dist/mcp/chant-hook.js.map +1 -1
- package/dist/notify/index.d.ts +11 -4
- package/dist/notify/index.js +18 -16
- package/dist/notify/index.js.map +1 -1
- package/dist/pr/forge.d.ts +7 -6
- package/dist/pr/forge.js +19 -56
- package/dist/pr/forge.js.map +1 -1
- package/dist/pr/issue.d.ts +27 -19
- package/dist/pr/issue.js +60 -11
- package/dist/pr/issue.js.map +1 -1
- package/dist/pr/note.d.ts +4 -2
- package/dist/pr/note.js +21 -16
- package/dist/pr/note.js.map +1 -1
- package/dist/pr/overdue.d.ts +5 -12
- package/dist/pr/overdue.js +7 -49
- package/dist/pr/overdue.js.map +1 -1
- package/dist/pr/render.d.ts +0 -2
- package/dist/pr/render.js +0 -2
- package/dist/pr/render.js.map +1 -1
- package/dist/pr/tenants.d.ts +1 -0
- package/dist/pr/tenants.js +6 -4
- package/dist/pr/tenants.js.map +1 -1
- package/dist/project.d.ts +1 -1
- package/dist/project.js +3 -2
- package/dist/project.js.map +1 -1
- package/dist/schemas.d.ts +1 -1
- package/dist/sources/index.js +2 -1
- package/dist/sources/index.js.map +1 -1
- package/dist/versioned/op.js +6 -3
- package/dist/versioned/op.js.map +1 -1
- package/package.json +9 -8
- package/schemas/event.schema.json +17 -2
package/dist/ci/render.js
CHANGED
|
@@ -79,6 +79,7 @@ import { existsSync } from "node:fs";
|
|
|
79
79
|
import { join, resolve } from "node:path";
|
|
80
80
|
import { pathToFileURL } from "node:url";
|
|
81
81
|
import { APPROVAL_MODES, WAVE_GATES, waveGateHint } from "../config/define.js";
|
|
82
|
+
import { commitFiles, COMMIT_TOKEN_ENV, GITLAB_COMMIT_FILES } from "./commit.js";
|
|
82
83
|
import { stepHooks } from "../apply-steps/index.js";
|
|
83
84
|
import { CLOUD_OF, jobLogins, loginSteps, tradeIdTokens } from "./login.js";
|
|
84
85
|
import { RECORD_PLANS_ENV } from "../approval-mode/index.js";
|
|
@@ -87,14 +88,13 @@ import { loadYodelConfig } from "../config/load.js";
|
|
|
87
88
|
import { testFiles } from "../testing/files.js";
|
|
88
89
|
import { CODE_QUALITY_FILE } from "../lint/annotations.js";
|
|
89
90
|
import { chantCheckHooks, checkPresets, declarationFile, declarationWithHooks } from "./check-hooks.js";
|
|
90
|
-
import {
|
|
91
|
+
import { ACTION_PINS, tagComments, usesRef } from "@intentius/chant/action-pins";
|
|
91
92
|
import { Concurrency, Job as ActionsJob, Permissions, Service as ActionsService, Step, Workflow as ActionsWorkflow, } from "@intentius/chant-lexicon-github";
|
|
92
93
|
import { githubSerializer } from "@intentius/chant-lexicon-github/serializer";
|
|
93
94
|
import { forgejoSerializer } from "@intentius/chant-lexicon-forgejo/serializer";
|
|
94
95
|
import { Artifacts as GitLabArtifacts, Image, Job as GitLabJob, Pipeline as GitLabPipeline, Rule, Service as GitLabService, Workflow as GitLabWorkflow, WorkflowRule, } from "@intentius/chant-lexicon-gitlab";
|
|
95
96
|
import { gitlabSerializer } from "@intentius/chant-lexicon-gitlab/serializer";
|
|
96
|
-
import { chantProduct, YODEL_WORDS } from "../ci-words.js";
|
|
97
|
-
import { CHANT_CI_FLOOR, installLine } from "../chant-floor-version.js";
|
|
97
|
+
import { chantProduct, WAVE_WORDS, YODEL_WORDS } from "../ci-words.js";
|
|
98
98
|
export const FORGES = ["github", "gitlab", "forgejo"];
|
|
99
99
|
/** The image every job runs in, where the forge runs jobs in a container. It needs git, so not -slim. */
|
|
100
100
|
export const IMAGE = "node:22-bookworm";
|
|
@@ -104,9 +104,14 @@ export const toolchainOf = (envs) => (envs.image ? { image: envs.image, yodelIma
|
|
|
104
104
|
/** The steps that install the project's packages and put their bins on PATH, as each forge writes them. */
|
|
105
105
|
const installLines = (tc, forge) => tc.yodelImage ? [] : ["npm ci", forge === "gitlab" ? 'export PATH="$PWD/node_modules/.bin:$PATH"' : 'echo "$PWD/node_modules/.bin" >> "$GITHUB_PATH"'];
|
|
106
106
|
export const MAIN = "main";
|
|
107
|
-
/**
|
|
108
|
-
|
|
109
|
-
|
|
107
|
+
/**
|
|
108
|
+
* The actions yodel's own jobs use, pinned by commit SHA from chant's
|
|
109
|
+
* ACTION_PINS (#319), with the release beside each `uses:` line
|
|
110
|
+
* ({@link renderActions}). The forgejo lexicon takes them to
|
|
111
|
+
* code.forgejo.org on Forgejo, and upload-artifact to its v3 row there.
|
|
112
|
+
*/
|
|
113
|
+
const CHECKOUT = usesRef(ACTION_PINS.checkout7);
|
|
114
|
+
const SETUP_NODE = usesRef(ACTION_PINS.setupNode7);
|
|
110
115
|
/** DEV for dev: the prefix of an environment's variables. */
|
|
111
116
|
export const prefixOf = (env) => env.toUpperCase().replace(/[^A-Z0-9]/g, "_");
|
|
112
117
|
/** The line the lint job runs chant's check moment with (#294): the hooks are read at the pull request's base. */
|
|
@@ -167,32 +172,11 @@ const idTokens = (clouds, logins = []) => ({
|
|
|
167
172
|
const loginsOf = (envs, held) => jobLogins(envs.login, held, (env, role) => envs.credentials?.[env]?.[role].cloud);
|
|
168
173
|
/** A job's login steps as the github lexicon's Steps. */
|
|
169
174
|
const actionsLogin = (forge, logins) => loginSteps(forge, logins).steps.map((st) => new Step({ name: st.name, run: st.lines.join("\n") }));
|
|
170
|
-
/** A job's login steps as plain step objects, for YAML chant rendered and yodel edits. */
|
|
171
|
-
const rawLogin = (forge, logins) => loginSteps(forge, logins).steps.map((st) => ({ name: st.name, run: st.lines.join("\n") }));
|
|
172
175
|
/** A GitLab job's login: its script lines and its variables. */
|
|
173
176
|
const gitlabLogin = (logins) => {
|
|
174
177
|
const out = loginSteps("gitlab", logins);
|
|
175
178
|
return { lines: out.steps.flatMap((st) => st.lines), variables: out.variables };
|
|
176
179
|
};
|
|
177
|
-
/** Puts `steps` before the first step `at` picks (refusing a job without one). */
|
|
178
|
-
function insertSteps(steps, add, at, what) {
|
|
179
|
-
if (add.length === 0)
|
|
180
|
-
return;
|
|
181
|
-
const i = steps.findIndex(at);
|
|
182
|
-
if (i < 0)
|
|
183
|
-
throw new Error(`${what}: no step to log in before`);
|
|
184
|
-
steps.splice(i, 0, ...add);
|
|
185
|
-
}
|
|
186
|
-
/** Puts `lines` before the first script line `at` picks (refusing a job without one). */
|
|
187
|
-
function insertLines(script, add, at, what) {
|
|
188
|
-
if (add.length === 0)
|
|
189
|
-
return;
|
|
190
|
-
const i = script?.findIndex(at) ?? -1;
|
|
191
|
-
if (!script || i < 0)
|
|
192
|
-
throw new Error(`${what}: no script line to log in before`);
|
|
193
|
-
script.splice(i, 0, ...add);
|
|
194
|
-
}
|
|
195
|
-
const runsChant = (st) => typeof st.run === "string" && st.run.startsWith("chant run ");
|
|
196
180
|
/** The roles a pull request's plan job for `env` holds: the environment's reader and the reader of the one it requires. */
|
|
197
181
|
const planRoles = (waves, env) => [env, ...requiredOf(waves, env)].map((e) => [e, "reader"]);
|
|
198
182
|
/** The roles a wave job for `env` holds: the writer, and the reader of the environment it requires. */
|
|
@@ -204,7 +188,7 @@ function identityHeader(forge, clouds) {
|
|
|
204
188
|
return [];
|
|
205
189
|
const names = clouds.join(", ");
|
|
206
190
|
if (forge === "github")
|
|
207
|
-
return [`# Cloud identity: a password here is a ${names} token, minted from the job's`, "# identity, so the
|
|
191
|
+
return [`# Cloud identity: a password here is a ${names} token, minted from the job's`, "# identity, so the jobs that hold one get id-token: write (an OIDC token)."];
|
|
208
192
|
if (forge === "gitlab")
|
|
209
193
|
return [`# Cloud identity: a password here is a ${names} token, minted from the job's`, `# identity: the jobs that hold one declare id_tokens (${clouds.map((c) => ID_TOKENS[c].variable).join(", ")}).`];
|
|
210
194
|
return [`# Cloud identity: a password here is a ${names} token. Forgejo gives a job no`, "# OIDC token, so the runner's own cloud identity mints it."];
|
|
@@ -215,10 +199,14 @@ const secret = (name) => `\${{ secrets.${name} }}`;
|
|
|
215
199
|
const secrets = (names) => Object.fromEntries(names.map((n) => [n, secret(n)]));
|
|
216
200
|
/**
|
|
217
201
|
* The approvals: `chant approve` records them on the chant/lifecycle branch,
|
|
218
|
-
* and approvers push it. A CI checkout fetches one ref, so
|
|
219
|
-
*
|
|
202
|
+
* and approvers push it. A CI checkout fetches one ref, so each job fetches
|
|
203
|
+
* this one too, through chant's reader (`yodel ci --fetch-ledger`, #326):
|
|
204
|
+
* yodel writes neither the branch nor the remote-tracking ref chant's push
|
|
205
|
+
* leases against. `yodel` is the project's, named by path, as the line may
|
|
206
|
+
* run before the install's PATH reaches the step; in the yodel image, the
|
|
207
|
+
* image's.
|
|
220
208
|
*/
|
|
221
|
-
export const
|
|
209
|
+
export const fetchApprovals = (tc = { yodelImage: false }) => `${tc.yodelImage ? "yodel" : "./node_modules/.bin/yodel"} ci --fetch-ledger`;
|
|
222
210
|
/** The watch reads; it never pushes. See the comment at the top. */
|
|
223
211
|
export const NO_PUSH = "git remote set-url --push origin no-push://the-drift-watch-only-reads";
|
|
224
212
|
/** After the watch's `chant run`: drift against the history, the environment's tracking issue, and exit 2 on drift. */
|
|
@@ -301,13 +289,9 @@ export const reportCommand = (env, tc = { image: IMAGE, yodelImage: false }) =>
|
|
|
301
289
|
};
|
|
302
290
|
/** The artifact a job keeps its report in. */
|
|
303
291
|
export const reportArtifact = (job) => `yodel-report-${job}`;
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
* forgejo lexicon resolves it, from code.forgejo.org/actions) refuses any
|
|
308
|
-
* server but github.com's and fails the step, so the job, on Forgejo.
|
|
309
|
-
*/
|
|
310
|
-
export const FORGEJO_UPLOAD_ARTIFACT = "https://code.forgejo.org/forgejo/upload-artifact@v4";
|
|
292
|
+
/** upload-artifact v4 on GitHub, and v3 from code.forgejo.org on Forgejo, where v4 refuses any server but github.com's (chant#4008), as chant's own Forgejo jobs have it. */
|
|
293
|
+
export const UPLOAD_ARTIFACT = usesRef(ACTION_PINS.uploadArtifact4);
|
|
294
|
+
export const FORGEJO_UPLOAD_ARTIFACT = `https://code.forgejo.org/${usesRef(ACTION_PINS.uploadArtifact3)}`;
|
|
311
295
|
const uploadArtifact = (forge) => (forge === "forgejo" ? FORGEJO_UPLOAD_ARTIFACT : UPLOAD_ARTIFACT);
|
|
312
296
|
/**
|
|
313
297
|
* The comment at the top of every file, as `#` lines. The files declared here
|
|
@@ -350,7 +334,7 @@ function setupSteps(inContainer, tc, approvals = true) {
|
|
|
350
334
|
steps.push(new Step({ uses: SETUP_NODE, with: { "node-version": 22, cache: "npm" } }));
|
|
351
335
|
steps.push(...installLines(tc, "github").map((run) => new Step({ run })));
|
|
352
336
|
if (approvals)
|
|
353
|
-
steps.push(new Step({ name: "Fetch the approvals (chant/lifecycle)", run:
|
|
337
|
+
steps.push(new Step({ name: "Fetch the approvals (chant/lifecycle)", run: fetchApprovals(tc) }));
|
|
354
338
|
return steps;
|
|
355
339
|
}
|
|
356
340
|
/** Forgejo runs every job in the image; GitHub on the runner's VM, unless the job asks for the container or the image is yodel's. */
|
|
@@ -537,7 +521,7 @@ export function gitlabPipeline(envs_) {
|
|
|
537
521
|
const jobs = new Map();
|
|
538
522
|
jobs.set(".yodel", new GitLabJob({
|
|
539
523
|
image: new Image({ name: tc.image }),
|
|
540
|
-
before_script: [...installLines(tc, "gitlab"),
|
|
524
|
+
before_script: [...installLines(tc, "gitlab"), fetchApprovals(tc)],
|
|
541
525
|
}));
|
|
542
526
|
const svc = REPLAY_SERVICE[dialect];
|
|
543
527
|
const types = envs_.typedColumns === true ? [TYPES_CHECK] : [];
|
|
@@ -623,6 +607,8 @@ export function gitlabPipeline(envs_) {
|
|
|
623
607
|
"# pipeline (yodel.config.ts ci.apply: false): migrations are applied outside CI.",
|
|
624
608
|
]),
|
|
625
609
|
`# The drift watches are in ${GITLAB_WATCH_FILE} (stage ops).`,
|
|
610
|
+
...(respondsNew(envs_) ? [`# /yodel new, chant's commit job, is in ${GITLAB_COMMIT_FILES.new} (stage ops).`] : []),
|
|
611
|
+
...(rebaseRespond(envs_) !== "off" ? [`# /yodel rebase, after the merge request's check, is in ${GITLAB_COMMIT_FILES.rebase} (stage ops).`] : []),
|
|
626
612
|
"#",
|
|
627
613
|
"# GitLab gives every job the project's CI/CD variables unless a variable is",
|
|
628
614
|
"# protected or scoped to an environment, so the split between reader and",
|
|
@@ -657,7 +643,7 @@ export function gitlabPipeline(envs_) {
|
|
|
657
643
|
const applyStages = apply ? [...plans, ...waveStages(waves), ...(envs_.resume ? [RESUME_STAGE] : [])] : [];
|
|
658
644
|
const pipeline = new GitLabPipeline({
|
|
659
645
|
comment: asComment(header),
|
|
660
|
-
include: [...(apply ? [{ local: GITLAB_APPLY_FILE }] : []), { local: GITLAB_WATCH_FILE }],
|
|
646
|
+
include: [...(apply ? [{ local: GITLAB_APPLY_FILE }] : []), { local: GITLAB_WATCH_FILE }, ...(respondsNew(envs_) ? [{ local: GITLAB_COMMIT_FILES.new }] : []), ...(rebaseRespond(envs_) !== "off" ? [{ local: GITLAB_COMMIT_FILES.rebase }] : [])],
|
|
661
647
|
stages: [...new Set([...jobStages, ...applyStages, "ops"])],
|
|
662
648
|
variables: { GIT_DEPTH: "0" },
|
|
663
649
|
});
|
|
@@ -674,7 +660,7 @@ export const WAVES_SPEC = "yodel-waves.json";
|
|
|
674
660
|
export const APPLY_PIPELINE = "yodel-apply";
|
|
675
661
|
/**
|
|
676
662
|
* The waves spec. What the file holds is the same for every forge; each
|
|
677
|
-
* forge's jobs get their own variables on top (`
|
|
663
|
+
* forge's jobs get their own variables and OIDC tokens on top (`job`).
|
|
678
664
|
*
|
|
679
665
|
* The spec names the sql lexicon's `sql-migrate` capability (#291,
|
|
680
666
|
* chant#4569), and chant plans and applies each run through it, which calls
|
|
@@ -693,7 +679,7 @@ export const APPLY_PIPELINE = "yodel-apply";
|
|
|
693
679
|
* the base commit, as it does run by hand. A step runs once, under the
|
|
694
680
|
* lock, as #107 asks.
|
|
695
681
|
*/
|
|
696
|
-
export function wavesSpec(waves,
|
|
682
|
+
export function wavesSpec(waves, job, resume) {
|
|
697
683
|
const first = waves[0];
|
|
698
684
|
if (!first)
|
|
699
685
|
throw new Error("no waves to render");
|
|
@@ -710,7 +696,7 @@ export function wavesSpec(waves, variables, resume) {
|
|
|
710
696
|
gate: w.gate,
|
|
711
697
|
...(w.shares !== undefined && w.shares > 1 ? { shares: w.shares } : {}),
|
|
712
698
|
...(w.approval !== undefined && w.approval !== "ledger" ? { approval: w.approval } : {}),
|
|
713
|
-
...(
|
|
699
|
+
...(job ? { environment: { name: w.env }, ...job(w.env) } : {}),
|
|
714
700
|
// The steps around apply (#64) as chant's stage hooks, which the apply runs under its lock (#206).
|
|
715
701
|
...hooksOf(w.steps),
|
|
716
702
|
})),
|
|
@@ -724,10 +710,32 @@ function hooksOf(steps) {
|
|
|
724
710
|
const plan = stepHooks(steps)?.before?.plan;
|
|
725
711
|
return plan && plan.length > 0 ? { hooks: { before: { plan } } } : {};
|
|
726
712
|
}
|
|
713
|
+
/**
|
|
714
|
+
* What a job holding the roles `held` adds on `forge` for its cloud login
|
|
715
|
+
* (#132), in the shape chant's generators take (#4791): the login's steps,
|
|
716
|
+
* its variables (GitLab's trade spec; on GitHub and Forgejo the steps write
|
|
717
|
+
* $GITHUB_ENV) and the OIDC tokens the job requests. Forgejo gives a job no
|
|
718
|
+
* OIDC token, so it requests none there.
|
|
719
|
+
*/
|
|
720
|
+
function jobLogin(forge, envs, held) {
|
|
721
|
+
const logins = loginsOf(envs, held);
|
|
722
|
+
const out = loginSteps(forge, logins);
|
|
723
|
+
const clouds = cloudsOf(envs, held);
|
|
724
|
+
const before = out.steps.map((st) => ({ name: st.name, run: st.lines }));
|
|
725
|
+
if (forge === "forgejo" || clouds.length === 0)
|
|
726
|
+
return { before, variables: out.variables };
|
|
727
|
+
const tokens = Object.fromEntries(Object.entries(idTokens(clouds, logins)).map(([name, t]) => [name, t.aud]));
|
|
728
|
+
return { before, variables: out.variables, idTokens: tokens };
|
|
729
|
+
}
|
|
727
730
|
/**
|
|
728
731
|
* The waves spec file and the apply pipeline on each forge. The spec comes
|
|
729
732
|
* from chant's productPipelineJobs under yodel's words (#289), and each job
|
|
730
|
-
* runs `yodel chant` where chant's own runs `chant` (
|
|
733
|
+
* runs `yodel chant` where chant's own runs `chant` (chant's `bin`,
|
|
734
|
+
* {@link productWaves}). What yodel adds to chant's jobs goes in as
|
|
735
|
+
* generateOpWavesPipeline's inputs (#4791): each wave job's cloud login,
|
|
736
|
+
* its `yodel report` and the report's artifact (`waveJobs`), the plans
|
|
737
|
+
* job's login, write probe and readers (`recordPlans`), and each wave's
|
|
738
|
+
* variables and OIDC tokens in the spec.
|
|
731
739
|
*/
|
|
732
740
|
export async function applyPipelines(dir, envs, product) {
|
|
733
741
|
const { dialect, waves } = envs;
|
|
@@ -736,22 +744,51 @@ export async function applyPipelines(dir, envs, product) {
|
|
|
736
744
|
const named = (forge, spec) => productWaves(product, forge, spec);
|
|
737
745
|
const out = [{ path: WAVES_SPEC, content: `${JSON.stringify(named("github", wavesSpec(waves)).spec, null, 2)}\n` }];
|
|
738
746
|
// A GitLab job already has the project's variables, a writer's only under its environment.
|
|
739
|
-
const
|
|
747
|
+
const jobOf = (forge) => (env) => {
|
|
740
748
|
const wave = waves.find((w) => w.env === env);
|
|
741
749
|
const required = wave?.requires;
|
|
750
|
+
const login = jobLogin(forge, envs, waveRoles(waves, env));
|
|
751
|
+
return {
|
|
752
|
+
variables: {
|
|
753
|
+
YODEL_CREDENTIALS: `writer:${env}`,
|
|
754
|
+
...(forge === "gitlab"
|
|
755
|
+
? {}
|
|
756
|
+
: {
|
|
757
|
+
...secrets([...writerVariables(env, dialect, envs.credentials?.[env]), ...WEBHOOK_VARIABLES]),
|
|
758
|
+
// chant's sql-migrate runs the wave's yodel plan --part and --wave-plan with the writer taken out
|
|
759
|
+
// (readerEnv in the sql lexicon), so they connect as the environment's reader.
|
|
760
|
+
...secrets(readerVariables(env, dialect, envs.credentials?.[env])),
|
|
761
|
+
...(required ? secrets(readerVariables(required, dialect, envs.credentials?.[required])) : {}),
|
|
762
|
+
// A pr-review wave (#48) reads the merged pull request's reviews with the job's token.
|
|
763
|
+
...(wave?.approval === "pr-review" ? { GITHUB_TOKEN: "${{ github.token }}" } : {}),
|
|
764
|
+
}),
|
|
765
|
+
...login.variables,
|
|
766
|
+
},
|
|
767
|
+
...(login.idTokens ? { idTokens: login.idTokens } : {}),
|
|
768
|
+
};
|
|
769
|
+
};
|
|
770
|
+
// Each wave's jobs: the cloud login (#132) before the wave's command, and the report, kept as an artifact whatever the exit (a wave that waits exits 3).
|
|
771
|
+
const waveJobs = (forge) => Object.fromEntries(waves.map((w) => {
|
|
772
|
+
const login = jobLogin(forge, envs, waveRoles(waves, w.env));
|
|
773
|
+
const inputs = {
|
|
774
|
+
...(login.before.length > 0 ? { before: login.before } : {}),
|
|
775
|
+
report: { name: `yodel report ${w.env}`, run: [reportCommand(w.env, tc)] },
|
|
776
|
+
artifact: { step: "Keep the report", name: reportArtifact("{job}"), path: forge === "gitlab" ? `${REPORT_DIR}/` : REPORT_DIR },
|
|
777
|
+
};
|
|
778
|
+
return [w.env, inputs];
|
|
779
|
+
}));
|
|
780
|
+
// The plans job of a pr-review wave (#48): each environment's reader, which it plans the waves with, and the write probe first, as every pull request job has (#50, #130).
|
|
781
|
+
const recordPlans = (forge) => {
|
|
782
|
+
const waveEnvs = [...new Set(waves.map((w) => w.env))];
|
|
783
|
+
const login = jobLogin(forge, envs, waveEnvs.map((e) => [e, "reader"]));
|
|
742
784
|
return {
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
:
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
...secrets(readerVariables(env, dialect, envs.credentials?.[env])),
|
|
751
|
-
...(required ? secrets(readerVariables(required, dialect, envs.credentials?.[required])) : {}),
|
|
752
|
-
// A pr-review wave (#48) reads the merged pull request's reviews with the job's token.
|
|
753
|
-
...(wave?.approval === "pr-review" ? { GITHUB_TOKEN: "${{ github.token }}" } : {}),
|
|
754
|
-
}),
|
|
785
|
+
before: [...login.before, { name: "This job cannot write", run: [writeProbe(waveEnvs)] }],
|
|
786
|
+
variables: {
|
|
787
|
+
[RECORD_PLANS_ENV]: "1",
|
|
788
|
+
...(forge === "gitlab" ? {} : secrets(waveEnvs.flatMap((e) => readerVariables(e, dialect, envs.credentials?.[e])))),
|
|
789
|
+
...login.variables,
|
|
790
|
+
},
|
|
791
|
+
...(login.idTokens ? { idTokens: login.idTokens } : {}),
|
|
755
792
|
};
|
|
756
793
|
};
|
|
757
794
|
const header = [
|
|
@@ -791,11 +828,14 @@ export async function applyPipelines(dir, envs, product) {
|
|
|
791
828
|
"# holds no writer's credentials: it plans with each environment's reader.",
|
|
792
829
|
];
|
|
793
830
|
for (const forge of FORGES) {
|
|
794
|
-
const { spec, jobs } = named(forge, wavesSpec(waves,
|
|
831
|
+
const { spec, jobs } = named(forge, wavesSpec(waves, jobOf(forge), envs.resume));
|
|
795
832
|
const result = await generateOpWavesPipeline(spec, forge, {
|
|
796
833
|
specFile: WAVES_SPEC,
|
|
834
|
+
bin: YODEL_WORDS.bin,
|
|
797
835
|
image: tc.image,
|
|
798
|
-
beforeScript: [...installLines(tc, forge),
|
|
836
|
+
beforeScript: [...installLines(tc, forge), fetchApprovals(tc)],
|
|
837
|
+
waveJobs: waveJobs(forge),
|
|
838
|
+
recordPlans: recordPlans(forge),
|
|
799
839
|
...(forge === "gitlab" ? { opsFileName: GITLAB_APPLY_FILE } : {}),
|
|
800
840
|
}, dir);
|
|
801
841
|
if (!result.success || !result.files)
|
|
@@ -803,140 +843,17 @@ export async function applyPipelines(dir, envs, product) {
|
|
|
803
843
|
for (const file of result.files) {
|
|
804
844
|
const at = forge === "gitlab" ? GITLAB_APPLY_FILE : `${forge === "github" ? ".github" : ".forgejo"}/workflows/${file.name}`;
|
|
805
845
|
const yaml = file.yaml.endsWith("\n") ? file.yaml : `${file.yaml}\n`;
|
|
806
|
-
// The resume workflow (GitHub, Forgejo) has no wave job to report on.
|
|
807
846
|
if (forge !== "gitlab" && file.name === `${APPLY_PIPELINE}-resume.yml`)
|
|
808
|
-
out.push({ path: at, content: withHeader(resumeHeader(forge),
|
|
847
|
+
out.push({ path: at, content: withHeader(resumeHeader(forge), yaml) });
|
|
809
848
|
// The pull request workflow that records the plans a review approves (#48).
|
|
810
849
|
else if (forge !== "gitlab" && file.name === `${APPLY_PIPELINE}-plans.yml`)
|
|
811
|
-
out.push({ path: at, content: withHeader(plansHeader,
|
|
850
|
+
out.push({ path: at, content: withHeader(plansHeader, yaml) });
|
|
812
851
|
else
|
|
813
|
-
out.push({ path: at, content: withHeader([...header, ...identityHeader(forge, waveClouds)], withWaveLines(forge,
|
|
852
|
+
out.push({ path: at, content: withHeader([...header, ...identityHeader(forge, waveClouds)], withWaveLines(forge, yaml, jobs)) });
|
|
814
853
|
}
|
|
815
854
|
}
|
|
816
855
|
return out;
|
|
817
856
|
}
|
|
818
|
-
/**
|
|
819
|
-
* The wave jobs' report: chant's generateOpWavesPipeline takes run lines
|
|
820
|
-
* after the wave's command but no step that runs when it fails (a wave that
|
|
821
|
-
* waits exits 3), and no artifact, so they are added to its output here.
|
|
822
|
-
* The output is parsed and emitted again; the emitter is checked to give
|
|
823
|
-
* back chant's text unchanged before anything is added, so a change in
|
|
824
|
-
* chant's emitter fails here instead of rendering something else.
|
|
825
|
-
*/
|
|
826
|
-
/**
|
|
827
|
-
* chant writes a SHA-pinned action as `uses: owner/action@<sha> # vX.Y.Z`
|
|
828
|
-
* (chant#4403, #4410); the `# vX.Y.Z` is a YAML comment, which parsing drops.
|
|
829
|
-
* Each re-emit below puts every one back on the line that names the same
|
|
830
|
-
* pinned ref, so the re-emitted text still matches chant's and the files keep
|
|
831
|
-
* the release each SHA stands for.
|
|
832
|
-
*/
|
|
833
|
-
export function withPinComments(original, emitted) {
|
|
834
|
-
const comments = new Map();
|
|
835
|
-
for (const [, ref, comment] of original.matchAll(/uses: (\S+@[0-9a-f]{40}) (# [^\n]*)$/gm)) {
|
|
836
|
-
if (ref !== undefined && comment !== undefined)
|
|
837
|
-
comments.set(ref, comment);
|
|
838
|
-
}
|
|
839
|
-
if (comments.size === 0)
|
|
840
|
-
return emitted;
|
|
841
|
-
return emitted.replace(/uses: (\S+@[0-9a-f]{40})$/gm, (line, ref) => {
|
|
842
|
-
const comment = comments.get(ref);
|
|
843
|
-
return comment ? `${line} ${comment}` : line;
|
|
844
|
-
});
|
|
845
|
-
}
|
|
846
|
-
function withWaveReports(forge, yaml, waves, tc, envs = {}) {
|
|
847
|
-
// A wide wave's jobs are wave-<k>-<env>-decide and -share-<i>.
|
|
848
|
-
const envOf = (job) => waves.find((w, i) => new RegExp(`^wave-${String(i + 1)}-${w.env.replace(/[^A-Za-z0-9]/g, "\\$&")}(-decide|-share-\\d+)?$`).test(job))?.env;
|
|
849
|
-
const body = yaml.search(/^[^#\n]/m);
|
|
850
|
-
const header = yaml.slice(0, body);
|
|
851
|
-
const doc = parseYAMLDocument(yaml);
|
|
852
|
-
if (forge === "gitlab") {
|
|
853
|
-
const emit = (d) => withPinComments(yaml, `${header}${Object.entries(d).map(([k, v]) => emitYAMLEntry(k, v)).join("\n\n")}\n`);
|
|
854
|
-
if (emit(doc) !== yaml)
|
|
855
|
-
throw new Error("gitlab: the apply waves do not emit back to chant's text");
|
|
856
|
-
// The merge request job that records the plans a review approves (#48): the waves as after the merge.
|
|
857
|
-
const record = doc[`${APPLY_PIPELINE}-record-plans`];
|
|
858
|
-
if (record) {
|
|
859
|
-
record.variables = { ...(record.variables ?? {}), [RECORD_PLANS_ENV]: "1" };
|
|
860
|
-
// It holds readers only, and shows it first, as every merge request job does (#50, #130).
|
|
861
|
-
const waveEnvs = [...new Set(waves.map((w) => w.env))];
|
|
862
|
-
const at = record.script?.findIndex((l) => l.includes("--record-plans")) ?? -1;
|
|
863
|
-
if (!record.script || at < 0)
|
|
864
|
-
throw new Error("gitlab: the record-plans job has no chant run wave --record-plans line");
|
|
865
|
-
record.script.splice(at, 0, writeProbe(waveEnvs));
|
|
866
|
-
const readerRoles = waveEnvs.map((e) => [e, "reader"]);
|
|
867
|
-
const readers = cloudsOf(envs, readerRoles);
|
|
868
|
-
const logins = loginsOf(envs, readerRoles);
|
|
869
|
-
if (readers.length > 0)
|
|
870
|
-
record.id_tokens = idTokens(readers, logins);
|
|
871
|
-
const login = gitlabLogin(logins);
|
|
872
|
-
insertLines(record.script, login.lines, (l) => l === writeProbe(waveEnvs), "gitlab: the record-plans job");
|
|
873
|
-
if (Object.keys(login.variables).length > 0)
|
|
874
|
-
record.variables = { ...record.variables, ...login.variables };
|
|
875
|
-
}
|
|
876
|
-
for (const [name, job] of Object.entries(doc)) {
|
|
877
|
-
const env = envOf(name);
|
|
878
|
-
if (!env || typeof job !== "object" || job === null)
|
|
879
|
-
continue;
|
|
880
|
-
const clouds = cloudsOf(envs, waveRoles(waves, env));
|
|
881
|
-
const logins = loginsOf(envs, waveRoles(waves, env));
|
|
882
|
-
const login = gitlabLogin(logins);
|
|
883
|
-
const w = job;
|
|
884
|
-
insertLines(w.script, login.lines, (l) => l.startsWith("chant run "), `gitlab: ${name}`);
|
|
885
|
-
if (Object.keys(login.variables).length > 0)
|
|
886
|
-
w.variables = { ...w.variables, ...login.variables };
|
|
887
|
-
Object.assign(job, { after_script: [reportCommand(env, tc)], artifacts: { name: reportArtifact(name), when: "always", paths: [`${REPORT_DIR}/`] }, ...(clouds.length > 0 ? { id_tokens: idTokens(clouds, logins) } : {}) });
|
|
888
|
-
}
|
|
889
|
-
return emit(doc);
|
|
890
|
-
}
|
|
891
|
-
const at = yaml.indexOf("\njobs:\n");
|
|
892
|
-
if (at < 0)
|
|
893
|
-
throw new Error(`${forge}: the apply waves have no jobs`);
|
|
894
|
-
const jobs = doc.jobs;
|
|
895
|
-
const top = yaml.slice(0, at + 1);
|
|
896
|
-
const emitJobs = () => withPinComments(yaml, `${emitYAMLEntry("jobs", jobs)}\n`);
|
|
897
|
-
if (`${top}${emitJobs()}` !== yaml)
|
|
898
|
-
throw new Error(`${forge}: the apply waves do not emit back to chant's text`);
|
|
899
|
-
for (const [name, job] of Object.entries(jobs)) {
|
|
900
|
-
const env = envOf(name);
|
|
901
|
-
if (!env)
|
|
902
|
-
continue;
|
|
903
|
-
// The cloud login (#132) before the wave's chant run, when a role it holds is cloud-minted.
|
|
904
|
-
insertSteps(job.steps, rawLogin(forge, loginsOf(envs, waveRoles(waves, env))), runsChant, `${forge}: ${name}`);
|
|
905
|
-
job.steps.push({ name: `yodel report ${env}`, if: "always()", run: reportCommand(env, tc) }, { name: "Keep the report", if: "always()", uses: uploadArtifact(forge), with: { name: reportArtifact(name), path: REPORT_DIR, "if-no-files-found": "ignore" } });
|
|
906
|
-
}
|
|
907
|
-
return `${withIdToken(forge, top, cloudsOf(envs, waves.flatMap((w) => waveRoles(waves, w.env))))}${emitJobs()}`;
|
|
908
|
-
}
|
|
909
|
-
/**
|
|
910
|
-
* The plans workflow (GitHub, Forgejo) for a pr-review wave (#48): chant's,
|
|
911
|
-
* with each environment's reader (the waves are planned on every one, and a
|
|
912
|
-
* wave reads the history of the one it requires) and {@link RECORD_PLANS_ENV}
|
|
913
|
-
* on its job. The output is parsed and emitted again, checked first to give
|
|
914
|
-
* back chant's text unchanged.
|
|
915
|
-
*/
|
|
916
|
-
function withPlansReaders(forge, yaml, waves, dialect, creds = {}) {
|
|
917
|
-
const at = yaml.indexOf("\njobs:\n");
|
|
918
|
-
if (at < 0)
|
|
919
|
-
throw new Error(`${forge}: the plans workflow has no jobs`);
|
|
920
|
-
const doc = parseYAMLDocument(yaml);
|
|
921
|
-
const top = yaml.slice(0, at + 1);
|
|
922
|
-
const emitJobs = () => withPinComments(yaml, `${emitYAMLEntry("jobs", doc.jobs)}\n`);
|
|
923
|
-
if (`${top}${emitJobs()}` !== yaml)
|
|
924
|
-
throw new Error(`${forge}: the plans workflow does not emit back to chant's text`);
|
|
925
|
-
const job = doc.jobs["record-plans"];
|
|
926
|
-
if (!job)
|
|
927
|
-
throw new Error(`${forge}: the plans workflow has no record-plans job`);
|
|
928
|
-
const envs = [...new Set(waves.map((w) => w.env))];
|
|
929
|
-
const steps = job.steps;
|
|
930
|
-
// It holds readers only, and shows it before it plans, as every pull request job does (#50, #130).
|
|
931
|
-
const recordAt = steps.findIndex((st) => typeof st.run === "string" && st.run.includes("--record-plans"));
|
|
932
|
-
if (recordAt < 0)
|
|
933
|
-
throw new Error(`${forge}: the record-plans job has no chant run wave --record-plans step`);
|
|
934
|
-
steps.splice(recordAt, 0, ...(forge === "gitlab" ? [] : rawLogin(forge, loginsOf(creds, envs.map((e) => [e, "reader"])))), { name: "This job cannot write", run: writeProbe(envs) });
|
|
935
|
-
delete job.steps;
|
|
936
|
-
Object.assign(job, { env: { [RECORD_PLANS_ENV]: "1", ...secrets(envs.flatMap((e) => readerVariables(e, dialect, creds.credentials?.[e]))) }, steps });
|
|
937
|
-
// A reader whose password is a cloud token (#50) is minted from the job's identity.
|
|
938
|
-
return `${withIdToken(forge, top, cloudsOf(creds, envs.map((e) => [e, "reader"])))}${emitJobs()}`;
|
|
939
|
-
}
|
|
940
857
|
/** yodel.config.ts `waves`, checked against the environments; one wave per environment, gate `always`, without it. */
|
|
941
858
|
export async function readWaves(dir, envs) {
|
|
942
859
|
const declared = (await readConfig(dir))?.waves;
|
|
@@ -989,8 +906,8 @@ export async function withTenants(dir, waves) {
|
|
|
989
906
|
// the names on the ledger since the waves began, so approvals already
|
|
990
907
|
// recorded keep counting), and each job running `yodel chant` where chant's
|
|
991
908
|
// own runs `chant`, so the job writes yodel's statuses and approve lines.
|
|
992
|
-
// The forge lexicons serialize the jobs
|
|
993
|
-
//
|
|
909
|
+
// The forge lexicons serialize the jobs, and yodel's reports, logins and
|
|
910
|
+
// readers go in as generateOpWavesPipeline's inputs (#4791).
|
|
994
911
|
//
|
|
995
912
|
// Still yodel's own: the plan jobs (`yodel plan <env> --comment` with the
|
|
996
913
|
// write probe), which post chant's note (../pr/note.ts, #290); the lint
|
|
@@ -999,7 +916,7 @@ export async function withTenants(dir, waves) {
|
|
|
999
916
|
// ({@link watchPipelines}).
|
|
1000
917
|
/** The spec under yodel's words, and the wave jobs chant's product pipeline names for `forge`. */
|
|
1001
918
|
export function productWaves(product, forge, spec) {
|
|
1002
|
-
const out = product.productPipelineJobs(forge, spec.waves[0]?.name ?? MAIN, { words:
|
|
919
|
+
const out = product.productPipelineJobs(forge, spec.waves[0]?.name ?? MAIN, { words: WAVE_WORDS, waves: spec, wavesSpecFile: WAVES_SPEC });
|
|
1003
920
|
if (!out.waves)
|
|
1004
921
|
throw new Error(`${forge}: chant's product pipeline gave no apply waves`);
|
|
1005
922
|
if (out.waves.specFile !== WAVES_SPEC)
|
|
@@ -1009,13 +926,6 @@ export function productWaves(product, forge, spec) {
|
|
|
1009
926
|
throw new Error(`${forge}: chant's product pipeline names the waves ${named.name}, and the ledger has them as ${APPLY_PIPELINE}`);
|
|
1010
927
|
return { spec: named, jobs: out.waves.jobs };
|
|
1011
928
|
}
|
|
1012
|
-
/** A `chant` command line in a rendered file: a step's `run:` or a GitLab script line, never a comment. */
|
|
1013
|
-
const CHANT_LINE = /^(\s*(?:- )?(?:run: )?)(chant [a-z].*)$/gm;
|
|
1014
|
-
/** `yaml` with yodel's bin in each chant command line (#289), through chant's own productCommand. */
|
|
1015
|
-
export function withBin(product, yaml) {
|
|
1016
|
-
const words = product.assertProductWords(YODEL_WORDS);
|
|
1017
|
-
return yaml.replace(CHANT_LINE, (_m, lead, line) => `${lead}${product.productCommand(words, line)}`);
|
|
1018
|
-
}
|
|
1019
929
|
/** Refuses an apply file whose wave jobs do not run the line chant's product pipeline gives each of them. */
|
|
1020
930
|
function withWaveLines(forge, yaml, jobs) {
|
|
1021
931
|
for (const job of jobs) {
|
|
@@ -1027,81 +937,43 @@ function withWaveLines(forge, yaml, jobs) {
|
|
|
1027
937
|
return yaml;
|
|
1028
938
|
}
|
|
1029
939
|
/**
|
|
1030
|
-
* The watch's spec for one environment: the Op,
|
|
1031
|
-
* reader
|
|
1032
|
-
*
|
|
1033
|
-
*
|
|
940
|
+
* The watch's spec for one environment: the Op, the cloud login (#132) when
|
|
941
|
+
* its reader is cloud-minted, as setup steps before the job's other lines,
|
|
942
|
+
* and on GitHub and Forgejo the reader's secrets. A GitLab job already has
|
|
943
|
+
* the project's variables (a writer's only under its environment, which the
|
|
944
|
+
* watch does not declare), and `NAME: $NAME` there would refer to itself;
|
|
945
|
+
* there the login's variables and the job's ID tokens go on the job.
|
|
1034
946
|
*/
|
|
1035
|
-
export function watchSpec(env,
|
|
1036
|
-
|
|
1037
|
-
|
|
947
|
+
export function watchSpec(env, forge, envs) {
|
|
948
|
+
const held = [[env, "reader"]];
|
|
949
|
+
const logins = loginsOf(envs, held);
|
|
950
|
+
const login = loginSteps(forge, logins);
|
|
951
|
+
// GitLab: a script line each, as the job's own lines are; GitHub and Forgejo: a step each.
|
|
952
|
+
const steps = forge === "gitlab" ? login.steps.flatMap((st) => st.lines.map((run) => ({ run }))) : login.steps.map((st) => ({ run: st.lines.join("\n") }));
|
|
953
|
+
const setup = steps.length > 0 ? { setup: steps } : {};
|
|
954
|
+
if (forge === "gitlab") {
|
|
955
|
+
const clouds = cloudsOf(envs, held);
|
|
956
|
+
return {
|
|
957
|
+
name: `watch-${env}`,
|
|
958
|
+
...setup,
|
|
959
|
+
...(Object.keys(login.variables).length > 0 ? { variables: login.variables } : {}),
|
|
960
|
+
...(clouds.length > 0 ? { gitlab: { idTokens: idTokens(clouds, logins) } } : {}),
|
|
961
|
+
};
|
|
962
|
+
}
|
|
963
|
+
const creds = envs.credentials?.[env];
|
|
1038
964
|
// The job's token, for the tracking issue: GitHub grants it issues: write here, Forgejo's can write already.
|
|
1039
965
|
return {
|
|
1040
966
|
name: `watch-${env}`,
|
|
1041
|
-
variables: { ...secrets([...readerVariables(env, dialect, creds), ...WEBHOOK_VARIABLES]), GITHUB_TOKEN: "${{ github.token }}" },
|
|
967
|
+
variables: { ...secrets([...readerVariables(env, envs.dialect, creds), ...WEBHOOK_VARIABLES]), GITHUB_TOKEN: "${{ github.token }}" },
|
|
1042
968
|
...(forge === "github" ? { permissions: { issues: "write", ...(creds?.reader.cloud ? { "id-token": "write" } : {}) } } : {}),
|
|
969
|
+
...setup,
|
|
1043
970
|
};
|
|
1044
971
|
}
|
|
1045
|
-
/** A GitHub workflow's top-level `permissions:` with `id-token: write` added, when a job holds a cloud-minted role. */
|
|
1046
|
-
function withIdToken(forge, top, clouds) {
|
|
1047
|
-
if (forge !== "github" || clouds.length === 0)
|
|
1048
|
-
return top;
|
|
1049
|
-
const added = top.replace(/^permissions:\n((?: {2}\S.*\n)*)/m, (_m, scopes) => `permissions:\n${scopes} id-token: write\n`);
|
|
1050
|
-
if (added === top)
|
|
1051
|
-
throw new Error("github: the apply waves have no top-level permissions to add id-token: write to");
|
|
1052
|
-
return added;
|
|
1053
|
-
}
|
|
1054
|
-
/**
|
|
1055
|
-
* A GitHub or Forgejo watch file with the cloud login (#132) before the
|
|
1056
|
-
* job's chant run, when its reader is cloud-minted and logs in. Parsed and
|
|
1057
|
-
* emitted again, checked against chant's text first.
|
|
1058
|
-
*/
|
|
1059
|
-
function withWatchLogin(forge, yaml, env, envs) {
|
|
1060
|
-
const login = rawLogin(forge, loginsOf(envs, [[env, "reader"]]));
|
|
1061
|
-
if (login.length === 0)
|
|
1062
|
-
return yaml;
|
|
1063
|
-
const at = yaml.indexOf("\njobs:\n");
|
|
1064
|
-
if (at < 0)
|
|
1065
|
-
throw new Error(`${forge}: the watch for ${env} has no jobs`);
|
|
1066
|
-
const doc = parseYAMLDocument(yaml);
|
|
1067
|
-
const top = yaml.slice(0, at + 1);
|
|
1068
|
-
const emitJobs = () => withPinComments(yaml, `${emitYAMLEntry("jobs", doc.jobs)}\n`);
|
|
1069
|
-
if (`${top}${emitJobs()}` !== yaml)
|
|
1070
|
-
throw new Error(`${forge}: the watch for ${env} does not emit back to chant's text`);
|
|
1071
|
-
const job = doc.jobs[`watch-${env}`];
|
|
1072
|
-
if (!job)
|
|
1073
|
-
throw new Error(`${forge}: the watch for ${env} has no watch-${env} job`);
|
|
1074
|
-
insertSteps(job.steps, login, runsChant, `${forge}: watch-${env}`);
|
|
1075
|
-
return `${top}${emitJobs()}`;
|
|
1076
|
-
}
|
|
1077
|
-
/** The GitLab watch file with `id_tokens` (and the cloud login, #132) on each watch job whose reader is cloud-minted. Parsed and emitted again, checked against chant's text first. */
|
|
1078
|
-
function withWatchIdTokens(yaml, envs) {
|
|
1079
|
-
const clouds = (env) => cloudsOf(envs, [[env, "reader"]]);
|
|
1080
|
-
if (!envs.names.some((env) => clouds(env).length > 0))
|
|
1081
|
-
return yaml;
|
|
1082
|
-
const body = yaml.search(/^[^#\n]/m);
|
|
1083
|
-
const header = yaml.slice(0, body);
|
|
1084
|
-
const doc = parseYAMLDocument(yaml);
|
|
1085
|
-
const emit = (d) => withPinComments(yaml, `${header}${Object.entries(d).map(([k, v]) => emitYAMLEntry(k, v)).join("\n\n")}\n`);
|
|
1086
|
-
if (emit(doc) !== yaml)
|
|
1087
|
-
throw new Error("gitlab: the watch file does not emit back to chant's text");
|
|
1088
|
-
for (const env of envs.names) {
|
|
1089
|
-
const job = doc[`watch-${env}`];
|
|
1090
|
-
if (clouds(env).length === 0 || job === undefined)
|
|
1091
|
-
continue;
|
|
1092
|
-
const logins = loginsOf(envs, [[env, "reader"]]);
|
|
1093
|
-
const login = gitlabLogin(logins);
|
|
1094
|
-
insertLines(job.script, login.lines, (l) => l.startsWith("chant run "), `gitlab: watch-${env}`);
|
|
1095
|
-
if (Object.keys(login.variables).length > 0)
|
|
1096
|
-
job.variables = { ...job.variables, ...login.variables };
|
|
1097
|
-
Object.assign(job, { id_tokens: idTokens(clouds(env), logins) });
|
|
1098
|
-
}
|
|
1099
|
-
return emit(doc);
|
|
1100
|
-
}
|
|
1101
972
|
/**
|
|
1102
|
-
* The drift watches, through chant's
|
|
1103
|
-
* `yodel chant run watch-<env>` where chant's runs `chant run watch-<env
|
|
1104
|
-
* so the Op's statuses and notices say
|
|
973
|
+
* The drift watches, through chant's generateOpsPipeline: each job runs
|
|
974
|
+
* `yodel chant run watch-<env>` where chant's runs `chant run watch-<env>`
|
|
975
|
+
* (the generator's `runCommand`), so the Op's statuses and notices say
|
|
976
|
+
* yodel (#289).
|
|
1105
977
|
*
|
|
1106
978
|
* The job stays chant's generateOpsPipeline and its last line stays `yodel
|
|
1107
979
|
* drift <env> --issue`, which plans the build for ClickHouse grant drift
|
|
@@ -1110,16 +982,17 @@ function withWatchIdTokens(yaml, envs) {
|
|
|
1110
982
|
* sql lexicon's live diff, the watch moves onto chant's drift job with
|
|
1111
983
|
* `stages.drift` and the workaround goes.
|
|
1112
984
|
*/
|
|
1113
|
-
export async function watchPipelines(dir, envs_
|
|
1114
|
-
const {
|
|
985
|
+
export async function watchPipelines(dir, envs_) {
|
|
986
|
+
const { names: envs } = envs_;
|
|
1115
987
|
const tc = toolchainOf(envs_);
|
|
1116
988
|
const { generateOpsPipeline } = (await import("@intentius/chant/op"));
|
|
1117
989
|
const out = [];
|
|
1118
990
|
const emit = async (forge, specs, extraScript) => {
|
|
1119
991
|
const result = await generateOpsPipeline(specs, forge, {
|
|
1120
992
|
image: tc.image,
|
|
1121
|
-
beforeScript: [...installLines(tc, forge),
|
|
993
|
+
beforeScript: [...installLines(tc, forge), fetchApprovals(tc), NO_PUSH],
|
|
1122
994
|
extraScript,
|
|
995
|
+
runCommand: [...YODEL_WORDS.bin.split(" "), "run", "{name}"],
|
|
1123
996
|
...(forge === "gitlab" ? { opsStage: "ops", opsFileName: GITLAB_WATCH_FILE } : {}),
|
|
1124
997
|
}, dir);
|
|
1125
998
|
if (!result.success || !result.files)
|
|
@@ -1127,18 +1000,62 @@ export async function watchPipelines(dir, envs_, product) {
|
|
|
1127
1000
|
for (const file of result.files) {
|
|
1128
1001
|
const at = forge === "gitlab" ? GITLAB_WATCH_FILE : `${forge === "github" ? ".github" : ".forgejo"}/workflows/${file.name}`;
|
|
1129
1002
|
const yaml = file.yaml.endsWith("\n") ? file.yaml : `${file.yaml}\n`;
|
|
1130
|
-
|
|
1131
|
-
const env = file.name.replace(/^watch-|\.yml$/g, "");
|
|
1132
|
-
out.push({ path: at, content: `${HEADER.join("\n")}\n${withBin(product, forge === "gitlab" ? withWatchIdTokens(yaml, envs_) : withWatchLogin(forge, yaml, env, envs_))}` });
|
|
1003
|
+
out.push({ path: at, content: `${HEADER.join("\n")}\n${yaml}` });
|
|
1133
1004
|
}
|
|
1134
1005
|
};
|
|
1135
1006
|
// `chant run` exits 0 when a watch finds drift; the last line, yodel drift, fails the job then.
|
|
1136
1007
|
for (const forge of ["github", "forgejo"]) {
|
|
1137
1008
|
for (const env of envs)
|
|
1138
|
-
await emit(forge, [watchSpec(env,
|
|
1009
|
+
await emit(forge, [watchSpec(env, forge, envs_)], [watchDrift(env)]);
|
|
1139
1010
|
}
|
|
1140
1011
|
// GitLab: one file for every watch, so the last line takes the environment from the job's own name.
|
|
1141
|
-
await emit("gitlab", envs.map((env) => watchSpec(env,
|
|
1012
|
+
await emit("gitlab", envs.map((env) => watchSpec(env, "gitlab", envs_)), [watchDrift('"${CI_JOB_NAME#watch-}"')]);
|
|
1013
|
+
return out;
|
|
1014
|
+
}
|
|
1015
|
+
/** Whether `/yodel new` gets chant's commit job (#312): `ci.respond.new` is not `off`. */
|
|
1016
|
+
export const respondsNew = (envs) => envs.checkOnly !== true && (envs.respondNew ?? "comment") !== "off";
|
|
1017
|
+
/** The environments whose reader's password a cloud token source mints, which chant's commit job gets no identity for yet. */
|
|
1018
|
+
const cloudReaders = (envs) => envs.names.filter((env) => envs.credentials?.[env]?.reader.cloud !== undefined);
|
|
1019
|
+
/**
|
|
1020
|
+
* What answers `/yodel rebase` (#313): `ci.respond.rebase`, by default
|
|
1021
|
+
* `comment`. The rebase reads every environment's history with its reader,
|
|
1022
|
+
* and chant's commit job takes no cloud identity yet, so with a
|
|
1023
|
+
* cloud-minted reader the default is `off`, and asking for it is refused.
|
|
1024
|
+
*/
|
|
1025
|
+
export function rebaseRespond(envs) {
|
|
1026
|
+
if (envs.checkOnly === true)
|
|
1027
|
+
return "off";
|
|
1028
|
+
const cloud = cloudReaders(envs);
|
|
1029
|
+
if (cloud.length === 0)
|
|
1030
|
+
return envs.respondRebase ?? "comment";
|
|
1031
|
+
if (envs.respondRebase === undefined || envs.respondRebase === "off")
|
|
1032
|
+
return "off";
|
|
1033
|
+
throw new Error(`yodel.config.ts ci.respond.rebase: ${envs.respondRebase} reads each environment's history in chant's commit job, and ${cloud.join(", ")}'s reader mints its password from a cloud identity, which that job does not get; set ci.respond.rebase: "off"`);
|
|
1034
|
+
}
|
|
1035
|
+
/**
|
|
1036
|
+
* `/yodel new` and `/yodel rebase`, chant's commit job (#312, #313,
|
|
1037
|
+
* ./commit.ts): a workflow per job on GitHub and Forgejo, and a file per
|
|
1038
|
+
* word GitLab's pipeline includes.
|
|
1039
|
+
*/
|
|
1040
|
+
export async function commitPipelines(dir, envs_) {
|
|
1041
|
+
const respondRebase = rebaseRespond(envs_);
|
|
1042
|
+
if (!respondsNew(envs_) && respondRebase === "off")
|
|
1043
|
+
return [];
|
|
1044
|
+
const tc = toolchainOf(envs_);
|
|
1045
|
+
// Both words read every applied environment's history with its reader. A cloud-minted reader gets no identity there, so then /yodel new reads none and /yodel rebase is off.
|
|
1046
|
+
const readers = cloudReaders(envs_).length > 0 ? undefined : { envs: envs_.names, variables: envs_.names.flatMap((env) => readerVariables(env, envs_.dialect, envs_.credentials?.[env])) };
|
|
1047
|
+
const r = { dir: resolve(dir), respond: respondsNew(envs_) ? (envs_.respondNew ?? "comment") : "off", respondRebase, ...(readers ? { readers } : {}), image: tc.image, yodelImage: tc.yodelImage };
|
|
1048
|
+
const out = [];
|
|
1049
|
+
for (const [forge, word] of FORGES.flatMap((f) => ["new", "rebase"].map((w) => [f, w]))) {
|
|
1050
|
+
for (const file of await commitFiles(r, forge, word)) {
|
|
1051
|
+
const at = forge === "gitlab" ? GITLAB_COMMIT_FILES[word] : `${forge === "github" ? ".github" : ".forgejo"}/workflows/${file.name}`;
|
|
1052
|
+
const yaml = file.yaml.endsWith("\n") ? file.yaml : `${file.yaml}\n`;
|
|
1053
|
+
const token = forge === "gitlab"
|
|
1054
|
+
? [`# The push job reads ${COMMIT_TOKEN_ENV}: a project access token with write_repository and api,`, `# as a protected, masked CI/CD variable (docs/workflows.md, /yodel ${word}).`]
|
|
1055
|
+
: [`# The push job reads the ${COMMIT_TOKEN_ENV} secret: a token of a person or an app that can push`, `# to the repository; a push with the job's own token would start no pipeline (docs/workflows.md, /yodel ${word}).`];
|
|
1056
|
+
out.push({ path: at, content: `${HEADER.join("\n")}\n${token.join("\n")}\n${yaml}` });
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1142
1059
|
return out;
|
|
1143
1060
|
}
|
|
1144
1061
|
// ── The project ─────────────────────────────────────────────────────────────
|
|
@@ -1204,6 +1121,8 @@ export async function environments(dir) {
|
|
|
1204
1121
|
...(ci?.apply === false ? { apply: false } : {}),
|
|
1205
1122
|
...(check ? { check: true } : {}),
|
|
1206
1123
|
...(ci?.tools && ci.tools.length > 0 ? { tools: ci.tools } : {}),
|
|
1124
|
+
...(ci?.respond?.new ? { respondNew: ci.respond.new } : {}),
|
|
1125
|
+
...(ci?.respond?.rebase ? { respondRebase: ci.respond.rebase } : {}),
|
|
1207
1126
|
};
|
|
1208
1127
|
}
|
|
1209
1128
|
/** The roles of each environment whose profile is made with credentials(). */
|
|
@@ -1262,8 +1181,7 @@ export function review(envs) {
|
|
|
1262
1181
|
}
|
|
1263
1182
|
/**
|
|
1264
1183
|
* The workspace declaration with yodel's check presets in `ci.hooks.check`
|
|
1265
|
-
* (#294), or undefined when the
|
|
1266
|
-
* project has no declaration. The hooks are one list for every forge: the
|
|
1184
|
+
* (#294), or undefined when the project has no declaration. The hooks are one list for every forge: the
|
|
1267
1185
|
* lint and check presets print with `--format forge`, so a GitHub or
|
|
1268
1186
|
* Forgejo job annotates the files and a GitLab job writes the code quality
|
|
1269
1187
|
* report its artifact keeps.
|
|
@@ -1271,8 +1189,6 @@ export function review(envs) {
|
|
|
1271
1189
|
export async function checkHooksDeclaration(dir, envs_, declaration) {
|
|
1272
1190
|
const chant = await chantCheckHooks();
|
|
1273
1191
|
const file = declarationFile(dir);
|
|
1274
|
-
if (!chant)
|
|
1275
|
-
return undefined;
|
|
1276
1192
|
if (file)
|
|
1277
1193
|
return { path: file, content: declarationWithHooks(chant, dir, file, checkHooksOf(envs_)) };
|
|
1278
1194
|
// A declaration `yodel upgrade` adds, rendered before it is written (#298).
|
|
@@ -1290,12 +1206,9 @@ export function forgeOf(path) {
|
|
|
1290
1206
|
return "gitlab";
|
|
1291
1207
|
return undefined;
|
|
1292
1208
|
}
|
|
1293
|
-
/** chant's product pipeline (#289), which every pipeline renders through
|
|
1209
|
+
/** chant's product pipeline (#289), which every pipeline renders through. */
|
|
1294
1210
|
export async function requireProduct() {
|
|
1295
|
-
|
|
1296
|
-
if (!product)
|
|
1297
|
-
throw new Error(`the installed chant has no product pipeline (@intentius/chant/ci-product), and yodel ci renders every pipeline on it from chant ${CHANT_CI_FLOOR}. Install it, then run yodel upgrade:\n ${installLine()}`);
|
|
1298
|
-
return product;
|
|
1211
|
+
return chantProduct();
|
|
1299
1212
|
}
|
|
1300
1213
|
/**
|
|
1301
1214
|
* Every file `yodel ci` writes, through chant's product pipeline (#289).
|
|
@@ -1304,9 +1217,12 @@ export async function requireProduct() {
|
|
|
1304
1217
|
* check hooks into when the project has none on disk: the one `yodel
|
|
1305
1218
|
* upgrade` adds (#298). A project with no declaration, in its directory or
|
|
1306
1219
|
* given here, keeps the lint job's own steps ({@link checkHooksDeclaration}).
|
|
1220
|
+
* `options.respondNew` and `options.respondRebase` stand in for yodel.config.ts `ci.respond`.
|
|
1307
1221
|
*/
|
|
1308
1222
|
export async function render(dir, options = {}) {
|
|
1309
|
-
const
|
|
1223
|
+
const read = await environments(dir);
|
|
1224
|
+
// The starter templates render as the release before #312 did until the release that ships /yodel new renders them (test/template.test.ts).
|
|
1225
|
+
const envs = { ...read, ...(options.respondNew ? { respondNew: options.respondNew } : {}), ...(options.respondRebase ? { respondRebase: options.respondRebase } : {}) };
|
|
1310
1226
|
const forges = envs.forges ?? FORGES;
|
|
1311
1227
|
if (envs.checkOnly)
|
|
1312
1228
|
return review(envs).filter((f) => forges.includes(forgeOf(f.path)));
|
|
@@ -1315,10 +1231,19 @@ export async function render(dir, options = {}) {
|
|
|
1315
1231
|
const hooks = await checkHooksDeclaration(dir, envs, options.declaration);
|
|
1316
1232
|
const reviewed = hooks ? { ...envs, checkHooks: true } : envs;
|
|
1317
1233
|
// ci.apply: false (#175): no waves spec and no apply pipeline on any forge.
|
|
1318
|
-
const files = [
|
|
1319
|
-
|
|
1234
|
+
const files = [
|
|
1235
|
+
...review(reviewed),
|
|
1236
|
+
...(hooks ? [hooks] : []),
|
|
1237
|
+
...(rendersApply(envs) ? await applyPipelines(dir, envs, product) : []),
|
|
1238
|
+
...(await watchPipelines(dir, envs)),
|
|
1239
|
+
...(await commitPipelines(dir, envs)),
|
|
1240
|
+
];
|
|
1241
|
+
// Each pinned `uses:` line with its release beside it, as chant writes them (#319).
|
|
1242
|
+
return files
|
|
1243
|
+
.filter((f) => {
|
|
1320
1244
|
const forge = forgeOf(f.path);
|
|
1321
1245
|
return forge === undefined || forges.includes(forge);
|
|
1322
|
-
})
|
|
1246
|
+
})
|
|
1247
|
+
.map((f) => (f.path.endsWith(".yml") ? { ...f, content: tagComments(f.content) } : f));
|
|
1323
1248
|
}
|
|
1324
1249
|
//# sourceMappingURL=render.js.map
|