@intentius/chant 0.124.70 → 0.124.72
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ci-agent.d.ts +12 -1
- package/dist/ci-agent.d.ts.map +1 -1
- package/dist/ci-agent.js +13 -8
- package/dist/ci-commit.d.ts +326 -0
- package/dist/ci-commit.d.ts.map +1 -0
- package/dist/ci-commit.js +725 -0
- package/dist/ci-estate/delivery.d.ts +75 -2
- package/dist/ci-estate/delivery.d.ts.map +1 -1
- package/dist/ci-estate/delivery.js +129 -0
- package/dist/ci-estate.d.ts +3 -1
- package/dist/ci-estate.d.ts.map +1 -1
- package/dist/ci-estate.js +4 -1
- package/dist/ci-product-words.d.ts +6 -1
- package/dist/ci-product-words.d.ts.map +1 -1
- package/dist/ci-product-words.js +3 -1
- package/dist/ci-product.d.ts +1 -1
- package/dist/ci-product.d.ts.map +1 -1
- package/dist/ci-product.js +1 -1
- package/dist/cli/handlers/approve.d.ts.map +1 -1
- package/dist/cli/handlers/approve.js +7 -2
- package/dist/cli/handlers/commit.d.ts +26 -0
- package/dist/cli/handlers/commit.d.ts.map +1 -0
- package/dist/cli/handlers/commit.js +145 -0
- package/dist/cli/handlers/notify-relay.d.ts.map +1 -1
- package/dist/cli/handlers/notify-relay.js +22 -2
- package/dist/cli/handlers/pr.d.ts +6 -0
- package/dist/cli/handlers/pr.d.ts.map +1 -1
- package/dist/cli/handlers/pr.js +16 -7
- package/dist/cli/handlers/release.d.ts +9 -0
- package/dist/cli/handlers/release.d.ts.map +1 -1
- package/dist/cli/handlers/release.js +114 -5
- package/dist/cli/main.d.ts.map +1 -1
- package/dist/cli/main.js +17 -0
- package/dist/components/product-pipeline.d.ts +33 -0
- package/dist/components/product-pipeline.d.ts.map +1 -1
- package/dist/components/product-pipeline.js +34 -1
- package/dist/components/release-compile.d.ts +2 -0
- package/dist/components/release-compile.d.ts.map +1 -1
- package/dist/components/release-compile.js +8 -4
- package/dist/config.d.ts +60 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +16 -0
- package/dist/lexicon.d.ts +54 -0
- package/dist/lexicon.d.ts.map +1 -1
- package/dist/lexicon.js +28 -0
- package/dist/lifecycle/__fixtures__/publish-fakes.d.ts +4 -0
- package/dist/lifecycle/__fixtures__/publish-fakes.d.ts.map +1 -1
- package/dist/lifecycle/__fixtures__/publish-fakes.js +6 -2
- package/dist/lifecycle/gated-request.d.ts +3 -1
- package/dist/lifecycle/gated-request.d.ts.map +1 -1
- package/dist/lifecycle/gated-request.js +5 -2
- package/dist/lifecycle/git.d.ts.map +1 -1
- package/dist/lifecycle/git.js +57 -0
- package/dist/lifecycle/publish-declaration.d.ts +16 -0
- package/dist/lifecycle/publish-declaration.d.ts.map +1 -1
- package/dist/lifecycle/publish-declaration.js +35 -3
- package/dist/lifecycle/publish-forge.d.ts +12 -0
- package/dist/lifecycle/publish-forge.d.ts.map +1 -1
- package/dist/lifecycle/publish-forge.js +102 -19
- package/dist/lifecycle/publish-npm.d.ts +15 -0
- package/dist/lifecycle/publish-npm.d.ts.map +1 -1
- package/dist/lifecycle/publish-npm.js +28 -1
- package/dist/lifecycle/publish-oci.d.ts +2 -0
- package/dist/lifecycle/publish-oci.d.ts.map +1 -1
- package/dist/lifecycle/publish-oci.js +10 -0
- package/dist/lifecycle/release-cut.d.ts +7 -1
- package/dist/lifecycle/release-cut.d.ts.map +1 -1
- package/dist/lifecycle/release-cut.js +8 -1
- package/dist/lifecycle/release-images.d.ts +87 -0
- package/dist/lifecycle/release-images.d.ts.map +1 -0
- package/dist/lifecycle/release-images.js +138 -0
- package/dist/lifecycle/release-publish.d.ts +2 -0
- package/dist/lifecycle/release-publish.d.ts.map +1 -1
- package/dist/notify-relay.d.ts +41 -1
- package/dist/notify-relay.d.ts.map +1 -1
- package/dist/notify-relay.js +40 -0
- package/dist/notify.d.ts +16 -3
- package/dist/notify.d.ts.map +1 -1
- package/dist/notify.js +16 -10
- package/dist/op/activities/review-agent.d.ts.map +1 -1
- package/dist/op/activities/review-agent.js +4 -2
- package/dist/op/gate.d.ts +5 -1
- package/dist/op/gate.d.ts.map +1 -1
- package/dist/op/gate.js +7 -2
- package/dist/op/op-waves-notify.d.ts +1 -1
- package/dist/op/op-waves-notify.d.ts.map +1 -1
- package/dist/op/op-waves-notify.js +2 -1
- package/dist/op/op-waves-run.d.ts.map +1 -1
- package/dist/op/op-waves-run.js +12 -8
- package/dist/op/op-waves.d.ts +9 -0
- package/dist/op/op-waves.d.ts.map +1 -1
- package/dist/op/op-waves.js +11 -0
- package/dist/pr-forge-base.d.ts +18 -0
- package/dist/pr-forge-base.d.ts.map +1 -0
- package/dist/pr-forge-base.js +29 -0
- package/dist/release-floors.d.ts +1 -1
- package/dist/release-floors.js +1 -1
- package/dist/wave-runtime.mjs +315 -315
- package/dist/workspace/ci-green.d.ts.map +1 -1
- package/dist/workspace/ci-green.js +1 -2
- package/dist/workspace/release-workflow.d.ts +47 -0
- package/dist/workspace/release-workflow.d.ts.map +1 -1
- package/dist/workspace/release-workflow.js +258 -14
- package/package.json +1 -1
- package/src/ci-agent.ts +13 -8
- package/src/ci-commit.test.ts +415 -0
- package/src/ci-commit.ts +901 -0
- package/src/ci-estate/__fixtures__/terragucci-dora.json +1305 -0
- package/src/ci-estate/delivery.ts +186 -2
- package/src/ci-estate/records-delivery.test.ts +55 -0
- package/src/ci-estate.ts +10 -0
- package/src/ci-product-words.ts +6 -2
- package/src/ci-product.ts +1 -0
- package/src/cli/handlers/approve.ts +6 -2
- package/src/cli/handlers/commit.ts +155 -0
- package/src/cli/handlers/notify-relay.ts +21 -2
- package/src/cli/handlers/operator.test.ts +2 -0
- package/src/cli/handlers/pr-wave-approve-lines.test.ts +32 -0
- package/src/cli/handlers/pr.ts +17 -7
- package/src/cli/handlers/release.ts +110 -5
- package/src/cli/main.ts +17 -0
- package/src/components/product-pipeline.test.ts +28 -1
- package/src/components/product-pipeline.ts +54 -1
- package/src/components/release-compile.ts +9 -3
- package/src/config.ts +31 -0
- package/src/lexicon.ts +72 -0
- package/src/lifecycle/__fixtures__/publish-fakes.ts +11 -5
- package/src/lifecycle/gated-request-spend.test.ts +115 -0
- package/src/lifecycle/gated-request.ts +5 -2
- package/src/lifecycle/git.ts +53 -0
- package/src/lifecycle/ledger-two-clones.test.ts +132 -0
- package/src/lifecycle/publish-declaration.ts +35 -3
- package/src/lifecycle/publish-forge-notes.test.ts +193 -0
- package/src/lifecycle/publish-forge.ts +100 -22
- package/src/lifecycle/publish-npm.ts +31 -2
- package/src/lifecycle/publish-oci.ts +10 -0
- package/src/lifecycle/publish-targets.test.ts +2 -1
- package/src/lifecycle/release-cut.ts +17 -1
- package/src/lifecycle/release-images.test.ts +242 -0
- package/src/lifecycle/release-images.ts +174 -0
- package/src/lifecycle/release-publish.ts +2 -0
- package/src/notify-relay.test.ts +55 -1
- package/src/notify-relay.ts +49 -1
- package/src/notify.ts +25 -12
- package/src/op/activities/review-agent.test.ts +20 -1
- package/src/op/activities/review-agent.ts +5 -2
- package/src/op/gate.ts +7 -2
- package/src/op/op-waves-approve-target.test.ts +108 -0
- package/src/op/op-waves-notify.ts +2 -2
- package/src/op/op-waves-run.ts +12 -7
- package/src/op/op-waves.ts +12 -0
- package/src/pr-forge-base.test.ts +37 -0
- package/src/pr-forge-base.ts +36 -0
- package/src/release-floors.ts +1 -1
- package/src/workspace/__fixtures__/release-workflow/forgejo-plain-member.yml +26 -0
- package/src/workspace/__fixtures__/release-workflow/forgejo-plain.yml +23 -0
- package/src/workspace/__fixtures__/release-workflow/forgejo-secure-member.yml +27 -0
- package/src/workspace/__fixtures__/release-workflow/forgejo-secure.yml +24 -0
- package/src/workspace/__fixtures__/release-workflow/github-plain-member.yml +26 -0
- package/src/workspace/__fixtures__/release-workflow/github-plain.yml +23 -0
- package/src/workspace/__fixtures__/release-workflow/github-secure-member.yml +27 -0
- package/src/workspace/__fixtures__/release-workflow/github-secure.yml +24 -0
- package/src/workspace/__fixtures__/release-workflow/gitlab-plain-member.yml +13 -0
- package/src/workspace/__fixtures__/release-workflow/gitlab-plain.yml +12 -0
- package/src/workspace/__fixtures__/release-workflow/gitlab-secure-member.yml +14 -0
- package/src/workspace/__fixtures__/release-workflow/gitlab-secure.yml +13 -0
- package/src/workspace/ci-green.ts +1 -2
- package/src/workspace/release-workflow.test.ts +199 -0
- package/src/workspace/release-workflow.ts +356 -14
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #4657 (option 1): the cut builds the release's images, records their
|
|
3
|
+
* digests, and hands them to the build that packs the package; one cut and
|
|
4
|
+
* one publish take the release out, and the package pins what is pushed.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { afterAll, describe, expect, test } from "vitest";
|
|
8
|
+
import { execFileSync } from "node:child_process";
|
|
9
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
10
|
+
import { dirname, join } from "node:path";
|
|
11
|
+
import { cutRelease, ReleaseCutError } from "./release-cut";
|
|
12
|
+
import { createRedactor } from "./publish-credentials";
|
|
13
|
+
import { PublishDeclarationSchema } from "./publish-declaration";
|
|
14
|
+
import { ociArchiveDigest } from "./publish-oci";
|
|
15
|
+
import { createPublishTargets } from "./publish-targets";
|
|
16
|
+
import { runPublish, PublishTargetError, type PublishContext } from "./release-publish";
|
|
17
|
+
import {
|
|
18
|
+
buildReleaseImages,
|
|
19
|
+
checkReleaseImages,
|
|
20
|
+
packagePinProblem,
|
|
21
|
+
pinnedDigests,
|
|
22
|
+
readPins,
|
|
23
|
+
releaseImageDigests,
|
|
24
|
+
ReleaseImageError,
|
|
25
|
+
unknownPins,
|
|
26
|
+
writeReleaseDigests,
|
|
27
|
+
} from "./release-images";
|
|
28
|
+
import type { Release, ReleaseArtifact } from "./release-store";
|
|
29
|
+
import { readFakeNpmRegistry, seedFakeNpmRegistry, startFakeRegistry, writeFakeNpm, writeLockfile, writeOciLayout, writePackage } from "./__fixtures__/publish-fakes";
|
|
30
|
+
import type { CompiledTarget, TargetCompiler } from "../components/release-compile";
|
|
31
|
+
import { TestRepo } from "../workspace/trust/test-repo";
|
|
32
|
+
|
|
33
|
+
const repos: TestRepo[] = [];
|
|
34
|
+
afterAll(() => {
|
|
35
|
+
for (const r of repos) rmSync(dirname(r.dir), { recursive: true, force: true });
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const D = (c: string) => `sha256:${c.repeat(64)}`;
|
|
39
|
+
const NO_OIDC = { oidc: false, ci: null, reason: "not in CI" };
|
|
40
|
+
|
|
41
|
+
describe("release.images (#4657)", () => {
|
|
42
|
+
test("names are checked, and an archive outside the member is refused", () => {
|
|
43
|
+
expect(() => checkReleaseImages([{ name: "a b", build: "x", archive: "a.tar" }])).toThrow(/not a name/);
|
|
44
|
+
expect(() => checkReleaseImages([{ name: "a", build: "x", archive: "a.tar" }, { name: "a", build: "y", archive: "b.tar" }])).toThrow(/twice/);
|
|
45
|
+
expect(() => checkReleaseImages([{ name: "a", build: "x", archive: "../a.tar" }])).toThrow(/not a path inside the member/);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("each build runs with SOURCE_DATE_EPOCH and the label, and its archive's top manifest digest is recorded", () => {
|
|
49
|
+
const r = new TestRepo("release-images-build");
|
|
50
|
+
repos.push(r);
|
|
51
|
+
const seen: Array<Record<string, string | undefined>> = [];
|
|
52
|
+
const built = buildReleaseImages({
|
|
53
|
+
images: [{ name: "tofu", build: "build tofu", archive: "out/tofu" }],
|
|
54
|
+
cwd: r.dir,
|
|
55
|
+
sourceDateEpoch: 1762473600,
|
|
56
|
+
label: "v2",
|
|
57
|
+
env: { PATH: process.env.PATH, CHANT_RELEASE_LABEL: "from the shell" },
|
|
58
|
+
archiveDigest: ociArchiveDigest,
|
|
59
|
+
run: (_command, o) => {
|
|
60
|
+
seen.push({ epoch: o.env.SOURCE_DATE_EPOCH, label: o.env.CHANT_RELEASE_LABEL });
|
|
61
|
+
writeOciLayout(join(o.cwd, "out/tofu"), "tofu layer");
|
|
62
|
+
return 0;
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
const expected = ociArchiveDigest(join(r.dir, "out/tofu"));
|
|
66
|
+
expect(built).toEqual([{ component: "tofu", kind: "image", path: "out/tofu", digest: expected }]);
|
|
67
|
+
expect(seen).toEqual([{ epoch: "1762473600", label: "v2" }]);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("a failed build and a build that writes no archive stop the cut", () => {
|
|
71
|
+
const base = { cwd: "/nowhere", sourceDateEpoch: 1, label: null, archiveDigest: () => D("a"), images: [{ name: "a", build: "false", archive: "a.tar" }] };
|
|
72
|
+
expect(() => buildReleaseImages({ ...base, run: () => 2 })).toThrow(ReleaseImageError);
|
|
73
|
+
expect(() => buildReleaseImages({ ...base, run: () => 0 })).toThrow(/wrote no a.tar/);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("pins: every sha256 digest in the file is checked against the release's images", () => {
|
|
77
|
+
const release = { artifactDigests: [{ component: "tofu", kind: "image" as const, path: "out/tofu", digest: D("a") }, { component: "x", kind: "asset" as const, path: "x", digest: D("b") }] };
|
|
78
|
+
expect(pinnedDigests(`{"tofu":"${D("a")}","again":"${D("a")}"}`)).toEqual([D("a")]);
|
|
79
|
+
expect(unknownPins(`{"tofu":"${D("a")}"}`, release)).toEqual([]);
|
|
80
|
+
// An asset's digest is not an image's.
|
|
81
|
+
expect(unknownPins(`{"tofu":"${D("b")}"}`, release)).toEqual([D("b")]);
|
|
82
|
+
expect(releaseImageDigests(release)).toEqual({ tofu: D("a") });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("pins are read from a package directory and from a packed .tgz", () => {
|
|
86
|
+
const r = new TestRepo("release-images-pins");
|
|
87
|
+
repos.push(r);
|
|
88
|
+
const pkg = join(r.dir, "pkg");
|
|
89
|
+
writePackage(pkg, "@acme/api", "1.0.0");
|
|
90
|
+
writeReleaseDigests(join(pkg, "dist", "digests.json"), { tofu: D("c") });
|
|
91
|
+
expect(readPins(pkg, "dist/digests.json", true)).toContain(D("c"));
|
|
92
|
+
expect(readPins(pkg, "dist/nope.json", true)).toBeNull();
|
|
93
|
+
const stage = join(r.dir, "stage");
|
|
94
|
+
mkdirSync(stage);
|
|
95
|
+
execFileSync("cp", ["-R", pkg, join(stage, "package")]);
|
|
96
|
+
execFileSync("tar", ["-czf", join(r.dir, "api.tgz"), "-C", stage, "package"]);
|
|
97
|
+
expect(readPins(join(r.dir, "api.tgz"), "dist/digests.json", false)).toContain(D("c"));
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
/** A member whose cut builds one image and packs one package that pins it, as terragucci's release does. */
|
|
102
|
+
async function cutWithImage(r: TestRepo, opts: { pin?: (digests: Record<string, string>) => Record<string, string> } = {}): Promise<{ release: Release; digestsFile: string; order: string[] }> {
|
|
103
|
+
r.write("chant.config.ts", `export default { environments: ["dev"] };\n`);
|
|
104
|
+
writeLockfile(r.dir);
|
|
105
|
+
r.write(".gitignore", "image/\npackages/\ndigests.json\n");
|
|
106
|
+
const head = r.commit("init");
|
|
107
|
+
r.git(["tag", `ci/green/${head}`]);
|
|
108
|
+
const digestsFile = join(r.dir, "digests.json");
|
|
109
|
+
const order: string[] = [];
|
|
110
|
+
const compiler: TargetCompiler = {
|
|
111
|
+
async compile(env): Promise<CompiledTarget> {
|
|
112
|
+
order.push(`compile ${env}`);
|
|
113
|
+
// The release's build command: pack the package with the digests the cut wrote.
|
|
114
|
+
const digests = JSON.parse(readFileSync(digestsFile, "utf-8")) as Record<string, string>;
|
|
115
|
+
const pkg = join(r.dir, "packages", "api");
|
|
116
|
+
writePackage(pkg, "@acme/api", "2.0.0");
|
|
117
|
+
writeReleaseDigests(join(pkg, "dist", "image-digests.json"), opts.pin ? opts.pin(digests) : digests);
|
|
118
|
+
return { env, params: [], components: [], buildSteps: {}, buildInputs: {}, componentFiles: {}, templates: [{ component: null, path: "dist/out.json", content: "{}\n" }] };
|
|
119
|
+
},
|
|
120
|
+
async buildArtifacts(): Promise<ReleaseArtifact[]> {
|
|
121
|
+
return [];
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
const cut = await cutRelease({
|
|
125
|
+
cwd: r.dir,
|
|
126
|
+
compiler,
|
|
127
|
+
label: "v2",
|
|
128
|
+
actor: "alice",
|
|
129
|
+
env: {},
|
|
130
|
+
now: () => new Date("2026-11-07T10:00:00.000Z"),
|
|
131
|
+
buildImages: () => {
|
|
132
|
+
order.push("images");
|
|
133
|
+
const built = buildReleaseImages({
|
|
134
|
+
images: [{ name: "tofu", build: "build", archive: "image" }],
|
|
135
|
+
cwd: r.dir,
|
|
136
|
+
sourceDateEpoch: 1762473600,
|
|
137
|
+
label: "v2",
|
|
138
|
+
archiveDigest: ociArchiveDigest,
|
|
139
|
+
run: (_c, o) => {
|
|
140
|
+
writeOciLayout(join(o.cwd, "image"), "tofu layer");
|
|
141
|
+
return 0;
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
writeReleaseDigests(digestsFile, Object.fromEntries(built.map((a) => [a.component, a.digest])));
|
|
145
|
+
return built;
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
return { release: cut.release, digestsFile, order };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
describe("one cut and one publish take an image and the package that pins it out (#4657)", () => {
|
|
152
|
+
test("the images are built before the compile, recorded, pinned by the package, pushed under that digest, and a publish run again sends nothing", async () => {
|
|
153
|
+
const r = new TestRepo("release-images-e2e");
|
|
154
|
+
repos.push(r);
|
|
155
|
+
const { release, order } = await cutWithImage(r);
|
|
156
|
+
expect(order).toEqual(["images", "compile dev"]);
|
|
157
|
+
const image = release.artifactDigests.find((a) => a.component === "tofu")!;
|
|
158
|
+
expect(image).toMatchObject({ kind: "image", path: "image" });
|
|
159
|
+
expect(readFileSync(join(r.dir, "packages/api/dist/image-digests.json"), "utf-8")).toContain(image.digest);
|
|
160
|
+
|
|
161
|
+
const reg = await startFakeRegistry({ username: "robot", password: "pw-images-1", token: "tok-images-1" });
|
|
162
|
+
try {
|
|
163
|
+
const log = join(dirname(r.dir), "npm.log");
|
|
164
|
+
seedFakeNpmRegistry(log, { packages: { "@acme/api": { versions: ["1.0.0"], distTags: { latest: "1.0.0" } } } });
|
|
165
|
+
const npm = writeFakeNpm(join(dirname(r.dir), "bin"), log);
|
|
166
|
+
const decls = [
|
|
167
|
+
{ type: "oci", image: "image", repository: `${reg.host}/acme/tofu` },
|
|
168
|
+
{ type: "npm", packages: ["packages/api"], pins: "dist/image-digests.json" },
|
|
169
|
+
].map((d) => PublishDeclarationSchema.parse(d));
|
|
170
|
+
const targets = await createPublishTargets(decls, { npm });
|
|
171
|
+
const env = { PATH: process.env.PATH ?? "", OCI_USERNAME: "robot", OCI_PASSWORD: "pw-images-1" };
|
|
172
|
+
const redactor = createRedactor(env);
|
|
173
|
+
const ctx: PublishContext = { release, label: "v2", releaseTag: "release/v2", provenance: NO_OIDC, cwd: r.dir, tag: null, env, redactor, log: () => {} };
|
|
174
|
+
const publish = () => runPublish({ cwd: r.dir, release, label: "v2", targets, ctx, say: () => {} });
|
|
175
|
+
|
|
176
|
+
await publish();
|
|
177
|
+
// The registry serves the image under the digest the package pins.
|
|
178
|
+
expect(reg.manifests.has(image.digest)).toBe(true);
|
|
179
|
+
expect(reg.manifests.has("v2")).toBe(true);
|
|
180
|
+
expect(readFakeNpmRegistry(log).packages["@acme/api"]!.distTags.latest).toBe("2.0.0");
|
|
181
|
+
|
|
182
|
+
// Run again: no image is pushed twice and the package is not published again.
|
|
183
|
+
const uploads = reg.uploads;
|
|
184
|
+
const npmLines = readFileSync(log, "utf-8").split("\n").filter(Boolean).length;
|
|
185
|
+
const again = await publish();
|
|
186
|
+
expect(reg.uploads).toBe(uploads);
|
|
187
|
+
const after = readFileSync(log, "utf-8").split("\n").filter(Boolean).slice(npmLines).map((l) => (JSON.parse(l) as { args: string[] }).args[0]);
|
|
188
|
+
expect(after).not.toContain("publish");
|
|
189
|
+
expect(again.published.length).toBe(2);
|
|
190
|
+
expect(existsSync(join(r.dir, "image", "index.json"))).toBe(true);
|
|
191
|
+
} finally {
|
|
192
|
+
await reg.close();
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test("a package that pins a digest the release does not record is refused before anything is published, and release verify's check names it", async () => {
|
|
197
|
+
const r = new TestRepo("release-images-bad-pin");
|
|
198
|
+
repos.push(r);
|
|
199
|
+
const { release } = await cutWithImage(r, { pin: () => ({ tofu: D("9") }) });
|
|
200
|
+
const decls = [PublishDeclarationSchema.parse({ type: "npm", packages: ["packages/api"], pins: "dist/image-digests.json" })];
|
|
201
|
+
const [target] = await createPublishTargets(decls, {});
|
|
202
|
+
const env = { PATH: process.env.PATH ?? "" };
|
|
203
|
+
const ctx: PublishContext = { release, label: "v2", releaseTag: "release/v2", provenance: NO_OIDC, cwd: r.dir, tag: null, env, redactor: createRedactor(env), log: () => {} };
|
|
204
|
+
const err = await target!.plan(ctx).catch((e: unknown) => e);
|
|
205
|
+
expect(err).toBeInstanceOf(PublishTargetError);
|
|
206
|
+
expect((err as PublishTargetError).code).toBe("publish-npm-pin-unknown");
|
|
207
|
+
expect(packagePinProblem(r.dir, release, decls)).toMatch(new RegExp(`packages/api pins ${D("9")} in dist/image-digests.json`));
|
|
208
|
+
|
|
209
|
+
const missing = [PublishDeclarationSchema.parse({ type: "npm", packages: ["packages/api"], pins: "dist/nope.json" })];
|
|
210
|
+
const [t2] = await createPublishTargets(missing, {});
|
|
211
|
+
expect(((await t2!.plan(ctx).catch((e: unknown) => e)) as PublishTargetError).code).toBe("publish-npm-pins-missing");
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test("an image named like a component is refused", async () => {
|
|
215
|
+
const r = new TestRepo("release-images-clash");
|
|
216
|
+
repos.push(r);
|
|
217
|
+
r.write("chant.config.ts", `export default { environments: ["dev"] };\n`);
|
|
218
|
+
const head = r.commit("init");
|
|
219
|
+
r.git(["tag", `ci/green/${head}`]);
|
|
220
|
+
const compiler: TargetCompiler = {
|
|
221
|
+
async compile(env) {
|
|
222
|
+
return { env, params: [], components: ["tofu"], buildSteps: {}, buildInputs: {}, componentFiles: { tofu: "tofu.component.ts" }, templates: [{ component: "tofu", path: "a.json", content: "{}" }] };
|
|
223
|
+
},
|
|
224
|
+
async buildArtifacts() {
|
|
225
|
+
return [];
|
|
226
|
+
},
|
|
227
|
+
};
|
|
228
|
+
const err = await cutRelease({ cwd: r.dir, compiler, label: "v1", actor: "alice", env: {}, buildImages: () => [{ component: "tofu", kind: "image", path: "image", digest: D("a") }] }).catch((e: unknown) => e);
|
|
229
|
+
expect(err).toBeInstanceOf(ReleaseCutError);
|
|
230
|
+
expect((err as ReleaseCutError).code).toBe("release-image-name-taken");
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
// Kept beside the e2e so a reader sees the shape the cut hands the build.
|
|
235
|
+
test("the digests file is sorted JSON of name to digest", () => {
|
|
236
|
+
const r = new TestRepo("release-images-file");
|
|
237
|
+
repos.push(r);
|
|
238
|
+
const file = join(r.dir, "d", "digests.json");
|
|
239
|
+
writeReleaseDigests(file, { z: D("1"), a: D("2") });
|
|
240
|
+
expect(readFileSync(file, "utf-8")).toBe(`{\n "a": "${D("2")}",\n "z": "${D("1")}"\n}\n`);
|
|
241
|
+
writeFileSync(file, "");
|
|
242
|
+
});
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Images a release builds in its cut, and the digests its package pins
|
|
3
|
+
* (#4657, option 1).
|
|
4
|
+
*
|
|
5
|
+
* A package that pins container images by digest needs those digests when it
|
|
6
|
+
* is packed, and they exist only once the images are built. So the cut builds
|
|
7
|
+
* each image `release.images` declares first, as an OCI archive, and records
|
|
8
|
+
* the archive's digest in the release as an image artifact. It writes the
|
|
9
|
+
* digests to a file, `{ "<name>": "sha256:..." }`, and runs the build that
|
|
10
|
+
* packs the package with {@link RELEASE_DIGESTS_ENV} naming that file. The
|
|
11
|
+
* digest of an OCI archive is the digest of its top manifest, which is the
|
|
12
|
+
* digest a registry serves once the archive is pushed by the `oci` target.
|
|
13
|
+
*
|
|
14
|
+
* The publish job builds the images again from the release's commit
|
|
15
|
+
* (`chant release images`), with `SOURCE_DATE_EPOCH` set to the commit's
|
|
16
|
+
* time as in the cut, and refuses an archive whose digest is not the one the
|
|
17
|
+
* release records, so what is pushed is what the package pins. An image build
|
|
18
|
+
* that writes the same archive twice is the requirement (#4657's thread has
|
|
19
|
+
* the check against terragucci's Dockerfiles).
|
|
20
|
+
*
|
|
21
|
+
* An npm target's `pins` names the file in a package that carries the
|
|
22
|
+
* digests. The publish plan and `chant release verify` read it and refuse a
|
|
23
|
+
* digest the release does not record as an image.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { spawnSync } from "node:child_process";
|
|
27
|
+
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
28
|
+
import { dirname, isAbsolute, relative, resolve } from "node:path";
|
|
29
|
+
import type { PublishDeclaration } from "./publish-declaration";
|
|
30
|
+
import type { Release, ReleaseArtifact } from "./release-store";
|
|
31
|
+
|
|
32
|
+
/** The variable that names the digests file a release's package build reads. */
|
|
33
|
+
export const RELEASE_DIGESTS_ENV = "CHANT_RELEASE_DIGESTS";
|
|
34
|
+
|
|
35
|
+
/** The first chant with `release.images`, `CHANT_RELEASE_DIGESTS`, `chant release images` and an npm target's `pins` (#4657). Products feature-detect on it. */
|
|
36
|
+
export const RELEASE_IMAGES_FLOOR = "0.124.71";
|
|
37
|
+
|
|
38
|
+
/** One image a release builds in its cut. */
|
|
39
|
+
export interface ReleaseImageDeclaration {
|
|
40
|
+
/** The image's name: its key in the digests file, and its artifact's `component` in the release. */
|
|
41
|
+
name: string;
|
|
42
|
+
/** The shell command, run in the member's directory, that writes the archive. */
|
|
43
|
+
build: string;
|
|
44
|
+
/** The OCI archive the command writes, a layout directory or a tar file, relative to the member. The `oci` target's `image`. */
|
|
45
|
+
archive: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export class ReleaseImageError extends Error {
|
|
49
|
+
constructor(
|
|
50
|
+
readonly code: string,
|
|
51
|
+
message: string,
|
|
52
|
+
readonly fix?: string,
|
|
53
|
+
) {
|
|
54
|
+
super(message);
|
|
55
|
+
this.name = "ReleaseImageError";
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,99}$/;
|
|
60
|
+
|
|
61
|
+
/** Check a member's `release.images`: names valid and unique, archives inside the member. */
|
|
62
|
+
export function checkReleaseImages(images: readonly ReleaseImageDeclaration[]): void {
|
|
63
|
+
const seen = new Set<string>();
|
|
64
|
+
for (const i of images) {
|
|
65
|
+
if (!NAME.test(i.name)) throw new ReleaseImageError("release-image-name-invalid", `release.images: "${i.name}" is not a name of letters, digits, ".", "_" and "-"`);
|
|
66
|
+
if (seen.has(i.name)) throw new ReleaseImageError("release-image-name-taken", `release.images names "${i.name}" twice`);
|
|
67
|
+
seen.add(i.name);
|
|
68
|
+
if (isAbsolute(i.archive) || i.archive.split(/[\\/]/).includes("..")) {
|
|
69
|
+
throw new ReleaseImageError("release-image-archive-outside", `release.images "${i.name}": the archive ${i.archive} is not a path inside the member`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The digest of an OCI archive: its top manifest's, as `index.json` names it. */
|
|
75
|
+
export type ArchiveDigest = (abs: string) => string;
|
|
76
|
+
|
|
77
|
+
export interface BuildImagesInput {
|
|
78
|
+
images: readonly ReleaseImageDeclaration[];
|
|
79
|
+
/** The member's directory. */
|
|
80
|
+
cwd: string;
|
|
81
|
+
/** The commit's time in seconds, passed to each build as `SOURCE_DATE_EPOCH`. */
|
|
82
|
+
sourceDateEpoch: number;
|
|
83
|
+
label: string | null;
|
|
84
|
+
env?: NodeJS.ProcessEnv;
|
|
85
|
+
archiveDigest: ArchiveDigest;
|
|
86
|
+
/** Run one build command; the default runs it through the shell with its output on stderr. */
|
|
87
|
+
run?: (command: string, o: { cwd: string; env: NodeJS.ProcessEnv }) => number | null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const defaultRun = (command: string, o: { cwd: string; env: NodeJS.ProcessEnv }): number | null =>
|
|
91
|
+
spawnSync(command, { cwd: o.cwd, env: o.env, shell: true, stdio: ["ignore", 2, 2] }).status;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Build each image and return it as the release's image artifact. The
|
|
95
|
+
* command runs with `SOURCE_DATE_EPOCH` and, for a cut with a label,
|
|
96
|
+
* `CHANT_RELEASE_LABEL`; an archive it did not write, or a command that
|
|
97
|
+
* fails, stops the cut.
|
|
98
|
+
*/
|
|
99
|
+
export function buildReleaseImages(i: BuildImagesInput): ReleaseArtifact[] {
|
|
100
|
+
checkReleaseImages(i.images);
|
|
101
|
+
const run = i.run ?? defaultRun;
|
|
102
|
+
const out: ReleaseArtifact[] = [];
|
|
103
|
+
for (const image of i.images) {
|
|
104
|
+
const env: NodeJS.ProcessEnv = { ...(i.env ?? process.env), SOURCE_DATE_EPOCH: String(i.sourceDateEpoch) };
|
|
105
|
+
delete env.CHANT_RELEASE_LABEL;
|
|
106
|
+
if (i.label !== null) env.CHANT_RELEASE_LABEL = i.label;
|
|
107
|
+
const status = run(image.build, { cwd: i.cwd, env });
|
|
108
|
+
if (status !== 0) throw new ReleaseImageError("release-image-build-failed", `the build of image "${image.name}" exited ${status ?? "with a signal"}: ${image.build}`);
|
|
109
|
+
const abs = resolve(i.cwd, image.archive);
|
|
110
|
+
if (!existsSync(abs)) throw new ReleaseImageError("release-image-no-archive", `the build of image "${image.name}" wrote no ${image.archive}`, "Make the command write the OCI archive at archive, such as docker buildx build --output type=oci,dest=<archive>,rewrite-timestamp=true.");
|
|
111
|
+
out.push({ component: image.name, kind: "image", path: relative(i.cwd, abs).split("\\").join("/"), digest: i.archiveDigest(abs) });
|
|
112
|
+
}
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** The release's built images by name: every image artifact with a path, which a handed image (#4694) has not. */
|
|
117
|
+
export function releaseImageDigests(release: Pick<Release, "artifactDigests">, names?: readonly string[]): Record<string, string> {
|
|
118
|
+
const out: Record<string, string> = {};
|
|
119
|
+
for (const a of release.artifactDigests) {
|
|
120
|
+
if (a.kind !== "image") continue;
|
|
121
|
+
if (names && !names.includes(a.component)) continue;
|
|
122
|
+
out[a.component] = a.digest;
|
|
123
|
+
}
|
|
124
|
+
return out;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Write the digests file a package build reads. */
|
|
128
|
+
export function writeReleaseDigests(file: string, digests: Record<string, string>): void {
|
|
129
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
130
|
+
const sorted = Object.fromEntries(Object.entries(digests).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));
|
|
131
|
+
writeFileSync(file, `${JSON.stringify(sorted, null, 2)}\n`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const DIGEST = /sha256:[0-9a-f]{64}/g;
|
|
135
|
+
|
|
136
|
+
/** Every `sha256:` digest in a pins file's text. */
|
|
137
|
+
export function pinnedDigests(text: string): string[] {
|
|
138
|
+
return [...new Set(text.match(DIGEST) ?? [])];
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** The digests in `text` that the release does not record as an image. */
|
|
142
|
+
export function unknownPins(text: string, release: Pick<Release, "artifactDigests">): string[] {
|
|
143
|
+
const known = new Set(release.artifactDigests.filter((a) => a.kind === "image").map((a) => a.digest));
|
|
144
|
+
return pinnedDigests(text).filter((d) => !known.has(d));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** A pins file read from a package directory or a packed `.tgz`, or null when the package has none. */
|
|
148
|
+
export function readPins(abs: string, pins: string, isDirectory: boolean): string | null {
|
|
149
|
+
if (isDirectory) {
|
|
150
|
+
const file = resolve(abs, pins);
|
|
151
|
+
return existsSync(file) ? readFileSync(file, "utf-8") : null;
|
|
152
|
+
}
|
|
153
|
+
const run = spawnSync("tar", ["-xzOf", abs, `package/${pins.replace(/^\.\//, "")}`], { encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"] });
|
|
154
|
+
return run.status === 0 ? run.stdout : null;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* For `chant release verify` (#4657): the first package an npm target's
|
|
159
|
+
* `pins` finds holding an image digest the release does not record, said in
|
|
160
|
+
* a sentence, or null. A package not built here is not read.
|
|
161
|
+
*/
|
|
162
|
+
export function packagePinProblem(cwd: string, release: Pick<Release, "artifactDigests">, targets: readonly PublishDeclaration[]): string | null {
|
|
163
|
+
for (const t of targets) {
|
|
164
|
+
if (t.type !== "npm" || !t.pins) continue;
|
|
165
|
+
for (const p of t.packages) {
|
|
166
|
+
const abs = resolve(cwd, p);
|
|
167
|
+
if (!existsSync(abs)) continue;
|
|
168
|
+
const text = readPins(abs, t.pins, statSync(abs).isDirectory());
|
|
169
|
+
const unknown = text === null ? [] : unknownPins(text, release);
|
|
170
|
+
if (unknown.length > 0) return `${p} pins ${unknown.join(", ")} in ${t.pins}, which the release does not record as an image`;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
@@ -90,6 +90,8 @@ export interface PublishItem {
|
|
|
90
90
|
attachment?: "sbom" | "provenance";
|
|
91
91
|
/** The SBOM file an npm package carries at its root (#4049). */
|
|
92
92
|
sbom?: string;
|
|
93
|
+
/** The notes a forge release is given (#4658), which a dry run prints. */
|
|
94
|
+
notes?: string;
|
|
93
95
|
}
|
|
94
96
|
|
|
95
97
|
/** The cut a publish carries the signature of (#4049): the cut whose signature verified, else the newest. */
|
package/src/notify-relay.test.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { afterEach, describe, expect, test } from "vitest";
|
|
7
7
|
import { assertProductWords, CHANT_WORDS } from "./ci-product-words";
|
|
8
|
-
import { makeNotifyEvent, notifyConfigProblem, relayRefOf, relayToken, sendNotify, slackMessage, teamsMessage, RELAY_APPROVE_ACTION, RELAY_DECLINE_ACTION, type NotifyFetch } from "./notify";
|
|
8
|
+
import { makeNotifyEvent, notifyConfigProblem, relayRefOf, relayToken, sendNotify, slackMessage, slackRelayActions, teamsMessage, teamsRelayReply, RELAY_APPROVE_ACTION, RELAY_DECLINE_ACTION, type NotifyFetch, type RelayButtons } from "./notify";
|
|
9
9
|
import { relayPrincipalFor, slackSignature, startRelayServer, teamsSignature, type RelayDeps, type RelayPending, type RelaySettings } from "./notify-relay";
|
|
10
10
|
import { relaySettingsFromEnv } from "./cli/handlers/notify-relay";
|
|
11
11
|
|
|
@@ -172,7 +172,61 @@ describe("chant notify relay (#4611)", () => {
|
|
|
172
172
|
});
|
|
173
173
|
});
|
|
174
174
|
|
|
175
|
+
describe("a gate that asks a decision point (#4611)", () => {
|
|
176
|
+
const point = (pass?: Array<string | boolean>): RelayPending => ({ op: "tf-apply", gate: "tf-apply-wave-2", digest, question: { point: "prod-apply", id: "q-17", ...(pass ? { pass } : {}) } });
|
|
177
|
+
function withAnswers(pending: RelayPending, refuse?: string) {
|
|
178
|
+
const m = memoryDeps({ pending: [pending] });
|
|
179
|
+
const answers: Array<{ id: string; answer: string | boolean; by: string; relayedBy: string }> = [];
|
|
180
|
+
const deps: RelayDeps = { ...m.deps, answer: async (input) => (refuse ? { ok: false, error: refuse } : (answers.push(input), { ok: true })) };
|
|
181
|
+
return { deps, answers, approvals: m.approvals };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
test("the click answers the question the gate waits on, as the clicker, relayed by the relay, and records no approval", async () => {
|
|
185
|
+
const { deps, answers, approvals } = withAnswers(point());
|
|
186
|
+
const r = await relay(deps);
|
|
187
|
+
const out = await r.teams(`approve ${tokenFor()}`);
|
|
188
|
+
expect(out.json.text).toMatch(/^tf-apply-wave-2 on tf-apply: decision point prod-apply, question q-17, answered true by github:ana, relayed by chant-relay, for sha256:a{64}/);
|
|
189
|
+
expect(answers).toEqual([{ id: "q-17", answer: true, by: "github:ana", relayedBy: "chant-relay" }]);
|
|
190
|
+
expect(approvals).toEqual([]);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test("a gate that passes on one named answer gets that answer; one that passes on several is refused", async () => {
|
|
194
|
+
const one = withAnswers(point(["go"]));
|
|
195
|
+
await (await relay(one.deps)).teams(`approve ${tokenFor()}`);
|
|
196
|
+
expect(one.answers.map((a) => a.answer)).toEqual(["go"]);
|
|
197
|
+
const several = withAnswers(point(["go", "canary"]));
|
|
198
|
+
expect((await (await relay(several.deps)).teams(`approve ${tokenFor()}`)).json.text).toMatch(/passes on go or canary, so a button names no one answer/);
|
|
199
|
+
expect(several.answers).toEqual([]);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
test("the point's refusal reaches the thread, and a relay with no way to answer refuses as before", async () => {
|
|
203
|
+
const refused = withAnswers(point(), "points-quorum: github:ana holds the agent role");
|
|
204
|
+
expect((await (await relay(refused.deps)).teams(`approve ${tokenFor()}`)).json.text).toMatch(/question q-17 was not answered for github:ana: points-quorum: github:ana holds the agent role/);
|
|
205
|
+
const { deps, approvals } = memoryDeps({ pending: [point()] });
|
|
206
|
+
expect((await (await relay(deps)).teams(`approve ${tokenFor()}`)).json.text).toMatch(/asks decision point prod-apply, so its answer passes the gate, not an approval/);
|
|
207
|
+
expect(approvals).toEqual([]);
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
|
|
175
211
|
describe("the buttons notify posts (#4611)", () => {
|
|
212
|
+
test("a product's own render gets the signed buttons, and its clicks reach the relay", async () => {
|
|
213
|
+
const bodies: string[] = [];
|
|
214
|
+
const fetch: NotifyFetch = async (_u, init) => (bodies.push(init.body), { ok: true, status: 200 });
|
|
215
|
+
const seen: Array<RelayButtons | undefined> = [];
|
|
216
|
+
const render = {
|
|
217
|
+
slack: (_e: unknown, relay?: RelayButtons) => (seen.push(relay), { text: "terragucci wave 2", ...(relay ? { blocks: [slackRelayActions(relay)] } : {}) }),
|
|
218
|
+
teams: (_e: unknown, relay?: RelayButtons) => ({ text: relay ? teamsRelayReply(relay) : "" }),
|
|
219
|
+
};
|
|
220
|
+
await sendNotify({ slack: "SLACK", teams: "TEAMS", relay: "CHANT_RELAY_KEY" }, waitingEvent(), { env: { SLACK: "http://127.0.0.1:9/s", TEAMS: "http://127.0.0.1:9/t", CHANT_RELAY_KEY: KEY }, fetch, log: () => {}, render });
|
|
221
|
+
expect(seen).toEqual([{ token: tokenFor() }]);
|
|
222
|
+
const slack = JSON.parse(bodies[0]!) as { blocks: Array<{ elements: Array<{ action_id: string; value: string }> }> };
|
|
223
|
+
expect(slack.blocks[0]!.elements.map((b) => b.action_id)).toEqual([RELAY_APPROVE_ACTION, RELAY_DECLINE_ACTION]);
|
|
224
|
+
expect(JSON.parse(bodies[1]!).text).toBe(`approve ${tokenFor()}`);
|
|
225
|
+
const { deps, approvals } = memoryDeps();
|
|
226
|
+
await (await relay(deps)).slack(slackBody("UANA", RELAY_APPROVE_ACTION, slack.blocks[0]!.elements[0]!.value));
|
|
227
|
+
expect(approvals).toHaveLength(1);
|
|
228
|
+
});
|
|
229
|
+
|
|
176
230
|
test("a waiting event's Slack message carries Approve and Decline with the signed reference, and Teams the reply", async () => {
|
|
177
231
|
const event = waitingEvent();
|
|
178
232
|
const token = tokenFor(event);
|
package/src/notify-relay.ts
CHANGED
|
@@ -25,6 +25,17 @@
|
|
|
25
25
|
* command applies: the decision point, `identity.gates` (the relay holds
|
|
26
26
|
* no key, so a sealed gate is refused), the gate policy and the push.
|
|
27
27
|
*
|
|
28
|
+
* A gate that asks a decision point (#3170) passes on the point's answer,
|
|
29
|
+
* not an approval. There the relay answers the question the gate waits on
|
|
30
|
+
* instead, as `chant workspace points answer <id> --answer <yes> --by
|
|
31
|
+
* <principal> --relayed-by <relay>` does, when the gate passes on one answer
|
|
32
|
+
* (a yes, or the one value its `pass` names). The point's quorum decides
|
|
33
|
+
* whether that principal counts. A gate that passes on several answers names
|
|
34
|
+
* no one answer for a button, and is refused.
|
|
35
|
+
*
|
|
36
|
+
* A product that renders its own chat messages gets the signed buttons from
|
|
37
|
+
* `sendNotify`'s `render` (`./notify.ts`), so its clicks reach this relay too.
|
|
38
|
+
*
|
|
28
39
|
* Decline records nothing and says who declined. The type-the-name prompt
|
|
29
40
|
* of `chant approve` (#4575) is for a terminal; a signed click is the
|
|
30
41
|
* person's confirmation here. The relay starts no job: the resume job
|
|
@@ -36,6 +47,15 @@ import { createServer, type IncomingMessage, type Server } from "node:http";
|
|
|
36
47
|
import type { CiProductWords } from "./ci-product-words";
|
|
37
48
|
import type { GatedRequest } from "./lifecycle/gated-request-digest";
|
|
38
49
|
import { readRelayToken, RELAY_APPROVE_ACTION, RELAY_DECLINE_ACTION, type RelayRef } from "./notify";
|
|
50
|
+
import { NEXT_RELEASE } from "./release-floors";
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The first chant whose relay answers a gate's decision point and whose
|
|
54
|
+
* `sendNotify` hands a product's own render the signed buttons
|
|
55
|
+
* (`slackRelayActions`, `teamsRelayReply`), so a product can drop a relay of
|
|
56
|
+
* its own (#4611). Products feature-detect on it.
|
|
57
|
+
*/
|
|
58
|
+
export const NOTIFY_RELAY_POINTS_FLOOR = NEXT_RELEASE;
|
|
39
59
|
|
|
40
60
|
/** The relay's settings, by environment variable. */
|
|
41
61
|
export const RELAY_ENV = {
|
|
@@ -198,6 +218,8 @@ export interface RelayPending {
|
|
|
198
218
|
environment?: string;
|
|
199
219
|
/** On a gated request (#4551): the request, whose requester may not approve it. */
|
|
200
220
|
request?: GatedRequest;
|
|
221
|
+
/** When the gate asks a decision point: the open question it waits on, and the answers that pass it (absent: a yes). */
|
|
222
|
+
question?: { point: string; id: string; pass?: Array<string | boolean> };
|
|
201
223
|
}
|
|
202
224
|
|
|
203
225
|
/** What the relay reads and writes, so a test stands in for the repo. */
|
|
@@ -208,6 +230,12 @@ export interface RelayDeps {
|
|
|
208
230
|
pending(digest: string): Promise<RelayPending[]>;
|
|
209
231
|
/** Record the approval as `chant approve` does. `error` is why it refused. */
|
|
210
232
|
approve(input: { op: string; gate: string; digest: string; environment?: string; actor: string; relayedBy: string }): Promise<{ ok: true } | { ok: false; error: string }>;
|
|
233
|
+
/**
|
|
234
|
+
* Answer a decision point's question as `chant workspace points answer`
|
|
235
|
+
* does, relayed by the relay (#4611). Absent: a gate that asks a decision
|
|
236
|
+
* point is refused.
|
|
237
|
+
*/
|
|
238
|
+
answer?(input: { id: string; answer: string | boolean; by: string; relayedBy: string }): Promise<{ ok: true } | { ok: false; error: string }>;
|
|
211
239
|
/** Who the relay records as `relayedBy`. */
|
|
212
240
|
principal: string;
|
|
213
241
|
words: CiProductWords;
|
|
@@ -227,7 +255,7 @@ export interface RelaySettings {
|
|
|
227
255
|
export interface RelayAnswer {
|
|
228
256
|
ok: boolean;
|
|
229
257
|
/** What the relay did: `approved`, `declined`, or the refusal's kind. */
|
|
230
|
-
outcome: "approved" | "declined" | "bad-reference" | "stale" | "ineligible" | "moved" | "refused";
|
|
258
|
+
outcome: "approved" | "answered" | "declined" | "bad-reference" | "stale" | "ineligible" | "moved" | "refused";
|
|
231
259
|
text: string;
|
|
232
260
|
/** The gate's op, when the click reached one. */
|
|
233
261
|
op?: string;
|
|
@@ -264,6 +292,26 @@ export async function decideRelayClick(click: RelayClick, settings: RelaySetting
|
|
|
264
292
|
if (pending.request && norm(pending.request.by) === norm(principal)) {
|
|
265
293
|
return { ok: false, outcome: "ineligible", op: pending.op, gate: pending.gate, principal, text: `Refused: ${principal} made the request, and a requester's own approval does not count. Someone else approves it.` };
|
|
266
294
|
}
|
|
295
|
+
if (pending.question) {
|
|
296
|
+
const q = pending.question;
|
|
297
|
+
if (!deps.answer) {
|
|
298
|
+
return { ok: false, outcome: "refused", op: pending.op, gate: pending.gate, principal, text: `Not approved: ${pending.gate} asks decision point ${q.point}, so its answer passes the gate, not an approval. Answer question ${q.id} at a shell.` };
|
|
299
|
+
}
|
|
300
|
+
if (q.pass && q.pass.length !== 1) {
|
|
301
|
+
return { ok: false, outcome: "refused", op: pending.op, gate: pending.gate, principal, text: `Not approved: ${pending.gate} asks decision point ${q.point}, which ${q.pass.length === 0 ? "names no answer that passes it" : `passes on ${q.pass.map(String).join(" or ")}`}, so a button names no one answer. Answer question ${q.id} at a shell.` };
|
|
302
|
+
}
|
|
303
|
+
const value = q.pass ? q.pass[0]! : true;
|
|
304
|
+
const answered = await deps.answer({ id: q.id, answer: value, by: principal, relayedBy: deps.principal });
|
|
305
|
+
if (!answered.ok) return { ok: false, outcome: "refused", op: pending.op, gate: pending.gate, principal, text: `Not approved: ${pending.gate} asks decision point ${q.point}, and question ${q.id} was not answered for ${principal}: ${answered.error}` };
|
|
306
|
+
return {
|
|
307
|
+
ok: true,
|
|
308
|
+
outcome: "answered",
|
|
309
|
+
op: pending.op,
|
|
310
|
+
gate: pending.gate,
|
|
311
|
+
principal,
|
|
312
|
+
text: `${pending.gate} on ${pending.op}: decision point ${q.point}, question ${q.id}, answered ${String(value)} by ${principal}, relayed by ${deps.principal}, for ${pending.digest} and no other plan. The resume job starts the ${words.stages.apply} once the answer passes the gate.`,
|
|
313
|
+
};
|
|
314
|
+
}
|
|
267
315
|
const recorded = await deps.approve({ op: pending.op, gate: pending.gate, digest: pending.digest, ...(pending.environment !== undefined ? { environment: pending.environment } : {}), actor: principal, relayedBy: deps.principal });
|
|
268
316
|
if (!recorded.ok) return { ok: false, outcome: "refused", op: pending.op, gate: pending.gate, principal, text: `Not approved: ${recorded.error}` };
|
|
269
317
|
return {
|
package/src/notify.ts
CHANGED
|
@@ -301,19 +301,27 @@ export function slackMessage(e: NotifyEvent, relay?: RelayButtons): { text: stri
|
|
|
301
301
|
}
|
|
302
302
|
const text = lines.join("\n");
|
|
303
303
|
if (!relay) return { text };
|
|
304
|
+
return { text, blocks: [{ type: "section", text: { type: "mrkdwn", text } }, slackRelayActions(relay)] };
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* The Slack actions block with the relay's Approve and Decline buttons
|
|
309
|
+
* (#4611), for a product that renders its own Slack message: its clicks reach
|
|
310
|
+
* `chant notify relay` like those on chant's own message.
|
|
311
|
+
*/
|
|
312
|
+
export function slackRelayActions(relay: RelayButtons): Record<string, unknown> {
|
|
304
313
|
const button = (label: string, action: string, style?: string) => ({ type: "button", text: { type: "plain_text", text: label }, action_id: action, value: relay.token, ...(style ? { style } : {}) });
|
|
305
|
-
return {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
};
|
|
314
|
+
return { type: "actions", elements: [button("Approve", RELAY_APPROVE_ACTION, "primary"), button("Decline", RELAY_DECLINE_ACTION)] };
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** The Teams reply that approves through the relay's outgoing webhook (#4611): `approve <token>`, after a mention of the relay. */
|
|
318
|
+
export function teamsRelayReply(relay: RelayButtons, action: "approve" | "decline" = "approve"): string {
|
|
319
|
+
return `${action} ${relay.token}`;
|
|
312
320
|
}
|
|
313
321
|
|
|
314
322
|
/** A Teams incoming webhook's body (a Workflows webhook): one Adaptive Card, with buttons that open the review page and the run. With `relay`, the reply that approves through the relay (#4611). */
|
|
315
323
|
export function teamsMessage(e: NotifyEvent, relay?: RelayButtons): Record<string, unknown> {
|
|
316
|
-
const all = [...facts(e), ...(relay ? [{ title: "Approve in chat", value: `mention the relay and reply:
|
|
324
|
+
const all = [...facts(e), ...(relay ? [{ title: "Approve in chat", value: `mention the relay and reply: ${teamsRelayReply(relay)}` }] : [])];
|
|
317
325
|
const actions = [
|
|
318
326
|
...(e.approve?.review ? [{ type: "Action.OpenUrl", title: "Review and approve", url: e.approve.review.url }] : []),
|
|
319
327
|
...(e.run?.url ? [{ type: "Action.OpenUrl", title: "Open the run", url: e.run.url }] : []),
|
|
@@ -434,8 +442,13 @@ export interface SendNotifyOptions {
|
|
|
434
442
|
/** The webhook headers' prefix. Default `x-chant`. */
|
|
435
443
|
prefix?: string;
|
|
436
444
|
userAgent?: string;
|
|
437
|
-
/**
|
|
438
|
-
|
|
445
|
+
/**
|
|
446
|
+
* A product's own Slack or Teams body, in place of {@link slackMessage} and
|
|
447
|
+
* {@link teamsMessage}. `relay` is the relay's signed button value for a
|
|
448
|
+
* waiting event when `notify.relay` names a key that is set (#4611), for
|
|
449
|
+
* {@link slackRelayActions} and {@link teamsRelayReply}.
|
|
450
|
+
*/
|
|
451
|
+
render?: { slack?: (e: NotifyEvent, relay?: RelayButtons) => unknown; teams?: (e: NotifyEvent, relay?: RelayButtons) => unknown };
|
|
439
452
|
/** Seconds since the epoch, for the signature's timestamp. Default: the clock. */
|
|
440
453
|
now?: number;
|
|
441
454
|
}
|
|
@@ -518,7 +531,7 @@ export async function sendNotify(config: NotifyConfig | undefined, event: Notify
|
|
|
518
531
|
const relayKey = config.relay ? env[config.relay] : undefined;
|
|
519
532
|
if (config.relay && ref && !relayKey) log(`notify: ${config.relay} is not set, so the message carries no relay buttons`);
|
|
520
533
|
const buttons = ref && relayKey ? { token: relayToken(relayKey, ref) } : undefined;
|
|
521
|
-
if (config.slack) await deliver("slack", config.slack, () => json(options.render?.slack ? options.render.slack(event) : slackMessage(event, buttons)));
|
|
522
|
-
if (config.teams) await deliver("teams", config.teams, () => json(options.render?.teams ? options.render.teams(event) : teamsMessage(event, buttons)));
|
|
534
|
+
if (config.slack) await deliver("slack", config.slack, () => json(options.render?.slack ? options.render.slack(event, buttons) : slackMessage(event, buttons)));
|
|
535
|
+
if (config.teams) await deliver("teams", config.teams, () => json(options.render?.teams ? options.render.teams(event, buttons) : teamsMessage(event, buttons)));
|
|
523
536
|
return out;
|
|
524
537
|
}
|