Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"grant": "tsx scripts/grant.ts"
},
"dependencies": {
"@intentius/chant": "0.124.116",
"@intentius/chant": "0.124.117",
"@intentius/chant-lexicon-forgejo": "0.124.104",
"@intentius/chant-lexicon-github": "0.124.104",
"@intentius/chant-lexicon-gitlab": "0.124.102",
Expand Down
51 changes: 51 additions & 0 deletions src/chant/pending-approvals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* chant's `pendingApprovals` (chant#5480): what waits for approval in an
* environment, across Op waves, pipeline stages, components and the Op
* ledgers named in `ops`. yodel approve asks it where a `--plan` waits.
*
* It exists from chant {@link PENDING_APPROVALS_FLOOR}. The module is
* imported by a name the compiler does not resolve, so yodel builds against
* an older chant too; {@link loadPendingApprovals} is undefined there, and
* yodel approve searches the gates it lists itself.
*/

/** The first chant with the call: `PENDING_APPROVALS_FLOOR` in `@intentius/chant/lifecycle/pending-approvals`. */
export const PENDING_APPROVALS_FLOOR = "0.124.117";

/** One standing fact, the parts yodel reads of chant's `PendingApproval`. */
export interface PendingApproval {
kind: "wave" | "stage" | "component" | "op";
op: string;
gate: string;
digest?: string;
environment?: string;
releaseDigest?: string;
changeSet?: string;
matched?: "planDigest" | "changeSet" | "releaseDigest";
description?: string;
timestamp: string;
expiresAt: string;
}

/** What chant's `pendingApprovals` takes, the parts yodel passes. */
export interface PendingApprovalsInput {
cwd: string;
env: string;
plan?: string;
ops?: readonly string[];
pipeline?: string | null;
}

/** chant's `pendingApprovals`. */
export type PendingApprovalsFn = (input: PendingApprovalsInput) => Promise<PendingApproval[]>;

/** The installed chant's `pendingApprovals`, or undefined below the floor. */
export async function loadPendingApprovals(): Promise<PendingApprovalsFn | undefined> {
const name = "@intentius/chant/lifecycle/pending-approvals";
try {
const mod = (await import(/* @vite-ignore */ name)) as { PENDING_APPROVALS_FLOOR?: unknown; pendingApprovals?: PendingApprovalsFn };
return "PENDING_APPROVALS_FLOOR" in mod && typeof mod.pendingApprovals === "function" ? mod.pendingApprovals : undefined;
} catch {
return undefined;
}
}
85 changes: 70 additions & 15 deletions src/commands/approve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { planEnvironment, renderPlan } from "../chant/plan.js";
import { findApplyOp } from "../chant/apply.js";
import { cleanupDigest, cleanupGate, describeRetained, findRetained, retainedServer } from "../cleanup/index.js";
import { gatedRequests } from "../chant/gated-request.js";
import { loadPendingApprovals, type PendingApproval, type PendingApprovalsFn } from "../chant/pending-approvals.js";
import { storedPlanText } from "../chant/stored-plan.js";
import { chantConfirms, confirmApproval } from "../chant/confirm.js";
import { baselineGate } from "../init/index.js";
Expand Down Expand Up @@ -133,6 +134,7 @@ Exit codes:
export function chantApproveArgs(op: string, gate: string, digest: string, resume: boolean, signing: Signing = {}): string[] {
return [
"approve", op, gate, "--plan", digest,
...(signing.environment !== undefined ? ["--env", signing.environment] : []),
...(signing.actor !== undefined ? ["--actor", signing.actor] : []),
...(signing.sign === true ? ["--sign", ...(signing.key !== undefined ? [signing.key] : [])] : []),
...(resume ? ["--resume"] : []),
Expand All @@ -144,6 +146,8 @@ export interface Signing {
sign?: boolean;
key?: string;
actor?: string;
/** The fact's own environment, for a pipeline stage's or a component's gate (chant approve --env). */
environment?: string;
}

/** Whether the project's git repository has a remote: without one there is nothing to push to and no CI job to resume. */
Expand Down Expand Up @@ -274,6 +278,11 @@ export const approve: Command = {
io.err(`yodel approve: ${report.refusal.message}\nNothing was approved.\n`);
return report.refusal.exitCode;
}
if (expected !== undefined && (report.status.pending.length === 0 || !report.digest) && (project.yodel?.config.waves ?? []).some((w) => w.env === env.name)) {
// A stage gate of the promotion pipeline can wait with nothing left to migrate here.
const done = await other();
if (done !== undefined) return done;
}
if (report.status.pending.length === 0 || !report.digest) {
io.out(`${env.name} is up to date: there is nothing to approve.\n`);
return 0;
Expand Down Expand Up @@ -416,6 +425,8 @@ export interface OtherContext {
signing: (mode: string | undefined) => Signing;
/** Runs chant (tests pass their own). */
run?: typeof runChant;
/** chant's `pendingApprovals`; null stands for a chant below its floor. Default: the installed chant's, when it has one. */
pendingApprovals?: PendingApprovalsFn | null;
}

/** A gate a run stopped at and recorded on chant/lifecycle, waiting for a person. */
Expand All @@ -426,6 +437,8 @@ export interface StandingGate {
/** When the run reached the gate. */
at: string;
description?: string;
/** The fact's own environment, on a pipeline stage's or a component's gate. */
environment?: string;
}

/** A gate yodel looks for a waiting plan at, with the approval mode that counts there. */
Expand Down Expand Up @@ -453,8 +466,16 @@ export function standingGate(
return { op, gate, digest: p.planDigest, at: p.timestamp, ...(p.description ? { description: p.description } : {}) };
}

/** A waiting plan found, the approval mode that counts at its gate, and how to read what waits there again. */
interface Found {
standing: StandingGate;
mode?: string | undefined;
/** The digest now waiting at the same gate, for a fact chant's `pendingApprovals` found. */
again?: () => Promise<string | undefined>;
}

/** The plan waiting at one of `gates` in the project's ledger: the one for `digest`, else (no digest) the only one. */
async function findStanding(dir: string, gates: readonly GateRef[], digest: string | undefined): Promise<{ standing: StandingGate; mode?: string | undefined } | undefined> {
async function findStanding(dir: string, gates: readonly GateRef[], digest: string | undefined): Promise<Found | undefined> {
const ledger = await gitLedger(dir);
for (const g of gates) {
const standing = standingGate(await ledger.read(g.op), g.op, g.gate);
Expand All @@ -463,8 +484,19 @@ async function findStanding(dir: string, gates: readonly GateRef[], digest: stri
return undefined;
}

/** The facts that differ in op, gate or digest: one fact per gate. */
function distinct(facts: readonly PendingApproval[]): PendingApproval[] {
const seen = new Set<string>();
return facts.filter((f) => {
const key = `${f.op}\0${f.gate}\0${f.digest ?? ""}`;
if (seen.has(key)) return false;
seen.add(key);
return true;
});
}

/** Shows the waiting plan, asks, reads the ledger again and records the approval. */
async function approveStanding(c: OtherContext, found: { standing: StandingGate; mode?: string | undefined }, gates: readonly GateRef[], next: string): Promise<number> {
async function approveStanding(c: OtherContext, found: Found, gates: readonly GateRef[], next: string): Promise<number> {
const { io, env } = c;
const s = found.standing;
io.out(`${s.op} is waiting at gate "${s.gate}" for approval of ${s.digest}, since ${s.at}${s.description ? ` (${s.description})` : ""}.\n`);
Expand All @@ -476,12 +508,13 @@ async function approveStanding(c: OtherContext, found: { standing: StandingGate;
const declined = await confirmApproval({ io, ask: c.ask, dir: c.project.dir, op: s.op, gate: s.gate, digest: s.digest, target: env.name, question: `\nApprove ${s.digest} for ${env.name}? Type ${env.name} to approve: ` });
if (declined !== undefined) return declined;
// What waits is read again: what is approved is what the person saw.
const again = await findStanding(c.project.dir, gates.filter((g) => g.op === s.op && g.gate === s.gate), undefined);
if (again?.standing.digest !== s.digest) {
io.err(`Not approved: the plan waiting at ${s.gate} moved while you were asked (now ${again?.standing.digest ?? "nothing waits"}); run yodel approve ${env.name} again.\n`);
const now = found.again ? await found.again() : (await findStanding(c.project.dir, gates.filter((g) => g.op === s.op && g.gate === s.gate), undefined))?.standing.digest;
if (now !== s.digest) {
io.err(`Not approved: the plan waiting at ${s.gate} moved while you were asked (now ${now ?? "nothing waits"}); run yodel approve ${env.name} again.\n`);
return EXIT_MOVED;
}
return record(io, c.project.dir, s.op, s.gate, s.digest, await c.resuming(c.project.dir), next, "", c.signing(found.mode), c.run);
const signing = c.signing(found.mode);
return record(io, c.project.dir, s.op, s.gate, s.digest, await c.resuming(c.project.dir), next, "", s.environment !== undefined ? { ...signing, environment: s.environment } : signing, c.run);
}

/**
Expand All @@ -493,25 +526,47 @@ async function approveStanding(c: OtherContext, found: { standing: StandingGate;
export async function approveOther(c: OtherContext, opName?: string, venv?: VersionedEnv): Promise<number | undefined> {
const { project, env, expected } = c;
if (expected === undefined) return undefined;
const gates: GateRef[] = [];
let migrate: MigrateOpRef | undefined;
try {
migrate = await findMigrateOp(project, env, opName);
} catch {
// No migrations Op (a waves project may have none): nothing waits at its gate.
}
const mode = migrate ? (await migrationsApprovalAtBase(project, env.name)).mode : undefined;
if (migrate) gates.push({ op: migrate.name, gate: migrate.gate, mode });
// chant's gated requests (#292, chant#4551): the drop yodel cleanup asks for and the record yodel init --baseline asks for wait as pending facts at their own gates.
if (migrate && (await gatedRequests())) gates.push({ op: migrate.name, gate: cleanupGate(migrate.gate), mode }, { op: migrate.name, gate: baselineGate(migrate.gate), mode });
const base = baseEnvironment(env.name);
const k = (project.yodel?.config.waves ?? []).findIndex((w) => w.env === base) + 1;
if (k > 0) {
const { waveGateName } = await import("@intentius/chant/gated-waves");
const { at } = await gateAtBase(project, base);
gates.push({ op: WAVES_PIPELINE, gate: waveGateName(WAVES_PIPELINE, k), mode: waveApproval(at.config, base) });
const waveMode = async (): Promise<string | undefined> => (k > 0 ? waveApproval((await gateAtBase(project, base)).at.config, base) : undefined);
const pending = c.pendingApprovals === undefined ? await loadPendingApprovals() : (c.pendingApprovals ?? undefined);
let found: Found | undefined;
const gates: GateRef[] = [];
if (pending) {
// chant says where env waits (chant#5480): a wave, a pipeline stage, a component or the migrations Op's gate, by the digest it binds.
const ask = { cwd: project.dir, env: base, plan: expected, ops: [...(migrate ? [migrate.name] : []), WAVES_PIPELINE], pipeline: WAVES_PIPELINE };
const facts = distinct(await pending(ask));
if (facts.length > 1) {
c.io.err(`yodel approve: ${expected} waits at ${String(facts.length)} gates of ${env.name}; nothing was approved. Approve one with chant approve:\n${facts.map((f) => ` chant approve ${f.op} ${f.gate} --plan ${f.digest ?? expected}${f.environment !== undefined ? ` --env ${f.environment}` : ""}${f.description ? ` (${f.description})` : ""}\n`).join("")}`);
return EXIT_FAILED;
}
const fact = facts[0];
if (fact) {
const digest = fact.digest ?? expected;
const gateMode = fact.kind === "wave" ? await waveMode() : fact.kind === "op" ? mode : undefined;
found = {
standing: { op: fact.op, gate: fact.gate, digest, at: fact.timestamp, ...(fact.description ? { description: fact.description } : {}), ...(fact.environment !== undefined ? { environment: fact.environment } : {}) },
mode: gateMode,
again: async () => distinct(await pending(ask)).find((f) => f.op === fact.op && f.gate === fact.gate)?.digest,
};
}
} else {
if (migrate) gates.push({ op: migrate.name, gate: migrate.gate, mode });
// chant's gated requests (#292, chant#4551): the drop yodel cleanup asks for and the record yodel init --baseline asks for wait as pending facts at their own gates.
if (migrate && (await gatedRequests())) gates.push({ op: migrate.name, gate: cleanupGate(migrate.gate), mode }, { op: migrate.name, gate: baselineGate(migrate.gate), mode });
if (k > 0) {
const { waveGateName } = await import("@intentius/chant/gated-waves");
gates.push({ op: WAVES_PIPELINE, gate: waveGateName(WAVES_PIPELINE, k), mode: await waveMode() });
}
found = await findStanding(project.dir, gates, expected);
}
const found = await findStanding(project.dir, gates, expected);
if (found) return approveStanding(c, found, gates, `Run the yodel command that stopped at ${found.standing.gate} again (push chant/lifecycle first if chant did not).`);
// The drop yodel cleanup asks for: nothing records it waiting, so its digest is worked out again here.
if (migrate && venv && !env.tenant) {
Expand Down
91 changes: 91 additions & 0 deletions test/approve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { GateResolutionRecord, PendingGateRecord } from "@intentius/chant/l
import { gitGateLedgerPort } from "@intentius/chant/op/gate";
import { approveDeclarative, approveOther, chantApproveArgs, standingGate, type OtherContext } from "../src/commands/approve.js";
import type { Io } from "../src/command.js";
import { loadPendingApprovals } from "../src/chant/pending-approvals.js";
import { approveCommand } from "../src/approval-mode/index.js";
import { findApplyOp } from "../src/chant/apply.js";
import { findMigrateOp } from "../src/versioned/index.js";
Expand Down Expand Up @@ -251,6 +252,96 @@ describe("yodel approve <env> --plan for a plan the migrations Op stopped for (#
});
});

const withCall = (await loadPendingApprovals()) !== undefined;

describe("yodel approve <env> --plan asks chant where the environment waits (#411)", () => {
// The first four need chant's call (0.124.117 and later); the fallback case runs on any chant.
const release = `jcs1-sha256:${"c".repeat(64)}`;
const waveDigest = `jcs1-sha256:${"d".repeat(64)}`;

async function setup(facts: Array<Partial<PendingGateRecord> & { op: string; gate: string }>) {
const { dir, dispose } = projectFrom("lint-clickhouse");
const project = await loadProject(dir);
const env = resolveEnvironment(project, "e2e");
const now = Date.now();
for (const f of facts) {
await gitGateLedgerPort({ cwd: dir }).appendPending({ timestamp: new Date(now).toISOString(), expiresAt: new Date(now + 3_600_000).toISOString(), ...f });
}
const ran: string[][] = [];
let out = "";
let err = "";
const c: OtherContext = {
project, env, expected: release, ask: () => Promise.resolve("e2e"), io: { out: (t) => (out += t), err: (t) => (err += t) },
resuming: () => Promise.resolve(false), signing: () => ({}),
run: (args) => {
ran.push([...args]);
return Promise.resolve({ code: 0, stdout: "", stderr: "" });
},
};
return { c, ran, dispose, text: () => ({ out, err }) };
}

it.skipIf(!withCall)("approves the pipeline stage's gate on the release digest, with the stage's environment", async () => {
const t = await setup([{ op: "yodel-apply", gate: "e2e", environment: "e2e", planDigest: release, releaseDigest: release }]);
try {
expect(await approveOther(t.c)).toBe(0);
expect(t.ran).toEqual([["approve", "yodel-apply", "e2e", "--plan", release, "--env", "e2e"]]);
expect(t.text().out).toContain(`yodel-apply is waiting at gate "e2e" for approval of ${release}`);
} finally {
t.dispose();
}
});

it.skipIf(!withCall)("approves a wave's gate on the wave's digest", async () => {
const t = await setup([{ op: "yodel-apply", gate: "yodel-apply-wave-1", planDigest: waveDigest }]);
try {
expect(await approveOther({ ...t.c, expected: waveDigest })).toBe(0);
expect(t.ran).toEqual([["approve", "yodel-apply", "yodel-apply-wave-1", "--plan", waveDigest]]);
} finally {
t.dispose();
}
});

it.skipIf(!withCall)("approves nothing for a digest no gate of the environment waits on", async () => {
const t = await setup([{ op: "yodel-apply", gate: "e2e", environment: "e2e", planDigest: release }, { op: "yodel-apply", gate: "other", environment: "prod", planDigest: `jcs1-sha256:${"e".repeat(64)}` }]);
try {
expect(await approveOther({ ...t.c, expected: `jcs1-sha256:${"f".repeat(64)}` })).toBeUndefined();
expect(t.ran).toEqual([]);
} finally {
t.dispose();
}
});

it.skipIf(!withCall)("refuses with a list when one digest waits at several gates", async () => {
const t = await setup([
{ op: "yodel-apply", gate: "e2e", environment: "e2e", planDigest: release },
{ op: "yodel-apply", gate: "e2e-soak", environment: "e2e", planDigest: release },
]);
try {
expect(await approveOther(t.c)).toBe(1);
expect(t.ran).toEqual([]);
expect(t.text().err).toContain("waits at 2 gates of e2e");
expect(t.text().err).toContain("chant approve yodel-apply e2e-soak");
} finally {
t.dispose();
}
});

it("below chant's floor searches the migrations Op and wave gates only", async () => {
const t = await setup([{ op: "yodel-apply", gate: "e2e", environment: "e2e", planDigest: release }]);
try {
expect(await approveOther({ ...t.c, pendingApprovals: null })).toBeUndefined();
expect(t.ran).toEqual([]);
const op = await findMigrateOp(t.c.project, t.c.env);
await gitGateLedgerPort({ cwd: t.c.project.dir }).appendPending({ op: op.name, gate: op.gate, timestamp: new Date().toISOString(), expiresAt: new Date(Date.now() + 3_600_000).toISOString(), planDigest: waveDigest });
expect(await approveOther({ ...t.c, expected: waveDigest, pendingApprovals: null })).toBe(0);
expect(t.ran).toEqual([["approve", op.name, op.gate, "--plan", waveDigest]]);
} finally {
t.dispose();
}
});
});

describe("yodel approve resumes the waiting job (#47)", () => {
const digest = `jcs1-sha256:${"0".repeat(64)}`;

Expand Down
Loading