@intentius/chant 0.124.76 → 0.124.78
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__/agent-guard/plywood.d.ts +8 -0
- package/dist/__fixtures__/agent-guard/plywood.d.ts.map +1 -0
- package/dist/__fixtures__/agent-guard/plywood.js +19 -0
- package/dist/__fixtures__/agent-guard/terragucci.d.ts +8 -0
- package/dist/__fixtures__/agent-guard/terragucci.d.ts.map +1 -0
- package/dist/__fixtures__/agent-guard/terragucci.js +30 -0
- package/dist/__fixtures__/gated-ledger.d.ts +10 -0
- package/dist/__fixtures__/gated-ledger.d.ts.map +1 -0
- package/dist/__fixtures__/gated-ledger.js +51 -0
- 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 +9 -0
- package/dist/agent-guard.d.ts +143 -0
- package/dist/agent-guard.d.ts.map +1 -0
- package/dist/agent-guard.js +455 -0
- package/dist/ci-commit.d.ts +120 -5
- package/dist/ci-commit.d.ts.map +1 -1
- package/dist/ci-commit.js +214 -23
- package/dist/ci-own-jobs.d.ts +81 -0
- package/dist/ci-own-jobs.d.ts.map +1 -0
- package/dist/ci-own-jobs.js +233 -0
- 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 +40 -1
- package/dist/ci-product.d.ts.map +1 -1
- package/dist/ci-product.js +40 -1
- package/dist/cli/handlers/approve.d.ts +5 -1
- package/dist/cli/handlers/approve.d.ts.map +1 -1
- package/dist/cli/handlers/approve.js +17 -11
- package/dist/cli/handlers/build.d.ts +1 -1
- package/dist/cli/handlers/build.d.ts.map +1 -1
- package/dist/cli/handlers/build.js +88 -2
- package/dist/cli/handlers/commit.d.ts.map +1 -1
- package/dist/cli/handlers/commit.js +3 -1
- package/dist/cli/handlers/notify-relay.d.ts +18 -1
- package/dist/cli/handlers/notify-relay.d.ts.map +1 -1
- package/dist/cli/handlers/notify-relay.js +90 -14
- package/dist/cli/handlers/pr.d.ts.map +1 -1
- package/dist/cli/handlers/pr.js +3 -1
- package/dist/cli/handlers/release.d.ts +5 -1
- package/dist/cli/handlers/release.d.ts.map +1 -1
- package/dist/cli/handlers/release.js +21 -9
- package/dist/cli/handlers/run-generate.d.ts.map +1 -1
- package/dist/cli/handlers/run-generate.js +2 -1
- package/dist/cli/main.d.ts.map +1 -1
- package/dist/cli/main.js +21 -1
- package/dist/cli/parse-args.d.ts.map +1 -1
- package/dist/cli/parse-args.js +60 -4
- package/dist/cli/registry.d.ts +16 -0
- package/dist/cli/registry.d.ts.map +1 -1
- package/dist/components/pr-env-components.d.ts +65 -0
- package/dist/components/pr-env-components.d.ts.map +1 -0
- package/dist/components/pr-env-components.js +162 -0
- package/dist/components/pr-policy.d.ts +15 -0
- package/dist/components/pr-policy.d.ts.map +1 -1
- package/dist/components/pr-policy.js +24 -1
- package/dist/components/release-compile.d.ts +15 -3
- package/dist/components/release-compile.d.ts.map +1 -1
- package/dist/components/release-compile.js +66 -15
- package/dist/config.d.ts +14 -8
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +16 -1
- package/dist/gate-policy.d.ts +14 -1
- package/dist/gate-policy.d.ts.map +1 -1
- package/dist/gate-policy.js +16 -6
- package/dist/lexicon.d.ts +25 -1
- package/dist/lexicon.d.ts.map +1 -1
- package/dist/lexicon.js +10 -0
- package/dist/lifecycle/gated-op.d.ts +95 -0
- package/dist/lifecycle/gated-op.d.ts.map +1 -0
- package/dist/lifecycle/gated-op.js +96 -0
- package/dist/lifecycle/git.d.ts.map +1 -1
- package/dist/lifecycle/git.js +83 -2
- package/dist/lifecycle/release-images.d.ts +77 -3
- package/dist/lifecycle/release-images.d.ts.map +1 -1
- package/dist/lifecycle/release-images.js +132 -6
- package/dist/notify-product.d.ts +11 -2
- package/dist/notify-product.d.ts.map +1 -1
- package/dist/notify-product.js +16 -4
- package/dist/notify-relay-repo.d.ts +131 -0
- package/dist/notify-relay-repo.d.ts.map +1 -0
- package/dist/notify-relay-repo.js +202 -0
- package/dist/notify-relay.d.ts +25 -1
- package/dist/notify-relay.d.ts.map +1 -1
- package/dist/notify-relay.js +27 -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 +116 -0
- package/dist/op/op-waves-capability.js +2 -2
- package/dist/policy-engine.d.ts +95 -0
- package/dist/policy-engine.d.ts.map +1 -0
- package/dist/policy-engine.js +157 -0
- 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 +362 -0
- package/dist/release-floors.d.ts +9 -1
- package/dist/release-floors.d.ts.map +1 -1
- package/dist/release-floors.js +9 -1
- package/dist/state-export.d.ts +126 -0
- package/dist/state-export.d.ts.map +1 -0
- package/dist/state-export.js +156 -0
- package/dist/state-migration.d.ts +148 -0
- package/dist/state-migration.d.ts.map +1 -0
- package/dist/state-migration.js +146 -0
- package/dist/state-unlock.d.ts +167 -0
- package/dist/state-unlock.d.ts.map +1 -0
- package/dist/state-unlock.js +196 -0
- package/dist/wave-runtime.mjs +332 -332
- package/dist/workspace/declaration.schema.json +1 -1
- package/dist/workspace/release-workflow.d.ts +10 -0
- package/dist/workspace/release-workflow.d.ts.map +1 -1
- package/dist/workspace/release-workflow.js +10 -4
- package/package.json +1 -1
- package/src/__fixtures__/agent-guard/plywood.ts +27 -0
- package/src/__fixtures__/agent-guard/terragucci.ts +39 -0
- package/src/__fixtures__/gated-ledger.ts +58 -0
- 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 +12 -1
- package/src/action-pins.ts +11 -0
- package/src/agent-guard.test.ts +319 -0
- package/src/agent-guard.ts +547 -0
- package/src/ci-commit.test.ts +213 -2
- package/src/ci-commit.ts +258 -27
- package/src/ci-own-jobs.test.ts +108 -0
- package/src/ci-own-jobs.ts +252 -0
- package/src/ci-product-words.ts +35 -7
- package/src/ci-product.ts +81 -1
- package/src/cli/handlers/approve-cwd.test.ts +112 -0
- package/src/cli/handlers/approve.ts +22 -11
- package/src/cli/handlers/build-own-jobs.test.ts +80 -0
- package/src/cli/handlers/build-pr-loop-control.test.ts +87 -0
- package/src/cli/handlers/build-run-group.test.ts +59 -0
- package/src/cli/handlers/build.ts +83 -3
- package/src/cli/handlers/commit.ts +3 -0
- package/src/cli/handlers/notify-relay.ts +99 -17
- package/src/cli/handlers/pr.ts +3 -1
- package/src/cli/handlers/release.ts +20 -10
- package/src/cli/handlers/run-generate.ts +2 -1
- package/src/cli/main.ts +21 -1
- package/src/cli/parse-args.ts +45 -3
- package/src/cli/registry.ts +16 -0
- package/src/components/pr-env-components.test.ts +137 -0
- package/src/components/pr-env-components.ts +213 -0
- package/src/components/pr-policy.test.ts +41 -1
- package/src/components/pr-policy.ts +25 -1
- package/src/components/release-compile.test.ts +47 -0
- package/src/components/release-compile.ts +70 -13
- package/src/config.ts +23 -3
- package/src/gate-policy-bundle.test.ts +14 -0
- package/src/gate-policy.test.ts +7 -0
- package/src/gate-policy.ts +22 -7
- package/src/lexicon.ts +28 -1
- package/src/lifecycle/gated-op.ts +158 -0
- package/src/lifecycle/git.test.ts +92 -0
- package/src/lifecycle/git.ts +71 -2
- package/src/lifecycle/release-images-dockerfile.test.ts +221 -0
- package/src/lifecycle/release-images.ts +167 -8
- package/src/notify-message-words.test.ts +186 -0
- package/src/notify-product.ts +17 -4
- package/src/notify-relay-repo.test.ts +284 -0
- package/src/notify-relay-repo.ts +234 -0
- package/src/notify-relay.test.ts +12 -0
- package/src/notify-relay.ts +29 -3
- 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 +200 -0
- package/src/op/op-waves-capability.ts +2 -2
- package/src/op/schedule-overdue.test.ts +26 -0
- package/src/policy-engine.test.ts +162 -0
- package/src/policy-engine.ts +200 -0
- 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 +521 -0
- package/src/release-floors.ts +13 -1
- package/src/state-export.test.ts +124 -0
- package/src/state-export.ts +227 -0
- package/src/state-migration.test.ts +133 -0
- package/src/state-migration.ts +236 -0
- package/src/state-unlock.test.ts +165 -0
- package/src/state-unlock.ts +293 -0
- package/src/workspace/declaration.schema.json +1 -1
- package/src/workspace/release-workflow.test.ts +34 -0
- package/src/workspace/release-workflow.ts +19 -4
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A repository's own jobs in a pipeline file chant generates (#4769).
|
|
3
|
+
*
|
|
4
|
+
* chant's generators write the whole file, so a job a repository adds by
|
|
5
|
+
* hand is gone at the next `chant build`. Two ways keep it:
|
|
6
|
+
*
|
|
7
|
+
* - a jobs file, or a map of jobs, merged after chant's jobs
|
|
8
|
+
* ({@link mergeOwnJobs}, `chant build --own-jobs <file>`). The file is in
|
|
9
|
+
* the forge's own syntax: a map of job name to job. A name chant's file
|
|
10
|
+
* already gives a job is refused, and on GitLab a top-level keyword too.
|
|
11
|
+
* terragucci's `own_jobs` works this way.
|
|
12
|
+
* - the jobs of the old file that the new one lacks, put back as they were
|
|
13
|
+
* written, comments and all ({@link keepOwnJobs}, `chant build
|
|
14
|
+
* --keep-own-jobs`). A job that runs `chant` or the product's bin counts as
|
|
15
|
+
* generated and is not kept. plywood's `init` and `ci` work this way.
|
|
16
|
+
*
|
|
17
|
+
* Either way the jobs go after chant's, under one comment line: inside
|
|
18
|
+
* `jobs:` at two spaces on GitHub and Forgejo, and at the top level on
|
|
19
|
+
* GitLab. The blocks are read as text by their indentation, so a kept job
|
|
20
|
+
* keeps its formatting. A file with no own jobs is returned unchanged.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { readFileSync } from "node:fs";
|
|
24
|
+
import { emitYAMLEntry, parseYAMLDocument } from "./yaml";
|
|
25
|
+
|
|
26
|
+
/** The first chant with `mergeOwnJobs`, `keepOwnJobs` and `chant build --own-jobs` / `--keep-own-jobs` (#4769). Products feature-detect on it. */
|
|
27
|
+
export const OWN_JOBS_FLOOR = "0.124.77";
|
|
28
|
+
|
|
29
|
+
/** The forges whose pipeline files take own jobs. */
|
|
30
|
+
export type OwnJobsForge = "github" | "gitlab" | "forgejo";
|
|
31
|
+
export const OWN_JOBS_FORGES: readonly OwnJobsForge[] = ["github", "gitlab", "forgejo"];
|
|
32
|
+
|
|
33
|
+
/** The default line above the own jobs. A product passes its own as `line`. */
|
|
34
|
+
export const OWN_JOBS_LINE = "# Your own jobs, kept as they are when chant writes this file again.";
|
|
35
|
+
|
|
36
|
+
/** GitLab's top-level keys that are not jobs: never read as a job, never kept. */
|
|
37
|
+
export const GITLAB_TOP_KEYWORDS: readonly string[] = ["default", "include", "stages", "variables", "workflow", "image", "services", "cache", "before_script", "after_script", "spec"];
|
|
38
|
+
|
|
39
|
+
/** The names an own job may not take on GitLab: the keywords, and `pages`, which GitLab reads as its Pages job. */
|
|
40
|
+
export const GITLAB_RESERVED_JOB_NAMES: readonly string[] = [...GITLAB_TOP_KEYWORDS, "pages"];
|
|
41
|
+
|
|
42
|
+
/** A job name every forge's YAML reads as a plain key. */
|
|
43
|
+
export const OWN_JOB_NAME = /^[A-Za-z_][A-Za-z0-9_-]*$/;
|
|
44
|
+
|
|
45
|
+
/** An own job refused: a collision, a reserved name, or a file that is not a map of jobs. */
|
|
46
|
+
export class OwnJobsError extends Error {
|
|
47
|
+
constructor(message: string) {
|
|
48
|
+
super(message);
|
|
49
|
+
this.name = "OwnJobsError";
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const KEY = /^([A-Za-z0-9_.-]+|"[^"]+"|'[^']+'):(\s|$)/;
|
|
54
|
+
const JOBS_LINE = /^jobs:\s*(#.*)?$/;
|
|
55
|
+
|
|
56
|
+
function indentOf(line: string): number {
|
|
57
|
+
return line.length - line.trimStart().length;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function isContent(line: string): boolean {
|
|
61
|
+
const t = line.trim();
|
|
62
|
+
return t !== "" && !t.startsWith("#");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function escapeRegExp(s: string): string {
|
|
66
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Each job's block of lines in `text`, by name, in file order. */
|
|
70
|
+
export function jobBlocks(text: string, forge: OwnJobsForge): Map<string, string> {
|
|
71
|
+
const lines = text.split("\n");
|
|
72
|
+
const out = new Map<string, string>();
|
|
73
|
+
let start = 0;
|
|
74
|
+
let end = lines.length;
|
|
75
|
+
let depth = 0;
|
|
76
|
+
if (forge !== "gitlab") {
|
|
77
|
+
const at = lines.findIndex((l) => JOBS_LINE.test(l));
|
|
78
|
+
if (at < 0) return out;
|
|
79
|
+
start = at + 1;
|
|
80
|
+
const next = lines.findIndex((l, i) => i > at && indentOf(l) === 0 && isContent(l));
|
|
81
|
+
end = next < 0 ? lines.length : next;
|
|
82
|
+
depth = 2;
|
|
83
|
+
}
|
|
84
|
+
let name: string | undefined;
|
|
85
|
+
let body: string[] = [];
|
|
86
|
+
const flush = (): void => {
|
|
87
|
+
if (name !== undefined) {
|
|
88
|
+
while (body.length > 0 && body[body.length - 1]!.trim() === "") body.pop();
|
|
89
|
+
out.set(name, body.join("\n"));
|
|
90
|
+
}
|
|
91
|
+
name = undefined;
|
|
92
|
+
body = [];
|
|
93
|
+
};
|
|
94
|
+
for (let i = start; i < end; i++) {
|
|
95
|
+
const line = lines[i]!;
|
|
96
|
+
if (isContent(line) && indentOf(line) === depth) {
|
|
97
|
+
const m = KEY.exec(line.slice(depth));
|
|
98
|
+
flush();
|
|
99
|
+
if (m) {
|
|
100
|
+
name = m[1]!.replace(/^["']|["']$/g, "");
|
|
101
|
+
body = [line];
|
|
102
|
+
}
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (isContent(line) && indentOf(line) < depth) {
|
|
106
|
+
flush();
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (name !== undefined) body.push(line);
|
|
110
|
+
}
|
|
111
|
+
flush();
|
|
112
|
+
if (forge === "gitlab") for (const k of [...out.keys()]) if (GITLAB_TOP_KEYWORDS.includes(k)) out.delete(k);
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** The job names of a pipeline file. */
|
|
117
|
+
export function jobNames(text: string, forge: OwnJobsForge): string[] {
|
|
118
|
+
return [...jobBlocks(text, forge).keys()];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** `generated` with `blocks` after chant's jobs, under `line`. */
|
|
122
|
+
function withBlocks(generated: string, blocks: string[], forge: OwnJobsForge, line: string): string {
|
|
123
|
+
if (blocks.length === 0) return generated;
|
|
124
|
+
const indent = forge === "gitlab" ? "" : " ";
|
|
125
|
+
const block = [`${indent}${line}`, ...blocks].join("\n");
|
|
126
|
+
const lines = generated.replace(/\n+$/, "").split("\n");
|
|
127
|
+
if (forge !== "gitlab") {
|
|
128
|
+
const at = lines.findIndex((l) => JOBS_LINE.test(l));
|
|
129
|
+
if (at >= 0) {
|
|
130
|
+
const next = lines.findIndex((l, i) => i > at && indentOf(l) === 0 && isContent(l));
|
|
131
|
+
if (next >= 0) {
|
|
132
|
+
let cut = next;
|
|
133
|
+
while (cut > at + 1 && lines[cut - 1]!.trim() === "") cut--;
|
|
134
|
+
return [...lines.slice(0, cut), block, ...lines.slice(cut)].join("\n") + "\n";
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return [...lines, block].join("\n") + "\n";
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** The problems with `own` as a map of job name to job; `where` names it in each. */
|
|
142
|
+
export function ownJobsProblems(own: unknown, where: string): string[] {
|
|
143
|
+
if (own === null || typeof own !== "object" || Array.isArray(own)) return [`${where} must be a map of job name to job`];
|
|
144
|
+
const problems: string[] = [];
|
|
145
|
+
for (const [name, job] of Object.entries(own as Record<string, unknown>)) {
|
|
146
|
+
if (!OWN_JOB_NAME.test(name)) problems.push(`${where}: ${JSON.stringify(name)} is not a job name; use letters, digits, "_" and "-"`);
|
|
147
|
+
else if (job === null || typeof job !== "object" || Array.isArray(job) || Object.keys(job).length === 0) problems.push(`${where}.${name} must be a job: a map of its keys, in the forge's own syntax`);
|
|
148
|
+
}
|
|
149
|
+
return problems;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** The map of jobs a YAML file holds. Throws {@link OwnJobsError} when it is missing, not YAML, or not a map of jobs. */
|
|
153
|
+
export function readOwnJobsFile(path: string, where = path): Record<string, Record<string, unknown>> {
|
|
154
|
+
let text: string;
|
|
155
|
+
try {
|
|
156
|
+
text = readFileSync(path, "utf-8");
|
|
157
|
+
} catch {
|
|
158
|
+
throw new OwnJobsError(`${where}: no such file`);
|
|
159
|
+
}
|
|
160
|
+
let doc: unknown;
|
|
161
|
+
try {
|
|
162
|
+
doc = parseYAMLDocument(text);
|
|
163
|
+
} catch (e) {
|
|
164
|
+
throw new OwnJobsError(`${where} is not YAML (${(e as Error).message})`);
|
|
165
|
+
}
|
|
166
|
+
const problems = ownJobsProblems(doc, where);
|
|
167
|
+
if (problems.length > 0) throw new OwnJobsError(problems.join("; "));
|
|
168
|
+
return doc as Record<string, Record<string, unknown>>;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export interface MergeOwnJobsOptions {
|
|
172
|
+
/** The line above the jobs. Default {@link OWN_JOBS_LINE}. */
|
|
173
|
+
line?: string;
|
|
174
|
+
/** What the jobs are called in a refusal, such as `own_jobs` or the file's path. Default `own jobs`. */
|
|
175
|
+
where?: string;
|
|
176
|
+
/** Names the generated file's jobs take, when the caller knows them; default: read from `generated`. */
|
|
177
|
+
generatedJobs?: readonly string[];
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* The YAML of `own`, as it goes after chant's jobs: {@link OWN_JOBS_LINE} (or
|
|
182
|
+
* `line`), then each job as it is. Empty for no jobs. Throws
|
|
183
|
+
* {@link OwnJobsError} on a name in `ours` or, on GitLab, a reserved one.
|
|
184
|
+
*/
|
|
185
|
+
export function ownJobsYAML(own: Record<string, unknown> | undefined, ours: readonly string[], forge: OwnJobsForge, options: Omit<MergeOwnJobsOptions, "generatedJobs"> = {}): string {
|
|
186
|
+
if (!own || Object.keys(own).length === 0) return "";
|
|
187
|
+
const where = options.where ?? "own jobs";
|
|
188
|
+
const problems = ownJobsProblems(own, where);
|
|
189
|
+
if (problems.length > 0) throw new OwnJobsError(problems.join("; "));
|
|
190
|
+
for (const name of Object.keys(own)) {
|
|
191
|
+
if (ours.includes(name)) throw new OwnJobsError(`${where}.${name}: chant's file has a job of that name; give yours another`);
|
|
192
|
+
if (forge === "gitlab" && GITLAB_RESERVED_JOB_NAMES.includes(name)) throw new OwnJobsError(`${where}.${name}: GitLab reads ${name} as a keyword, not a job; give yours another name`);
|
|
193
|
+
}
|
|
194
|
+
const indent = forge === "gitlab" ? 0 : 1;
|
|
195
|
+
return [`${forge === "gitlab" ? "" : " "}${options.line ?? OWN_JOBS_LINE}`, ...Object.entries(own).map(([name, job]) => emitYAMLEntry(name, job, indent))].join("\n") + "\n";
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** `generated` with `own` after chant's jobs (terragucci's `own_jobs` model). Unchanged for no jobs. */
|
|
199
|
+
export function mergeOwnJobs(generated: string, forge: OwnJobsForge, own: Record<string, unknown> | undefined, options: MergeOwnJobsOptions = {}): string {
|
|
200
|
+
if (!own || Object.keys(own).length === 0) return generated;
|
|
201
|
+
const ours = options.generatedJobs ?? jobNames(generated, forge);
|
|
202
|
+
const rendered = ownJobsYAML(own, ours, forge, options).replace(/\n$/, "");
|
|
203
|
+
const [line, ...rest] = rendered.split("\n");
|
|
204
|
+
// The rendered text starts with the indented line; withBlocks adds the indent itself.
|
|
205
|
+
return withBlocks(generated, [rest.join("\n")], forge, line!.trimStart());
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** The bins whose lines mark a job as generated: `chant`, and each of `bins` with its last word (`npx --no-install plywood` and `plywood`). */
|
|
209
|
+
function generatedLine(bins: readonly string[]): RegExp {
|
|
210
|
+
const names = new Set<string>(["chant"]);
|
|
211
|
+
for (const bin of bins) {
|
|
212
|
+
const b = bin.trim();
|
|
213
|
+
if (b === "") continue;
|
|
214
|
+
names.add(b);
|
|
215
|
+
names.add(b.split(/\s+/).pop()!.split("/").pop()!);
|
|
216
|
+
}
|
|
217
|
+
const alt = [...names].sort((a, b) => b.length - a.length).map(escapeRegExp).join("|");
|
|
218
|
+
return new RegExp(`(^|[\\s"'(]|\\$\\()(?:${alt}) (?=[a-z])`, "m");
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** Whether a job's block runs `chant` or one of `bins`: a job chant's generator wrote. */
|
|
222
|
+
export function isGeneratedJob(block: string, bins: readonly string[] = []): boolean {
|
|
223
|
+
return generatedLine(bins).test(block);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export interface KeepOwnJobsOptions {
|
|
227
|
+
/** The product's bin, such as `npx --no-install plywood`: a job that runs it is generated. `chant` always counts. */
|
|
228
|
+
bins?: readonly string[];
|
|
229
|
+
/** The line above the kept jobs. Default {@link OWN_JOBS_LINE}. */
|
|
230
|
+
line?: string;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** The jobs of `previous` that `generated` lacks and that run neither `chant` nor a bin of `bins`. */
|
|
234
|
+
export function ownJobsOf(previous: string, generated: string, forge: OwnJobsForge, bins: readonly string[] = []): Map<string, string> {
|
|
235
|
+
const ours = jobBlocks(generated, forge);
|
|
236
|
+
const out = new Map<string, string>();
|
|
237
|
+
for (const [name, block] of jobBlocks(previous, forge)) {
|
|
238
|
+
if (ours.has(name) || isGeneratedJob(block, bins)) continue;
|
|
239
|
+
out.set(name, block);
|
|
240
|
+
}
|
|
241
|
+
return out;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* The old file's own jobs carried into the new one (plywood's model):
|
|
246
|
+
* `previous` undefined means there was no file. `kept` names them.
|
|
247
|
+
*/
|
|
248
|
+
export function keepOwnJobs(previous: string | undefined, generated: string, forge: OwnJobsForge, options: KeepOwnJobsOptions = {}): { text: string; kept: string[] } {
|
|
249
|
+
if (previous === undefined) return { text: generated, kept: [] };
|
|
250
|
+
const own = ownJobsOf(previous, generated, forge, options.bins ?? []);
|
|
251
|
+
return { text: withBlocks(generated, [...own.values()], forge, options.line ?? OWN_JOBS_LINE), kept: [...own.keys()] };
|
|
252
|
+
}
|
package/src/ci-product-words.ts
CHANGED
|
@@ -19,10 +19,12 @@
|
|
|
19
19
|
* command's lifetime. Every renderer here reads {@link activeProductWords}
|
|
20
20
|
* when it is given none.
|
|
21
21
|
*
|
|
22
|
-
* This module imports nothing
|
|
23
|
-
* pr-loop-bundle.test.ts) carries it.
|
|
22
|
+
* This module imports nothing at run time (the one import is a type): the
|
|
23
|
+
* pull request loop bundle (pr-loop.ts, pr-loop-bundle.test.ts) carries it.
|
|
24
24
|
*/
|
|
25
25
|
|
|
26
|
+
import type { NotifyMessageWords } from "./notify";
|
|
27
|
+
|
|
26
28
|
/** The stages a product names: the status context after the prefix, and the word in the note. */
|
|
27
29
|
export type CiProductStage = "plan" | "apply" | "record" | "check" | "drift" | "lock" | "approval" | "control";
|
|
28
30
|
|
|
@@ -139,6 +141,8 @@ export interface CiProductNotifyWordsInput {
|
|
|
139
141
|
wave?: string;
|
|
140
142
|
/** The signed webhook's header prefix. Default `x-<product>`, so chant's stays `x-chant`. */
|
|
141
143
|
headers?: string;
|
|
144
|
+
/** The labels, re-run button and other words of chant's Slack and Teams messages (#4818). Default: chant's. */
|
|
145
|
+
message?: NotifyMessageWords;
|
|
142
146
|
}
|
|
143
147
|
|
|
144
148
|
/** The notify slots, filled. */
|
|
@@ -146,6 +150,7 @@ export interface CiProductNotifyWords {
|
|
|
146
150
|
source: string;
|
|
147
151
|
wave: string;
|
|
148
152
|
headers: string;
|
|
153
|
+
message?: NotifyMessageWords;
|
|
149
154
|
}
|
|
150
155
|
|
|
151
156
|
/** chant's own wave label in a notify summary. */
|
|
@@ -220,6 +225,7 @@ export function resolveProductWords(input: CiProductWordsInput | CiProductWords
|
|
|
220
225
|
source: input.notify?.source ?? product,
|
|
221
226
|
wave: input.notify?.wave ?? CHANT_NOTIFY_WAVE_TEMPLATE,
|
|
222
227
|
headers: input.notify?.headers ?? `x-${product}`,
|
|
228
|
+
...(input.notify?.message ? { message: input.notify.message } : {}),
|
|
223
229
|
},
|
|
224
230
|
};
|
|
225
231
|
}
|
|
@@ -465,7 +471,7 @@ export type ParsedProductComment =
|
|
|
465
471
|
*/
|
|
466
472
|
| { kind: "action"; member: string; address: string }
|
|
467
473
|
/** A commit job's command (#4722), by the word typed: `/terragucci fmt`. Several words may each map onto `commit`. */
|
|
468
|
-
| { kind: "commit"; word: string }
|
|
474
|
+
| { kind: "commit"; word: string; arg?: string }
|
|
469
475
|
| { kind: "refused"; reason: string };
|
|
470
476
|
|
|
471
477
|
/** A member as a comment may name it: path segments, no leading dash, no shell or glob syntax. */
|
|
@@ -483,6 +489,12 @@ const TARGET = /^[A-Za-z0-9_][A-Za-z0-9_.:\/\[\]"*@=+$,-]{0,499}$/;
|
|
|
483
489
|
* No leading dash, no spaces, no shell or glob syntax.
|
|
484
490
|
*/
|
|
485
491
|
const ACTION = /^[A-Za-z_][A-Za-z0-9_.\[\]"-]{0,299}$/;
|
|
492
|
+
/**
|
|
493
|
+
* The one argument a commit word may take after it (#4820), on top of the
|
|
494
|
+
* product's own pattern: a plain name, no leading dash, no path or shell
|
|
495
|
+
* syntax, at most 100 characters.
|
|
496
|
+
*/
|
|
497
|
+
export const COMMIT_COMMENT_ARG = /^[A-Za-z0-9][A-Za-z0-9_.-]{0,99}$/;
|
|
486
498
|
/** The longest ask an agent comment may carry. */
|
|
487
499
|
export const PRODUCT_COMMENT_ASK_MAX = 2000;
|
|
488
500
|
/** Commands a comment never runs, named in the reply so nobody waits on them. */
|
|
@@ -496,9 +508,17 @@ const NEVER = new Set(["approve", "override", "force-unlock", "unlock-state", "i
|
|
|
496
508
|
* comment is untrusted input: only a member path checked against a pattern
|
|
497
509
|
* with no shell syntax, and a wave number, leave this function.
|
|
498
510
|
*
|
|
499
|
-
* `env` turns on an alias whose `when` variable is `1` there.
|
|
511
|
+
* `env` turns on an alias whose `when` variable is `1` there. `commitArgs`
|
|
512
|
+
* names the commit words that take one argument after them (#4820), each
|
|
513
|
+
* with the pattern it must match; it must also match
|
|
514
|
+
* {@link COMMIT_COMMENT_ARG}. Any other commit word takes nothing.
|
|
500
515
|
*/
|
|
501
|
-
export function parseProductComment(
|
|
516
|
+
export function parseProductComment(
|
|
517
|
+
words: CiProductWords,
|
|
518
|
+
body: unknown,
|
|
519
|
+
env: Record<string, string | undefined> = {},
|
|
520
|
+
commitArgs: Readonly<Record<string, RegExp>> = {},
|
|
521
|
+
): ParsedProductComment | undefined {
|
|
502
522
|
if (typeof body !== "string") return undefined;
|
|
503
523
|
let text = body.trim();
|
|
504
524
|
const prefix = `/${words.product}`;
|
|
@@ -541,8 +561,16 @@ export function parseProductComment(words: CiProductWords, body: unknown, env: R
|
|
|
541
561
|
case "lock":
|
|
542
562
|
case "unlock":
|
|
543
563
|
return parts.length === 2 ? { kind: command } : { kind: "refused", reason: `${line} takes nothing after it` };
|
|
544
|
-
case "commit":
|
|
545
|
-
|
|
564
|
+
case "commit": {
|
|
565
|
+
if (parts.length === 2) return { kind: "commit", word: typed };
|
|
566
|
+
const pattern = Object.prototype.hasOwnProperty.call(commitArgs, typed) ? commitArgs[typed] : undefined;
|
|
567
|
+
if (pattern === undefined) return { kind: "refused", reason: `${line} takes nothing after it` };
|
|
568
|
+
const arg = parts[2]!;
|
|
569
|
+
if (parts.length !== 3 || !COMMIT_COMMENT_ARG.test(arg) || !pattern.test(arg)) {
|
|
570
|
+
return { kind: "refused", reason: `${line} takes nothing, or one name after it matching \`${pattern.source}\`` };
|
|
571
|
+
}
|
|
572
|
+
return { kind: "commit", word: typed, arg };
|
|
573
|
+
}
|
|
546
574
|
case "agent": {
|
|
547
575
|
const ask = text.slice(`${prefix} ${typed}`.length).trim();
|
|
548
576
|
if (ask === "") return { kind: "refused", reason: `${line} needs an ask after it, on the same line` };
|
package/src/ci-product.ts
CHANGED
|
@@ -64,7 +64,7 @@ export {
|
|
|
64
64
|
type ParsedProductComment,
|
|
65
65
|
type ProductActionRequest,
|
|
66
66
|
} from "./ci-product-words";
|
|
67
|
-
export { driftNotifyFields, productNotifySource, productNotifyWords, productSendOptions, type DriftNotifyInput } from "./notify-product";
|
|
67
|
+
export { driftNotifyFields, NOTIFY_MESSAGE_WORDS_FLOOR, productNotifySource, productNotifyWords, productSendOptions, type DriftNotifyInput } from "./notify-product";
|
|
68
68
|
|
|
69
69
|
/** The first chant with `chant notify relay` and the notify relay buttons (#4611). */
|
|
70
70
|
export const NOTIFY_RELAY_FLOOR = "0.124.55";
|
|
@@ -167,6 +167,59 @@ export const CI_RESPOND_FLOOR = "0.124.56";
|
|
|
167
167
|
* its pull request jobs with the build feature-detects on this floor.
|
|
168
168
|
*/
|
|
169
169
|
export const PR_LOOP_BUILD_FLAGS_FLOOR = "0.124.56";
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* A repository's own jobs in the generated pipeline file (#4769):
|
|
173
|
+
* `mergeOwnJobs` adds a map of jobs (or `readOwnJobsFile`'s) after chant's
|
|
174
|
+
* and refuses a name chant's file has or a GitLab keyword; `keepOwnJobs`
|
|
175
|
+
* carries over the old file's jobs that run neither `chant` nor the
|
|
176
|
+
* product's bin. `chant build --components --generate <forge> --own-jobs
|
|
177
|
+
* <file>` and `--keep-own-jobs` do the same on the build line. A product
|
|
178
|
+
* feature-detects on `OWN_JOBS_FLOOR`.
|
|
179
|
+
*/
|
|
180
|
+
export {
|
|
181
|
+
GITLAB_RESERVED_JOB_NAMES,
|
|
182
|
+
GITLAB_TOP_KEYWORDS,
|
|
183
|
+
isGeneratedJob,
|
|
184
|
+
jobBlocks,
|
|
185
|
+
jobNames,
|
|
186
|
+
keepOwnJobs,
|
|
187
|
+
mergeOwnJobs,
|
|
188
|
+
OWN_JOB_NAME,
|
|
189
|
+
OWN_JOBS_FLOOR,
|
|
190
|
+
OWN_JOBS_FORGES,
|
|
191
|
+
OWN_JOBS_LINE,
|
|
192
|
+
OwnJobsError,
|
|
193
|
+
ownJobsOf,
|
|
194
|
+
ownJobsProblems,
|
|
195
|
+
ownJobsYAML,
|
|
196
|
+
readOwnJobsFile,
|
|
197
|
+
type KeepOwnJobsOptions,
|
|
198
|
+
type MergeOwnJobsOptions,
|
|
199
|
+
type OwnJobsForge,
|
|
200
|
+
} from "./ci-own-jobs";
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* The Forgejo generators write a workflow-level run group a product asks for
|
|
204
|
+
* (#4771): `runGroup: "pull-request"` on the components pipeline (`chant
|
|
205
|
+
* build --components --generate forgejo --run-group pull-request`) and
|
|
206
|
+
* `runGroup: "commit"` on an op waves spec (its `options`, or `chant run
|
|
207
|
+
* --generate forgejo --spec <file> --run-group commit`). A workflow with a
|
|
208
|
+
* group of its own keeps it, and GitHub and GitLab files are unchanged. A
|
|
209
|
+
* product feature-detects on this floor.
|
|
210
|
+
*/
|
|
211
|
+
export const FORGEJO_RUN_GROUP_FLOOR = "0.124.77";
|
|
212
|
+
export { RUN_GROUP_KEYS, type RunGroupKey } from "./lexicon";
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* `chant build --pr-loop` takes the control repo (#4772): `--control <repo>`
|
|
216
|
+
* and `--control-setting <key>=<value>` (op, member, readTokenSecret, and
|
|
217
|
+
* the rest of the control settings) are the pull request loop's `control`,
|
|
218
|
+
* so the note job the build writes reads the control repo's records itself,
|
|
219
|
+
* the step `productPipelineJobs(...).post.control` returns. A product that
|
|
220
|
+
* writes its pipeline with the build feature-detects on this floor.
|
|
221
|
+
*/
|
|
222
|
+
export const PR_LOOP_CONTROL_FLOOR = "0.124.77";
|
|
170
223
|
export {
|
|
171
224
|
CI_RESPOND_DEFAULTS,
|
|
172
225
|
CI_RESPOND_ENV,
|
|
@@ -203,6 +256,33 @@ export async function runChantInProcess(argv: string[], words: CiProductWordsInp
|
|
|
203
256
|
*/
|
|
204
257
|
export { CI_AGENT_FLOOR, agentForbiddenPaths, type AgentSettings, type OpAgentSpec } from "./ci-agent";
|
|
205
258
|
|
|
259
|
+
/**
|
|
260
|
+
* The agent guard (#4770): a Claude Code PreToolUse hook on Bash and a
|
|
261
|
+
* wrapper per engine binary that refuse an agent's apply, approve or
|
|
262
|
+
* override. A product passes its words (its name, the refusal) and rules
|
|
263
|
+
* (engines, their refused subcommands and flags, its own refused commands)
|
|
264
|
+
* to `installAgentGuard`, and `agentGuard` checks a command line in process
|
|
265
|
+
* with the same rule. Feature-detect on `AGENT_GUARD_FLOOR`.
|
|
266
|
+
*/
|
|
267
|
+
export {
|
|
268
|
+
AGENT_GUARD_FLOOR,
|
|
269
|
+
agentGuard,
|
|
270
|
+
agentGuardScript,
|
|
271
|
+
agentGuardWrapper,
|
|
272
|
+
CLAUDE_SETTINGS_LOCAL,
|
|
273
|
+
CLAUDE_SETTINGS_SHARED,
|
|
274
|
+
installAgentGuard,
|
|
275
|
+
withAgentGuardHook,
|
|
276
|
+
type AgentGuard,
|
|
277
|
+
type AgentGuardEngine,
|
|
278
|
+
type AgentGuardInstall,
|
|
279
|
+
type AgentGuardInstallOptions,
|
|
280
|
+
type AgentGuardRules,
|
|
281
|
+
type AgentGuardSpec,
|
|
282
|
+
type AgentGuardVerdict,
|
|
283
|
+
type AgentGuardWords,
|
|
284
|
+
} from "./agent-guard";
|
|
285
|
+
|
|
206
286
|
/**
|
|
207
287
|
* After-plan hooks in a wave job's process (#4614): `runChantInProcess(["run",
|
|
208
288
|
* "wave", ...], words, { wave: { afterPlan, capabilities } })`, or
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #4760: `recordGateApproval(op, gate, { cwd })` reads, appends and pushes the
|
|
3
|
+
* ledger of the repository at `cwd`, never the one the process runs in.
|
|
4
|
+
*
|
|
5
|
+
* Both repositories are temp repos with temp bare remotes, and the process is
|
|
6
|
+
* moved into the bystander for the call, so a regression writes into and
|
|
7
|
+
* pushes a throwaway repository rather than the checkout running the tests.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { afterEach, describe, expect, test, vi } from "vitest";
|
|
11
|
+
import { withTestDir } from "@intentius/chant-test-utils";
|
|
12
|
+
import { spawnSync } from "node:child_process";
|
|
13
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
14
|
+
import { join } from "node:path";
|
|
15
|
+
import { appendPendingGate, readGateLedger } from "../../lifecycle/gate-ledger";
|
|
16
|
+
import { resetGateOrigin } from "../../lifecycle/gate-origin";
|
|
17
|
+
|
|
18
|
+
vi.mock("../../op/discover", () => ({ discoverOps: async () => ({ errors: [], ops: new Map() }) }));
|
|
19
|
+
|
|
20
|
+
const { recordGateApproval } = await import("./approve");
|
|
21
|
+
|
|
22
|
+
function git(cwd: string, ...args: string[]): { status: number | null; out: string } {
|
|
23
|
+
const r = spawnSync("git", args, { cwd, encoding: "utf-8" });
|
|
24
|
+
return { status: r.status, out: (r.stdout ?? "").trim() };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function ok(cwd: string, ...args: string[]): string {
|
|
28
|
+
const r = spawnSync("git", args, { cwd, encoding: "utf-8" });
|
|
29
|
+
if (r.status !== 0) throw new Error(`git ${args.join(" ")}: ${r.stderr}`);
|
|
30
|
+
return r.stdout.trim();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const PLAN = `jcs1-sha256:${"a".repeat(64)}`;
|
|
34
|
+
|
|
35
|
+
/** A repo at `<root>/<name>` with one commit on main, pushed to a bare remote at `<root>/<name>.git`. */
|
|
36
|
+
function repoWithRemote(root: string, name: string): { dir: string; remote: string } {
|
|
37
|
+
const dir = join(root, name);
|
|
38
|
+
const remote = join(root, `${name}.git`);
|
|
39
|
+
mkdirSync(dir);
|
|
40
|
+
ok(root, "init", "-q", "--bare", "-b", "main", remote);
|
|
41
|
+
ok(dir, "init", "-q", "-b", "main");
|
|
42
|
+
ok(dir, "config", "user.email", "t@chant.dev");
|
|
43
|
+
ok(dir, "config", "user.name", "T");
|
|
44
|
+
writeFileSync(join(dir, "README"), `${name}\n`);
|
|
45
|
+
ok(dir, "add", ".");
|
|
46
|
+
ok(dir, "commit", "-q", "-m", "init");
|
|
47
|
+
ok(dir, "remote", "add", "origin", remote);
|
|
48
|
+
ok(dir, "push", "-q", "origin", "main");
|
|
49
|
+
return { dir, remote };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function hasLifecycleBranch(gitDir: string): boolean {
|
|
53
|
+
return git(gitDir, "rev-parse", "--verify", "--quiet", "refs/heads/chant/lifecycle").status === 0;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
describe("recordGateApproval with a cwd other than the process's (#4760)", () => {
|
|
57
|
+
const home = process.cwd();
|
|
58
|
+
afterEach(() => {
|
|
59
|
+
process.chdir(home);
|
|
60
|
+
resetGateOrigin();
|
|
61
|
+
vi.restoreAllMocks();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
async function inTwoRepos(fn: (target: { dir: string; remote: string }, bystander: { dir: string; remote: string }) => Promise<void>) {
|
|
65
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
66
|
+
await withTestDir(async (root) => {
|
|
67
|
+
const target = repoWithRemote(root, "target");
|
|
68
|
+
const bystander = repoWithRemote(root, "bystander");
|
|
69
|
+
process.chdir(bystander.dir);
|
|
70
|
+
try {
|
|
71
|
+
await fn(target, bystander);
|
|
72
|
+
} finally {
|
|
73
|
+
process.chdir(home);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function expectUntouched(bystander: { dir: string; remote: string }) {
|
|
79
|
+
expect(hasLifecycleBranch(bystander.dir)).toBe(false);
|
|
80
|
+
expect(hasLifecycleBranch(bystander.remote)).toBe(false);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
test("answers the pending fact in the cwd's ledger and pushes there, leaving the process's repo alone", async () => {
|
|
84
|
+
await inTwoRepos(async (target, bystander) => {
|
|
85
|
+
await appendPendingGate(
|
|
86
|
+
{ op: "deploy", gate: "prod", timestamp: "2026-11-07T10:00:00.000Z", expiresAt: "2999-01-01T00:00:00.000Z", planDigest: PLAN },
|
|
87
|
+
{ cwd: target.dir },
|
|
88
|
+
);
|
|
89
|
+
ok(target.dir, "push", "-q", "origin", "chant/lifecycle");
|
|
90
|
+
|
|
91
|
+
const outcome = await recordGateApproval("deploy", "prod", { origin: "cli", actor: "alice", cwd: target.dir });
|
|
92
|
+
|
|
93
|
+
expect(outcome).toMatchObject({ ok: true, pushed: true });
|
|
94
|
+
expect((await readGateLedger("deploy", { cwd: target.dir })).resolutions).toEqual([
|
|
95
|
+
expect.objectContaining({ gate: "prod", resolvedBy: "alice", planDigest: PLAN }),
|
|
96
|
+
]);
|
|
97
|
+
expect(ok(target.remote, "rev-parse", "refs/heads/chant/lifecycle")).toBe(ok(target.dir, "rev-parse", "refs/heads/chant/lifecycle"));
|
|
98
|
+
expectUntouched(bystander);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("with --plan and nothing pending anywhere, the record and the push still go to the cwd", async () => {
|
|
103
|
+
await inTwoRepos(async (target, bystander) => {
|
|
104
|
+
const outcome = await recordGateApproval("deploy", "prod", { origin: "cli", actor: "alice", plan: PLAN, cwd: target.dir });
|
|
105
|
+
|
|
106
|
+
expect(outcome).toMatchObject({ ok: true, pushed: true });
|
|
107
|
+
expect((await readGateLedger("deploy", { cwd: target.dir })).resolutions).toHaveLength(1);
|
|
108
|
+
expect(hasLifecycleBranch(target.remote)).toBe(true);
|
|
109
|
+
expectUntouched(bystander);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
});
|
|
@@ -73,9 +73,10 @@ const LIFECYCLE_BRANCH = "chant/lifecycle";
|
|
|
73
73
|
* failure; the exit code is unchanged. With no remote at all there is nobody
|
|
74
74
|
* elsewhere to tell, so that case is a quiet suffix and no warning (#3677).
|
|
75
75
|
*/
|
|
76
|
-
async function reportedPush(consequence: string): Promise<{ pushed: boolean; suffix: string }> {
|
|
76
|
+
async function reportedPush(consequence: string, cwd?: string): Promise<{ pushed: boolean; suffix: string }> {
|
|
77
77
|
// #4637: a push another writer beat is replayed onto the remote's tip and tried again; an approval line appends.
|
|
78
|
-
|
|
78
|
+
// #4760: and the push is of the repository at `cwd`, not whatever one the process runs in.
|
|
79
|
+
const { pushed, pushWarning } = await pushLifecycleRebasing(...(cwd !== undefined ? [{ cwd }] : [])).catch((err: unknown) => {
|
|
79
80
|
if (err instanceof LifecyclePushLostError) return { pushed: false, pushWarning: err.message };
|
|
80
81
|
throw err;
|
|
81
82
|
});
|
|
@@ -328,7 +329,11 @@ export interface GateApprovalOptions {
|
|
|
328
329
|
* `--statement` and `--signature`.
|
|
329
330
|
*/
|
|
330
331
|
binding?: string;
|
|
331
|
-
/**
|
|
332
|
+
/**
|
|
333
|
+
* The repository the approval is for: its ops, its workspace and key file,
|
|
334
|
+
* and its `chant/lifecycle`, which is read, appended to and pushed (#4760).
|
|
335
|
+
* Defaults to the process's directory.
|
|
336
|
+
*/
|
|
332
337
|
cwd?: string;
|
|
333
338
|
/** Replaces the lexicon evaluator a gate policy is loaded from. For tests. */
|
|
334
339
|
evaluator?: GatePolicyEvaluator;
|
|
@@ -428,7 +433,13 @@ async function approveGate(
|
|
|
428
433
|
}));
|
|
429
434
|
return { ok: false };
|
|
430
435
|
}
|
|
431
|
-
|
|
436
|
+
// #4760: every ledger read, append and push below acts on the repository at
|
|
437
|
+
// `opts.cwd`, the one the scope and signing key come from, and never on
|
|
438
|
+
// process.cwd() when a caller names another. With no `opts.cwd` the calls
|
|
439
|
+
// are made exactly as before, with no location, so they default as they did.
|
|
440
|
+
const cwd = opts.cwd ?? process.cwd();
|
|
441
|
+
const at: [] | [{ cwd: string }] = opts.cwd !== undefined ? [{ cwd: opts.cwd }] : [];
|
|
442
|
+
const { ops } = await discoverOps(...at);
|
|
432
443
|
// `fan-out` is the op name every `chant components fan-out` gate is recorded
|
|
433
444
|
// under (../handlers/fan-out.ts). It is a command rather than a declaration,
|
|
434
445
|
// so there is no `*.op.ts` to find and nothing is wrong when none is there.
|
|
@@ -446,7 +457,7 @@ async function approveGate(
|
|
|
446
457
|
// single commit holding only this resolution — the pending fact it is
|
|
447
458
|
// answering is discarded rather than appended to.
|
|
448
459
|
try {
|
|
449
|
-
await requireLifecycleLedger();
|
|
460
|
+
await requireLifecycleLedger(...at);
|
|
450
461
|
} catch (err) {
|
|
451
462
|
console.error(formatError({
|
|
452
463
|
message: `Cannot record the approval: ${err instanceof Error ? err.message : String(err)}`,
|
|
@@ -459,7 +470,7 @@ async function approveGate(
|
|
|
459
470
|
// the new gate has no standing fact of its own. Once it has one, approve
|
|
460
471
|
// the gate the runs now stop at.
|
|
461
472
|
if (successor) {
|
|
462
|
-
const theirs = latestPendingGate((await readGateLedger(successor.op)).pending, successor.gate);
|
|
473
|
+
const theirs = latestPendingGate((await readGateLedger(successor.op, ...at)).pending, successor.gate);
|
|
463
474
|
if (theirs && !isPendingGateExpired(theirs, new Date().toISOString())) {
|
|
464
475
|
console.error(formatError({
|
|
465
476
|
message:
|
|
@@ -498,7 +509,7 @@ async function approveGate(
|
|
|
498
509
|
// recorded in, and an approval answers one environment. `--env` picks it;
|
|
499
510
|
// without it, the newest standing fact decides, and the success line below
|
|
500
511
|
// names the environment it chose.
|
|
501
|
-
const ledgerBefore = await readGateLedger(opName);
|
|
512
|
+
const ledgerBefore = await readGateLedger(opName, ...at);
|
|
502
513
|
const pendingFor = (all: PendingGateRecord[]): PendingGateRecord[] => {
|
|
503
514
|
const bound = all.some((p) => p.gate === gate && p.environment !== undefined);
|
|
504
515
|
return bound && opts.environment !== undefined ? all.filter((p) => p.environment === opts.environment) : all;
|
|
@@ -552,7 +563,7 @@ async function approveGate(
|
|
|
552
563
|
// commit is superseded. Its promote refuses the release, so an approval of
|
|
553
564
|
// it would count for nothing; refuse it here, whatever plan is named.
|
|
554
565
|
if (standing?.releaseDigest !== undefined) {
|
|
555
|
-
const refused = await refuseRevokedRelease(opName, gate, standing.releaseDigest,
|
|
566
|
+
const refused = await refuseRevokedRelease(opName, gate, standing.releaseDigest, cwd);
|
|
556
567
|
if (refused) return { ok: false };
|
|
557
568
|
}
|
|
558
569
|
|
|
@@ -563,7 +574,7 @@ async function approveGate(
|
|
|
563
574
|
// resolved over the same one has no second party in it. Refused by default,
|
|
564
575
|
// the way #2300 refuses a plan nobody approved.
|
|
565
576
|
const origin = opts.origin ?? currentGateOrigin();
|
|
566
|
-
const ledger = await readGateLedger(opName);
|
|
577
|
+
const ledger = await readGateLedger(opName, ...at);
|
|
567
578
|
const standingForOrigin = latestPendingGate(pendingFor(ledger.pending), gate);
|
|
568
579
|
|
|
569
580
|
// chant#3485: a gate that is never resolved over MCP or ACP says so on its
|
|
@@ -625,7 +636,6 @@ async function approveGate(
|
|
|
625
636
|
// #3163, ws-080: the declaration at base may require a checkable approver
|
|
626
637
|
// (identity.attribution "identified") and a signed approval for this gate
|
|
627
638
|
// (identity.gates). Both are judged before anything is written.
|
|
628
|
-
const cwd = opts.cwd ?? process.cwd();
|
|
629
639
|
const identity = await import("../../workspace/identity");
|
|
630
640
|
const { scopeSource } = await import("../../workspace/write-scope");
|
|
631
641
|
const scope = scopeSource(cwd);
|
|
@@ -832,10 +842,11 @@ async function approveGate(
|
|
|
832
842
|
...(standing?.runId ? { runId: standing.runId } : {}),
|
|
833
843
|
origin,
|
|
834
844
|
...(refusal && opts.allowSameOrigin ? { sameOriginOverride: true } : {}),
|
|
835
|
-
});
|
|
845
|
+
}, ...at);
|
|
836
846
|
const push = await reportedPush(
|
|
837
847
|
`The resolution is recorded locally on ${LIFECYCLE_BRANCH}. Until it reaches the remote, ` +
|
|
838
848
|
`a run in another checkout will not see it.`,
|
|
849
|
+
opts.cwd,
|
|
839
850
|
);
|
|
840
851
|
|
|
841
852
|
console.error(formatSuccess(
|