@intentius/chant 0.124.76 → 0.124.77
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/agent-guard.d.ts +143 -0
- package/dist/agent-guard.d.ts.map +1 -0
- package/dist/agent-guard.js +456 -0
- package/dist/ci-commit.d.ts +54 -3
- package/dist/ci-commit.d.ts.map +1 -1
- package/dist/ci-commit.js +69 -7
- 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 +234 -0
- package/dist/ci-product.d.ts +39 -0
- package/dist/ci-product.d.ts.map +1 -1
- package/dist/ci-product.js +40 -0
- 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 +17 -5
- 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 +163 -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/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 +17 -1
- package/dist/lexicon.d.ts.map +1 -1
- package/dist/lexicon.js +1 -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/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 +28 -2
- 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 +158 -0
- package/dist/release-floors.d.ts +1 -1
- package/dist/release-floors.js +1 -1
- package/dist/state-export.d.ts +126 -0
- package/dist/state-export.d.ts.map +1 -0
- package/dist/state-export.js +157 -0
- package/dist/state-migration.d.ts +148 -0
- package/dist/state-migration.d.ts.map +1 -0
- package/dist/state-migration.js +147 -0
- package/dist/state-unlock.d.ts +167 -0
- package/dist/state-unlock.d.ts.map +1 -0
- package/dist/state-unlock.js +197 -0
- package/dist/wave-runtime.mjs +329 -329
- 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 +8 -3
- 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/agent-guard.test.ts +320 -0
- package/src/agent-guard.ts +548 -0
- package/src/ci-commit.test.ts +50 -2
- package/src/ci-commit.ts +93 -9
- package/src/ci-own-jobs.test.ts +109 -0
- package/src/ci-own-jobs.ts +253 -0
- package/src/ci-product.ts +81 -0
- 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 +88 -0
- package/src/cli/handlers/build-run-group.test.ts +60 -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 +15 -5
- 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 +138 -0
- package/src/components/pr-env-components.ts +214 -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/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 +18 -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/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 +30 -3
- package/src/op/op-waves-capability.ts +2 -2
- package/src/policy-engine.test.ts +163 -0
- package/src/policy-engine.ts +201 -0
- package/src/release-floors.ts +1 -1
- package/src/state-export.test.ts +125 -0
- package/src/state-export.ts +228 -0
- package/src/state-migration.test.ts +134 -0
- package/src/state-migration.ts +237 -0
- package/src/state-unlock.test.ts +166 -0
- package/src/state-unlock.ts +294 -0
- package/src/workspace/declaration.schema.json +1 -1
- package/src/workspace/release-workflow.test.ts +34 -0
- package/src/workspace/release-workflow.ts +17 -3
|
@@ -7,8 +7,10 @@
|
|
|
7
7
|
* imported with a plain `import()`, which caches the module. So each
|
|
8
8
|
* environment is compiled in a child process of its own
|
|
9
9
|
* ({@link spawnTargetCompiler}), running `chant release compile --env <env>`,
|
|
10
|
-
* which calls {@link compileEnvironment} in that process and
|
|
11
|
-
* result as JSON
|
|
10
|
+
* which calls {@link compileEnvironment} in that process and writes the
|
|
11
|
+
* result as JSON to the file the parent names in
|
|
12
|
+
* {@link RELEASE_COMPILE_RESULT_ENV} (#4764), so a module the project loads
|
|
13
|
+
* that prints to stdout can't stand in for it. The artifacts are built in one more child, for the first
|
|
12
14
|
* environment in which each component is enabled ({@link buildReleaseArtifacts}).
|
|
13
15
|
*
|
|
14
16
|
* What counts as a template is data, as the build and publish kinds are in
|
|
@@ -39,8 +41,9 @@
|
|
|
39
41
|
|
|
40
42
|
import { spawn, spawnSync } from "node:child_process";
|
|
41
43
|
import { isUtf8 } from "node:buffer";
|
|
42
|
-
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
43
|
-
import {
|
|
44
|
+
import { existsSync, mkdtempSync, readFileSync, rmSync, statSync } from "node:fs";
|
|
45
|
+
import { tmpdir } from "node:os";
|
|
46
|
+
import { isAbsolute, join, relative, resolve } from "node:path";
|
|
44
47
|
import { loadChantConfig, type ChantConfig } from "../config";
|
|
45
48
|
import { resolveFlaggedBuildParams } from "../flag-params";
|
|
46
49
|
import { discoverComponents } from "./discover";
|
|
@@ -562,6 +565,25 @@ export async function buildReleaseArtifacts(
|
|
|
562
565
|
return artifacts;
|
|
563
566
|
}
|
|
564
567
|
|
|
568
|
+
/**
|
|
569
|
+
* The variable that names the file `chant release compile` writes its result
|
|
570
|
+
* to (#4764). Without it the result goes to stdout, as it did before.
|
|
571
|
+
*/
|
|
572
|
+
export const RELEASE_COMPILE_RESULT_ENV = "CHANT_RELEASE_COMPILE_RESULT";
|
|
573
|
+
|
|
574
|
+
const isRecord = (v: unknown): v is Record<string, unknown> => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
575
|
+
|
|
576
|
+
/** Why `value` is not the target `chant release compile --env <env>` writes, or undefined when it is one. */
|
|
577
|
+
export function notATarget(value: unknown, env: string): string | undefined {
|
|
578
|
+
if (!isRecord(value)) return "it is not an object";
|
|
579
|
+
if (value.env !== env) return `its env is ${JSON.stringify(value.env)}`;
|
|
580
|
+
if (!Array.isArray(value.components) || !value.components.every((c) => typeof c === "string")) return "it has no components list";
|
|
581
|
+
if (!Array.isArray(value.params)) return "it has no params list";
|
|
582
|
+
if (!Array.isArray(value.templates)) return "it has no templates list";
|
|
583
|
+
for (const field of ["buildSteps", "buildInputs", "componentFiles"]) if (!isRecord(value[field])) return `it has no ${field}`;
|
|
584
|
+
return undefined;
|
|
585
|
+
}
|
|
586
|
+
|
|
565
587
|
/** How {@link spawnTargetCompiler} starts this chant. */
|
|
566
588
|
export function ownChantCommand(): string[] {
|
|
567
589
|
const main = process.argv[1] ?? "";
|
|
@@ -575,6 +597,7 @@ function runChild(
|
|
|
575
597
|
env: string,
|
|
576
598
|
releaseLabel?: string,
|
|
577
599
|
releaseDigests?: string,
|
|
600
|
+
resultFile?: string,
|
|
578
601
|
): Promise<{ code: number; stdout: string; stderr: string }> {
|
|
579
602
|
return new Promise((resolvePromise, reject) => {
|
|
580
603
|
const [bin, ...lead] = command;
|
|
@@ -584,6 +607,8 @@ function runChild(
|
|
|
584
607
|
delete childEnv[RELEASE_DIGESTS_ENV];
|
|
585
608
|
if (releaseLabel !== undefined) childEnv[RELEASE_LABEL_ENV] = releaseLabel;
|
|
586
609
|
if (releaseDigests !== undefined) childEnv[RELEASE_DIGESTS_ENV] = releaseDigests;
|
|
610
|
+
delete childEnv[RELEASE_COMPILE_RESULT_ENV];
|
|
611
|
+
if (resultFile !== undefined) childEnv[RELEASE_COMPILE_RESULT_ENV] = resultFile;
|
|
587
612
|
const child = spawn(bin, [...lead, ...args], { cwd, stdio: ["ignore", "pipe", "pipe"], env: childEnv });
|
|
588
613
|
const out: Buffer[] = [];
|
|
589
614
|
const err: Buffer[] = [];
|
|
@@ -600,7 +625,10 @@ function runChild(
|
|
|
600
625
|
/**
|
|
601
626
|
* The compiler the CLI uses: each environment in a child process of this same
|
|
602
627
|
* chant (`chant release compile --env <env> --json`), with `CHANT_ENV` set to
|
|
603
|
-
* that environment before anything imports the project.
|
|
628
|
+
* that environment before anything imports the project. The child writes its
|
|
629
|
+
* result to a file this process names (#4764), and the result is checked for
|
|
630
|
+
* the target's shape, so a project module that prints JSON on load, or prints
|
|
631
|
+
* it and exits, is refused by the environment's name.
|
|
604
632
|
*/
|
|
605
633
|
export function spawnTargetCompiler(inputs: CompileInputs, command: readonly string[] = ownChantCommand()): TargetCompiler {
|
|
606
634
|
const common = (): string[] => {
|
|
@@ -612,21 +640,50 @@ export function spawnTargetCompiler(inputs: CompileInputs, command: readonly str
|
|
|
612
640
|
if (inputs.paramsFile) args.push("--params-file", inputs.paramsFile);
|
|
613
641
|
return args;
|
|
614
642
|
};
|
|
615
|
-
const call = async (env: string, extra: string[]): Promise<unknown> => {
|
|
616
|
-
const
|
|
617
|
-
|
|
643
|
+
const call = async (env: string, extra: string[], what: string, check: (value: unknown) => string | undefined): Promise<unknown> => {
|
|
644
|
+
const dir = mkdtempSync(join(tmpdir(), "chant-release-compile-"));
|
|
645
|
+
const resultFile = join(dir, "result.json");
|
|
618
646
|
try {
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
647
|
+
const run = await runChild(
|
|
648
|
+
command,
|
|
649
|
+
["release", "compile", "--env", env, ...common(), ...extra, "--json"],
|
|
650
|
+
inputs.projectDir,
|
|
651
|
+
env,
|
|
652
|
+
inputs.releaseLabel,
|
|
653
|
+
inputs.releaseDigests,
|
|
654
|
+
resultFile,
|
|
655
|
+
);
|
|
656
|
+
if (run.code !== 0) throw new ReleaseCompileError(`compiling "${env}" failed (exit ${run.code})`);
|
|
657
|
+
const printed = run.stdout.trim();
|
|
658
|
+
const stdoutNote = printed === "" ? "" : `; its stdout began: ${printed.length > 200 ? `${printed.slice(0, 200)}...` : printed}`;
|
|
659
|
+
if (!existsSync(resultFile)) {
|
|
660
|
+
throw new ReleaseCompileError(
|
|
661
|
+
`compiling "${env}" ${printed === "" ? `exited 0 without writing ${what}` : `printed something that is not ${what} and exited 0 without writing one`}; ` +
|
|
662
|
+
`a module the project loads may have exited on load (for a project with no components, set sourceDir or --src to the build root)${stdoutNote}`,
|
|
663
|
+
);
|
|
664
|
+
}
|
|
665
|
+
let value: unknown;
|
|
666
|
+
try {
|
|
667
|
+
value = JSON.parse(readFileSync(resultFile, "utf-8"));
|
|
668
|
+
} catch {
|
|
669
|
+
throw new ReleaseCompileError(`compiling "${env}" wrote something that is not JSON`);
|
|
670
|
+
}
|
|
671
|
+
const why = check(value);
|
|
672
|
+
if (why !== undefined) throw new ReleaseCompileError(`compiling "${env}" printed something that is not ${what}: ${why}${stdoutNote}`);
|
|
673
|
+
return value;
|
|
674
|
+
} finally {
|
|
675
|
+
rmSync(dir, { recursive: true, force: true });
|
|
622
676
|
}
|
|
623
677
|
};
|
|
624
678
|
return {
|
|
625
679
|
async compile(env) {
|
|
626
|
-
return (await call(env, inputs.buildCommand ? ["--build", inputs.buildCommand] : [])) as CompiledTarget;
|
|
680
|
+
return (await call(env, inputs.buildCommand ? ["--build", inputs.buildCommand] : [], "a target", (v) => notATarget(v, env))) as CompiledTarget;
|
|
627
681
|
},
|
|
628
682
|
async buildArtifacts(env, components) {
|
|
629
|
-
|
|
683
|
+
const result = await call(env, ["--artifacts", "--component", components.join(",")], "a list of artifacts", (v) =>
|
|
684
|
+
isRecord(v) && Array.isArray(v.artifacts) ? undefined : "it has no artifacts list",
|
|
685
|
+
);
|
|
686
|
+
return (result as { artifacts: ReleaseArtifact[] }).artifacts;
|
|
630
687
|
},
|
|
631
688
|
};
|
|
632
689
|
}
|
|
@@ -25,4 +25,18 @@ describe("the gate-policy bundle", () => {
|
|
|
25
25
|
expect(Object.keys(result.metafile.inputs).map((i) => i.replace(/^.*?packages\//, "packages/"))).toEqual(["packages/core/src/gate-policy.ts"]);
|
|
26
26
|
expect(result.outputFiles.reduce((n, f) => n + f.contents.byteLength, 0)).toBeLessThan(16 * 1024);
|
|
27
27
|
});
|
|
28
|
+
|
|
29
|
+
test("the policy engine install (#4774) bundles with only the release floor beside it", async () => {
|
|
30
|
+
const result = await build({
|
|
31
|
+
entryPoints: [join(REPO, "packages", "core", "src", "policy-engine.ts")],
|
|
32
|
+
bundle: true,
|
|
33
|
+
platform: "node",
|
|
34
|
+
format: "esm",
|
|
35
|
+
write: false,
|
|
36
|
+
metafile: true,
|
|
37
|
+
outdir: "out",
|
|
38
|
+
logLevel: "silent",
|
|
39
|
+
});
|
|
40
|
+
expect(Object.keys(result.metafile.inputs).map((i) => i.replace(/^.*?packages\//, "packages/")).sort()).toEqual(["packages/core/src/policy-engine.ts", "packages/core/src/release-floors.ts"]);
|
|
41
|
+
});
|
|
28
42
|
});
|
package/src/gate-policy.test.ts
CHANGED
|
@@ -91,6 +91,10 @@ describe("running the engine (#4294)", () => {
|
|
|
91
91
|
expect(gatePolicyArgv({ engine: "conftest", namespace: "infra" }, { policy: "/p", input: "/i.json" })).toContain("--namespace");
|
|
92
92
|
expect(gatePolicyArgv({ engine: "opa" }, { policy: "/p", input: "/i.json" })).toEqual(["opa", "eval", "--format", "json", "--data", "/p", "--input", "/i.json", "data.main"]);
|
|
93
93
|
expect(gatePolicyArgv({ engine: "opa", command: ["./bin/check", "--rules={policy}", "{input}"] }, { policy: "/p", input: "/i.json" })).toEqual(["./bin/check", "--rules=/p", "/i.json"]);
|
|
94
|
+
// #4774: an installed engine runs from where it was installed; a command ignores it.
|
|
95
|
+
expect(gatePolicyArgv({ engine: "opa" }, { policy: "/p", input: "/i.json", binary: "/cache/opa-1.21.4-linux-x64/opa" })[0]).toBe("/cache/opa-1.21.4-linux-x64/opa");
|
|
96
|
+
expect(gatePolicyArgv({}, { policy: "/p", input: "/i.json", binary: "/cache/conftest" })[0]).toBe("/cache/conftest");
|
|
97
|
+
expect(gatePolicyArgv({ command: ["./bin/check", "{input}"] }, { policy: "/p", input: "/i.json", binary: "/cache/conftest" })[0]).toBe("./bin/check");
|
|
94
98
|
});
|
|
95
99
|
|
|
96
100
|
test("the input is written to the file the engine reads, and a denial is a verdict with its sorted rules", () => {
|
|
@@ -157,6 +161,9 @@ describe("ci.policy settings (#4294)", () => {
|
|
|
157
161
|
expect(() => assertGatePolicySettings("ci.policy", { outcomes: ["deny", "allow_unattended"] })).toThrow(/no unattended outcome/);
|
|
158
162
|
expect(() => assertGatePolicySettings("ci.policy", { allow_unattended: true })).toThrow(/no unattended outcome/);
|
|
159
163
|
expect(() => assertGatePolicySettings("ci.policy", { rules: [] })).toThrow(/not a setting/);
|
|
164
|
+
expect(() => assertGatePolicySettings("ci.policy", { engine: "opa", install: true })).not.toThrow();
|
|
165
|
+
expect(() => assertGatePolicySettings("ci.policy", { install: "yes" })).toThrow(/install must be true or false/);
|
|
166
|
+
expect(() => assertGatePolicySettings("ci.policy", { install: true, command: ["./check", "{input}"] })).toThrow(/install and ci.policy.command cannot both be set/);
|
|
160
167
|
expect(() => assertGatePolicySettings("ci.policy", [])).toThrow(/must be/);
|
|
161
168
|
});
|
|
162
169
|
});
|
package/src/gate-policy.ts
CHANGED
|
@@ -120,6 +120,12 @@ export interface GatePolicySettings {
|
|
|
120
120
|
* prints the result shape `engine` names.
|
|
121
121
|
*/
|
|
122
122
|
command?: string[];
|
|
123
|
+
/**
|
|
124
|
+
* Fetch the engine when it is not on PATH (#4774): conftest or OPA at the
|
|
125
|
+
* release chant pins, checked against its recorded sha256
|
|
126
|
+
* (`./policy-engine.ts`). Refused beside `command`, which names its own.
|
|
127
|
+
*/
|
|
128
|
+
install?: boolean;
|
|
123
129
|
}
|
|
124
130
|
|
|
125
131
|
/** One CrossGuard pack named by `ci.policy.packs`. */
|
|
@@ -251,13 +257,17 @@ export function gatePolicyVerdict(engine: GatePolicyEngine, found: GatePolicyFin
|
|
|
251
257
|
|
|
252
258
|
const errorVerdict = (engine: GatePolicyEngine, error: string): GatePolicyVerdict => ({ result: "error", engine, denials: [], rules: [], holds: [], warnings: [], error });
|
|
253
259
|
|
|
254
|
-
/**
|
|
255
|
-
|
|
260
|
+
/**
|
|
261
|
+
* The argv one check runs: the settings' `command` with its placeholders
|
|
262
|
+
* filled, or the engine's own. `binary` is the engine's executable when it
|
|
263
|
+
* is not the one on PATH, such as one `./policy-engine.ts` installed.
|
|
264
|
+
*/
|
|
265
|
+
export function gatePolicyArgv(settings: GatePolicySettings, at: { policy: string; input: string; binary?: string }): string[] {
|
|
256
266
|
if (settings.command) return settings.command.map((a) => a.replaceAll("{policy}", at.policy).replaceAll("{input}", at.input));
|
|
257
267
|
if ((settings.engine ?? "conftest") === "opa") {
|
|
258
|
-
return ["opa", "eval", "--format", "json", "--data", at.policy, "--input", at.input, `data.${settings.namespace ?? "main"}`];
|
|
268
|
+
return [at.binary ?? "opa", "eval", "--format", "json", "--data", at.policy, "--input", at.input, `data.${settings.namespace ?? "main"}`];
|
|
259
269
|
}
|
|
260
|
-
return ["conftest", "test", "--no-color", "--output", "json", "--policy", at.policy, ...(settings.namespace ? ["--namespace", settings.namespace] : ["--all-namespaces"]), at.input];
|
|
270
|
+
return [at.binary ?? "conftest", "test", "--no-color", "--output", "json", "--policy", at.policy, ...(settings.namespace ? ["--namespace", settings.namespace] : ["--all-namespaces"]), at.input];
|
|
261
271
|
}
|
|
262
272
|
|
|
263
273
|
/** What one run of the engine printed and how it exited. `status` is null when it did not start. */
|
|
@@ -284,6 +294,8 @@ export interface CheckGatePolicyOptions {
|
|
|
284
294
|
/** Where the engine runs. Default the policy directory. */
|
|
285
295
|
cwd?: string;
|
|
286
296
|
exec?: GatePolicyExec;
|
|
297
|
+
/** The engine's executable, when it is not the one on PATH. Not used with `command`. */
|
|
298
|
+
binary?: string;
|
|
287
299
|
}
|
|
288
300
|
|
|
289
301
|
const tail = (text: string): string => text.trim().split("\n").slice(-3).join(" ");
|
|
@@ -296,7 +308,7 @@ export function checkGatePolicy(options: CheckGatePolicyOptions): GatePolicyVerd
|
|
|
296
308
|
try {
|
|
297
309
|
const file = join(work, "input.json");
|
|
298
310
|
writeFileSync(file, typeof options.input === "string" ? options.input : JSON.stringify(options.input));
|
|
299
|
-
const argv = gatePolicyArgv(settings, { policy: options.policyDir, input: file });
|
|
311
|
+
const argv = gatePolicyArgv(settings, { policy: options.policyDir, input: file, ...(options.binary ? { binary: options.binary } : {}) });
|
|
300
312
|
const r = (options.exec ?? defaultGatePolicyExec)(argv, options.cwd ?? options.policyDir);
|
|
301
313
|
if (r.status === null) return errorVerdict(engine, `${argv[0]} did not run: ${tail(r.stderr) || "no such command"}`);
|
|
302
314
|
const found = engine === "opa" ? opaFindings(r.stdout, settings.namespace ?? "main") : conftestFindings(r.stdout);
|
|
@@ -353,7 +365,8 @@ export function exportGatePolicy(tree: GatePolicyTree, path: string): { dir: str
|
|
|
353
365
|
|
|
354
366
|
/** The settings, checked. Throws with what is wrong. `where` names them, such as `ci.policy`. */
|
|
355
367
|
export function assertGatePolicySettings(where: string, raw: unknown): asserts raw is GatePolicySettings {
|
|
356
|
-
if (raw === null || typeof raw !== "object" || Array.isArray(raw)) throw new Error(`${where} must be { engine?, path?, source?, namespace?, command?, packs? }`);
|
|
368
|
+
if (raw === null || typeof raw !== "object" || Array.isArray(raw)) throw new Error(`${where} must be { engine?, path?, source?, namespace?, command?, packs?, install? }`);
|
|
369
|
+
if ("install" in raw && "command" in raw) throw new Error(`${where}.install and ${where}.command cannot both be set; install fetches conftest or opa, and the command names its own executable`);
|
|
357
370
|
if ("path" in raw && "source" in raw) throw new Error(`${where}.path and ${where}.source cannot both be set; with a source, ${where}.source.path names the directory inside it`);
|
|
358
371
|
for (const [key, value] of Object.entries(raw)) {
|
|
359
372
|
if (key === "engine") {
|
|
@@ -378,10 +391,12 @@ export function assertGatePolicySettings(where: string, raw: unknown): asserts r
|
|
|
378
391
|
} else if (key === "command") {
|
|
379
392
|
if (!Array.isArray(value) || value.length === 0 || !value.every((a) => typeof a === "string" && a !== "")) throw new Error(`${where}.command must be argv, a non-empty list of strings`);
|
|
380
393
|
if (!value.some((a: string) => a.includes("{input}"))) throw new Error(`${where}.command must pass the input file as {input}`);
|
|
394
|
+
} else if (key === "install") {
|
|
395
|
+
if (typeof value !== "boolean") throw new Error(`${where}.install must be true or false`);
|
|
381
396
|
} else if (key === "outcomes" || key === "allow_unattended" || key === "allowUnattended") {
|
|
382
397
|
throw new Error(`${where}.${key}: chant's policy step denies and holds only; a non-empty plan always waits for a person (ws-104), so there is no unattended outcome`);
|
|
383
398
|
} else {
|
|
384
|
-
throw new Error(`${where}.${key} is not a setting; the settings are engine, path, source, namespace, command and
|
|
399
|
+
throw new Error(`${where}.${key} is not a setting; the settings are engine, path, source, namespace, command, packs and install`);
|
|
385
400
|
}
|
|
386
401
|
}
|
|
387
402
|
}
|
package/src/lexicon.ts
CHANGED
|
@@ -547,8 +547,25 @@ export interface ComponentPipelineJob {
|
|
|
547
547
|
needs: string[];
|
|
548
548
|
}
|
|
549
549
|
|
|
550
|
+
/**
|
|
551
|
+
* What a Forgejo workflow's run group is keyed on (#4771): `commit`, one
|
|
552
|
+
* group per event and commit, so a merge's run is never cancelled by another
|
|
553
|
+
* commit's; `pull-request`, one group per run except that a pull request's
|
|
554
|
+
* runs share one and cancel each other.
|
|
555
|
+
*/
|
|
556
|
+
export type RunGroupKey = "commit" | "pull-request";
|
|
557
|
+
export const RUN_GROUP_KEYS: readonly RunGroupKey[] = ["commit", "pull-request"];
|
|
558
|
+
|
|
550
559
|
/** Generic knobs for generate mode, applied once across every generated job (never per component). */
|
|
551
560
|
export interface ComponentPipelineOptions {
|
|
561
|
+
/**
|
|
562
|
+
* A workflow-level run group (#4771), written on Forgejo only. Forgejo
|
|
563
|
+
* gives a workflow with no workflow-level `concurrency:` a group per ref,
|
|
564
|
+
* workflow and event, and cancels the run in progress when a push starts
|
|
565
|
+
* another. GitHub cancels nothing without a group and GitLab's jobs are not
|
|
566
|
+
* `interruptible`, so their files are unchanged. Unset, no group.
|
|
567
|
+
*/
|
|
568
|
+
runGroup?: RunGroupKey;
|
|
552
569
|
/** Target environment threaded into the trigger command. */
|
|
553
570
|
env?: string;
|
|
554
571
|
/** The trigger command per component, argv with `{name}` substituted. */
|
|
@@ -1105,7 +1122,7 @@ export interface OpPipelineResult {
|
|
|
1105
1122
|
* of the spec file the jobs read.
|
|
1106
1123
|
*/
|
|
1107
1124
|
export interface OpWavesPipelineOptions
|
|
1108
|
-
extends Pick<ComponentPipelineOptions, "image" | "beforeScript" | "extraScript" | "variables" | "opsStage" | "opsFileName"> {
|
|
1125
|
+
extends Pick<ComponentPipelineOptions, "image" | "beforeScript" | "extraScript" | "variables" | "opsStage" | "opsFileName" | "runGroup"> {
|
|
1109
1126
|
/** The spec's path from the repository root. Every job runs `chant run wave --spec <specFile>`. */
|
|
1110
1127
|
specFile: string;
|
|
1111
1128
|
/**
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One gated operation outside op waves (#4762): ask for a gated request
|
|
3
|
+
* (#4551), spend its approval, act, and record how it ended. The walk every
|
|
4
|
+
* engine's state operation shares: a Terraform root's export or lock
|
|
5
|
+
* release, a Pulumi stack's, a migration whose request carries its proof.
|
|
6
|
+
*
|
|
7
|
+
* It came from the terraform lexicon's `state-ops.ts` (#4618), which still
|
|
8
|
+
* exports it as `gatedStateOp`, so a product written against the lexicon
|
|
9
|
+
* keeps working.
|
|
10
|
+
*
|
|
11
|
+
* A request asked again binds the same digest: chant's digest covers the
|
|
12
|
+
* request's `at`, so {@link requestedAt} reuses the `at` of the newest
|
|
13
|
+
* standing, unspent request with the same kind, subject, requester and
|
|
14
|
+
* fields. The spend is pushed before `act` runs, so no approval acts twice.
|
|
15
|
+
*/
|
|
16
|
+
import { createHash } from "node:crypto";
|
|
17
|
+
import {
|
|
18
|
+
finishGatedRequest,
|
|
19
|
+
readGatedRequests,
|
|
20
|
+
requestGate,
|
|
21
|
+
spendGatedRequest,
|
|
22
|
+
type GatedRequest,
|
|
23
|
+
type GatedRequestRules,
|
|
24
|
+
type RequestGateInput,
|
|
25
|
+
type RequestGateResult,
|
|
26
|
+
} from "./gated-request";
|
|
27
|
+
|
|
28
|
+
/** The gated-request ledger, injectable for tests. Default chant's on `chant/lifecycle`. */
|
|
29
|
+
export interface GatedRequestLedger {
|
|
30
|
+
requestGate: typeof requestGate;
|
|
31
|
+
spendGatedRequest: typeof spendGatedRequest;
|
|
32
|
+
finishGatedRequest: typeof finishGatedRequest;
|
|
33
|
+
readGatedRequests: typeof readGatedRequests;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** chant's ledger on `chant/lifecycle`. */
|
|
37
|
+
export const chantGatedLedger: GatedRequestLedger = { requestGate, spendGatedRequest, finishGatedRequest, readGatedRequests };
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The time a request was first asked, so asking again binds the same
|
|
41
|
+
* digest. The newest standing, unspent request with the same kind, subject,
|
|
42
|
+
* requester and fields gives its `at`; with none, `now`.
|
|
43
|
+
*/
|
|
44
|
+
export async function requestedAt(
|
|
45
|
+
op: string,
|
|
46
|
+
request: Omit<GatedRequest, "at">,
|
|
47
|
+
options: { now?: string; cwd?: string; ledger?: Pick<GatedRequestLedger, "readGatedRequests"> } = {},
|
|
48
|
+
): Promise<string> {
|
|
49
|
+
const now = options.now ?? new Date().toISOString();
|
|
50
|
+
const read = await (options.ledger ?? chantGatedLedger).readGatedRequests(op, options.cwd ? { cwd: options.cwd } : undefined).catch(() => []);
|
|
51
|
+
const same = JSON.stringify(Object.entries(request.fields).sort());
|
|
52
|
+
const standing = read
|
|
53
|
+
.filter((g) => !g.applied && g.request.kind === request.kind && g.request.subject === request.subject && g.request.by === request.by)
|
|
54
|
+
.filter((g) => JSON.stringify(Object.entries(g.request.fields).sort()) === same)
|
|
55
|
+
.filter((g) => !g.pending.expiresAt || g.pending.expiresAt > now)
|
|
56
|
+
.at(-1);
|
|
57
|
+
return standing?.request.at ?? now;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The digest `requestGate` puts in `fields.proof` for an attachment. */
|
|
61
|
+
export const attachmentDigest = (attachment: string): string => `sha256:${createHash("sha256").update(attachment).digest("hex")}`;
|
|
62
|
+
|
|
63
|
+
export interface GatedOpInput {
|
|
64
|
+
op: string;
|
|
65
|
+
/** The request, less `at`, which {@link requestedAt} fills. */
|
|
66
|
+
request: Omit<GatedRequest, "at"> & { at?: string };
|
|
67
|
+
rules: GatedRequestRules;
|
|
68
|
+
description?: string;
|
|
69
|
+
expiry?: string;
|
|
70
|
+
base?: string;
|
|
71
|
+
/** A document the request carries, such as a migration's proof; its digest joins the request. */
|
|
72
|
+
attachment?: string;
|
|
73
|
+
cwd?: string;
|
|
74
|
+
now?: string;
|
|
75
|
+
runId?: string;
|
|
76
|
+
commit?: string;
|
|
77
|
+
ledger?: GatedRequestLedger;
|
|
78
|
+
gates?: RequestGateInput["gates"];
|
|
79
|
+
/**
|
|
80
|
+
* A last check just before the spend, for a fact that can change while the
|
|
81
|
+
* request waited (a run that may hold the lock came back). A string refuses
|
|
82
|
+
* the operation with that reason, and the approval is not spent.
|
|
83
|
+
*/
|
|
84
|
+
beforeSpend?: () => Promise<string | undefined>;
|
|
85
|
+
/** The flat facts of a failed act's finished line, beside its `error`, such as the binary's exit code and last lines. */
|
|
86
|
+
failedDetail?: (error: Error) => Record<string, string | number | boolean>;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** How a gated operation ended. Only `done` and `failed` acted. */
|
|
90
|
+
export type GatedOpResult<T> =
|
|
91
|
+
| Exclude<RequestGateResult, { status: "approved" }>
|
|
92
|
+
| { status: "held"; digest: string; request: GatedRequest; reason: string }
|
|
93
|
+
| { status: "done"; digest: string; request: GatedRequest; approvedBy: string; output: T; recorded: boolean }
|
|
94
|
+
| { status: "failed"; digest: string; request: GatedRequest; approvedBy: string; error: Error; recorded: boolean };
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* What an approved operation's act returns: its output, and the flat facts
|
|
98
|
+
* its finished line records. `failed` records the finished line as failed
|
|
99
|
+
* while the output still comes back, for an act that ran and did not succeed
|
|
100
|
+
* (a migration whose write left a change).
|
|
101
|
+
*/
|
|
102
|
+
export type GatedOpAct<T> = (approved: { digest: string; request: GatedRequest; approvedBy: string }) => Promise<{ output: T; detail?: Record<string, string | number | boolean>; failed?: boolean }>;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Run `act` once its request is approved: ask (or read) the request, spend
|
|
106
|
+
* the approval, act, and record how it ended. A spend that cannot be pushed
|
|
107
|
+
* throws before `act` runs. A finished line that cannot be written is
|
|
108
|
+
* reported as `recorded: false`; the act already ran.
|
|
109
|
+
*/
|
|
110
|
+
export async function gatedOp<T>(input: GatedOpInput, act: GatedOpAct<T>): Promise<GatedOpResult<T>> {
|
|
111
|
+
const ledger = input.ledger ?? chantGatedLedger;
|
|
112
|
+
// The proof's digest is a field of the stored request, so the standing request is matched with it.
|
|
113
|
+
const fields = input.attachment !== undefined ? { ...input.request.fields, proof: attachmentDigest(input.attachment) } : input.request.fields;
|
|
114
|
+
const at = input.request.at ?? (await requestedAt(input.op, { ...input.request, fields }, { ...(input.now ? { now: input.now } : {}), ...(input.cwd ? { cwd: input.cwd } : {}), ledger }));
|
|
115
|
+
const request: GatedRequest = { kind: input.request.kind, subject: input.request.subject, by: input.request.by, at, fields };
|
|
116
|
+
const asked = await ledger.requestGate({
|
|
117
|
+
op: input.op,
|
|
118
|
+
request,
|
|
119
|
+
rules: input.rules,
|
|
120
|
+
...(input.description ? { description: input.description } : {}),
|
|
121
|
+
...(input.expiry ? { expiry: input.expiry } : {}),
|
|
122
|
+
...(input.base ? { base: input.base } : {}),
|
|
123
|
+
...(input.attachment !== undefined ? { attachment: input.attachment } : {}),
|
|
124
|
+
...(input.cwd ? { cwd: input.cwd } : {}),
|
|
125
|
+
...(input.now ? { now: input.now } : {}),
|
|
126
|
+
...(input.gates ? { gates: input.gates } : {}),
|
|
127
|
+
});
|
|
128
|
+
if (asked.status !== "approved") return asked;
|
|
129
|
+
const { digest } = asked;
|
|
130
|
+
const approvedBy = asked.approval.resolvedBy;
|
|
131
|
+
const held = await input.beforeSpend?.();
|
|
132
|
+
if (held) return { status: "held", digest, request: asked.request, reason: held };
|
|
133
|
+
const spent = await ledger.spendGatedRequest({
|
|
134
|
+
op: input.op,
|
|
135
|
+
gate: request.subject,
|
|
136
|
+
digest,
|
|
137
|
+
approval: asked.approval,
|
|
138
|
+
...(input.runId ? { runId: input.runId } : {}),
|
|
139
|
+
...(input.commit ? { commit: input.commit } : {}),
|
|
140
|
+
...(input.cwd ? { cwd: input.cwd } : {}),
|
|
141
|
+
...(input.now ? { now: input.now } : {}),
|
|
142
|
+
});
|
|
143
|
+
const finish = async (result: "applied" | "failed", detail?: Record<string, string | number | boolean>): Promise<boolean> => {
|
|
144
|
+
try {
|
|
145
|
+
const f = await ledger.finishGatedRequest({ op: input.op, gate: request.subject, digest, applied: spent.timestamp, result, ...(detail ? { detail } : {}), ...(input.cwd ? { cwd: input.cwd } : {}) });
|
|
146
|
+
return f.pushed;
|
|
147
|
+
} catch {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
try {
|
|
152
|
+
const { output, detail, failed } = await act({ digest, request: asked.request, approvedBy });
|
|
153
|
+
return { status: "done", digest, request: asked.request, approvedBy, output, recorded: await finish(failed ? "failed" : "applied", detail) };
|
|
154
|
+
} catch (e) {
|
|
155
|
+
const error = e instanceof Error ? e : new Error(String(e));
|
|
156
|
+
return { status: "failed", digest, request: asked.request, approvedBy, error, recorded: await finish("failed", { ...(input.failedDetail?.(error) ?? {}), error: error.message.slice(0, 400) }) };
|
|
157
|
+
}
|
|
158
|
+
}
|
|
@@ -687,6 +687,98 @@ describe("lifecycle/git", () => {
|
|
|
687
687
|
}
|
|
688
688
|
});
|
|
689
689
|
|
|
690
|
+
describe("on git older than 2.40, without merge-tree --merge-base (#4768)", () => {
|
|
691
|
+
/** A `git` ahead of the real one on PATH that refuses `merge-tree --merge-base` as git 2.39 does: usage text, exit 129. */
|
|
692
|
+
async function withOldGit(fn: () => Promise<void>): Promise<void> {
|
|
693
|
+
const real = spawnSync("sh", ["-c", "command -v git"], { encoding: "utf-8" }).stdout.trim();
|
|
694
|
+
const bin = join(tmpdir(), `chant-old-git-${Date.now()}-${Math.random()}`);
|
|
695
|
+
mkdirSync(bin);
|
|
696
|
+
writeFileSync(
|
|
697
|
+
join(bin, "git"),
|
|
698
|
+
`#!/bin/sh\nfor a in "$@"; do case "$a" in --merge-base=*) echo "usage: git merge-tree [--write-tree] [<options>] <branch1> <branch2>" >&2; exit 129;; esac; done\nexec "${real}" "$@"\n`,
|
|
699
|
+
{ mode: 0o755 },
|
|
700
|
+
);
|
|
701
|
+
const path = process.env.PATH;
|
|
702
|
+
process.env.PATH = `${bin}:${path ?? ""}`;
|
|
703
|
+
try {
|
|
704
|
+
await fn();
|
|
705
|
+
} finally {
|
|
706
|
+
process.env.PATH = path;
|
|
707
|
+
const { rm } = await import("node:fs/promises");
|
|
708
|
+
await rm(bin, { recursive: true, force: true });
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
test("a lost race replays on the remote's tip and lands, with both writes kept", async () => {
|
|
713
|
+
const { a, b, remotePath, done } = await twoWriters();
|
|
714
|
+
try {
|
|
715
|
+
await withOldGit(async () => {
|
|
716
|
+
await advance(a, "wave.json", '{"wave":1}');
|
|
717
|
+
await writeBlobToPath("prod", "merge.json", '{"merge":1}', "Merge record: abc", { cwd: b });
|
|
718
|
+
expect(await pushLifecycleRebasing({ cwd: b })).toEqual({ pushed: true });
|
|
719
|
+
});
|
|
720
|
+
const tip = git(["rev-parse", "refs/heads/chant/lifecycle"], remotePath).stdout.trim();
|
|
721
|
+
expect(git(["show", `${tip}:prod/merge.json`], remotePath).stdout).toBe('{"merge":1}');
|
|
722
|
+
expect(git(["show", `${tip}:prod/wave.json`], remotePath).stdout).toBe('{"wave":1}');
|
|
723
|
+
expect(git(["log", "-1", "--format=%s|%an", tip], remotePath).stdout.trim()).toBe("Merge record: abc|Record");
|
|
724
|
+
expect(git(["log", "-1", "--format=%s", `${tip}^`], remotePath).stdout.trim()).toBe("wave: wave.json");
|
|
725
|
+
} finally {
|
|
726
|
+
await done();
|
|
727
|
+
}
|
|
728
|
+
});
|
|
729
|
+
|
|
730
|
+
test("two writers that append lines to one ledger file both land, in push order", async () => {
|
|
731
|
+
const { a, b, remotePath, done } = await twoWriters();
|
|
732
|
+
try {
|
|
733
|
+
await withOldGit(async () => {
|
|
734
|
+
git(["fetch", "-q", "origin", "+refs/heads/chant/lifecycle:refs/remotes/origin/chant/lifecycle"], a);
|
|
735
|
+
await appendReleaseRecordLine("prod", '{"from":"wave"}', { cwd: a });
|
|
736
|
+
expect(await pushLifecycle({ cwd: a })).toBe(true);
|
|
737
|
+
await appendReleaseRecordLine("prod", '{"from":"record"}', { cwd: b });
|
|
738
|
+
expect(await pushLifecycleRebasing({ cwd: b })).toEqual({ pushed: true });
|
|
739
|
+
});
|
|
740
|
+
const tip = git(["rev-parse", "refs/heads/chant/lifecycle"], remotePath).stdout.trim();
|
|
741
|
+
expect(git(["show", `${tip}:prod/releases.jsonl`], remotePath).stdout).toBe('{"from":"wave"}\n{"from":"record"}\n');
|
|
742
|
+
} finally {
|
|
743
|
+
await done();
|
|
744
|
+
}
|
|
745
|
+
});
|
|
746
|
+
|
|
747
|
+
test("edits to different lines of one file by the two writers merge as merge-tree merges them", async () => {
|
|
748
|
+
const lines = (first: string, last: string) => [first, "2", "3", "4", "5", "6", "7", "8", last].join("\n") + "\n";
|
|
749
|
+
const { a, b, remotePath, done } = await twoWriters();
|
|
750
|
+
try {
|
|
751
|
+
await advance(a, "notes.txt", lines("1", "9"));
|
|
752
|
+
git(["fetch", "-q", "origin", "chant/lifecycle:chant/lifecycle"], b);
|
|
753
|
+
git(["fetch", "-q", "origin", "+refs/heads/chant/lifecycle:refs/remotes/origin/chant/lifecycle"], b);
|
|
754
|
+
await withOldGit(async () => {
|
|
755
|
+
await advance(a, "notes.txt", lines("wave", "9"));
|
|
756
|
+
await writeBlobToPath("prod", "notes.txt", lines("1", "record"), "record: notes", { cwd: b });
|
|
757
|
+
expect(await pushLifecycleRebasing({ cwd: b })).toEqual({ pushed: true });
|
|
758
|
+
});
|
|
759
|
+
const tip = git(["rev-parse", "refs/heads/chant/lifecycle"], remotePath).stdout.trim();
|
|
760
|
+
expect(git(["show", `${tip}:prod/notes.txt`], remotePath).stdout).toBe(lines("wave", "record"));
|
|
761
|
+
} finally {
|
|
762
|
+
await done();
|
|
763
|
+
}
|
|
764
|
+
});
|
|
765
|
+
|
|
766
|
+
test("a commit that rewrote the file the other writer changed is still refused by name", async () => {
|
|
767
|
+
const { a, b, done } = await twoWriters();
|
|
768
|
+
try {
|
|
769
|
+
await withOldGit(async () => {
|
|
770
|
+
await advance(a, "merge.json", '{"merge":"wave"}');
|
|
771
|
+
await writeBlobToPath("prod", "merge.json", '{"merge":"record"}', "Merge record: abc", { cwd: b });
|
|
772
|
+
const err = await pushLifecycleRebasing({ cwd: b }).catch((e: unknown) => e);
|
|
773
|
+
expect(err).toBeInstanceOf(LifecyclePushLostError);
|
|
774
|
+
expect((err as Error).message).toContain("changes files the other writer changed too");
|
|
775
|
+
});
|
|
776
|
+
} finally {
|
|
777
|
+
await done();
|
|
778
|
+
}
|
|
779
|
+
});
|
|
780
|
+
});
|
|
781
|
+
|
|
690
782
|
test("no remote reads as the local note, as pushLifecycleReport says it", async () => {
|
|
691
783
|
await withTestDir(async (dir) => {
|
|
692
784
|
await initRepo(dir);
|
package/src/lifecycle/git.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* All operations use git plumbing commands — no checkout, no branch switching,
|
|
5
5
|
* no working tree changes.
|
|
6
6
|
*/
|
|
7
|
-
import { unlinkSync } from "node:fs";
|
|
7
|
+
import { unlinkSync, writeFileSync } from "node:fs";
|
|
8
8
|
import { isAbsolute, join } from "node:path";
|
|
9
9
|
import { getRuntime } from "../runtime-adapter";
|
|
10
10
|
import { memberLedgerPrefix, resolveMemberLedger } from "./member-ledger";
|
|
@@ -1028,7 +1028,9 @@ async function rebaseLifecycleOnRemote(remote: string, cwd?: string): Promise<{
|
|
|
1028
1028
|
for (const commit of ours) {
|
|
1029
1029
|
const parent = await run(["rev-parse", "--verify", "--quiet", `${commit}^1`]);
|
|
1030
1030
|
if (parent.exitCode !== 0) return { error: `${commit.slice(0, 12)} shares no history with the remote's ${STATE_BRANCH}` };
|
|
1031
|
-
|
|
1031
|
+
let merged = await run(["merge-tree", "--write-tree", `--merge-base=${parent.stdout.trim()}`, onto, commit]);
|
|
1032
|
+
// #4768: git before 2.40 has no --merge-base and exits 129 with its usage text (node:22-bookworm-slim's apt git is 2.39).
|
|
1033
|
+
if (merged.exitCode === 129) merged = await mergeTreesInIndex(parent.stdout.trim(), onto, commit, cwd);
|
|
1032
1034
|
const subject = firstLine((await run(["log", "-1", "--format=%s", commit])).stdout);
|
|
1033
1035
|
let tree: string;
|
|
1034
1036
|
if (merged.exitCode === 1) {
|
|
@@ -1064,6 +1066,73 @@ async function rebaseLifecycleOnRemote(remote: string, cwd?: string): Promise<{
|
|
|
1064
1066
|
}
|
|
1065
1067
|
}
|
|
1066
1068
|
|
|
1069
|
+
/**
|
|
1070
|
+
* `git merge-tree --write-tree --merge-base=<base> <ours> <theirs>` for git
|
|
1071
|
+
* older than 2.40 (#4768), with the same exit codes: 0 with the merged tree
|
|
1072
|
+
* on stdout, 1 when a path conflicts, anything else when git failed. A 3-way
|
|
1073
|
+
* `read-tree` into a scratch index takes every path only one side changed;
|
|
1074
|
+
* a file both sides changed is merged by `git merge-file`, and any other
|
|
1075
|
+
* path both sides touched (an add on both sides, a delete against a change)
|
|
1076
|
+
* is a conflict. Nothing touches a work tree.
|
|
1077
|
+
*/
|
|
1078
|
+
async function mergeTreesInIndex(base: string, ours: string, theirs: string, cwd?: string): Promise<{ exitCode: number; stdout: string; stderr: string }> {
|
|
1079
|
+
const rt = getRuntime();
|
|
1080
|
+
const scratch = (await rt.spawn(["git", "rev-parse", "--git-path", `chant-lifecycle-merge-${process.pid}`], { cwd, env: C_LOCALE_ENV })).stdout.trim();
|
|
1081
|
+
if (!scratch) return { exitCode: 2, stdout: "", stderr: "no scratch index path" };
|
|
1082
|
+
const index = isAbsolute(scratch) || !cwd ? scratch : join(cwd, scratch);
|
|
1083
|
+
const env = { ...C_LOCALE_ENV, GIT_INDEX_FILE: `${index}.index` };
|
|
1084
|
+
const run = (args: string[], stdin?: string) => rt.spawn(["git", ...args], { cwd, env, ...(stdin !== undefined ? { stdin } : {}) });
|
|
1085
|
+
const files: string[] = [];
|
|
1086
|
+
try {
|
|
1087
|
+
const read = await run(["read-tree", "-i", "-m", "--aggressive", base, ours, theirs]);
|
|
1088
|
+
if (read.exitCode !== 0) return read;
|
|
1089
|
+
const unmerged = await run(["ls-files", "-u", "-z"]);
|
|
1090
|
+
if (unmerged.exitCode !== 0) return unmerged;
|
|
1091
|
+
// "<mode> <sha> <stage>\t<path>" per stage of each path both sides changed.
|
|
1092
|
+
const stages = new Map<string, Map<number, { mode: string; sha: string }>>();
|
|
1093
|
+
for (const entry of unmerged.stdout.split("\0").filter((e) => e !== "")) {
|
|
1094
|
+
const tab = entry.indexOf("\t");
|
|
1095
|
+
const [mode, sha, stage] = entry.slice(0, tab).split(" ");
|
|
1096
|
+
const path = entry.slice(tab + 1);
|
|
1097
|
+
if (!stages.has(path)) stages.set(path, new Map());
|
|
1098
|
+
stages.get(path)!.set(Number(stage), { mode: mode!, sha: sha! });
|
|
1099
|
+
}
|
|
1100
|
+
for (const [path, by] of stages) {
|
|
1101
|
+
const b = by.get(1);
|
|
1102
|
+
const o = by.get(2);
|
|
1103
|
+
const t = by.get(3);
|
|
1104
|
+
if (!b || !o || !t || o.mode !== t.mode || !o.mode.startsWith("100")) return { exitCode: 1, stdout: "", stderr: `${path} conflicts` };
|
|
1105
|
+
const blobs: string[] = [];
|
|
1106
|
+
for (const [i, s] of [b, o, t].entries()) {
|
|
1107
|
+
const content = await run(["cat-file", "blob", s.sha]);
|
|
1108
|
+
if (content.exitCode !== 0) return content;
|
|
1109
|
+
const file = `${index}.${i}`;
|
|
1110
|
+
writeFileSync(file, content.stdout);
|
|
1111
|
+
files.push(file);
|
|
1112
|
+
blobs.push(file);
|
|
1113
|
+
}
|
|
1114
|
+
// merge-file <current> <base> <other>: ours is current, as merge-tree's first branch is.
|
|
1115
|
+
const mergedFile = await rt.spawn(["git", "merge-file", "-p", blobs[1]!, blobs[0]!, blobs[2]!], { cwd, env: C_LOCALE_ENV });
|
|
1116
|
+
if (mergedFile.exitCode !== 0) return { exitCode: mergedFile.exitCode > 0 && mergedFile.exitCode < 128 ? 1 : 2, stdout: "", stderr: mergedFile.stderr || `${path} conflicts` };
|
|
1117
|
+
const hashed = await run(["hash-object", "-w", "--stdin"], mergedFile.stdout);
|
|
1118
|
+
if (hashed.exitCode !== 0) return hashed;
|
|
1119
|
+
const removed = await run(["update-index", "--force-remove", "--", path]);
|
|
1120
|
+
if (removed.exitCode !== 0) return removed;
|
|
1121
|
+
const added = await run(["update-index", "--add", "--cacheinfo", `${o.mode},${hashed.stdout.trim()},${path}`]);
|
|
1122
|
+
if (added.exitCode !== 0) return added;
|
|
1123
|
+
}
|
|
1124
|
+
return await run(["write-tree"]);
|
|
1125
|
+
} finally {
|
|
1126
|
+
for (const f of [...files, `${index}.index`]) {
|
|
1127
|
+
try {
|
|
1128
|
+
unlinkSync(f);
|
|
1129
|
+
} catch {
|
|
1130
|
+
// Not written.
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1067
1136
|
/**
|
|
1068
1137
|
* Replay a commit whose only changes are lines appended to `.jsonl` ledger
|
|
1069
1138
|
* files, or new `.jsonl` files, onto `onto` (plywood#22 finding 9): each
|