@intentius/chant 0.124.44 → 0.124.45
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/ci-identity.d.ts +183 -12
- package/dist/ci-identity.d.ts.map +1 -1
- package/dist/ci-identity.js +362 -53
- package/dist/cli/handlers/build.d.ts.map +1 -1
- package/dist/cli/handlers/build.js +17 -1
- package/dist/cli/handlers/pr-env.d.ts +8 -2
- package/dist/cli/handlers/pr-env.d.ts.map +1 -1
- package/dist/cli/handlers/pr-env.js +126 -225
- package/dist/cli/handlers/pr.d.ts +3 -0
- package/dist/cli/handlers/pr.d.ts.map +1 -1
- package/dist/cli/handlers/pr.js +1 -0
- package/dist/components/cli-support.d.ts +2 -0
- package/dist/components/cli-support.d.ts.map +1 -1
- package/dist/components/cli-support.js +4 -2
- package/dist/components/fan-out-support.d.ts +3 -0
- package/dist/components/fan-out-support.d.ts.map +1 -1
- package/dist/components/fan-out-support.js +1 -1
- package/dist/components/pr-env-run.d.ts +270 -0
- package/dist/components/pr-env-run.d.ts.map +1 -0
- package/dist/components/pr-env-run.js +440 -0
- package/dist/components/pr-environment.d.ts +89 -14
- package/dist/components/pr-environment.d.ts.map +1 -1
- package/dist/components/pr-environment.js +94 -8
- package/dist/components/pr-pipeline.d.ts +10 -1
- package/dist/components/pr-pipeline.d.ts.map +1 -1
- package/dist/op/op-waves.d.ts +17 -0
- package/dist/op/op-waves.d.ts.map +1 -1
- package/dist/op/op-waves.js +23 -1
- package/dist/pr-forge.d.ts +15 -1
- package/dist/pr-forge.d.ts.map +1 -1
- package/dist/pr-forge.js +28 -16
- package/dist/pr-requires.d.ts +5 -1
- package/dist/pr-requires.d.ts.map +1 -1
- package/dist/wave-runtime.mjs +179 -179
- package/dist/workspace/ci-cloud-identity.d.ts +3 -1
- package/dist/workspace/ci-cloud-identity.d.ts.map +1 -1
- package/dist/workspace/ci-cloud-identity.js +16 -1
- package/dist/workspace/ci-run.d.ts +35 -8
- package/dist/workspace/ci-run.d.ts.map +1 -1
- package/dist/workspace/ci-run.js +95 -21
- package/dist/workspace/ci-secrets.d.ts.map +1 -1
- package/dist/workspace/ci-secrets.js +4 -3
- package/dist/workspace/declaration.d.ts +10 -0
- package/dist/workspace/declaration.d.ts.map +1 -1
- package/dist/workspace/declaration.js +17 -1
- package/dist/workspace/declaration.schema.json +36 -1
- package/package.json +1 -1
- package/src/ci-identity-targets.test.ts +275 -0
- package/src/ci-identity.ts +394 -61
- package/src/cli/handlers/build.ts +15 -1
- package/src/cli/handlers/pr-env.ts +136 -227
- package/src/cli/handlers/pr.ts +4 -0
- package/src/components/cli-support.ts +6 -1
- package/src/components/fan-out-support.ts +4 -1
- package/src/components/pr-env-run.test.ts +290 -0
- package/src/components/pr-env-run.ts +634 -0
- package/src/components/pr-environment.ts +159 -12
- package/src/components/pr-pipeline.ts +10 -1
- package/src/op/op-waves.ts +38 -1
- package/src/pr-forge-bundle.test.ts +1 -1
- package/src/pr-forge.ts +44 -17
- package/src/pr-requires.test.ts +43 -6
- package/src/pr-requires.ts +5 -1
- package/src/workspace/ci-cloud-identity.test.ts +20 -0
- package/src/workspace/ci-cloud-identity.ts +14 -1
- package/src/workspace/ci-run.test.ts +74 -1
- package/src/workspace/ci-run.ts +122 -29
- package/src/workspace/ci-secrets.test.ts +1 -1
- package/src/workspace/ci-secrets.ts +4 -3
- package/src/workspace/declaration.schema.json +36 -1
- package/src/workspace/declaration.test.ts +7 -0
- package/src/workspace/declaration.ts +24 -2
|
@@ -0,0 +1,634 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Running a pull request's environment (#4330): `up`, `down` and `sweep` as
|
|
3
|
+
* functions over an adapter, so a product that plans and applies with a tool
|
|
4
|
+
* chant does not run (terragucci, with Terraform roots under suffixed state
|
|
5
|
+
* keys) keeps its own plan, apply and teardown and takes the rest from chant:
|
|
6
|
+
*
|
|
7
|
+
* - the gate: a non-empty plan applies only once a person's approval of its
|
|
8
|
+
* digest stands under gate `pr-env` (ws-080, ws-104), and an empty one
|
|
9
|
+
* applies at once;
|
|
10
|
+
* - the records on chant/lifecycle (`_pr-envs/records.jsonl`), the TTL and
|
|
11
|
+
* which environments stand ({@link livePrEnvironments});
|
|
12
|
+
* - the ownership check on teardown ({@link teardownRefusal}), and the
|
|
13
|
+
* marker check on every candidate before anything is deleted
|
|
14
|
+
* ({@link tearDownPrEnvironment});
|
|
15
|
+
* - the sweep over expired and closed pull requests;
|
|
16
|
+
* - the report and the note ({@link renderPrEnvNote}).
|
|
17
|
+
*
|
|
18
|
+
* `chant components pr-env` (../cli/handlers/pr-env.ts) runs these with the
|
|
19
|
+
* components adapter and the lexicons' teardown. The ports default to
|
|
20
|
+
* chant/lifecycle in the working directory and the gate ledger there
|
|
21
|
+
* ({@link lifecyclePrEnvPorts}).
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { spawnSync } from "node:child_process";
|
|
25
|
+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
26
|
+
import { tmpdir } from "node:os";
|
|
27
|
+
import { join } from "node:path";
|
|
28
|
+
import { withoutForgeTokens } from "../ci-handover";
|
|
29
|
+
import type { ChantConfig } from "../config";
|
|
30
|
+
import type { TeardownCandidate, TeardownHole, TeardownOutcome } from "../lexicon";
|
|
31
|
+
import type { ObservationLexicon } from "../lexicon";
|
|
32
|
+
import type { PrForge } from "../pr-forge";
|
|
33
|
+
import {
|
|
34
|
+
PR_ENV_EXIT,
|
|
35
|
+
PR_ENV_GATE,
|
|
36
|
+
PR_ENV_LEDGER_DIR,
|
|
37
|
+
PR_ENV_LEDGER_FILE,
|
|
38
|
+
expired,
|
|
39
|
+
livePrEnvironments,
|
|
40
|
+
parsePrEnvRecords,
|
|
41
|
+
prEnvNoteMarker,
|
|
42
|
+
prEnvStatusContext,
|
|
43
|
+
prEnvStatusDescription,
|
|
44
|
+
prEnvStatusState,
|
|
45
|
+
prEnvironmentName,
|
|
46
|
+
renderPrEnvNote,
|
|
47
|
+
teardownComplete,
|
|
48
|
+
teardownCounts,
|
|
49
|
+
tearDownPrEnvironment,
|
|
50
|
+
ttlMs,
|
|
51
|
+
type LivePrEnvironment,
|
|
52
|
+
type PrEnvRecord,
|
|
53
|
+
type PrEnvReport,
|
|
54
|
+
type PrEnvReportRow,
|
|
55
|
+
type PrEnvTeardownAdapter,
|
|
56
|
+
type PrEnvTeardownAdapterInput,
|
|
57
|
+
} from "./pr-environment";
|
|
58
|
+
|
|
59
|
+
/** The environment one run works on. */
|
|
60
|
+
export interface PrEnvTarget {
|
|
61
|
+
pr: number;
|
|
62
|
+
/** `pr-<n>`. */
|
|
63
|
+
env: string;
|
|
64
|
+
template: string;
|
|
65
|
+
/** The marker every resource of the environment carries. */
|
|
66
|
+
marker: { stack: string; env: string };
|
|
67
|
+
/** The commit planned and applied: the pull request's head. */
|
|
68
|
+
head: string;
|
|
69
|
+
member?: string;
|
|
70
|
+
/** The op the gate is recorded under: `pr-<n>`, or `pr-<n>-<member>`. */
|
|
71
|
+
op: string;
|
|
72
|
+
endpoint?: string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** What an adapter's plan came to. */
|
|
76
|
+
export interface PrEnvPlanned {
|
|
77
|
+
/** The digest an approval binds. */
|
|
78
|
+
digest: string;
|
|
79
|
+
/** Nothing changes: the plan applies with no approval (ws-104). */
|
|
80
|
+
empty: boolean;
|
|
81
|
+
/** Something failed to plan, or a hook stopped it: nothing applies. */
|
|
82
|
+
failed?: boolean;
|
|
83
|
+
/** One row per component (a Terraform root, a chant component), in apply order. None: the change reaches nothing deployed, and the environment gets nothing. */
|
|
84
|
+
rows: PrEnvReportRow[];
|
|
85
|
+
message?: string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** What an adapter's apply came to. */
|
|
89
|
+
export interface PrEnvApplied {
|
|
90
|
+
/**
|
|
91
|
+
* `waiting`: a component read an upstream output that moved, so it waits
|
|
92
|
+
* for the next run. `stopped`: nothing was applied (a before-apply hook
|
|
93
|
+
* failed), so no record is kept.
|
|
94
|
+
*/
|
|
95
|
+
status: "applied" | "failed" | "waiting" | "stopped";
|
|
96
|
+
/** The rows again, with each component's status after the apply. */
|
|
97
|
+
rows: PrEnvReportRow[];
|
|
98
|
+
/** Where each component's copy lives, kept on the record for the teardown (a suffixed state key). */
|
|
99
|
+
locations?: Record<string, string>;
|
|
100
|
+
message?: string;
|
|
101
|
+
/** The apply stands, and something after it failed (an after-apply hook): the record says applied and the run exits 1. */
|
|
102
|
+
afterFailure?: string;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** What `up` hands an adapter's apply: the plan, and who approved it. */
|
|
106
|
+
export interface PrEnvApplyInput {
|
|
107
|
+
planned: PrEnvPlanned;
|
|
108
|
+
/** Who approved the digest; absent for an empty plan. */
|
|
109
|
+
approvedBy?: string[];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The plan and apply of a pull request's environment, which a product
|
|
114
|
+
* supplies. `plan` must not change anything; `apply` applies exactly what
|
|
115
|
+
* `plan` answered, once `up` decided it may.
|
|
116
|
+
*/
|
|
117
|
+
export interface PrEnvAdapter {
|
|
118
|
+
plan(target: PrEnvTarget): Promise<PrEnvPlanned>;
|
|
119
|
+
apply(target: PrEnvTarget, input: PrEnvApplyInput): Promise<PrEnvApplied>;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Where the approval of a digest stands, as `decideGate` answers. */
|
|
123
|
+
export interface PrEnvApproval {
|
|
124
|
+
status: "approved" | "changed" | "pending";
|
|
125
|
+
approvedBy?: string[];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** What the runs read and write, which tests and products replace. */
|
|
129
|
+
export interface PrEnvPorts {
|
|
130
|
+
/** Every record, oldest first. */
|
|
131
|
+
readRecords(): Promise<PrEnvRecord[]>;
|
|
132
|
+
/** Keep one record. A failure should warn: what was applied or torn down stands. */
|
|
133
|
+
appendRecord(record: PrEnvRecord): Promise<void>;
|
|
134
|
+
/** Where the approval of `digest` stands under `gate` of `op`. Throws when the ledger cannot be read. */
|
|
135
|
+
decideGate(input: { op: string; gate: string; digest: string; description: string }): Promise<PrEnvApproval>;
|
|
136
|
+
/** Post the report: the note and the commit status. Absent: nothing is posted. */
|
|
137
|
+
publish?(report: PrEnvReport): Promise<void>;
|
|
138
|
+
/** Whether a pull request is still open, for the sweep. Absent: only the TTL counts. */
|
|
139
|
+
pullRequestState?(pr: number): Promise<"open" | "closed" | "merged">;
|
|
140
|
+
log?(line: string): void;
|
|
141
|
+
warn?(message: string): void;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** What one run came to. */
|
|
145
|
+
export interface PrEnvRunResult {
|
|
146
|
+
/** {@link PR_ENV_EXIT}: 0 done, 1 failed or refused, 3 waiting on an approval. */
|
|
147
|
+
exit: number;
|
|
148
|
+
report: PrEnvReport;
|
|
149
|
+
/** The record kept, when the run kept one. */
|
|
150
|
+
record?: PrEnvRecord;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The op a pull request's gate is recorded under, as `prOp` in ../pr-loop.ts names it. */
|
|
154
|
+
export function prEnvOp(pr: number, member?: string): string {
|
|
155
|
+
return member ? `pr-${pr}-${member}` : `pr-${pr}`;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** The approve command an adapter's caller prints, the same the note shows. */
|
|
159
|
+
export { prEnvApproveCommand } from "./pr-environment";
|
|
160
|
+
|
|
161
|
+
// ── up ─────────────────────────────────────────────────────────────────────
|
|
162
|
+
|
|
163
|
+
export interface PrEnvUpInput {
|
|
164
|
+
pr: number;
|
|
165
|
+
/** The pull request's head, which is planned and applied. */
|
|
166
|
+
head: string;
|
|
167
|
+
/** `ci.prEnvironment.template`, read at base. */
|
|
168
|
+
template: string;
|
|
169
|
+
/** `ci.prEnvironment.ttl`, read at base: `72h`, `90m`, `7d`. Default 72h. */
|
|
170
|
+
ttl?: string;
|
|
171
|
+
/** The ownership stack every resource's marker names. */
|
|
172
|
+
stack: string;
|
|
173
|
+
member?: string;
|
|
174
|
+
endpoint?: string;
|
|
175
|
+
adapter: PrEnvAdapter;
|
|
176
|
+
ports: PrEnvPorts;
|
|
177
|
+
/** Who the record names. Default `chant`. */
|
|
178
|
+
actor?: string;
|
|
179
|
+
runUrl?: string;
|
|
180
|
+
now?: () => Date;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const ttlOf = (ttl: string | undefined): number => (ttl ? ttlMs(ttl) : undefined) ?? ttlMs("72h")!;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Plan pull request `pr`'s environment through the adapter and apply it once
|
|
187
|
+
* the plan is empty or an approval of its digest stands under gate `pr-env`.
|
|
188
|
+
* An apply that failed part way is kept as live too, so what applied is torn
|
|
189
|
+
* down with the rest.
|
|
190
|
+
*/
|
|
191
|
+
export async function prEnvUp(input: PrEnvUpInput): Promise<PrEnvRunResult> {
|
|
192
|
+
const { pr, ports } = input;
|
|
193
|
+
const env = prEnvironmentName(pr);
|
|
194
|
+
const target: PrEnvTarget = {
|
|
195
|
+
pr,
|
|
196
|
+
env,
|
|
197
|
+
template: input.template,
|
|
198
|
+
marker: { stack: input.stack, env },
|
|
199
|
+
head: input.head,
|
|
200
|
+
...(input.member ? { member: input.member } : {}),
|
|
201
|
+
op: prEnvOp(pr, input.member),
|
|
202
|
+
...(input.endpoint ? { endpoint: input.endpoint } : {}),
|
|
203
|
+
};
|
|
204
|
+
const base: PrEnvReport = {
|
|
205
|
+
stage: "up",
|
|
206
|
+
pr,
|
|
207
|
+
env,
|
|
208
|
+
...(input.member ? { member: input.member } : {}),
|
|
209
|
+
template: input.template,
|
|
210
|
+
head: input.head,
|
|
211
|
+
op: target.op,
|
|
212
|
+
gate: PR_ENV_GATE,
|
|
213
|
+
digest: "",
|
|
214
|
+
status: "nothing",
|
|
215
|
+
...(input.endpoint ? { endpoint: input.endpoint } : {}),
|
|
216
|
+
components: [],
|
|
217
|
+
};
|
|
218
|
+
const settle = async (report: PrEnvReport, exit: number, record?: PrEnvRecord): Promise<PrEnvRunResult> => {
|
|
219
|
+
await ports.publish?.(report);
|
|
220
|
+
return { exit, report, ...(record ? { record } : {}) };
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
let planned: PrEnvPlanned;
|
|
224
|
+
try {
|
|
225
|
+
planned = await input.adapter.plan(target);
|
|
226
|
+
} catch (err) {
|
|
227
|
+
return settle({ ...base, status: "plan-failed", message: messageOf(err) }, PR_ENV_EXIT.failed);
|
|
228
|
+
}
|
|
229
|
+
if (planned.rows.length === 0) {
|
|
230
|
+
ports.log?.(`pr-env up: nothing this change touches is deployed, so ${env} gets nothing`);
|
|
231
|
+
return settle({ ...base, ...(planned.message ? { message: planned.message } : {}) }, PR_ENV_EXIT.done);
|
|
232
|
+
}
|
|
233
|
+
const digest = planned.digest;
|
|
234
|
+
if (planned.failed) {
|
|
235
|
+
return settle({ ...base, digest, status: "plan-failed", ...(planned.message ? { message: planned.message } : {}), components: planned.rows }, PR_ENV_EXIT.failed);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// ws-080, ws-104: a non-empty plan applies only under a person's approval of its digest. An empty one passes.
|
|
239
|
+
let approvedBy: string[] | undefined;
|
|
240
|
+
if (!planned.empty) {
|
|
241
|
+
let approval: PrEnvApproval;
|
|
242
|
+
try {
|
|
243
|
+
approval = await ports.decideGate({ op: target.op, gate: PR_ENV_GATE, digest, description: `${env}: ${planned.rows.map((r) => r.component).join(", ")}` });
|
|
244
|
+
} catch (err) {
|
|
245
|
+
return settle({ ...base, digest, status: "failed", message: `could not read the gate ledger, so ${env} does not apply: ${messageOf(err)}`, components: planned.rows }, PR_ENV_EXIT.failed);
|
|
246
|
+
}
|
|
247
|
+
if (approval.status !== "approved") {
|
|
248
|
+
ports.log?.(`pr-env up: ${env} waits for an approval of ${digest}`);
|
|
249
|
+
return settle({ ...base, digest, status: approval.status === "changed" ? "changed" : "waiting", components: planned.rows }, PR_ENV_EXIT.waiting);
|
|
250
|
+
}
|
|
251
|
+
approvedBy = approval.approvedBy ?? [];
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
let applied: PrEnvApplied;
|
|
255
|
+
try {
|
|
256
|
+
applied = await input.adapter.apply(target, { planned, ...(approvedBy ? { approvedBy } : {}) });
|
|
257
|
+
} catch (err) {
|
|
258
|
+
applied = { status: "failed", rows: planned.rows, message: messageOf(err) };
|
|
259
|
+
}
|
|
260
|
+
if (applied.status === "stopped") {
|
|
261
|
+
return settle({ ...base, digest, status: "failed", components: applied.rows, ...(approvedBy ? { approvedBy } : {}), ...(applied.message ? { message: applied.message } : {}) }, PR_ENV_EXIT.failed);
|
|
262
|
+
}
|
|
263
|
+
const ok = applied.status === "applied";
|
|
264
|
+
const now = (input.now ?? (() => new Date()))();
|
|
265
|
+
const expiresAt = new Date(now.getTime() + ttlOf(input.ttl)).toISOString();
|
|
266
|
+
const record: PrEnvRecord = {
|
|
267
|
+
version: 1,
|
|
268
|
+
kind: "up",
|
|
269
|
+
pr,
|
|
270
|
+
env,
|
|
271
|
+
...(input.member ? { member: input.member } : {}),
|
|
272
|
+
template: input.template,
|
|
273
|
+
marker: { ...target.marker },
|
|
274
|
+
commit: input.head,
|
|
275
|
+
components: planned.rows.map((r) => r.component),
|
|
276
|
+
...(applied.locations && Object.keys(applied.locations).length > 0 ? { locations: { ...applied.locations } } : {}),
|
|
277
|
+
planDigest: digest,
|
|
278
|
+
result: ok ? "applied" : "failed",
|
|
279
|
+
by: input.actor ?? "chant",
|
|
280
|
+
timestamp: now.toISOString(),
|
|
281
|
+
expiresAt,
|
|
282
|
+
...(approvedBy ? { approvedBy } : {}),
|
|
283
|
+
releaseTarget: false,
|
|
284
|
+
...(input.runUrl ? { runUrl: input.runUrl } : {}),
|
|
285
|
+
};
|
|
286
|
+
// A copy that failed part way is live too: what applied is torn down on close or at expiry like the rest.
|
|
287
|
+
await ports.appendRecord(record);
|
|
288
|
+
const message = ok
|
|
289
|
+
? applied.afterFailure ?? applied.message
|
|
290
|
+
: applied.message ?? (applied.status === "waiting" ? "A component read an upstream output that moved when it applied; run the job again to plan it against the new value." : "A component failed to apply; what applied stays in the environment and is torn down with it.");
|
|
291
|
+
ports.log?.(`pr-env up: ${env} ${ok ? "applied" : applied.status}; it expires ${expiresAt}`);
|
|
292
|
+
return settle(
|
|
293
|
+
{ ...base, digest, status: ok ? "applied" : "failed", components: applied.rows, ...(approvedBy ? { approvedBy } : {}), expiresAt, ...(message ? { message } : {}) },
|
|
294
|
+
ok && !applied.afterFailure ? PR_ENV_EXIT.done : PR_ENV_EXIT.failed,
|
|
295
|
+
record,
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// ── down ───────────────────────────────────────────────────────────────────
|
|
300
|
+
|
|
301
|
+
/** What tears an environment down: the lexicons, and the adapters a product supplies. */
|
|
302
|
+
export interface PrEnvTeardownSources {
|
|
303
|
+
/** The project's config, for its declared environments and stacks. */
|
|
304
|
+
config?: ChantConfig;
|
|
305
|
+
plugins?: ObservationLexicon[];
|
|
306
|
+
adapters?: PrEnvTeardownAdapter[];
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export interface PrEnvDownInput extends PrEnvTeardownSources {
|
|
310
|
+
pr: number;
|
|
311
|
+
reason: "closed" | "expired";
|
|
312
|
+
member?: string;
|
|
313
|
+
ports: PrEnvPorts;
|
|
314
|
+
/** The environment as the records say it stands. Default: read through `ports`. */
|
|
315
|
+
live?: LivePrEnvironment;
|
|
316
|
+
/** The commit the teardown runs at, for the record. */
|
|
317
|
+
commit: string;
|
|
318
|
+
actor?: string;
|
|
319
|
+
runUrl?: string;
|
|
320
|
+
now?: () => Date;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Tear down pull request `pr`'s environment: the ownership check first, then
|
|
325
|
+
* only what carries `{ stack, env: "pr-<n>" }`, through the lexicons and the
|
|
326
|
+
* adapters. No gate holds it: closing the pull request, or the TTL the base
|
|
327
|
+
* set, is the decision. A teardown that left something keeps the
|
|
328
|
+
* environment live for the next sweep.
|
|
329
|
+
*/
|
|
330
|
+
export async function prEnvDown(input: PrEnvDownInput): Promise<PrEnvRunResult> {
|
|
331
|
+
const { pr, ports, reason } = input;
|
|
332
|
+
const env = prEnvironmentName(pr);
|
|
333
|
+
const live = input.live ?? livePrEnvironments(await ports.readRecords()).get(env);
|
|
334
|
+
const report: PrEnvReport = { stage: "down", pr, env, ...(input.member ? { member: input.member } : {}), template: live?.template ?? "", head: "", op: prEnvOp(pr, input.member), gate: PR_ENV_GATE, digest: "", status: "absent", components: [] };
|
|
335
|
+
if (!live) {
|
|
336
|
+
ports.log?.(`pr-env down: ${env} has no live record, so there is nothing to tear down`);
|
|
337
|
+
await ports.publish?.(report);
|
|
338
|
+
return { exit: PR_ENV_EXIT.done, report };
|
|
339
|
+
}
|
|
340
|
+
const torn = await tearDownPrEnvironment({ pr, live, ...(input.config ? { config: input.config } : {}), ...(input.plugins ? { plugins: input.plugins } : {}), ...(input.adapters ? { adapters: input.adapters } : {}), ...(ports.warn ? { warn: ports.warn } : {}) });
|
|
341
|
+
if ("refusal" in torn) {
|
|
342
|
+
const refused = { ...report, template: live.template, status: "refused" as const, message: torn.refusal };
|
|
343
|
+
await ports.publish?.(refused);
|
|
344
|
+
return { exit: PR_ENV_EXIT.failed, report: refused };
|
|
345
|
+
}
|
|
346
|
+
const { report: result, dropped, marker } = torn;
|
|
347
|
+
const counts = teardownCounts(result);
|
|
348
|
+
const complete = teardownComplete(result);
|
|
349
|
+
const now = (input.now ?? (() => new Date()))();
|
|
350
|
+
const record: PrEnvRecord = {
|
|
351
|
+
version: 1,
|
|
352
|
+
kind: "down",
|
|
353
|
+
pr,
|
|
354
|
+
env,
|
|
355
|
+
...(live.member ? { member: live.member } : {}),
|
|
356
|
+
template: live.template,
|
|
357
|
+
marker,
|
|
358
|
+
commit: input.commit,
|
|
359
|
+
components: [...new Set(result.outcomes.map((o) => o.lexicon))].sort(),
|
|
360
|
+
planDigest: "",
|
|
361
|
+
result: complete ? "destroyed" : "incomplete",
|
|
362
|
+
by: input.actor ?? "chant",
|
|
363
|
+
timestamp: now.toISOString(),
|
|
364
|
+
reason,
|
|
365
|
+
outcomes: counts,
|
|
366
|
+
releaseTarget: false,
|
|
367
|
+
...(input.runUrl ? { runUrl: input.runUrl } : {}),
|
|
368
|
+
};
|
|
369
|
+
await ports.appendRecord(record);
|
|
370
|
+
const why: string[] = [];
|
|
371
|
+
if (dropped > 0) why.push(`${dropped} candidate${dropped === 1 ? "" : "s"} whose marker was not ${marker.stack}/${env} left alone`);
|
|
372
|
+
if (result.unimplemented.length > 0) why.push(`no teardown execution in ${result.unimplemented.join(", ")}`);
|
|
373
|
+
if (result.plan.holes.length > 0) why.push(`${result.plan.holes.length} part${result.plan.holes.length === 1 ? "" : "s"} could not be read`);
|
|
374
|
+
for (const o of result.outcomes.filter((x) => x.outcome === "failed")) why.push(`${o.lexicon} ${o.name}: ${o.detail ?? "failed"}`);
|
|
375
|
+
const done: PrEnvReport = { ...report, template: live.template, status: complete ? "destroyed" : "incomplete", outcomes: counts, ...(why.length > 0 ? { message: why.join("; ") } : {}) };
|
|
376
|
+
ports.log?.(`pr-env down: ${env} ${complete ? "torn down" : "not wholly torn down; it stays live and the sweep tries again"} (${counts.deleted} deleted, ${counts.failed} failed, ${counts.skipped} skipped)`);
|
|
377
|
+
await ports.publish?.(done);
|
|
378
|
+
return { exit: complete ? PR_ENV_EXIT.done : PR_ENV_EXIT.failed, report: done, record };
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// ── sweep ──────────────────────────────────────────────────────────────────
|
|
382
|
+
|
|
383
|
+
export interface PrEnvSweepInput extends PrEnvTeardownSources {
|
|
384
|
+
ports: PrEnvPorts;
|
|
385
|
+
/** Only this member's environments. Default: every environment the records name. */
|
|
386
|
+
member?: string;
|
|
387
|
+
commit: string;
|
|
388
|
+
actor?: string;
|
|
389
|
+
runUrl?: string;
|
|
390
|
+
now?: () => Date;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Tear down each live environment whose TTL passed, or whose pull request
|
|
395
|
+
* the forge says is closed or merged. A state that cannot be read leaves the
|
|
396
|
+
* TTL to decide. Exits 1 when any teardown did not finish.
|
|
397
|
+
*/
|
|
398
|
+
export async function prEnvSweep(input: PrEnvSweepInput): Promise<{ exit: number; results: PrEnvRunResult[] }> {
|
|
399
|
+
const { ports } = input;
|
|
400
|
+
const live = [...livePrEnvironments(await ports.readRecords()).values()].filter((e) => input.member === undefined || e.member === input.member);
|
|
401
|
+
if (live.length === 0) {
|
|
402
|
+
ports.log?.("pr-env sweep: no pull request environment is live");
|
|
403
|
+
return { exit: PR_ENV_EXIT.done, results: [] };
|
|
404
|
+
}
|
|
405
|
+
const now = (input.now ?? (() => new Date()))();
|
|
406
|
+
let exit: number = PR_ENV_EXIT.done;
|
|
407
|
+
const results: PrEnvRunResult[] = [];
|
|
408
|
+
for (const e of live) {
|
|
409
|
+
let reason: "closed" | "expired" | undefined = expired(e, now) ? "expired" : undefined;
|
|
410
|
+
if (!reason && ports.pullRequestState) {
|
|
411
|
+
try {
|
|
412
|
+
if ((await ports.pullRequestState(e.pr)) !== "open") reason = "closed";
|
|
413
|
+
} catch (err) {
|
|
414
|
+
ports.warn?.(`could not read whether #${e.pr} is open (${messageOf(err)}); its TTL still holds`);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
if (!reason) {
|
|
418
|
+
ports.log?.(`pr-env sweep: ${e.env} stays; it expires ${e.expiresAt}`);
|
|
419
|
+
continue;
|
|
420
|
+
}
|
|
421
|
+
const r = await prEnvDown({ ...input, pr: e.pr, reason, live: e, ...(e.member ? { member: e.member } : {}) });
|
|
422
|
+
results.push(r);
|
|
423
|
+
if (r.exit !== PR_ENV_EXIT.done) exit = PR_ENV_EXIT.failed;
|
|
424
|
+
}
|
|
425
|
+
return { exit, results };
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// ── the ports chant itself uses ────────────────────────────────────────────
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* The note and the `chant/pr-env` status on the forge. A refusal from the
|
|
432
|
+
* forge warns and never fails the run.
|
|
433
|
+
*/
|
|
434
|
+
export function forgePrEnvPublisher(forge: PrForge, warn: (message: string) => void = () => {}): (report: PrEnvReport) => Promise<void> {
|
|
435
|
+
return async (report) => {
|
|
436
|
+
const { forgePrincipalOf } = await import("../pr-forge");
|
|
437
|
+
const note = renderPrEnvNote(report, forgePrincipalOf(forge.kind, forge.host, "<your-login>"));
|
|
438
|
+
if (report.head) {
|
|
439
|
+
try {
|
|
440
|
+
await forge.setStatus(report.head, { context: prEnvStatusContext(report.member), state: prEnvStatusState(report), description: prEnvStatusDescription(report), ...(forge.runUrl ? { url: forge.runUrl } : {}) });
|
|
441
|
+
} catch (err) {
|
|
442
|
+
warn(`could not set the ${prEnvStatusContext(report.member)} status: ${messageOf(err)}`);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
try {
|
|
446
|
+
await forge.upsertNote(report.pr, prEnvNoteMarker(report.env, report.member), note);
|
|
447
|
+
} catch (err) {
|
|
448
|
+
warn(`could not post the note on #${report.pr}: ${messageOf(err)}`);
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* The ports over chant/lifecycle in `cwd` (default the working directory):
|
|
455
|
+
* the records at `_pr-envs/records.jsonl`, appended and pushed, and the gate
|
|
456
|
+
* ledger read without writing a pending fact. With `forge`, the note and
|
|
457
|
+
* status are posted and the sweep asks it whether a pull request is open.
|
|
458
|
+
*/
|
|
459
|
+
export function lifecyclePrEnvPorts(opts: { cwd?: string; forge?: PrForge; log?: (line: string) => void; warn?: (message: string) => void } = {}): PrEnvPorts {
|
|
460
|
+
const at = opts.cwd ? { cwd: opts.cwd } : undefined;
|
|
461
|
+
const warn = opts.warn ?? (() => {});
|
|
462
|
+
const forge = opts.forge;
|
|
463
|
+
return {
|
|
464
|
+
async readRecords() {
|
|
465
|
+
const { fetchLifecycle, readBlobFromPath } = await import("../lifecycle/git");
|
|
466
|
+
await fetchLifecycle(at).catch(() => false);
|
|
467
|
+
return parsePrEnvRecords(await readBlobFromPath(PR_ENV_LEDGER_DIR, PR_ENV_LEDGER_FILE, at));
|
|
468
|
+
},
|
|
469
|
+
async appendRecord(record) {
|
|
470
|
+
const { appendLedgerLine, fetchLifecycle, pushLifecycleReport } = await import("../lifecycle/git");
|
|
471
|
+
try {
|
|
472
|
+
await fetchLifecycle(at).catch(() => false);
|
|
473
|
+
await appendLedgerLine(PR_ENV_LEDGER_DIR, PR_ENV_LEDGER_FILE, JSON.stringify(record), `Pull request environment ${record.env} ${record.kind} (${record.result})`, at);
|
|
474
|
+
const pushed = await pushLifecycleReport(at);
|
|
475
|
+
if (pushed.pushWarning) warn(pushed.pushWarning);
|
|
476
|
+
} catch (err) {
|
|
477
|
+
warn(`could not record ${record.env}'s ${record.kind} on chant/lifecycle: ${messageOf(err)}`);
|
|
478
|
+
}
|
|
479
|
+
},
|
|
480
|
+
async decideGate(input) {
|
|
481
|
+
const { approvalOf, decidePrGate, readOnlyLedger } = await import("./pr-run");
|
|
482
|
+
const { gitGateLedgerPort } = await import("../op/gate");
|
|
483
|
+
const approval = approvalOf(await decidePrGate(readOnlyLedger(gitGateLedgerPort(at)), input));
|
|
484
|
+
return { status: approval.status, ...(approval.approvedBy ? { approvedBy: approval.approvedBy } : {}) };
|
|
485
|
+
},
|
|
486
|
+
...(forge ? { publish: forgePrEnvPublisher(forge, warn) } : {}),
|
|
487
|
+
...(forge?.pullRequestState ? { pullRequestState: (pr: number) => forge.pullRequestState!(pr) } : {}),
|
|
488
|
+
...(opts.log ? { log: opts.log } : {}),
|
|
489
|
+
warn,
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// ── a teardown command ─────────────────────────────────────────────────────
|
|
494
|
+
|
|
495
|
+
/** `ci.prEnvironment.teardown` (#4330): a command that tears down what no lexicon does. */
|
|
496
|
+
export interface PrEnvTeardownCommand {
|
|
497
|
+
/** The name its outcomes are attributed to. Default `teardown`. */
|
|
498
|
+
name?: string;
|
|
499
|
+
/** Argv. `{env}`, `{pr}` and `{stage}` are filled in. */
|
|
500
|
+
command: string[];
|
|
501
|
+
/** The lexicons it tears down in place of, as {@link PrEnvTeardownAdapter.lexicons}. */
|
|
502
|
+
lexicons?: string[];
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/** The variables a teardown command reads. */
|
|
506
|
+
export const PR_ENV_TEARDOWN_ENV = {
|
|
507
|
+
/** `owned` or `teardown`. */
|
|
508
|
+
stage: "CHANT_PR_ENV_STAGE",
|
|
509
|
+
/** `pr-<n>`. */
|
|
510
|
+
env: "CHANT_PR_ENV",
|
|
511
|
+
pr: "CHANT_PR_NUMBER",
|
|
512
|
+
stack: "CHANT_PR_ENV_STACK",
|
|
513
|
+
template: "CHANT_PR_ENV_TEMPLATE",
|
|
514
|
+
/** The commit last applied to the environment. */
|
|
515
|
+
commit: "CHANT_PR_ENV_COMMIT",
|
|
516
|
+
/** A JSON file: `{ components, locations }` from the records. */
|
|
517
|
+
live: "CHANT_PR_ENV_LIVE",
|
|
518
|
+
/** `teardown` only: a JSON file holding the candidates to delete. */
|
|
519
|
+
candidates: "CHANT_PR_ENV_CANDIDATES",
|
|
520
|
+
/** The JSON file the command writes its answer to. */
|
|
521
|
+
result: "CHANT_PR_ENV_RESULT",
|
|
522
|
+
} as const;
|
|
523
|
+
|
|
524
|
+
/** Runs one argv in `cwd` with `env` as its whole environment; returns its exit code. Injected so tests run no processes. */
|
|
525
|
+
export type PrEnvTeardownExec = (argv: string[], cwd: string, env: Record<string, string>) => number;
|
|
526
|
+
|
|
527
|
+
const spawnTeardown: PrEnvTeardownExec = (argv, cwd, env) => {
|
|
528
|
+
const r = spawnSync(argv[0]!, argv.slice(1), { cwd, env, stdio: ["ignore", "inherit", "inherit"] });
|
|
529
|
+
if (r.error) throw r.error;
|
|
530
|
+
return r.status ?? 1;
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
const MARKER_OK = (m: unknown): m is { stack: string; env: string } => typeof m === "object" && m !== null && typeof (m as { stack?: unknown }).stack === "string" && typeof (m as { env?: unknown }).env === "string";
|
|
534
|
+
|
|
535
|
+
/** The candidates and holes a teardown command wrote, checked as data. Throws when they do not read. */
|
|
536
|
+
export function readTeardownEnumeration(raw: unknown): { candidates: TeardownCandidate[]; holes: TeardownHole[] } {
|
|
537
|
+
const doc = raw as { candidates?: unknown; holes?: unknown } | null;
|
|
538
|
+
if (typeof doc !== "object" || doc === null || !Array.isArray(doc.candidates)) throw new Error("the teardown command's owned stage wrote no { candidates: [...] }");
|
|
539
|
+
const candidates: TeardownCandidate[] = [];
|
|
540
|
+
for (const c of doc.candidates as Array<Record<string, unknown>>) {
|
|
541
|
+
if (typeof c?.name !== "string" || typeof c.type !== "string" || !MARKER_OK(c.marker)) throw new Error("each candidate needs a name, a type and the marker { stack, env } read back from it");
|
|
542
|
+
candidates.push({ name: c.name, type: c.type, ...(typeof c.physicalId === "string" ? { physicalId: c.physicalId } : {}), marker: { stack: c.marker.stack, env: c.marker.env } });
|
|
543
|
+
}
|
|
544
|
+
const holes: TeardownHole[] = [];
|
|
545
|
+
for (const h of Array.isArray(doc.holes) ? (doc.holes as Array<Record<string, unknown>>) : []) {
|
|
546
|
+
if (typeof h?.name !== "string") continue;
|
|
547
|
+
holes.push({ name: h.name, reason: "read-failed", ...(typeof h.type === "string" ? { type: h.type } : {}), ...(typeof h.detail === "string" ? { detail: h.detail } : {}) });
|
|
548
|
+
}
|
|
549
|
+
return { candidates, holes };
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/** The outcomes a teardown command wrote, checked as data. A candidate it leaves out counts as failed in core. */
|
|
553
|
+
export function readTeardownOutcomes(raw: unknown): TeardownOutcome[] {
|
|
554
|
+
const doc = raw as { outcomes?: unknown } | null;
|
|
555
|
+
if (typeof doc !== "object" || doc === null || !Array.isArray(doc.outcomes)) throw new Error("the teardown command's teardown stage wrote no { outcomes: [...] }");
|
|
556
|
+
const kinds = new Set(["deleted", "failed", "not-prunable", "retained"]);
|
|
557
|
+
return (doc.outcomes as Array<Record<string, unknown>>)
|
|
558
|
+
.filter((o) => typeof o?.name === "string" && typeof o.outcome === "string" && kinds.has(o.outcome))
|
|
559
|
+
.map((o) => ({ name: o.name as string, outcome: o.outcome as TeardownOutcome["outcome"], ...(typeof o.detail === "string" ? { detail: o.detail } : {}) }));
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* `ci.prEnvironment.teardown` as an adapter (#4330). chant runs the command
|
|
564
|
+
* twice, in `cwd`, with the job's environment less every forge token
|
|
565
|
+
* variable, and these besides ({@link PR_ENV_TEARDOWN_ENV}):
|
|
566
|
+
*
|
|
567
|
+
* - `CHANT_PR_ENV_STAGE=owned`: the command writes `{ candidates, holes }`
|
|
568
|
+
* to `CHANT_PR_ENV_RESULT`, each candidate with the marker it read back.
|
|
569
|
+
* - `CHANT_PR_ENV_STAGE=teardown`: it deletes the candidates in
|
|
570
|
+
* `CHANT_PR_ENV_CANDIDATES`, which chant has checked against the marker,
|
|
571
|
+
* and writes `{ outcomes }` to `CHANT_PR_ENV_RESULT`.
|
|
572
|
+
*
|
|
573
|
+
* A non-zero exit, or an answer that does not read, is a hole (`owned`) or a
|
|
574
|
+
* failure for every candidate (`teardown`), so the environment stays live.
|
|
575
|
+
*/
|
|
576
|
+
export function commandTeardownAdapter(hook: PrEnvTeardownCommand, opts: { cwd: string; env?: Record<string, string | undefined>; exec?: PrEnvTeardownExec }): PrEnvTeardownAdapter {
|
|
577
|
+
const exec = opts.exec ?? spawnTeardown;
|
|
578
|
+
const name = hook.name ?? "teardown";
|
|
579
|
+
const run = (stage: "owned" | "teardown", input: PrEnvTeardownAdapterInput, candidates?: TeardownCandidate[]): unknown => {
|
|
580
|
+
const dir = mkdtempSync(join(tmpdir(), "chant-pr-env-teardown-"));
|
|
581
|
+
try {
|
|
582
|
+
const result = join(dir, "result.json");
|
|
583
|
+
const liveFile = join(dir, "live.json");
|
|
584
|
+
writeFileSync(liveFile, JSON.stringify({ components: input.live.components, locations: input.live.locations ?? {} }));
|
|
585
|
+
const extra: Record<string, string> = {
|
|
586
|
+
[PR_ENV_TEARDOWN_ENV.stage]: stage,
|
|
587
|
+
[PR_ENV_TEARDOWN_ENV.env]: input.environment,
|
|
588
|
+
[PR_ENV_TEARDOWN_ENV.pr]: String(input.live.pr),
|
|
589
|
+
[PR_ENV_TEARDOWN_ENV.stack]: input.marker.stack,
|
|
590
|
+
[PR_ENV_TEARDOWN_ENV.template]: input.live.template,
|
|
591
|
+
[PR_ENV_TEARDOWN_ENV.commit]: input.live.commit,
|
|
592
|
+
[PR_ENV_TEARDOWN_ENV.live]: liveFile,
|
|
593
|
+
[PR_ENV_TEARDOWN_ENV.result]: result,
|
|
594
|
+
};
|
|
595
|
+
if (candidates) {
|
|
596
|
+
const file = join(dir, "candidates.json");
|
|
597
|
+
writeFileSync(file, JSON.stringify(candidates));
|
|
598
|
+
extra[PR_ENV_TEARDOWN_ENV.candidates] = file;
|
|
599
|
+
}
|
|
600
|
+
const base: Record<string, string> = {};
|
|
601
|
+
for (const [k, v] of Object.entries(withoutForgeTokens(opts.env ?? process.env))) if (v !== undefined) base[k] = v;
|
|
602
|
+
const argv = hook.command.map((a) => a.split("{env}").join(input.environment).split("{pr}").join(String(input.live.pr)).split("{stage}").join(stage));
|
|
603
|
+
const code = exec(argv, opts.cwd, { ...base, ...extra });
|
|
604
|
+
if (code !== 0) throw new Error(`${argv.join(" ")} exited ${code} in its ${stage} stage`);
|
|
605
|
+
let text: string;
|
|
606
|
+
try {
|
|
607
|
+
text = readFileSync(result, "utf-8");
|
|
608
|
+
} catch {
|
|
609
|
+
throw new Error(`${argv.join(" ")} wrote nothing to ${PR_ENV_TEARDOWN_ENV.result} in its ${stage} stage`);
|
|
610
|
+
}
|
|
611
|
+
return JSON.parse(text);
|
|
612
|
+
} finally {
|
|
613
|
+
rmSync(dir, { recursive: true, force: true });
|
|
614
|
+
}
|
|
615
|
+
};
|
|
616
|
+
return {
|
|
617
|
+
name,
|
|
618
|
+
...(hook.lexicons ? { lexicons: [...hook.lexicons] } : {}),
|
|
619
|
+
async owned(input) {
|
|
620
|
+
return readTeardownEnumeration(run("owned", input));
|
|
621
|
+
},
|
|
622
|
+
async teardown(input) {
|
|
623
|
+
try {
|
|
624
|
+
return { outcomes: readTeardownOutcomes(run("teardown", input, input.candidates)) };
|
|
625
|
+
} catch (err) {
|
|
626
|
+
return { outcomes: input.candidates.map((c) => ({ name: c.name, outcome: "failed" as const, detail: messageOf(err) })) };
|
|
627
|
+
}
|
|
628
|
+
},
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
function messageOf(err: unknown): string {
|
|
633
|
+
return err instanceof Error ? err.message : String(err);
|
|
634
|
+
}
|