@intentius/sql-yodeler 0.12.0 → 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 +215 -285
- 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,43 +172,23 @@ 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. */
|
|
199
|
-
|
|
183
|
+
// chant's sql-migrate plans a wave with the writer taken out (readerEnv), so the job holds its environment's reader too.
|
|
184
|
+
const waveRoles = (waves, env) => [[env, "writer"], [env, "reader"], ...requiredOf(waves, env).map((e) => [e, "reader"])];
|
|
200
185
|
/** The header lines saying what the cloud identity is for, when a job here holds a cloud-minted role. */
|
|
201
186
|
function identityHeader(forge, clouds) {
|
|
202
187
|
if (clouds.length === 0)
|
|
203
188
|
return [];
|
|
204
189
|
const names = clouds.join(", ");
|
|
205
190
|
if (forge === "github")
|
|
206
|
-
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)."];
|
|
207
192
|
if (forge === "gitlab")
|
|
208
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(", ")}).`];
|
|
209
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."];
|
|
@@ -214,10 +199,14 @@ const secret = (name) => `\${{ secrets.${name} }}`;
|
|
|
214
199
|
const secrets = (names) => Object.fromEntries(names.map((n) => [n, secret(n)]));
|
|
215
200
|
/**
|
|
216
201
|
* The approvals: `chant approve` records them on the chant/lifecycle branch,
|
|
217
|
-
* and approvers push it. A CI checkout fetches one ref, so
|
|
218
|
-
*
|
|
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.
|
|
219
208
|
*/
|
|
220
|
-
export const
|
|
209
|
+
export const fetchApprovals = (tc = { yodelImage: false }) => `${tc.yodelImage ? "yodel" : "./node_modules/.bin/yodel"} ci --fetch-ledger`;
|
|
221
210
|
/** The watch reads; it never pushes. See the comment at the top. */
|
|
222
211
|
export const NO_PUSH = "git remote set-url --push origin no-push://the-drift-watch-only-reads";
|
|
223
212
|
/** After the watch's `chant run`: drift against the history, the environment's tracking issue, and exit 2 on drift. */
|
|
@@ -300,13 +289,9 @@ export const reportCommand = (env, tc = { image: IMAGE, yodelImage: false }) =>
|
|
|
300
289
|
};
|
|
301
290
|
/** The artifact a job keeps its report in. */
|
|
302
291
|
export const reportArtifact = (job) => `yodel-report-${job}`;
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
* forgejo lexicon resolves it, from code.forgejo.org/actions) refuses any
|
|
307
|
-
* server but github.com's and fails the step, so the job, on Forgejo.
|
|
308
|
-
*/
|
|
309
|
-
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)}`;
|
|
310
295
|
const uploadArtifact = (forge) => (forge === "forgejo" ? FORGEJO_UPLOAD_ARTIFACT : UPLOAD_ARTIFACT);
|
|
311
296
|
/**
|
|
312
297
|
* The comment at the top of every file, as `#` lines. The files declared here
|
|
@@ -349,7 +334,7 @@ function setupSteps(inContainer, tc, approvals = true) {
|
|
|
349
334
|
steps.push(new Step({ uses: SETUP_NODE, with: { "node-version": 22, cache: "npm" } }));
|
|
350
335
|
steps.push(...installLines(tc, "github").map((run) => new Step({ run })));
|
|
351
336
|
if (approvals)
|
|
352
|
-
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) }));
|
|
353
338
|
return steps;
|
|
354
339
|
}
|
|
355
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. */
|
|
@@ -536,7 +521,7 @@ export function gitlabPipeline(envs_) {
|
|
|
536
521
|
const jobs = new Map();
|
|
537
522
|
jobs.set(".yodel", new GitLabJob({
|
|
538
523
|
image: new Image({ name: tc.image }),
|
|
539
|
-
before_script: [...installLines(tc, "gitlab"),
|
|
524
|
+
before_script: [...installLines(tc, "gitlab"), fetchApprovals(tc)],
|
|
540
525
|
}));
|
|
541
526
|
const svc = REPLAY_SERVICE[dialect];
|
|
542
527
|
const types = envs_.typedColumns === true ? [TYPES_CHECK] : [];
|
|
@@ -622,6 +607,8 @@ export function gitlabPipeline(envs_) {
|
|
|
622
607
|
"# pipeline (yodel.config.ts ci.apply: false): migrations are applied outside CI.",
|
|
623
608
|
]),
|
|
624
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).`] : []),
|
|
625
612
|
"#",
|
|
626
613
|
"# GitLab gives every job the project's CI/CD variables unless a variable is",
|
|
627
614
|
"# protected or scoped to an environment, so the split between reader and",
|
|
@@ -656,7 +643,7 @@ export function gitlabPipeline(envs_) {
|
|
|
656
643
|
const applyStages = apply ? [...plans, ...waveStages(waves), ...(envs_.resume ? [RESUME_STAGE] : [])] : [];
|
|
657
644
|
const pipeline = new GitLabPipeline({
|
|
658
645
|
comment: asComment(header),
|
|
659
|
-
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 }] : [])],
|
|
660
647
|
stages: [...new Set([...jobStages, ...applyStages, "ops"])],
|
|
661
648
|
variables: { GIT_DEPTH: "0" },
|
|
662
649
|
});
|
|
@@ -673,7 +660,7 @@ export const WAVES_SPEC = "yodel-waves.json";
|
|
|
673
660
|
export const APPLY_PIPELINE = "yodel-apply";
|
|
674
661
|
/**
|
|
675
662
|
* The waves spec. What the file holds is the same for every forge; each
|
|
676
|
-
* forge's jobs get their own variables on top (`
|
|
663
|
+
* forge's jobs get their own variables and OIDC tokens on top (`job`).
|
|
677
664
|
*
|
|
678
665
|
* The spec names the sql lexicon's `sql-migrate` capability (#291,
|
|
679
666
|
* chant#4569), and chant plans and applies each run through it, which calls
|
|
@@ -692,7 +679,7 @@ export const APPLY_PIPELINE = "yodel-apply";
|
|
|
692
679
|
* the base commit, as it does run by hand. A step runs once, under the
|
|
693
680
|
* lock, as #107 asks.
|
|
694
681
|
*/
|
|
695
|
-
export function wavesSpec(waves,
|
|
682
|
+
export function wavesSpec(waves, job, resume) {
|
|
696
683
|
const first = waves[0];
|
|
697
684
|
if (!first)
|
|
698
685
|
throw new Error("no waves to render");
|
|
@@ -709,7 +696,7 @@ export function wavesSpec(waves, variables, resume) {
|
|
|
709
696
|
gate: w.gate,
|
|
710
697
|
...(w.shares !== undefined && w.shares > 1 ? { shares: w.shares } : {}),
|
|
711
698
|
...(w.approval !== undefined && w.approval !== "ledger" ? { approval: w.approval } : {}),
|
|
712
|
-
...(
|
|
699
|
+
...(job ? { environment: { name: w.env }, ...job(w.env) } : {}),
|
|
713
700
|
// The steps around apply (#64) as chant's stage hooks, which the apply runs under its lock (#206).
|
|
714
701
|
...hooksOf(w.steps),
|
|
715
702
|
})),
|
|
@@ -723,10 +710,32 @@ function hooksOf(steps) {
|
|
|
723
710
|
const plan = stepHooks(steps)?.before?.plan;
|
|
724
711
|
return plan && plan.length > 0 ? { hooks: { before: { plan } } } : {};
|
|
725
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
|
+
}
|
|
726
730
|
/**
|
|
727
731
|
* The waves spec file and the apply pipeline on each forge. The spec comes
|
|
728
732
|
* from chant's productPipelineJobs under yodel's words (#289), and each job
|
|
729
|
-
* 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.
|
|
730
739
|
*/
|
|
731
740
|
export async function applyPipelines(dir, envs, product) {
|
|
732
741
|
const { dialect, waves } = envs;
|
|
@@ -735,19 +744,51 @@ export async function applyPipelines(dir, envs, product) {
|
|
|
735
744
|
const named = (forge, spec) => productWaves(product, forge, spec);
|
|
736
745
|
const out = [{ path: WAVES_SPEC, content: `${JSON.stringify(named("github", wavesSpec(waves)).spec, null, 2)}\n` }];
|
|
737
746
|
// A GitLab job already has the project's variables, a writer's only under its environment.
|
|
738
|
-
const
|
|
747
|
+
const jobOf = (forge) => (env) => {
|
|
739
748
|
const wave = waves.find((w) => w.env === env);
|
|
740
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"]));
|
|
741
784
|
return {
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
:
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
...(wave?.approval === "pr-review" ? { GITHUB_TOKEN: "${{ github.token }}" } : {}),
|
|
750
|
-
}),
|
|
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 } : {}),
|
|
751
792
|
};
|
|
752
793
|
};
|
|
753
794
|
const header = [
|
|
@@ -760,9 +801,10 @@ export async function applyPipelines(dir, envs, product) {
|
|
|
760
801
|
"# exits 3 with the yodel approve command, so the waves after it do not run.",
|
|
761
802
|
"# yodel approve <env> records the approval and starts the waiting job again",
|
|
762
803
|
"# (or approve, push chant/lifecycle, and run the failed job again). These are",
|
|
763
|
-
"# the only jobs that hold a writer's credentials, one environment's each
|
|
764
|
-
"#
|
|
765
|
-
"#
|
|
804
|
+
"# the only jobs that hold a writer's credentials, one environment's each. Each",
|
|
805
|
+
"# also holds its environment's reader, which the plan runs as, and the reader of",
|
|
806
|
+
"# the environment before (waves[].requires): yodel apply refuses a migration",
|
|
807
|
+
"# that environment's history does not record as applied.",
|
|
766
808
|
];
|
|
767
809
|
const resumeHeader = (forge) => [
|
|
768
810
|
...HEADER,
|
|
@@ -786,11 +828,14 @@ export async function applyPipelines(dir, envs, product) {
|
|
|
786
828
|
"# holds no writer's credentials: it plans with each environment's reader.",
|
|
787
829
|
];
|
|
788
830
|
for (const forge of FORGES) {
|
|
789
|
-
const { spec, jobs } = named(forge, wavesSpec(waves,
|
|
831
|
+
const { spec, jobs } = named(forge, wavesSpec(waves, jobOf(forge), envs.resume));
|
|
790
832
|
const result = await generateOpWavesPipeline(spec, forge, {
|
|
791
833
|
specFile: WAVES_SPEC,
|
|
834
|
+
bin: YODEL_WORDS.bin,
|
|
792
835
|
image: tc.image,
|
|
793
|
-
beforeScript: [...installLines(tc, forge),
|
|
836
|
+
beforeScript: [...installLines(tc, forge), fetchApprovals(tc)],
|
|
837
|
+
waveJobs: waveJobs(forge),
|
|
838
|
+
recordPlans: recordPlans(forge),
|
|
794
839
|
...(forge === "gitlab" ? { opsFileName: GITLAB_APPLY_FILE } : {}),
|
|
795
840
|
}, dir);
|
|
796
841
|
if (!result.success || !result.files)
|
|
@@ -798,140 +843,17 @@ export async function applyPipelines(dir, envs, product) {
|
|
|
798
843
|
for (const file of result.files) {
|
|
799
844
|
const at = forge === "gitlab" ? GITLAB_APPLY_FILE : `${forge === "github" ? ".github" : ".forgejo"}/workflows/${file.name}`;
|
|
800
845
|
const yaml = file.yaml.endsWith("\n") ? file.yaml : `${file.yaml}\n`;
|
|
801
|
-
// The resume workflow (GitHub, Forgejo) has no wave job to report on.
|
|
802
846
|
if (forge !== "gitlab" && file.name === `${APPLY_PIPELINE}-resume.yml`)
|
|
803
|
-
out.push({ path: at, content: withHeader(resumeHeader(forge),
|
|
847
|
+
out.push({ path: at, content: withHeader(resumeHeader(forge), yaml) });
|
|
804
848
|
// The pull request workflow that records the plans a review approves (#48).
|
|
805
849
|
else if (forge !== "gitlab" && file.name === `${APPLY_PIPELINE}-plans.yml`)
|
|
806
|
-
out.push({ path: at, content: withHeader(plansHeader,
|
|
850
|
+
out.push({ path: at, content: withHeader(plansHeader, yaml) });
|
|
807
851
|
else
|
|
808
|
-
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)) });
|
|
809
853
|
}
|
|
810
854
|
}
|
|
811
855
|
return out;
|
|
812
856
|
}
|
|
813
|
-
/**
|
|
814
|
-
* The wave jobs' report: chant's generateOpWavesPipeline takes run lines
|
|
815
|
-
* after the wave's command but no step that runs when it fails (a wave that
|
|
816
|
-
* waits exits 3), and no artifact, so they are added to its output here.
|
|
817
|
-
* The output is parsed and emitted again; the emitter is checked to give
|
|
818
|
-
* back chant's text unchanged before anything is added, so a change in
|
|
819
|
-
* chant's emitter fails here instead of rendering something else.
|
|
820
|
-
*/
|
|
821
|
-
/**
|
|
822
|
-
* chant writes a SHA-pinned action as `uses: owner/action@<sha> # vX.Y.Z`
|
|
823
|
-
* (chant#4403, #4410); the `# vX.Y.Z` is a YAML comment, which parsing drops.
|
|
824
|
-
* Each re-emit below puts every one back on the line that names the same
|
|
825
|
-
* pinned ref, so the re-emitted text still matches chant's and the files keep
|
|
826
|
-
* the release each SHA stands for.
|
|
827
|
-
*/
|
|
828
|
-
export function withPinComments(original, emitted) {
|
|
829
|
-
const comments = new Map();
|
|
830
|
-
for (const [, ref, comment] of original.matchAll(/uses: (\S+@[0-9a-f]{40}) (# [^\n]*)$/gm)) {
|
|
831
|
-
if (ref !== undefined && comment !== undefined)
|
|
832
|
-
comments.set(ref, comment);
|
|
833
|
-
}
|
|
834
|
-
if (comments.size === 0)
|
|
835
|
-
return emitted;
|
|
836
|
-
return emitted.replace(/uses: (\S+@[0-9a-f]{40})$/gm, (line, ref) => {
|
|
837
|
-
const comment = comments.get(ref);
|
|
838
|
-
return comment ? `${line} ${comment}` : line;
|
|
839
|
-
});
|
|
840
|
-
}
|
|
841
|
-
function withWaveReports(forge, yaml, waves, tc, envs = {}) {
|
|
842
|
-
// A wide wave's jobs are wave-<k>-<env>-decide and -share-<i>.
|
|
843
|
-
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;
|
|
844
|
-
const body = yaml.search(/^[^#\n]/m);
|
|
845
|
-
const header = yaml.slice(0, body);
|
|
846
|
-
const doc = parseYAMLDocument(yaml);
|
|
847
|
-
if (forge === "gitlab") {
|
|
848
|
-
const emit = (d) => withPinComments(yaml, `${header}${Object.entries(d).map(([k, v]) => emitYAMLEntry(k, v)).join("\n\n")}\n`);
|
|
849
|
-
if (emit(doc) !== yaml)
|
|
850
|
-
throw new Error("gitlab: the apply waves do not emit back to chant's text");
|
|
851
|
-
// The merge request job that records the plans a review approves (#48): the waves as after the merge.
|
|
852
|
-
const record = doc[`${APPLY_PIPELINE}-record-plans`];
|
|
853
|
-
if (record) {
|
|
854
|
-
record.variables = { ...(record.variables ?? {}), [RECORD_PLANS_ENV]: "1" };
|
|
855
|
-
// It holds readers only, and shows it first, as every merge request job does (#50, #130).
|
|
856
|
-
const waveEnvs = [...new Set(waves.map((w) => w.env))];
|
|
857
|
-
const at = record.script?.findIndex((l) => l.includes("--record-plans")) ?? -1;
|
|
858
|
-
if (!record.script || at < 0)
|
|
859
|
-
throw new Error("gitlab: the record-plans job has no chant run wave --record-plans line");
|
|
860
|
-
record.script.splice(at, 0, writeProbe(waveEnvs));
|
|
861
|
-
const readerRoles = waveEnvs.map((e) => [e, "reader"]);
|
|
862
|
-
const readers = cloudsOf(envs, readerRoles);
|
|
863
|
-
const logins = loginsOf(envs, readerRoles);
|
|
864
|
-
if (readers.length > 0)
|
|
865
|
-
record.id_tokens = idTokens(readers, logins);
|
|
866
|
-
const login = gitlabLogin(logins);
|
|
867
|
-
insertLines(record.script, login.lines, (l) => l === writeProbe(waveEnvs), "gitlab: the record-plans job");
|
|
868
|
-
if (Object.keys(login.variables).length > 0)
|
|
869
|
-
record.variables = { ...record.variables, ...login.variables };
|
|
870
|
-
}
|
|
871
|
-
for (const [name, job] of Object.entries(doc)) {
|
|
872
|
-
const env = envOf(name);
|
|
873
|
-
if (!env || typeof job !== "object" || job === null)
|
|
874
|
-
continue;
|
|
875
|
-
const clouds = cloudsOf(envs, waveRoles(waves, env));
|
|
876
|
-
const logins = loginsOf(envs, waveRoles(waves, env));
|
|
877
|
-
const login = gitlabLogin(logins);
|
|
878
|
-
const w = job;
|
|
879
|
-
insertLines(w.script, login.lines, (l) => l.startsWith("chant run "), `gitlab: ${name}`);
|
|
880
|
-
if (Object.keys(login.variables).length > 0)
|
|
881
|
-
w.variables = { ...w.variables, ...login.variables };
|
|
882
|
-
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) } : {}) });
|
|
883
|
-
}
|
|
884
|
-
return emit(doc);
|
|
885
|
-
}
|
|
886
|
-
const at = yaml.indexOf("\njobs:\n");
|
|
887
|
-
if (at < 0)
|
|
888
|
-
throw new Error(`${forge}: the apply waves have no jobs`);
|
|
889
|
-
const jobs = doc.jobs;
|
|
890
|
-
const top = yaml.slice(0, at + 1);
|
|
891
|
-
const emitJobs = () => withPinComments(yaml, `${emitYAMLEntry("jobs", jobs)}\n`);
|
|
892
|
-
if (`${top}${emitJobs()}` !== yaml)
|
|
893
|
-
throw new Error(`${forge}: the apply waves do not emit back to chant's text`);
|
|
894
|
-
for (const [name, job] of Object.entries(jobs)) {
|
|
895
|
-
const env = envOf(name);
|
|
896
|
-
if (!env)
|
|
897
|
-
continue;
|
|
898
|
-
// The cloud login (#132) before the wave's chant run, when a role it holds is cloud-minted.
|
|
899
|
-
insertSteps(job.steps, rawLogin(forge, loginsOf(envs, waveRoles(waves, env))), runsChant, `${forge}: ${name}`);
|
|
900
|
-
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" } });
|
|
901
|
-
}
|
|
902
|
-
return `${withIdToken(forge, top, cloudsOf(envs, waves.flatMap((w) => waveRoles(waves, w.env))))}${emitJobs()}`;
|
|
903
|
-
}
|
|
904
|
-
/**
|
|
905
|
-
* The plans workflow (GitHub, Forgejo) for a pr-review wave (#48): chant's,
|
|
906
|
-
* with each environment's reader (the waves are planned on every one, and a
|
|
907
|
-
* wave reads the history of the one it requires) and {@link RECORD_PLANS_ENV}
|
|
908
|
-
* on its job. The output is parsed and emitted again, checked first to give
|
|
909
|
-
* back chant's text unchanged.
|
|
910
|
-
*/
|
|
911
|
-
function withPlansReaders(forge, yaml, waves, dialect, creds = {}) {
|
|
912
|
-
const at = yaml.indexOf("\njobs:\n");
|
|
913
|
-
if (at < 0)
|
|
914
|
-
throw new Error(`${forge}: the plans workflow has no jobs`);
|
|
915
|
-
const doc = parseYAMLDocument(yaml);
|
|
916
|
-
const top = yaml.slice(0, at + 1);
|
|
917
|
-
const emitJobs = () => withPinComments(yaml, `${emitYAMLEntry("jobs", doc.jobs)}\n`);
|
|
918
|
-
if (`${top}${emitJobs()}` !== yaml)
|
|
919
|
-
throw new Error(`${forge}: the plans workflow does not emit back to chant's text`);
|
|
920
|
-
const job = doc.jobs["record-plans"];
|
|
921
|
-
if (!job)
|
|
922
|
-
throw new Error(`${forge}: the plans workflow has no record-plans job`);
|
|
923
|
-
const envs = [...new Set(waves.map((w) => w.env))];
|
|
924
|
-
const steps = job.steps;
|
|
925
|
-
// It holds readers only, and shows it before it plans, as every pull request job does (#50, #130).
|
|
926
|
-
const recordAt = steps.findIndex((st) => typeof st.run === "string" && st.run.includes("--record-plans"));
|
|
927
|
-
if (recordAt < 0)
|
|
928
|
-
throw new Error(`${forge}: the record-plans job has no chant run wave --record-plans step`);
|
|
929
|
-
steps.splice(recordAt, 0, ...(forge === "gitlab" ? [] : rawLogin(forge, loginsOf(creds, envs.map((e) => [e, "reader"])))), { name: "This job cannot write", run: writeProbe(envs) });
|
|
930
|
-
delete job.steps;
|
|
931
|
-
Object.assign(job, { env: { [RECORD_PLANS_ENV]: "1", ...secrets(envs.flatMap((e) => readerVariables(e, dialect, creds.credentials?.[e]))) }, steps });
|
|
932
|
-
// A reader whose password is a cloud token (#50) is minted from the job's identity.
|
|
933
|
-
return `${withIdToken(forge, top, cloudsOf(creds, envs.map((e) => [e, "reader"])))}${emitJobs()}`;
|
|
934
|
-
}
|
|
935
857
|
/** yodel.config.ts `waves`, checked against the environments; one wave per environment, gate `always`, without it. */
|
|
936
858
|
export async function readWaves(dir, envs) {
|
|
937
859
|
const declared = (await readConfig(dir))?.waves;
|
|
@@ -984,8 +906,8 @@ export async function withTenants(dir, waves) {
|
|
|
984
906
|
// the names on the ledger since the waves began, so approvals already
|
|
985
907
|
// recorded keep counting), and each job running `yodel chant` where chant's
|
|
986
908
|
// own runs `chant`, so the job writes yodel's statuses and approve lines.
|
|
987
|
-
// The forge lexicons serialize the jobs
|
|
988
|
-
//
|
|
909
|
+
// The forge lexicons serialize the jobs, and yodel's reports, logins and
|
|
910
|
+
// readers go in as generateOpWavesPipeline's inputs (#4791).
|
|
989
911
|
//
|
|
990
912
|
// Still yodel's own: the plan jobs (`yodel plan <env> --comment` with the
|
|
991
913
|
// write probe), which post chant's note (../pr/note.ts, #290); the lint
|
|
@@ -994,7 +916,7 @@ export async function withTenants(dir, waves) {
|
|
|
994
916
|
// ({@link watchPipelines}).
|
|
995
917
|
/** The spec under yodel's words, and the wave jobs chant's product pipeline names for `forge`. */
|
|
996
918
|
export function productWaves(product, forge, spec) {
|
|
997
|
-
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 });
|
|
998
920
|
if (!out.waves)
|
|
999
921
|
throw new Error(`${forge}: chant's product pipeline gave no apply waves`);
|
|
1000
922
|
if (out.waves.specFile !== WAVES_SPEC)
|
|
@@ -1004,13 +926,6 @@ export function productWaves(product, forge, spec) {
|
|
|
1004
926
|
throw new Error(`${forge}: chant's product pipeline names the waves ${named.name}, and the ledger has them as ${APPLY_PIPELINE}`);
|
|
1005
927
|
return { spec: named, jobs: out.waves.jobs };
|
|
1006
928
|
}
|
|
1007
|
-
/** A `chant` command line in a rendered file: a step's `run:` or a GitLab script line, never a comment. */
|
|
1008
|
-
const CHANT_LINE = /^(\s*(?:- )?(?:run: )?)(chant [a-z].*)$/gm;
|
|
1009
|
-
/** `yaml` with yodel's bin in each chant command line (#289), through chant's own productCommand. */
|
|
1010
|
-
export function withBin(product, yaml) {
|
|
1011
|
-
const words = product.assertProductWords(YODEL_WORDS);
|
|
1012
|
-
return yaml.replace(CHANT_LINE, (_m, lead, line) => `${lead}${product.productCommand(words, line)}`);
|
|
1013
|
-
}
|
|
1014
929
|
/** Refuses an apply file whose wave jobs do not run the line chant's product pipeline gives each of them. */
|
|
1015
930
|
function withWaveLines(forge, yaml, jobs) {
|
|
1016
931
|
for (const job of jobs) {
|
|
@@ -1022,81 +937,43 @@ function withWaveLines(forge, yaml, jobs) {
|
|
|
1022
937
|
return yaml;
|
|
1023
938
|
}
|
|
1024
939
|
/**
|
|
1025
|
-
* The watch's spec for one environment: the Op,
|
|
1026
|
-
* reader
|
|
1027
|
-
*
|
|
1028
|
-
*
|
|
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.
|
|
1029
946
|
*/
|
|
1030
|
-
export function watchSpec(env,
|
|
1031
|
-
|
|
1032
|
-
|
|
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];
|
|
1033
964
|
// The job's token, for the tracking issue: GitHub grants it issues: write here, Forgejo's can write already.
|
|
1034
965
|
return {
|
|
1035
966
|
name: `watch-${env}`,
|
|
1036
|
-
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 }}" },
|
|
1037
968
|
...(forge === "github" ? { permissions: { issues: "write", ...(creds?.reader.cloud ? { "id-token": "write" } : {}) } } : {}),
|
|
969
|
+
...setup,
|
|
1038
970
|
};
|
|
1039
971
|
}
|
|
1040
|
-
/** A GitHub workflow's top-level `permissions:` with `id-token: write` added, when a job holds a cloud-minted role. */
|
|
1041
|
-
function withIdToken(forge, top, clouds) {
|
|
1042
|
-
if (forge !== "github" || clouds.length === 0)
|
|
1043
|
-
return top;
|
|
1044
|
-
const added = top.replace(/^permissions:\n((?: {2}\S.*\n)*)/m, (_m, scopes) => `permissions:\n${scopes} id-token: write\n`);
|
|
1045
|
-
if (added === top)
|
|
1046
|
-
throw new Error("github: the apply waves have no top-level permissions to add id-token: write to");
|
|
1047
|
-
return added;
|
|
1048
|
-
}
|
|
1049
|
-
/**
|
|
1050
|
-
* A GitHub or Forgejo watch file with the cloud login (#132) before the
|
|
1051
|
-
* job's chant run, when its reader is cloud-minted and logs in. Parsed and
|
|
1052
|
-
* emitted again, checked against chant's text first.
|
|
1053
|
-
*/
|
|
1054
|
-
function withWatchLogin(forge, yaml, env, envs) {
|
|
1055
|
-
const login = rawLogin(forge, loginsOf(envs, [[env, "reader"]]));
|
|
1056
|
-
if (login.length === 0)
|
|
1057
|
-
return yaml;
|
|
1058
|
-
const at = yaml.indexOf("\njobs:\n");
|
|
1059
|
-
if (at < 0)
|
|
1060
|
-
throw new Error(`${forge}: the watch for ${env} has no jobs`);
|
|
1061
|
-
const doc = parseYAMLDocument(yaml);
|
|
1062
|
-
const top = yaml.slice(0, at + 1);
|
|
1063
|
-
const emitJobs = () => withPinComments(yaml, `${emitYAMLEntry("jobs", doc.jobs)}\n`);
|
|
1064
|
-
if (`${top}${emitJobs()}` !== yaml)
|
|
1065
|
-
throw new Error(`${forge}: the watch for ${env} does not emit back to chant's text`);
|
|
1066
|
-
const job = doc.jobs[`watch-${env}`];
|
|
1067
|
-
if (!job)
|
|
1068
|
-
throw new Error(`${forge}: the watch for ${env} has no watch-${env} job`);
|
|
1069
|
-
insertSteps(job.steps, login, runsChant, `${forge}: watch-${env}`);
|
|
1070
|
-
return `${top}${emitJobs()}`;
|
|
1071
|
-
}
|
|
1072
|
-
/** 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. */
|
|
1073
|
-
function withWatchIdTokens(yaml, envs) {
|
|
1074
|
-
const clouds = (env) => cloudsOf(envs, [[env, "reader"]]);
|
|
1075
|
-
if (!envs.names.some((env) => clouds(env).length > 0))
|
|
1076
|
-
return yaml;
|
|
1077
|
-
const body = yaml.search(/^[^#\n]/m);
|
|
1078
|
-
const header = yaml.slice(0, body);
|
|
1079
|
-
const doc = parseYAMLDocument(yaml);
|
|
1080
|
-
const emit = (d) => withPinComments(yaml, `${header}${Object.entries(d).map(([k, v]) => emitYAMLEntry(k, v)).join("\n\n")}\n`);
|
|
1081
|
-
if (emit(doc) !== yaml)
|
|
1082
|
-
throw new Error("gitlab: the watch file does not emit back to chant's text");
|
|
1083
|
-
for (const env of envs.names) {
|
|
1084
|
-
const job = doc[`watch-${env}`];
|
|
1085
|
-
if (clouds(env).length === 0 || job === undefined)
|
|
1086
|
-
continue;
|
|
1087
|
-
const logins = loginsOf(envs, [[env, "reader"]]);
|
|
1088
|
-
const login = gitlabLogin(logins);
|
|
1089
|
-
insertLines(job.script, login.lines, (l) => l.startsWith("chant run "), `gitlab: watch-${env}`);
|
|
1090
|
-
if (Object.keys(login.variables).length > 0)
|
|
1091
|
-
job.variables = { ...job.variables, ...login.variables };
|
|
1092
|
-
Object.assign(job, { id_tokens: idTokens(clouds(env), logins) });
|
|
1093
|
-
}
|
|
1094
|
-
return emit(doc);
|
|
1095
|
-
}
|
|
1096
972
|
/**
|
|
1097
|
-
* The drift watches, through chant's
|
|
1098
|
-
* `yodel chant run watch-<env>` where chant's runs `chant run watch-<env
|
|
1099
|
-
* 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).
|
|
1100
977
|
*
|
|
1101
978
|
* The job stays chant's generateOpsPipeline and its last line stays `yodel
|
|
1102
979
|
* drift <env> --issue`, which plans the build for ClickHouse grant drift
|
|
@@ -1105,16 +982,17 @@ function withWatchIdTokens(yaml, envs) {
|
|
|
1105
982
|
* sql lexicon's live diff, the watch moves onto chant's drift job with
|
|
1106
983
|
* `stages.drift` and the workaround goes.
|
|
1107
984
|
*/
|
|
1108
|
-
export async function watchPipelines(dir, envs_
|
|
1109
|
-
const {
|
|
985
|
+
export async function watchPipelines(dir, envs_) {
|
|
986
|
+
const { names: envs } = envs_;
|
|
1110
987
|
const tc = toolchainOf(envs_);
|
|
1111
988
|
const { generateOpsPipeline } = (await import("@intentius/chant/op"));
|
|
1112
989
|
const out = [];
|
|
1113
990
|
const emit = async (forge, specs, extraScript) => {
|
|
1114
991
|
const result = await generateOpsPipeline(specs, forge, {
|
|
1115
992
|
image: tc.image,
|
|
1116
|
-
beforeScript: [...installLines(tc, forge),
|
|
993
|
+
beforeScript: [...installLines(tc, forge), fetchApprovals(tc), NO_PUSH],
|
|
1117
994
|
extraScript,
|
|
995
|
+
runCommand: [...YODEL_WORDS.bin.split(" "), "run", "{name}"],
|
|
1118
996
|
...(forge === "gitlab" ? { opsStage: "ops", opsFileName: GITLAB_WATCH_FILE } : {}),
|
|
1119
997
|
}, dir);
|
|
1120
998
|
if (!result.success || !result.files)
|
|
@@ -1122,18 +1000,62 @@ export async function watchPipelines(dir, envs_, product) {
|
|
|
1122
1000
|
for (const file of result.files) {
|
|
1123
1001
|
const at = forge === "gitlab" ? GITLAB_WATCH_FILE : `${forge === "github" ? ".github" : ".forgejo"}/workflows/${file.name}`;
|
|
1124
1002
|
const yaml = file.yaml.endsWith("\n") ? file.yaml : `${file.yaml}\n`;
|
|
1125
|
-
|
|
1126
|
-
const env = file.name.replace(/^watch-|\.yml$/g, "");
|
|
1127
|
-
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}` });
|
|
1128
1004
|
}
|
|
1129
1005
|
};
|
|
1130
1006
|
// `chant run` exits 0 when a watch finds drift; the last line, yodel drift, fails the job then.
|
|
1131
1007
|
for (const forge of ["github", "forgejo"]) {
|
|
1132
1008
|
for (const env of envs)
|
|
1133
|
-
await emit(forge, [watchSpec(env,
|
|
1009
|
+
await emit(forge, [watchSpec(env, forge, envs_)], [watchDrift(env)]);
|
|
1134
1010
|
}
|
|
1135
1011
|
// GitLab: one file for every watch, so the last line takes the environment from the job's own name.
|
|
1136
|
-
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
|
+
}
|
|
1137
1059
|
return out;
|
|
1138
1060
|
}
|
|
1139
1061
|
// ── The project ─────────────────────────────────────────────────────────────
|
|
@@ -1199,6 +1121,8 @@ export async function environments(dir) {
|
|
|
1199
1121
|
...(ci?.apply === false ? { apply: false } : {}),
|
|
1200
1122
|
...(check ? { check: true } : {}),
|
|
1201
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 } : {}),
|
|
1202
1126
|
};
|
|
1203
1127
|
}
|
|
1204
1128
|
/** The roles of each environment whose profile is made with credentials(). */
|
|
@@ -1257,8 +1181,7 @@ export function review(envs) {
|
|
|
1257
1181
|
}
|
|
1258
1182
|
/**
|
|
1259
1183
|
* The workspace declaration with yodel's check presets in `ci.hooks.check`
|
|
1260
|
-
* (#294), or undefined when the
|
|
1261
|
-
* 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
|
|
1262
1185
|
* lint and check presets print with `--format forge`, so a GitHub or
|
|
1263
1186
|
* Forgejo job annotates the files and a GitLab job writes the code quality
|
|
1264
1187
|
* report its artifact keeps.
|
|
@@ -1266,8 +1189,6 @@ export function review(envs) {
|
|
|
1266
1189
|
export async function checkHooksDeclaration(dir, envs_, declaration) {
|
|
1267
1190
|
const chant = await chantCheckHooks();
|
|
1268
1191
|
const file = declarationFile(dir);
|
|
1269
|
-
if (!chant)
|
|
1270
|
-
return undefined;
|
|
1271
1192
|
if (file)
|
|
1272
1193
|
return { path: file, content: declarationWithHooks(chant, dir, file, checkHooksOf(envs_)) };
|
|
1273
1194
|
// A declaration `yodel upgrade` adds, rendered before it is written (#298).
|
|
@@ -1285,12 +1206,9 @@ export function forgeOf(path) {
|
|
|
1285
1206
|
return "gitlab";
|
|
1286
1207
|
return undefined;
|
|
1287
1208
|
}
|
|
1288
|
-
/** chant's product pipeline (#289), which every pipeline renders through
|
|
1209
|
+
/** chant's product pipeline (#289), which every pipeline renders through. */
|
|
1289
1210
|
export async function requireProduct() {
|
|
1290
|
-
|
|
1291
|
-
if (!product)
|
|
1292
|
-
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()}`);
|
|
1293
|
-
return product;
|
|
1211
|
+
return chantProduct();
|
|
1294
1212
|
}
|
|
1295
1213
|
/**
|
|
1296
1214
|
* Every file `yodel ci` writes, through chant's product pipeline (#289).
|
|
@@ -1299,9 +1217,12 @@ export async function requireProduct() {
|
|
|
1299
1217
|
* check hooks into when the project has none on disk: the one `yodel
|
|
1300
1218
|
* upgrade` adds (#298). A project with no declaration, in its directory or
|
|
1301
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`.
|
|
1302
1221
|
*/
|
|
1303
1222
|
export async function render(dir, options = {}) {
|
|
1304
|
-
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 } : {}) };
|
|
1305
1226
|
const forges = envs.forges ?? FORGES;
|
|
1306
1227
|
if (envs.checkOnly)
|
|
1307
1228
|
return review(envs).filter((f) => forges.includes(forgeOf(f.path)));
|
|
@@ -1310,10 +1231,19 @@ export async function render(dir, options = {}) {
|
|
|
1310
1231
|
const hooks = await checkHooksDeclaration(dir, envs, options.declaration);
|
|
1311
1232
|
const reviewed = hooks ? { ...envs, checkHooks: true } : envs;
|
|
1312
1233
|
// ci.apply: false (#175): no waves spec and no apply pipeline on any forge.
|
|
1313
|
-
const files = [
|
|
1314
|
-
|
|
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) => {
|
|
1315
1244
|
const forge = forgeOf(f.path);
|
|
1316
1245
|
return forge === undefined || forges.includes(forge);
|
|
1317
|
-
})
|
|
1246
|
+
})
|
|
1247
|
+
.map((f) => (f.path.endsWith(".yml") ? { ...f, content: tagComments(f.content) } : f));
|
|
1318
1248
|
}
|
|
1319
1249
|
//# sourceMappingURL=render.js.map
|