@intentius/chant 0.124.77 → 0.124.79
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__fixtures__/notify-messages/events.d.ts +9 -0
- package/dist/__fixtures__/notify-messages/events.d.ts.map +1 -0
- package/dist/__fixtures__/notify-messages/events.js +21 -0
- package/dist/__fixtures__/product-words.d.ts +14 -0
- package/dist/__fixtures__/product-words.d.ts.map +1 -1
- package/dist/__fixtures__/product-words.js +11 -0
- package/dist/__fixtures__/terragucci-notify.d.ts +66 -0
- package/dist/__fixtures__/terragucci-notify.d.ts.map +1 -0
- package/dist/__fixtures__/terragucci-notify.js +133 -0
- package/dist/action-pins.d.ts +8 -0
- package/dist/action-pins.d.ts.map +1 -1
- package/dist/action-pins.js +8 -0
- package/dist/agent-guard.d.ts.map +1 -1
- package/dist/agent-guard.js +1 -2
- package/dist/change-set.d.ts +10 -1
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +4 -1
- package/dist/ci-agent.d.ts +16 -2
- package/dist/ci-agent.d.ts.map +1 -1
- package/dist/ci-agent.js +6 -1
- package/dist/ci-commit.d.ts +66 -2
- package/dist/ci-commit.d.ts.map +1 -1
- package/dist/ci-commit.js +147 -19
- package/dist/ci-own-jobs.d.ts.map +1 -1
- package/dist/ci-own-jobs.js +1 -2
- package/dist/ci-product-words.d.ts +18 -4
- package/dist/ci-product-words.d.ts.map +1 -1
- package/dist/ci-product-words.js +26 -6
- package/dist/ci-product.d.ts +7 -3
- package/dist/ci-product.d.ts.map +1 -1
- package/dist/ci-product.js +8 -5
- package/dist/cli/handlers/release.d.ts +8 -0
- package/dist/cli/handlers/release.d.ts.map +1 -1
- package/dist/cli/handlers/release.js +68 -6
- package/dist/cli/handlers/run-wave.js +1 -0
- package/dist/cli/main.d.ts.map +1 -1
- package/dist/cli/main.js +7 -0
- package/dist/cli/mcp/product-tools.d.ts +6 -0
- package/dist/cli/mcp/product-tools.d.ts.map +1 -1
- package/dist/cli/mcp/product-tools.js +7 -0
- package/dist/cli/mcp/read-only.d.ts +12 -0
- package/dist/cli/mcp/read-only.d.ts.map +1 -1
- package/dist/cli/mcp/read-only.js +16 -0
- package/dist/cli/mcp/server.d.ts +11 -0
- package/dist/cli/mcp/server.d.ts.map +1 -1
- package/dist/cli/mcp/server.js +26 -5
- package/dist/cli/mcp/types.d.ts +5 -1
- package/dist/cli/mcp/types.d.ts.map +1 -1
- package/dist/components/capability.d.ts +42 -0
- package/dist/components/capability.d.ts.map +1 -1
- package/dist/components/group-digest.d.ts +17 -0
- package/dist/components/group-digest.d.ts.map +1 -0
- package/dist/components/group-digest.js +18 -0
- package/dist/components/groups.d.ts +2 -11
- package/dist/components/groups.d.ts.map +1 -1
- package/dist/components/groups.js +2 -12
- package/dist/components/index.d.ts +1 -1
- package/dist/components/index.d.ts.map +1 -1
- package/dist/components/pr-env-components.d.ts.map +1 -1
- package/dist/components/pr-env-components.js +1 -2
- package/dist/config.d.ts +28 -8
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +21 -1
- package/dist/lexicon.d.ts +8 -0
- package/dist/lexicon.d.ts.map +1 -1
- package/dist/lexicon.js +8 -0
- package/dist/lifecycle/release-cut.d.ts +7 -1
- package/dist/lifecycle/release-cut.d.ts.map +1 -1
- package/dist/lifecycle/release-cut.js +1 -0
- package/dist/lifecycle/release-images.d.ts +86 -3
- package/dist/lifecycle/release-images.d.ts.map +1 -1
- package/dist/lifecycle/release-images.js +131 -6
- package/dist/lifecycle/release-publish.d.ts +8 -1
- package/dist/lifecycle/release-publish.d.ts.map +1 -1
- package/dist/lifecycle/release-publish.js +30 -3
- package/dist/lifecycle/release-version.d.ts +86 -0
- package/dist/lifecycle/release-version.d.ts.map +1 -0
- package/dist/lifecycle/release-version.js +209 -0
- package/dist/mcp/types.d.ts +26 -0
- package/dist/mcp/types.d.ts.map +1 -1
- package/dist/notify-product.d.ts +11 -2
- package/dist/notify-product.d.ts.map +1 -1
- package/dist/notify-product.js +15 -4
- package/dist/notify-relay.d.ts.map +1 -1
- package/dist/notify-relay.js +1 -2
- package/dist/notify.d.ts +36 -4
- package/dist/notify.d.ts.map +1 -1
- package/dist/notify.js +31 -21
- package/dist/notify.schema.json +1 -0
- package/dist/op/activities/review-product.d.ts +111 -0
- package/dist/op/activities/review-product.d.ts.map +1 -0
- package/dist/op/activities/review-product.js +115 -0
- package/dist/op/gate.d.ts +17 -0
- package/dist/op/gate.d.ts.map +1 -1
- package/dist/op/gate.js +36 -0
- package/dist/op/index.d.ts +8 -6
- package/dist/op/index.d.ts.map +1 -1
- package/dist/op/index.js +5 -4
- package/dist/op/op-waves-after-plan.d.ts +76 -1
- package/dist/op/op-waves-after-plan.d.ts.map +1 -1
- package/dist/op/op-waves-after-plan.js +142 -3
- package/dist/op/op-waves-capability-spec.js +1 -2
- package/dist/op/op-waves-capability.d.ts +89 -2
- package/dist/op/op-waves-capability.d.ts.map +1 -1
- package/dist/op/op-waves-capability.js +215 -7
- package/dist/op/op-waves-run-hooks.d.ts +104 -0
- package/dist/op/op-waves-run-hooks.d.ts.map +1 -0
- package/dist/op/op-waves-run-hooks.js +146 -0
- package/dist/op/op-waves-run.d.ts +21 -0
- package/dist/op/op-waves-run.d.ts.map +1 -1
- package/dist/op/op-waves-run.js +233 -13
- package/dist/op/op-waves.d.ts +11 -1
- package/dist/op/op-waves.d.ts.map +1 -1
- package/dist/op/op-waves.js +7 -1
- package/dist/op/runtime.d.ts +2 -0
- package/dist/op/runtime.d.ts.map +1 -1
- package/dist/policy-engine.d.ts.map +1 -1
- package/dist/policy-engine.js +1 -2
- package/dist/pr-forge-reads.d.ts +124 -0
- package/dist/pr-forge-reads.d.ts.map +1 -0
- package/dist/pr-forge-reads.js +322 -0
- package/dist/pr-forge.d.ts +14 -0
- package/dist/pr-forge.d.ts.map +1 -1
- package/dist/pr-forge.js +21 -13
- package/dist/pr-plan-note.d.ts +293 -0
- package/dist/pr-plan-note.d.ts.map +1 -0
- package/dist/pr-plan-note.js +361 -0
- package/dist/release-floors.d.ts +13 -1
- package/dist/release-floors.d.ts.map +1 -1
- package/dist/release-floors.js +13 -1
- package/dist/state-export.d.ts.map +1 -1
- package/dist/state-export.js +1 -2
- package/dist/state-migration.d.ts.map +1 -1
- package/dist/state-migration.js +1 -2
- package/dist/state-unlock.d.ts.map +1 -1
- package/dist/state-unlock.js +1 -2
- package/dist/wave-runtime.mjs +314 -314
- package/dist/workspace/change-set.schema.json +7 -0
- package/dist/workspace/release-workflow.d.ts +7 -0
- package/dist/workspace/release-workflow.d.ts.map +1 -1
- package/dist/workspace/release-workflow.js +13 -2
- package/package.json +1 -1
- package/src/__fixtures__/notify-messages/events.ts +23 -0
- package/src/__fixtures__/notify-messages/messages.json +50 -0
- package/src/__fixtures__/pr-loop/terragucci-plan.note.golden.md +53 -0
- package/src/__fixtures__/pr-loop/terragucci-plan.note.json +83 -0
- package/src/__fixtures__/product-words.ts +12 -0
- package/src/__fixtures__/review-agent/terragucci-review.json +94 -0
- package/src/__fixtures__/terragucci-notify.ts +170 -0
- package/src/action-pins.test.ts +11 -1
- package/src/action-pins.ts +9 -0
- package/src/agent-guard.test.ts +1 -2
- package/src/agent-guard.ts +1 -2
- package/src/change-set.test.ts +20 -0
- package/src/change-set.ts +11 -1
- package/src/ci-agent.test.ts +9 -0
- package/src/ci-agent.ts +15 -2
- package/src/ci-commit.test.ts +162 -0
- package/src/ci-commit.ts +167 -21
- package/src/ci-own-jobs.test.ts +1 -2
- package/src/ci-own-jobs.ts +1 -2
- package/src/ci-product-words.ts +35 -7
- package/src/ci-product.ts +9 -6
- package/src/cli/handlers/build-pr-loop-control.test.ts +1 -2
- package/src/cli/handlers/build-run-group.test.ts +1 -2
- package/src/cli/handlers/release.ts +60 -7
- package/src/cli/handlers/run-wave.ts +1 -0
- package/src/cli/main.ts +7 -0
- package/src/cli/mcp/product-tools.test.ts +50 -0
- package/src/cli/mcp/product-tools.ts +8 -0
- package/src/cli/mcp/read-only.ts +19 -0
- package/src/cli/mcp/server.test.ts +30 -0
- package/src/cli/mcp/server.ts +28 -5
- package/src/cli/mcp/types.ts +6 -1
- package/src/components/capability.ts +41 -0
- package/src/components/group-digest.ts +20 -0
- package/src/components/groups.ts +3 -13
- package/src/components/index.ts +2 -0
- package/src/components/pr-env-components.test.ts +1 -2
- package/src/components/pr-env-components.ts +1 -2
- package/src/config.ts +38 -3
- package/src/gate-policy-bundle.test.ts +1 -1
- package/src/lexicon.ts +9 -0
- package/src/lifecycle/release-cut.ts +11 -0
- package/src/lifecycle/release-images-dockerfile.test.ts +229 -0
- package/src/lifecycle/release-images.ts +171 -8
- package/src/lifecycle/release-publish.ts +34 -3
- package/src/lifecycle/release-version.test.ts +155 -0
- package/src/lifecycle/release-version.ts +227 -0
- package/src/mcp/types.ts +27 -0
- package/src/notify-message-words.test.ts +185 -0
- package/src/notify-product.ts +16 -4
- package/src/notify-relay.ts +1 -2
- package/src/notify.schema.json +1 -0
- package/src/notify.ts +59 -19
- package/src/op/activities/review-product.test.ts +116 -0
- package/src/op/activities/review-product.ts +199 -0
- package/src/op/gate.ts +46 -0
- package/src/op/index.ts +10 -4
- package/src/op/op-waves-after-plan-wave.test.ts +226 -0
- package/src/op/op-waves-after-plan.ts +170 -5
- package/src/op/op-waves-capability-spec.ts +1 -1
- package/src/op/op-waves-capability.test.ts +6 -0
- package/src/op/op-waves-capability.ts +243 -10
- package/src/op/op-waves-run-hooks.test.ts +184 -0
- package/src/op/op-waves-run-hooks.ts +195 -0
- package/src/op/op-waves-run.ts +240 -16
- package/src/op/op-waves-step-wave.test.ts +277 -0
- package/src/op/op-waves.ts +15 -2
- package/src/op/runtime.ts +2 -0
- package/src/op/schedule-overdue.test.ts +26 -0
- package/src/policy-engine.test.ts +1 -2
- package/src/policy-engine.ts +1 -2
- package/src/pr-forge-reads.test.ts +285 -0
- package/src/pr-forge-reads.ts +424 -0
- package/src/pr-forge.ts +30 -16
- package/src/pr-plan-note.test.ts +105 -0
- package/src/pr-plan-note.ts +520 -0
- package/src/release-floors.ts +19 -1
- package/src/state-export.test.ts +1 -2
- package/src/state-export.ts +1 -2
- package/src/state-migration.test.ts +1 -2
- package/src/state-migration.ts +1 -2
- package/src/state-unlock.test.ts +1 -2
- package/src/state-unlock.ts +1 -2
- package/src/workspace/change-set.schema.json +7 -0
- package/src/workspace/release-workflow.test.ts +18 -0
- package/src/workspace/release-workflow.ts +23 -2
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run hooks in the wave job's process (#4842): a product gives each run its
|
|
3
|
+
* environment and checks, and acts before and after its apply, around
|
|
4
|
+
* chant's step.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, expect, test } from "vitest";
|
|
8
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
import { tmpdir } from "node:os";
|
|
11
|
+
import type { OpWavesSpec } from "./op-waves";
|
|
12
|
+
import { runOpWave, type OpWaveShowAtBase } from "./op-waves-run";
|
|
13
|
+
import { memoryGateLedgerPort } from "./gate";
|
|
14
|
+
import { CapabilityRegistry, type Capability, type DeployContext } from "../components/capability";
|
|
15
|
+
import type { ChangeSetPart } from "../change-set";
|
|
16
|
+
import type { OpRunRecordInput } from "./runtime";
|
|
17
|
+
import { opWaveRunHooksProblems, type OpWaveRunHookApplied, type OpWaveRunHooks } from "./op-waves-run-hooks";
|
|
18
|
+
import { activeProductWaveHook, withProductWaveHook } from "./op-waves-after-plan";
|
|
19
|
+
|
|
20
|
+
const digest = (n: string) => `sha256:${n.repeat(64)}`;
|
|
21
|
+
|
|
22
|
+
const part = (member: string): ChangeSetPart => ({
|
|
23
|
+
member: { member, lexicon: "terraform", planner: "chant", status: "planned", planDigest: digest("9"), holes: [] },
|
|
24
|
+
entries: [{ member, address: `aws_s3_bucket.${member}`, type: "aws_s3_bucket", action: "create" } as ChangeSetPart["entries"][number]],
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
interface Seen {
|
|
28
|
+
planned: Array<{ root: string; env?: Record<string, string> }>;
|
|
29
|
+
applied: Array<{ root: string; env?: Record<string, string> }>;
|
|
30
|
+
order: string[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function capability(digests: Record<string, string>, fail: string[] = []): { registry: CapabilityRegistry; seen: Seen } {
|
|
34
|
+
const seen: Seen = { planned: [], applied: [], order: [] };
|
|
35
|
+
const cap: Capability<Record<string, unknown>, unknown> = {
|
|
36
|
+
kind: "fake-apply",
|
|
37
|
+
async plan(ctx: DeployContext, input) {
|
|
38
|
+
const root = String(input.root);
|
|
39
|
+
seen.order.push(`init ${root}`);
|
|
40
|
+
await ctx.afterInit?.();
|
|
41
|
+
seen.order.push(`plan ${root}`);
|
|
42
|
+
seen.planned.push({ root, ...(ctx.processEnv ? { env: ctx.processEnv } : {}) });
|
|
43
|
+
return { member: root, planDigest: digests[root]!, artifact: { planFile: `${root}.tfplan` }, changeSet: part(root) };
|
|
44
|
+
},
|
|
45
|
+
async run(ctx: DeployContext, input) {
|
|
46
|
+
const root = String(input.root);
|
|
47
|
+
seen.order.push(`apply ${root}`);
|
|
48
|
+
if (fail.includes(root)) throw new Error(`${root} did not apply`);
|
|
49
|
+
seen.applied.push({ root, ...(ctx.processEnv ? { env: ctx.processEnv } : {}) });
|
|
50
|
+
return { root, outputs: { id: `${root}-1` } };
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
return { registry: new CapabilityRegistry().register(cap), seen };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const spec: OpWavesSpec = {
|
|
57
|
+
name: "tf-apply",
|
|
58
|
+
op: "tf-apply",
|
|
59
|
+
step: { kind: "fake-apply", root: "{target}" },
|
|
60
|
+
waves: [{ name: "wave-1", runs: [{ target: "network" }, { target: "app" }], gate: "never" }],
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const digests = { network: digest("1"), app: digest("2") };
|
|
64
|
+
|
|
65
|
+
function harness(s: OpWavesSpec = spec) {
|
|
66
|
+
const cwd = mkdtempSync(join(tmpdir(), "op-waves-run-hooks-"));
|
|
67
|
+
const show: OpWaveShowAtBase = () => ({ sha: "b".repeat(40), text: JSON.stringify(s) });
|
|
68
|
+
return { cwd, show, done: () => rmSync(cwd, { recursive: true, force: true }) };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const run = (h: ReturnType<typeof harness>, registry: CapabilityRegistry, runHooks: OpWaveRunHooks | OpWaveRunHooks[], extra: { runs?: { append: (r: OpRunRecordInput) => Promise<void> }; spec?: OpWavesSpec } = {}) =>
|
|
72
|
+
runOpWave({ spec: extra.spec ?? spec, specFile: "waves.json", wave: 1, cwd: h.cwd, show: h.show, gates: memoryGateLedgerPort(), capabilities: registry, log: () => {}, runHooks, ...(extra.runs ? { runs: extra.runs } : {}) });
|
|
73
|
+
|
|
74
|
+
describe("run hooks in the job's process (#4842)", () => {
|
|
75
|
+
test("a run's env reaches its plan and its apply, the later hook's value winning", async () => {
|
|
76
|
+
const h = harness();
|
|
77
|
+
const { registry, seen } = capability(digests);
|
|
78
|
+
const result = await run(h, registry, [
|
|
79
|
+
{ name: "roles", beforePlan: (r) => ({ env: { AWS_ROLE_ARN: `arn:${r.target}`, TF_VAR_x: "a" } }) },
|
|
80
|
+
{ name: "secrets", targets: ["app"], beforePlan: () => ({ env: { TF_VAR_x: "b" } }) },
|
|
81
|
+
]);
|
|
82
|
+
expect(result.exitCode).toBe(0);
|
|
83
|
+
expect(seen.planned).toEqual([
|
|
84
|
+
{ root: "network", env: { AWS_ROLE_ARN: "arn:network", TF_VAR_x: "a" } },
|
|
85
|
+
{ root: "app", env: { AWS_ROLE_ARN: "arn:app", TF_VAR_x: "b" } },
|
|
86
|
+
]);
|
|
87
|
+
expect(seen.applied).toEqual(seen.planned);
|
|
88
|
+
h.done();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("a check after init runs between the engine's init and its plan, and a refusal there stops the wave", async () => {
|
|
92
|
+
const h = harness();
|
|
93
|
+
const { registry, seen } = capability(digests);
|
|
94
|
+
// A run that cannot be planned fails the job, as a plan that fails does.
|
|
95
|
+
await expect(run(h, registry, {
|
|
96
|
+
name: "sources",
|
|
97
|
+
beforePlan: (r) => ({
|
|
98
|
+
afterInit: () => {
|
|
99
|
+
seen.order.push(`check ${r.target}`);
|
|
100
|
+
if (r.target === "app") throw new Error("module git::https://example.com/m is not on the list at base");
|
|
101
|
+
},
|
|
102
|
+
}),
|
|
103
|
+
})).rejects.toThrow("planning app through fake-apply failed: the check after init of sources refused it: module git::https://example.com/m is not on the list at base");
|
|
104
|
+
expect(seen.order).toEqual(["init network", "check network", "plan network", "init app", "check app"]);
|
|
105
|
+
expect(seen.applied).toEqual([]);
|
|
106
|
+
h.done();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("a refusal before the plan stops the run before its engine starts, and nothing applies", async () => {
|
|
110
|
+
const h = harness();
|
|
111
|
+
const { registry, seen } = capability(digests);
|
|
112
|
+
await expect(run(h, registry, { name: "sources", beforePlan: (r) => (r.target === "network" ? { refuse: "a module source outside the list at base" } : undefined) }))
|
|
113
|
+
.rejects.toThrow("network was not planned: the before-plan hook sources refused it: a module source outside the list at base");
|
|
114
|
+
expect(seen.order).toEqual([]);
|
|
115
|
+
h.done();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("a before-apply hook that throws fails its run, and the others apply", async () => {
|
|
119
|
+
const h = harness();
|
|
120
|
+
const { registry, seen } = capability(digests);
|
|
121
|
+
const result = await run(h, registry, { name: "steps", beforeApply: (r) => { if (r.target === "network") throw new Error("tflint failed"); } });
|
|
122
|
+
expect(result.exitCode).toBe(1);
|
|
123
|
+
expect(result.failed).toEqual(["network"]);
|
|
124
|
+
expect(result.applied).toEqual(["app"]);
|
|
125
|
+
expect(seen.order).not.toContain("apply network");
|
|
126
|
+
expect(result.targets?.network?.error).toContain("the before-apply hook steps failed, so nothing of it was applied: tflint failed");
|
|
127
|
+
h.done();
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("an after-apply hook sees each outcome and output, its files go on the run record, and a throw after an apply fails the run", async () => {
|
|
131
|
+
const h = harness();
|
|
132
|
+
const { registry } = capability(digests, ["app"]);
|
|
133
|
+
const got: OpWaveRunHookApplied[] = [];
|
|
134
|
+
const records: OpRunRecordInput[] = [];
|
|
135
|
+
const result = await run(h, registry, { name: "report", afterApply: (r) => (got.push(r), { files: [`states/${r.target}.json`] }) }, { runs: { append: async (r) => void records.push(r) } });
|
|
136
|
+
expect(got.map((r) => [r.target, r.outcome, r.planDigest])).toEqual([["network", "applied", digests.network], ["app", "failed", digests.app]]);
|
|
137
|
+
expect(got[0]!.output).toEqual({ root: "network", outputs: { id: "network-1" } });
|
|
138
|
+
expect(got[1]!.reason).toContain("app did not apply");
|
|
139
|
+
expect(result.exitCode).toBe(1);
|
|
140
|
+
expect(records[0]!.wave!.targets?.network?.files).toEqual(["states/network.json"]);
|
|
141
|
+
|
|
142
|
+
const again = harness();
|
|
143
|
+
const ok = capability(digests);
|
|
144
|
+
const thrown = await run(again, ok.registry, { name: "versions", targets: ["app"], afterApply: () => { throw new Error("the state version could not be read"); } });
|
|
145
|
+
expect(thrown.exitCode).toBe(1);
|
|
146
|
+
expect(thrown.applied).toEqual(["network"]);
|
|
147
|
+
expect(thrown.failed).toEqual(["app"]);
|
|
148
|
+
expect(thrown.targets?.app?.error).toContain("the after-apply hook versions failed: the state version could not be read");
|
|
149
|
+
h.done();
|
|
150
|
+
again.done();
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("runs applied at once take their hooks too", async () => {
|
|
154
|
+
const wide: OpWavesSpec = { ...spec, parallelism: 2 };
|
|
155
|
+
const h = harness(wide);
|
|
156
|
+
const { registry, seen } = capability(digests);
|
|
157
|
+
const after: string[] = [];
|
|
158
|
+
const result = await run(h, registry, { name: "roles", beforePlan: (r) => ({ env: { ROLE: r.target } }), afterApply: (r) => void after.push(r.target) }, { spec: wide });
|
|
159
|
+
expect(result.exitCode).toBe(0);
|
|
160
|
+
expect(seen.applied.map((a) => a.env)).toEqual([{ ROLE: "network" }, { ROLE: "app" }]);
|
|
161
|
+
expect(after.sort()).toEqual(["app", "network"]);
|
|
162
|
+
h.done();
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("a product's wave hook hands its run hooks to the job", async () => {
|
|
166
|
+
const hook = { runHooks: { name: "x", beforePlan: () => {} } };
|
|
167
|
+
await withProductWaveHook(hook, async () => expect(activeProductWaveHook()?.runHooks).toBe(hook.runHooks));
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test("the hook list is checked", async () => {
|
|
171
|
+
expect(opWaveRunHooksProblems({ name: "", beforePlan: 1 })).toEqual(["runHooks[0] needs a name", "runHooks[0].beforePlan must be a function"]);
|
|
172
|
+
expect(opWaveRunHooksProblems([{ name: "x" }])).toEqual(["runHooks[0] gives none of beforePlan, beforeApply and afterApply"]);
|
|
173
|
+
const h = harness();
|
|
174
|
+
const { registry } = capability(digests);
|
|
175
|
+
await expect(run(h, registry, { name: "" } as OpWaveRunHooks)).rejects.toThrow(/runHooks\[0\] needs a name/);
|
|
176
|
+
h.done();
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test("the floor is the next release", async () => {
|
|
180
|
+
const entry = await import("./index");
|
|
181
|
+
const { NEXT_RELEASE } = await import("../release-floors");
|
|
182
|
+
expect(entry.OP_WAVE_RUN_HOOKS_FLOOR).toBe(NEXT_RELEASE);
|
|
183
|
+
});
|
|
184
|
+
});
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run hooks in the wave job's process (#4842): what a product does around
|
|
3
|
+
* each run of a wave with a `step`, beside its after-plan hooks
|
|
4
|
+
* (`./op-waves-after-plan.ts`). A product that runs chant in process
|
|
5
|
+
* (`runOpWave`, or a bin built on `runChantInProcess`) passes `runHooks`.
|
|
6
|
+
*
|
|
7
|
+
* - `beforePlan` is called before the run's step plans. Its answer can give
|
|
8
|
+
* the run an `env`, the environment variables its engine plans and
|
|
9
|
+
* applies with over the job's (a role's credentials, values read from a
|
|
10
|
+
* secret store), and an `afterInit` check, which the capability calls
|
|
11
|
+
* once its engine has initialised the run and before the plan runs any of
|
|
12
|
+
* the run's code (a module and provider source check). `refuse` stops the
|
|
13
|
+
* run before its engine starts: the wave then plans nothing more and
|
|
14
|
+
* applies nothing, as a run whose plan failed.
|
|
15
|
+
* - `beforeApply` is called before the run's step applies the plan the gate
|
|
16
|
+
* decided. A hook that throws fails the run, and nothing of it applies.
|
|
17
|
+
* - `afterApply` is called once the step's apply came back, whatever the
|
|
18
|
+
* outcome, with the capability's output when it applied. Its `files` go
|
|
19
|
+
* on the run record beside the run's apply. A hook that throws after an
|
|
20
|
+
* apply fails the run, as an after-apply command hook does.
|
|
21
|
+
*
|
|
22
|
+
* Several hooks run in the order given. Their `env` values are laid over one
|
|
23
|
+
* another, the later winning. `targets` limits a hook to the runs it
|
|
24
|
+
* matches, as for a command hook. A wave whose spec names no `step` has no
|
|
25
|
+
* plan or apply of chant's to run them around, so they are not called.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { isAbsolute } from "node:path";
|
|
29
|
+
import { NEXT_RELEASE } from "../release-floors";
|
|
30
|
+
import { opWaveHookRuns } from "./op-waves";
|
|
31
|
+
|
|
32
|
+
/** The first chant that runs a product's `runHooks` around each run of a wave with a `step` (#4842), and hands its `env` and `afterInit` to the capability (`DeployContext.processEnv`, `DeployContext.afterInit`). */
|
|
33
|
+
export const OP_WAVE_RUN_HOOKS_FLOOR = NEXT_RELEASE;
|
|
34
|
+
|
|
35
|
+
/** One run of a wave, as a run hook sees it (#4842). */
|
|
36
|
+
export interface OpWaveRunHookRun {
|
|
37
|
+
/** The spec's name and `op`. */
|
|
38
|
+
spec: string;
|
|
39
|
+
op: string;
|
|
40
|
+
wave: number;
|
|
41
|
+
waveName: string;
|
|
42
|
+
/** The job: `wave` for a whole wave, `decide` for a wide wave's deciding job, `share` or `group` for its apply jobs. */
|
|
43
|
+
job: "wave" | "decide" | "share" | "group";
|
|
44
|
+
target: string;
|
|
45
|
+
/** The run's own op. */
|
|
46
|
+
runOp: string;
|
|
47
|
+
/** The job's directory. */
|
|
48
|
+
cwd: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A planned run, as `beforeApply` and `afterApply` see it. */
|
|
52
|
+
export interface OpWaveRunHookPlanned extends OpWaveRunHookRun {
|
|
53
|
+
planDigest: string;
|
|
54
|
+
/** The run's plan file, relative to the job's directory: `{plan}`. */
|
|
55
|
+
plan: string;
|
|
56
|
+
/** The capability's artifact. */
|
|
57
|
+
artifact?: unknown;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** An applied run, as `afterApply` sees it. */
|
|
61
|
+
export interface OpWaveRunHookApplied extends OpWaveRunHookPlanned {
|
|
62
|
+
outcome: "applied" | "moved" | "failed";
|
|
63
|
+
/** The capability's output, when it applied. */
|
|
64
|
+
output?: unknown;
|
|
65
|
+
/** Why it did not apply. */
|
|
66
|
+
reason?: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** What `beforePlan` gives a run. */
|
|
70
|
+
export interface OpWaveBeforePlanResult {
|
|
71
|
+
/** Environment variables the run's engine plans and applies with, over the job's. */
|
|
72
|
+
env?: Record<string, string>;
|
|
73
|
+
/** A check the capability calls after its engine's init and before its plan. It throws to stop the run. */
|
|
74
|
+
afterInit?: () => void | Promise<void>;
|
|
75
|
+
/** Why the run is not planned at all. */
|
|
76
|
+
refuse?: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** What `afterApply` adds to a run. */
|
|
80
|
+
export interface OpWaveAfterApplyResult {
|
|
81
|
+
/** Files the hook wrote for the run, relative to the job's directory. They go on the run record. */
|
|
82
|
+
files?: string[];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Hooks around each run of a wave with a `step`, in the job's process (#4842). */
|
|
86
|
+
export interface OpWaveRunHooks {
|
|
87
|
+
/** The hook's name in the log. */
|
|
88
|
+
name: string;
|
|
89
|
+
/** The runs it is for, as a command hook's `targets`. Default: every run. */
|
|
90
|
+
targets?: string[];
|
|
91
|
+
beforePlan?(run: OpWaveRunHookRun): OpWaveBeforePlanResult | void | Promise<OpWaveBeforePlanResult | void>;
|
|
92
|
+
beforeApply?(run: OpWaveRunHookPlanned): void | Promise<void>;
|
|
93
|
+
afterApply?(run: OpWaveRunHookApplied): OpWaveAfterApplyResult | void | Promise<OpWaveAfterApplyResult | void>;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** What is wrong with `runHooks`, empty when nothing is. */
|
|
97
|
+
export function opWaveRunHooksProblems(hooks: unknown): string[] {
|
|
98
|
+
if (hooks === undefined) return [];
|
|
99
|
+
const list = Array.isArray(hooks) ? hooks : [hooks];
|
|
100
|
+
const problems: string[] = [];
|
|
101
|
+
list.forEach((h: unknown, i) => {
|
|
102
|
+
const at = `runHooks[${i}]`;
|
|
103
|
+
const hook = h as Partial<Record<keyof OpWaveRunHooks, unknown>> | null;
|
|
104
|
+
if (!hook || typeof hook !== "object") return void problems.push(`${at} must be { name, beforePlan?, beforeApply?, afterApply?, targets? }`);
|
|
105
|
+
if (typeof hook.name !== "string" || hook.name.trim() === "") problems.push(`${at} needs a name`);
|
|
106
|
+
const moments = (["beforePlan", "beforeApply", "afterApply"] as const).filter((m) => hook[m] !== undefined);
|
|
107
|
+
if (moments.length === 0) problems.push(`${at} gives none of beforePlan, beforeApply and afterApply`);
|
|
108
|
+
for (const m of moments) if (typeof hook[m] !== "function") problems.push(`${at}.${m} must be a function`);
|
|
109
|
+
if (hook.targets !== undefined && (!Array.isArray(hook.targets) || hook.targets.length === 0 || !hook.targets.every((t) => typeof t === "string" && t !== ""))) problems.push(`${at}.targets must be a non-empty list of targets or patterns`);
|
|
110
|
+
});
|
|
111
|
+
return problems;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The hooks as a list. */
|
|
115
|
+
export function opWaveRunHookList(given: OpWaveRunHooks | readonly OpWaveRunHooks[] | undefined): readonly OpWaveRunHooks[] {
|
|
116
|
+
if (given === undefined) return [];
|
|
117
|
+
return Array.isArray(given) ? (given as readonly OpWaveRunHooks[]) : [given as OpWaveRunHooks];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const forRun = <T extends { target: string }>(hook: OpWaveRunHooks, run: T): boolean => opWaveHookRuns(hook, [run]).length > 0;
|
|
121
|
+
|
|
122
|
+
/** What the `beforePlan` hooks gave one run. */
|
|
123
|
+
export type OpWaveBeforePlanOutcome = { refused: string } | { env?: Record<string, string>; afterInit?: () => Promise<void> };
|
|
124
|
+
|
|
125
|
+
/** Call each hook's `beforePlan` for `run`, in order. A hook that throws or refuses stops the rest and refuses the run. */
|
|
126
|
+
export async function runOpWaveBeforePlan(hooks: readonly OpWaveRunHooks[], run: OpWaveRunHookRun): Promise<OpWaveBeforePlanOutcome> {
|
|
127
|
+
let env: Record<string, string> | undefined;
|
|
128
|
+
const checks: Array<() => void | Promise<void>> = [];
|
|
129
|
+
for (const hook of hooks) {
|
|
130
|
+
if (!hook.beforePlan || !forRun(hook, run)) continue;
|
|
131
|
+
let answer: OpWaveBeforePlanResult | void;
|
|
132
|
+
try {
|
|
133
|
+
answer = await hook.beforePlan(run);
|
|
134
|
+
} catch (err) {
|
|
135
|
+
return { refused: `the before-plan hook ${hook.name} failed: ${err instanceof Error ? err.message : String(err)}` };
|
|
136
|
+
}
|
|
137
|
+
if (!answer) continue;
|
|
138
|
+
if (answer.refuse !== undefined) return { refused: `the before-plan hook ${hook.name} refused it: ${answer.refuse}` };
|
|
139
|
+
if (answer.env !== undefined) {
|
|
140
|
+
const bad = typeof answer.env !== "object" || answer.env === null || Array.isArray(answer.env) || !Object.values(answer.env).every((v) => typeof v === "string");
|
|
141
|
+
if (bad) return { refused: `the before-plan hook ${hook.name} answered with an env that is not a map of names to strings` };
|
|
142
|
+
env = { ...env, ...answer.env };
|
|
143
|
+
}
|
|
144
|
+
if (answer.afterInit !== undefined) {
|
|
145
|
+
if (typeof answer.afterInit !== "function") return { refused: `the before-plan hook ${hook.name} answered with an afterInit that is not a function` };
|
|
146
|
+
const name = hook.name;
|
|
147
|
+
const check = answer.afterInit;
|
|
148
|
+
checks.push(async () => {
|
|
149
|
+
try {
|
|
150
|
+
await check();
|
|
151
|
+
} catch (err) {
|
|
152
|
+
throw new Error(`the check after init of ${name} refused it: ${err instanceof Error ? err.message : String(err)}`);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return {
|
|
158
|
+
...(env ? { env } : {}),
|
|
159
|
+
...(checks.length > 0 ? { afterInit: async () => { for (const c of checks) await c(); } } : {}),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Call each hook's `beforeApply` for `run`. Returns why the run must not apply, or undefined. */
|
|
164
|
+
export async function runOpWaveBeforeApply(hooks: readonly OpWaveRunHooks[], run: OpWaveRunHookPlanned): Promise<string | undefined> {
|
|
165
|
+
for (const hook of hooks) {
|
|
166
|
+
if (!hook.beforeApply || !forRun(hook, run)) continue;
|
|
167
|
+
try {
|
|
168
|
+
await hook.beforeApply(run);
|
|
169
|
+
} catch (err) {
|
|
170
|
+
return `the before-apply hook ${hook.name} failed, so nothing of it was applied: ${err instanceof Error ? err.message : String(err)}`;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return undefined;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Call each hook's `afterApply` for `run`: the files they wrote, and why the run fails when one threw or answered badly. */
|
|
177
|
+
export async function runOpWaveAfterApply(hooks: readonly OpWaveRunHooks[], run: OpWaveRunHookApplied): Promise<{ files: string[]; failed?: string }> {
|
|
178
|
+
const files: string[] = [];
|
|
179
|
+
for (const hook of hooks) {
|
|
180
|
+
if (!hook.afterApply || !forRun(hook, run)) continue;
|
|
181
|
+
let answer: OpWaveAfterApplyResult | void;
|
|
182
|
+
try {
|
|
183
|
+
answer = await hook.afterApply(run);
|
|
184
|
+
} catch (err) {
|
|
185
|
+
return { files, failed: `the after-apply hook ${hook.name} failed: ${err instanceof Error ? err.message : String(err)}` };
|
|
186
|
+
}
|
|
187
|
+
const given = answer?.files;
|
|
188
|
+
if (given === undefined) continue;
|
|
189
|
+
if (!Array.isArray(given) || !given.every((f) => typeof f === "string" && f !== "" && !isAbsolute(f) && !f.split("/").includes(".."))) {
|
|
190
|
+
return { files, failed: `the after-apply hook ${hook.name} answered with files that are not paths inside the job's directory` };
|
|
191
|
+
}
|
|
192
|
+
for (const f of given) if (!files.includes(f)) files.push(f);
|
|
193
|
+
}
|
|
194
|
+
return { files };
|
|
195
|
+
}
|