@intentius/chant 0.124.76 → 0.124.78
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__fixtures__/agent-guard/plywood.d.ts +8 -0
- package/dist/__fixtures__/agent-guard/plywood.d.ts.map +1 -0
- package/dist/__fixtures__/agent-guard/plywood.js +19 -0
- package/dist/__fixtures__/agent-guard/terragucci.d.ts +8 -0
- package/dist/__fixtures__/agent-guard/terragucci.d.ts.map +1 -0
- package/dist/__fixtures__/agent-guard/terragucci.js +30 -0
- package/dist/__fixtures__/gated-ledger.d.ts +10 -0
- package/dist/__fixtures__/gated-ledger.d.ts.map +1 -0
- package/dist/__fixtures__/gated-ledger.js +51 -0
- package/dist/__fixtures__/notify-messages/events.d.ts +9 -0
- package/dist/__fixtures__/notify-messages/events.d.ts.map +1 -0
- package/dist/__fixtures__/notify-messages/events.js +21 -0
- package/dist/__fixtures__/product-words.d.ts +14 -0
- package/dist/__fixtures__/product-words.d.ts.map +1 -1
- package/dist/__fixtures__/product-words.js +11 -0
- package/dist/__fixtures__/terragucci-notify.d.ts +66 -0
- package/dist/__fixtures__/terragucci-notify.d.ts.map +1 -0
- package/dist/__fixtures__/terragucci-notify.js +133 -0
- package/dist/action-pins.d.ts +8 -0
- package/dist/action-pins.d.ts.map +1 -1
- package/dist/action-pins.js +9 -0
- package/dist/agent-guard.d.ts +143 -0
- package/dist/agent-guard.d.ts.map +1 -0
- package/dist/agent-guard.js +455 -0
- package/dist/ci-commit.d.ts +120 -5
- package/dist/ci-commit.d.ts.map +1 -1
- package/dist/ci-commit.js +214 -23
- package/dist/ci-own-jobs.d.ts +81 -0
- package/dist/ci-own-jobs.d.ts.map +1 -0
- package/dist/ci-own-jobs.js +233 -0
- package/dist/ci-product-words.d.ts +18 -4
- package/dist/ci-product-words.d.ts.map +1 -1
- package/dist/ci-product-words.js +26 -6
- package/dist/ci-product.d.ts +40 -1
- package/dist/ci-product.d.ts.map +1 -1
- package/dist/ci-product.js +40 -1
- package/dist/cli/handlers/approve.d.ts +5 -1
- package/dist/cli/handlers/approve.d.ts.map +1 -1
- package/dist/cli/handlers/approve.js +17 -11
- package/dist/cli/handlers/build.d.ts +1 -1
- package/dist/cli/handlers/build.d.ts.map +1 -1
- package/dist/cli/handlers/build.js +88 -2
- package/dist/cli/handlers/commit.d.ts.map +1 -1
- package/dist/cli/handlers/commit.js +3 -1
- package/dist/cli/handlers/notify-relay.d.ts +18 -1
- package/dist/cli/handlers/notify-relay.d.ts.map +1 -1
- package/dist/cli/handlers/notify-relay.js +90 -14
- package/dist/cli/handlers/pr.d.ts.map +1 -1
- package/dist/cli/handlers/pr.js +3 -1
- package/dist/cli/handlers/release.d.ts +5 -1
- package/dist/cli/handlers/release.d.ts.map +1 -1
- package/dist/cli/handlers/release.js +21 -9
- package/dist/cli/handlers/run-generate.d.ts.map +1 -1
- package/dist/cli/handlers/run-generate.js +2 -1
- package/dist/cli/main.d.ts.map +1 -1
- package/dist/cli/main.js +21 -1
- package/dist/cli/parse-args.d.ts.map +1 -1
- package/dist/cli/parse-args.js +60 -4
- package/dist/cli/registry.d.ts +16 -0
- package/dist/cli/registry.d.ts.map +1 -1
- package/dist/components/pr-env-components.d.ts +65 -0
- package/dist/components/pr-env-components.d.ts.map +1 -0
- package/dist/components/pr-env-components.js +162 -0
- package/dist/components/pr-policy.d.ts +15 -0
- package/dist/components/pr-policy.d.ts.map +1 -1
- package/dist/components/pr-policy.js +24 -1
- package/dist/components/release-compile.d.ts +15 -3
- package/dist/components/release-compile.d.ts.map +1 -1
- package/dist/components/release-compile.js +66 -15
- package/dist/config.d.ts +14 -8
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +16 -1
- package/dist/gate-policy.d.ts +14 -1
- package/dist/gate-policy.d.ts.map +1 -1
- package/dist/gate-policy.js +16 -6
- package/dist/lexicon.d.ts +25 -1
- package/dist/lexicon.d.ts.map +1 -1
- package/dist/lexicon.js +10 -0
- package/dist/lifecycle/gated-op.d.ts +95 -0
- package/dist/lifecycle/gated-op.d.ts.map +1 -0
- package/dist/lifecycle/gated-op.js +96 -0
- package/dist/lifecycle/git.d.ts.map +1 -1
- package/dist/lifecycle/git.js +83 -2
- package/dist/lifecycle/release-images.d.ts +77 -3
- package/dist/lifecycle/release-images.d.ts.map +1 -1
- package/dist/lifecycle/release-images.js +132 -6
- package/dist/notify-product.d.ts +11 -2
- package/dist/notify-product.d.ts.map +1 -1
- package/dist/notify-product.js +16 -4
- package/dist/notify-relay-repo.d.ts +131 -0
- package/dist/notify-relay-repo.d.ts.map +1 -0
- package/dist/notify-relay-repo.js +202 -0
- package/dist/notify-relay.d.ts +25 -1
- package/dist/notify-relay.d.ts.map +1 -1
- package/dist/notify-relay.js +27 -2
- package/dist/notify.d.ts +36 -4
- package/dist/notify.d.ts.map +1 -1
- package/dist/notify.js +31 -21
- package/dist/notify.schema.json +1 -0
- package/dist/op/activities/review-product.d.ts +111 -0
- package/dist/op/activities/review-product.d.ts.map +1 -0
- package/dist/op/activities/review-product.js +116 -0
- package/dist/op/op-waves-capability.js +2 -2
- package/dist/policy-engine.d.ts +95 -0
- package/dist/policy-engine.d.ts.map +1 -0
- package/dist/policy-engine.js +157 -0
- package/dist/pr-forge-reads.d.ts +124 -0
- package/dist/pr-forge-reads.d.ts.map +1 -0
- package/dist/pr-forge-reads.js +322 -0
- package/dist/pr-forge.d.ts +14 -0
- package/dist/pr-forge.d.ts.map +1 -1
- package/dist/pr-forge.js +21 -13
- package/dist/pr-plan-note.d.ts +293 -0
- package/dist/pr-plan-note.d.ts.map +1 -0
- package/dist/pr-plan-note.js +362 -0
- package/dist/release-floors.d.ts +9 -1
- package/dist/release-floors.d.ts.map +1 -1
- package/dist/release-floors.js +9 -1
- package/dist/state-export.d.ts +126 -0
- package/dist/state-export.d.ts.map +1 -0
- package/dist/state-export.js +156 -0
- package/dist/state-migration.d.ts +148 -0
- package/dist/state-migration.d.ts.map +1 -0
- package/dist/state-migration.js +146 -0
- package/dist/state-unlock.d.ts +167 -0
- package/dist/state-unlock.d.ts.map +1 -0
- package/dist/state-unlock.js +196 -0
- package/dist/wave-runtime.mjs +332 -332
- package/dist/workspace/declaration.schema.json +1 -1
- package/dist/workspace/release-workflow.d.ts +10 -0
- package/dist/workspace/release-workflow.d.ts.map +1 -1
- package/dist/workspace/release-workflow.js +10 -4
- package/package.json +1 -1
- package/src/__fixtures__/agent-guard/plywood.ts +27 -0
- package/src/__fixtures__/agent-guard/terragucci.ts +39 -0
- package/src/__fixtures__/gated-ledger.ts +58 -0
- package/src/__fixtures__/notify-messages/events.ts +23 -0
- package/src/__fixtures__/notify-messages/messages.json +50 -0
- package/src/__fixtures__/pr-loop/terragucci-plan.note.golden.md +53 -0
- package/src/__fixtures__/pr-loop/terragucci-plan.note.json +83 -0
- package/src/__fixtures__/product-words.ts +12 -0
- package/src/__fixtures__/review-agent/terragucci-review.json +94 -0
- package/src/__fixtures__/terragucci-notify.ts +170 -0
- package/src/action-pins.test.ts +12 -1
- package/src/action-pins.ts +11 -0
- package/src/agent-guard.test.ts +319 -0
- package/src/agent-guard.ts +547 -0
- package/src/ci-commit.test.ts +213 -2
- package/src/ci-commit.ts +258 -27
- package/src/ci-own-jobs.test.ts +108 -0
- package/src/ci-own-jobs.ts +252 -0
- package/src/ci-product-words.ts +35 -7
- package/src/ci-product.ts +81 -1
- package/src/cli/handlers/approve-cwd.test.ts +112 -0
- package/src/cli/handlers/approve.ts +22 -11
- package/src/cli/handlers/build-own-jobs.test.ts +80 -0
- package/src/cli/handlers/build-pr-loop-control.test.ts +87 -0
- package/src/cli/handlers/build-run-group.test.ts +59 -0
- package/src/cli/handlers/build.ts +83 -3
- package/src/cli/handlers/commit.ts +3 -0
- package/src/cli/handlers/notify-relay.ts +99 -17
- package/src/cli/handlers/pr.ts +3 -1
- package/src/cli/handlers/release.ts +20 -10
- package/src/cli/handlers/run-generate.ts +2 -1
- package/src/cli/main.ts +21 -1
- package/src/cli/parse-args.ts +45 -3
- package/src/cli/registry.ts +16 -0
- package/src/components/pr-env-components.test.ts +137 -0
- package/src/components/pr-env-components.ts +213 -0
- package/src/components/pr-policy.test.ts +41 -1
- package/src/components/pr-policy.ts +25 -1
- package/src/components/release-compile.test.ts +47 -0
- package/src/components/release-compile.ts +70 -13
- package/src/config.ts +23 -3
- package/src/gate-policy-bundle.test.ts +14 -0
- package/src/gate-policy.test.ts +7 -0
- package/src/gate-policy.ts +22 -7
- package/src/lexicon.ts +28 -1
- package/src/lifecycle/gated-op.ts +158 -0
- package/src/lifecycle/git.test.ts +92 -0
- package/src/lifecycle/git.ts +71 -2
- package/src/lifecycle/release-images-dockerfile.test.ts +221 -0
- package/src/lifecycle/release-images.ts +167 -8
- package/src/notify-message-words.test.ts +186 -0
- package/src/notify-product.ts +17 -4
- package/src/notify-relay-repo.test.ts +284 -0
- package/src/notify-relay-repo.ts +234 -0
- package/src/notify-relay.test.ts +12 -0
- package/src/notify-relay.ts +29 -3
- package/src/notify.schema.json +1 -0
- package/src/notify.ts +59 -19
- package/src/op/activities/review-product.test.ts +116 -0
- package/src/op/activities/review-product.ts +200 -0
- package/src/op/op-waves-capability.ts +2 -2
- package/src/op/schedule-overdue.test.ts +26 -0
- package/src/policy-engine.test.ts +162 -0
- package/src/policy-engine.ts +200 -0
- package/src/pr-forge-reads.test.ts +285 -0
- package/src/pr-forge-reads.ts +424 -0
- package/src/pr-forge.ts +30 -16
- package/src/pr-plan-note.test.ts +105 -0
- package/src/pr-plan-note.ts +521 -0
- package/src/release-floors.ts +13 -1
- package/src/state-export.test.ts +124 -0
- package/src/state-export.ts +227 -0
- package/src/state-migration.test.ts +133 -0
- package/src/state-migration.ts +236 -0
- package/src/state-unlock.test.ts +165 -0
- package/src/state-unlock.ts +293 -0
- package/src/workspace/declaration.schema.json +1 -1
- package/src/workspace/release-workflow.test.ts +34 -0
- package/src/workspace/release-workflow.ts +19 -4
|
@@ -21,14 +21,66 @@
|
|
|
21
21
|
* An npm target's `pins` names the file in a package that carries the
|
|
22
22
|
* digests. The publish plan and `chant release verify` read it and refuse a
|
|
23
23
|
* digest the release does not record as an image.
|
|
24
|
+
*
|
|
25
|
+
* An entry may name a Dockerfile instead of a command (#4819): chant runs
|
|
26
|
+
* the reproducible build itself ({@link releaseImageBuildCommand}), a
|
|
27
|
+
* `docker-container` buildx builder writing an OCI archive with timestamps
|
|
28
|
+
* rewritten from `SOURCE_DATE_EPOCH` and no attestations, for every
|
|
29
|
+
* platform. Its `prepare` command (the bundle the image carries, a packed
|
|
30
|
+
* package) runs first with no digests file named, so the image is the same
|
|
31
|
+
* in the cut and the publish job. A product's package build turns the
|
|
32
|
+
* digests file and its `{ name: reference }` into the table it bakes in with
|
|
33
|
+
* {@link releaseImagePins}, and writes the pins file with
|
|
34
|
+
* {@link writeReleaseImagePins}. This module reaches only Node's built-ins at
|
|
35
|
+
* run time, so a product's build script imports it under plain Node.
|
|
24
36
|
*/
|
|
25
37
|
import { spawnSync } from "node:child_process";
|
|
26
|
-
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
27
|
-
import { dirname, isAbsolute, relative, resolve } from "node:path";
|
|
38
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
39
|
+
import { dirname, isAbsolute, posix, relative, resolve } from "node:path";
|
|
40
|
+
import { NEXT_RELEASE } from "../release-floors.js";
|
|
28
41
|
/** The variable that names the digests file a release's package build reads. */
|
|
29
42
|
export const RELEASE_DIGESTS_ENV = "CHANT_RELEASE_DIGESTS";
|
|
30
43
|
/** The first chant with `release.images`, `CHANT_RELEASE_DIGESTS`, `chant release images` and an npm target's `pins` (#4657). Products feature-detect on it. */
|
|
31
44
|
export const RELEASE_IMAGES_FLOOR = "0.124.71";
|
|
45
|
+
/**
|
|
46
|
+
* The first chant whose `release.images` takes a Dockerfile entry, with
|
|
47
|
+
* `releaseImagePins` and `writeReleaseImagePins` for a product's package
|
|
48
|
+
* build (#4819).
|
|
49
|
+
*/
|
|
50
|
+
export const RELEASE_IMAGE_DOCKERFILE_FLOOR = NEXT_RELEASE;
|
|
51
|
+
/** The platforms a Dockerfile entry builds by default. Building arm64 on amd64 needs QEMU's binfmt handlers. */
|
|
52
|
+
export const RELEASE_IMAGE_PLATFORMS = ["linux/amd64", "linux/arm64"];
|
|
53
|
+
/** The buildx builder a Dockerfile entry builds with, created with the `docker-container` driver when missing: the default driver cannot rewrite timestamps in an OCI archive. */
|
|
54
|
+
export const RELEASE_IMAGE_BUILDER = "chant-release";
|
|
55
|
+
const PLATFORM = /^[a-z0-9]+\/[a-z0-9_]+(?:\/[a-z0-9]+)?$/;
|
|
56
|
+
/** A word the shell reads as itself. */
|
|
57
|
+
const sh = (s) => (/^[A-Za-z0-9_./:=,@+%-]+$/.test(s) ? s : `'${s.replace(/'/g, "'\\''")}'`);
|
|
58
|
+
const isDockerfile = (i) => "dockerfile" in i && i.dockerfile !== undefined;
|
|
59
|
+
const archiveOf = (i) => i.archive ?? `dist/images/${i.name}.tar`;
|
|
60
|
+
/**
|
|
61
|
+
* The shell command a Dockerfile entry builds with (#4819), the one
|
|
62
|
+
* terragucci's and plywood's `scripts/release-image.sh` ran: `--no-cache`,
|
|
63
|
+
* no provenance or SBOM in the archive (chant attaches its own), the
|
|
64
|
+
* `SOURCE_DATE_EPOCH` the cut sets passed as a build argument and used to
|
|
65
|
+
* rewrite every timestamp, and every platform in one image index.
|
|
66
|
+
*/
|
|
67
|
+
export function releaseImageBuildCommand(i) {
|
|
68
|
+
const archive = archiveOf(i);
|
|
69
|
+
const b = RELEASE_IMAGE_BUILDER;
|
|
70
|
+
return [
|
|
71
|
+
`{ docker buildx inspect ${b} >/dev/null 2>&1 || docker buildx create --name ${b} --driver docker-container >/dev/null; }`,
|
|
72
|
+
`mkdir -p ${sh(posix.dirname(archive))}`,
|
|
73
|
+
[
|
|
74
|
+
`docker buildx build --builder ${b} --no-cache --platform ${sh((i.platforms ?? RELEASE_IMAGE_PLATFORMS).join(","))}`,
|
|
75
|
+
`--provenance=false --sbom=false --build-arg SOURCE_DATE_EPOCH="$SOURCE_DATE_EPOCH"`,
|
|
76
|
+
`--output ${sh(`type=oci,dest=${archive},rewrite-timestamp=true`)} -f ${sh(i.dockerfile)} ${sh(i.context ?? ".")} >&2`,
|
|
77
|
+
].join(" "),
|
|
78
|
+
].join(" && ");
|
|
79
|
+
}
|
|
80
|
+
/** Each entry as a command and an archive: a Dockerfile entry's command is {@link releaseImageBuildCommand}, its archive the default when it names none. */
|
|
81
|
+
export function resolveReleaseImages(images) {
|
|
82
|
+
return images.map((i) => (isDockerfile(i) ? { name: i.name, build: releaseImageBuildCommand(i), archive: archiveOf(i) } : i));
|
|
83
|
+
}
|
|
32
84
|
export class ReleaseImageError extends Error {
|
|
33
85
|
constructor(code, message, fix) {
|
|
34
86
|
super(message);
|
|
@@ -38,7 +90,8 @@ export class ReleaseImageError extends Error {
|
|
|
38
90
|
}
|
|
39
91
|
}
|
|
40
92
|
const NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,99}$/;
|
|
41
|
-
|
|
93
|
+
const outside = (p) => isAbsolute(p) || p.split(/[\\/]/).includes("..");
|
|
94
|
+
/** Check a member's `release.images`: names valid and unique, one of `build` and `dockerfile`, paths inside the member, platforms of the form os/arch. */
|
|
42
95
|
export function checkReleaseImages(images) {
|
|
43
96
|
const seen = new Set();
|
|
44
97
|
for (const i of images) {
|
|
@@ -47,8 +100,15 @@ export function checkReleaseImages(images) {
|
|
|
47
100
|
if (seen.has(i.name))
|
|
48
101
|
throw new ReleaseImageError("release-image-name-taken", `release.images names "${i.name}" twice`);
|
|
49
102
|
seen.add(i.name);
|
|
50
|
-
if (
|
|
51
|
-
throw new ReleaseImageError("release-image-
|
|
103
|
+
if (isDockerfile(i) && "build" in i)
|
|
104
|
+
throw new ReleaseImageError("release-image-invalid", `release.images "${i.name}" names a build command and a Dockerfile; chant builds a Dockerfile itself, so name one`);
|
|
105
|
+
const paths = isDockerfile(i) ? [["the archive", archiveOf(i)], ["the Dockerfile", i.dockerfile], ["the context", i.context]] : [["the archive", i.archive]];
|
|
106
|
+
for (const [what, p] of paths) {
|
|
107
|
+
if (p !== undefined && outside(p))
|
|
108
|
+
throw new ReleaseImageError("release-image-archive-outside", `release.images "${i.name}": ${what} ${p} is not a path inside the member`);
|
|
109
|
+
}
|
|
110
|
+
if (isDockerfile(i) && i.platforms !== undefined && (i.platforms.length === 0 || !i.platforms.every((p) => PLATFORM.test(p)))) {
|
|
111
|
+
throw new ReleaseImageError("release-image-invalid", `release.images "${i.name}": platforms is a list of os/arch such as linux/amd64, not ${JSON.stringify(i.platforms)}`);
|
|
52
112
|
}
|
|
53
113
|
}
|
|
54
114
|
}
|
|
@@ -63,11 +123,27 @@ export function buildReleaseImages(i) {
|
|
|
63
123
|
checkReleaseImages(i.images);
|
|
64
124
|
const run = i.run ?? defaultRun;
|
|
65
125
|
const out = [];
|
|
66
|
-
|
|
126
|
+
const envFor = () => {
|
|
67
127
|
const env = { ...(i.env ?? process.env), SOURCE_DATE_EPOCH: String(i.sourceDateEpoch) };
|
|
68
128
|
delete env.CHANT_RELEASE_LABEL;
|
|
69
129
|
if (i.label !== null)
|
|
70
130
|
env.CHANT_RELEASE_LABEL = i.label;
|
|
131
|
+
return env;
|
|
132
|
+
};
|
|
133
|
+
// #4819: each prepare command once, before any build, with no digests file, as the image's contents are built in the cut and the publish job alike.
|
|
134
|
+
const prepared = new Set();
|
|
135
|
+
for (const image of i.images) {
|
|
136
|
+
if (!isDockerfile(image) || !image.prepare || prepared.has(image.prepare))
|
|
137
|
+
continue;
|
|
138
|
+
prepared.add(image.prepare);
|
|
139
|
+
const env = envFor();
|
|
140
|
+
delete env[RELEASE_DIGESTS_ENV];
|
|
141
|
+
const status = run(image.prepare, { cwd: i.cwd, env });
|
|
142
|
+
if (status !== 0)
|
|
143
|
+
throw new ReleaseImageError("release-image-build-failed", `the prepare command of image "${image.name}" exited ${status ?? "with a signal"}: ${image.prepare}`);
|
|
144
|
+
}
|
|
145
|
+
for (const image of resolveReleaseImages(i.images)) {
|
|
146
|
+
const env = envFor();
|
|
71
147
|
const status = run(image.build, { cwd: i.cwd, env });
|
|
72
148
|
if (status !== 0)
|
|
73
149
|
throw new ReleaseImageError("release-image-build-failed", `the build of image "${image.name}" exited ${status ?? "with a signal"}: ${image.build}`);
|
|
@@ -90,6 +166,56 @@ export function releaseImageDigests(release, names) {
|
|
|
90
166
|
}
|
|
91
167
|
return out;
|
|
92
168
|
}
|
|
169
|
+
// ── a product's package build (#4819) ──────────────────────────────────
|
|
170
|
+
const SHA256 = /^sha256:[0-9a-f]{64}$/;
|
|
171
|
+
/**
|
|
172
|
+
* The digest table a release's package build bakes into its bundle: each
|
|
173
|
+
* reference in `images` to the digest the cut recorded for its name, in
|
|
174
|
+
* `images`' order. Undefined outside a release build (no
|
|
175
|
+
* `CHANT_RELEASE_DIGESTS`), when a pipeline names the images by tag. Refuses
|
|
176
|
+
* a label that is not the version and a name the digests file has no digest
|
|
177
|
+
* for.
|
|
178
|
+
*/
|
|
179
|
+
export function releaseImagePins(i) {
|
|
180
|
+
const env = i.env ?? process.env;
|
|
181
|
+
const file = env[RELEASE_DIGESTS_ENV];
|
|
182
|
+
if (!file)
|
|
183
|
+
return undefined;
|
|
184
|
+
const label = env.CHANT_RELEASE_LABEL;
|
|
185
|
+
if (label && label !== i.version) {
|
|
186
|
+
throw new ReleaseImageError("release-label-not-version", `the release is labelled ${label}, and the package is at ${i.version}`, `Set the package's version to ${label} on the default branch, then cut the merged commit.`);
|
|
187
|
+
}
|
|
188
|
+
const named = JSON.parse(readFileSync(file, "utf-8"));
|
|
189
|
+
const table = {};
|
|
190
|
+
for (const [name, reference] of Object.entries(i.images)) {
|
|
191
|
+
const digest = named[name];
|
|
192
|
+
if (typeof digest !== "string" || !SHA256.test(digest))
|
|
193
|
+
throw new ReleaseImageError("release-image-digest-missing", `${file} has no digest for the ${name} image, which the release builds`);
|
|
194
|
+
table[reference] = digest;
|
|
195
|
+
}
|
|
196
|
+
return table;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Write the pins file an npm target's `pins` names, once the bundle is
|
|
200
|
+
* built: the table as JSON, after checking that every bundle in `bundles`
|
|
201
|
+
* carries each digest. With no table (not a release build), remove the file,
|
|
202
|
+
* so a package never ships a stale one.
|
|
203
|
+
*/
|
|
204
|
+
export function writeReleaseImagePins(table, o) {
|
|
205
|
+
if (!table) {
|
|
206
|
+
rmSync(o.out, { force: true });
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
for (const bundle of o.bundles ?? []) {
|
|
210
|
+
const text = readFileSync(bundle, "utf-8");
|
|
211
|
+
for (const [reference, digest] of Object.entries(table)) {
|
|
212
|
+
if (!text.includes(digest))
|
|
213
|
+
throw new ReleaseImageError("release-pin-not-bundled", `${bundle} does not carry ${reference}@${digest}`);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
mkdirSync(dirname(o.out), { recursive: true });
|
|
217
|
+
writeFileSync(o.out, `${JSON.stringify(table, null, 2)}\n`);
|
|
218
|
+
}
|
|
93
219
|
/** Write the digests file a package build reads. */
|
|
94
220
|
export function writeReleaseDigests(file, digests) {
|
|
95
221
|
mkdirSync(dirname(file), { recursive: true });
|
package/dist/notify-product.d.ts
CHANGED
|
@@ -13,13 +13,22 @@
|
|
|
13
13
|
*/
|
|
14
14
|
import { type CiProductNotifyWords, type CiProductWords } from "./ci-product-words.js";
|
|
15
15
|
import type { NotifyFields, SendNotifyOptions } from "./notify.js";
|
|
16
|
+
/**
|
|
17
|
+
* The first chant whose Slack and Teams messages take a product's words, a
|
|
18
|
+
* drift event's `rerunUrl` and the relay's name (#4818): `notify.message` in
|
|
19
|
+
* the words, `message` in sendNotify's options, and the third argument of
|
|
20
|
+
* `slackMessage` and `teamsMessage`.
|
|
21
|
+
*/
|
|
22
|
+
export declare const NOTIFY_MESSAGE_WORDS_FLOOR = "0.124.78";
|
|
16
23
|
/** The notify slots of `words`, chant's when a caller built words before #4610. */
|
|
17
24
|
export declare function productNotifyWords(words: CiProductWords): CiProductNotifyWords;
|
|
18
25
|
/** The event's `source` under `words`: `notify.source`, else the product's name. */
|
|
19
26
|
export declare function productNotifySource(words: CiProductWords): string;
|
|
20
27
|
/**
|
|
21
|
-
* `options` with the product's header prefix, unless the caller names one
|
|
22
|
-
*
|
|
28
|
+
* `options` with the product's header prefix, unless the caller names one,
|
|
29
|
+
* and its message words (`notify.message`, #4818) under the caller's
|
|
30
|
+
* `message`. chant's own words with no message words leave `options` as they
|
|
31
|
+
* are, so chant sends `x-chant`.
|
|
23
32
|
*/
|
|
24
33
|
export declare function productSendOptions(words: CiProductWords, options?: SendNotifyOptions): SendNotifyOptions;
|
|
25
34
|
/** What a drift notice concerns. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"notify-product.d.ts","sourceRoot":"","sources":["../src/notify-product.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAA6B,KAAK,oBAAoB,EAAE,KAAK,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAC/G,OAAO,KAAK,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"notify-product.d.ts","sourceRoot":"","sources":["../src/notify-product.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAA6B,KAAK,oBAAoB,EAAE,KAAK,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAC/G,OAAO,KAAK,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAGhE;;;;;GAKG;AACH,eAAO,MAAM,0BAA0B,aAAe,CAAC;AAEvD,mFAAmF;AACnF,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,cAAc,GAAG,oBAAoB,CAE9E;AAED,oFAAoF;AACpF,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,cAAc,GAAG,MAAM,CAEjE;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,cAAc,EAAE,OAAO,GAAE,iBAAsB,GAAG,iBAAiB,CAK5G;AAED,oCAAoC;AACpC,MAAM,WAAW,gBAAgB;IAC/B,6CAA6C;IAC7C,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,mEAAmE;IACnE,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,cAAc,EAAE,KAAK,EAAE,gBAAgB,GAAG,YAAY,CAqB9F"}
|
package/dist/notify-product.js
CHANGED
|
@@ -12,6 +12,14 @@
|
|
|
12
12
|
* ```
|
|
13
13
|
*/
|
|
14
14
|
import { CHANT_WORDS, isChantWords } from "./ci-product-words.js";
|
|
15
|
+
import { NEXT_RELEASE } from "./release-floors.js";
|
|
16
|
+
/**
|
|
17
|
+
* The first chant whose Slack and Teams messages take a product's words, a
|
|
18
|
+
* drift event's `rerunUrl` and the relay's name (#4818): `notify.message` in
|
|
19
|
+
* the words, `message` in sendNotify's options, and the third argument of
|
|
20
|
+
* `slackMessage` and `teamsMessage`.
|
|
21
|
+
*/
|
|
22
|
+
export const NOTIFY_MESSAGE_WORDS_FLOOR = NEXT_RELEASE;
|
|
15
23
|
/** The notify slots of `words`, chant's when a caller built words before #4610. */
|
|
16
24
|
export function productNotifyWords(words) {
|
|
17
25
|
return words.notify ?? CHANT_WORDS.notify;
|
|
@@ -21,13 +29,17 @@ export function productNotifySource(words) {
|
|
|
21
29
|
return productNotifyWords(words).source;
|
|
22
30
|
}
|
|
23
31
|
/**
|
|
24
|
-
* `options` with the product's header prefix, unless the caller names one
|
|
25
|
-
*
|
|
32
|
+
* `options` with the product's header prefix, unless the caller names one,
|
|
33
|
+
* and its message words (`notify.message`, #4818) under the caller's
|
|
34
|
+
* `message`. chant's own words with no message words leave `options` as they
|
|
35
|
+
* are, so chant sends `x-chant`.
|
|
26
36
|
*/
|
|
27
37
|
export function productSendOptions(words, options = {}) {
|
|
38
|
+
const own = productNotifyWords(words);
|
|
39
|
+
const out = own.message ? { ...options, message: { ...own.message, ...options.message } } : options;
|
|
28
40
|
if (options.prefix !== undefined || isChantWords(words))
|
|
29
|
-
return
|
|
30
|
-
return { ...
|
|
41
|
+
return out;
|
|
42
|
+
return { ...out, prefix: own.headers };
|
|
31
43
|
}
|
|
32
44
|
/**
|
|
33
45
|
* A `drift` event under `words`: `2 stacks drifted: shop-dev, shop-prod` for
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `chant notify relay --repo <url> --token-env <VAR>` (#4759): the relay
|
|
3
|
+
* clones the repository itself, so a product's relay container needs no
|
|
4
|
+
* entrypoint of its own.
|
|
5
|
+
*
|
|
6
|
+
* Before it serves, the relay:
|
|
7
|
+
*
|
|
8
|
+
* 1. refuses a token that is more than approve-only. An approve-only token
|
|
9
|
+
* reads the repo and pushes `chant/lifecycle`: it neither administers the
|
|
10
|
+
* repo nor may push to the default branch, and on GitLab it carries no
|
|
11
|
+
* scope beyond `read_api`, `read_repository` and `write_repository`
|
|
12
|
+
* ({@link relayTokenVerdict}). The forge is read with a few GETs of its
|
|
13
|
+
* own ({@link readTokenReach}), kept out of `./pr-forge.ts` so the forge
|
|
14
|
+
* client's bundles do not grow for the relay;
|
|
15
|
+
* 2. clones the repo into a checkout of its default branch. The token goes
|
|
16
|
+
* to git as an `http.extraHeader` in the environment
|
|
17
|
+
* (`GIT_CONFIG_COUNT`/`KEY`/`VALUE`), for this process's git calls only,
|
|
18
|
+
* and is never written to a file or into the remote's address.
|
|
19
|
+
*
|
|
20
|
+
* Each click then fetches the default branch before it reads the signers
|
|
21
|
+
* (`../cli/handlers/notify-relay.ts`), so a signer added since start counts
|
|
22
|
+
* without a restart.
|
|
23
|
+
*/
|
|
24
|
+
import type { ForgeFetch, ForgeKind } from "./pr-forge.js";
|
|
25
|
+
/** The repository the relay clones, read from its clone address. */
|
|
26
|
+
export interface RelayRepo {
|
|
27
|
+
forge: ForgeKind;
|
|
28
|
+
/** Lower-cased, with the port when there is one. */
|
|
29
|
+
host: string;
|
|
30
|
+
/** `<scheme>://<host>[:port]`. */
|
|
31
|
+
web: string;
|
|
32
|
+
/** `owner/name`, or GitLab's `group/subgroup/name`. */
|
|
33
|
+
path: string;
|
|
34
|
+
/** The clone address as given. */
|
|
35
|
+
url: string;
|
|
36
|
+
/** The forge's REST base. */
|
|
37
|
+
apiBase: string;
|
|
38
|
+
}
|
|
39
|
+
/** The relay's clone address and forge, or why they do not name a repository. */
|
|
40
|
+
export declare function relayRepo(url: string, forge?: string): RelayRepo | {
|
|
41
|
+
error: string;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* What a token may do in a repository, as {@link readTokenReach} reads it.
|
|
45
|
+
*
|
|
46
|
+
* - GitHub: whether it is a classic token (GitHub sends `x-oauth-scopes` for
|
|
47
|
+
* one), whether its account administers the repository, and whether the
|
|
48
|
+
* default branch is protected.
|
|
49
|
+
* - GitLab: the token's scopes, its user's access level on the project
|
|
50
|
+
* (project or group, the higher), and the push access levels of the
|
|
51
|
+
* default branch's protection, or null when it is not protected.
|
|
52
|
+
* - Forgejo: its user's login, whether that user administers the
|
|
53
|
+
* repository, and whether it may push to the default branch.
|
|
54
|
+
*/
|
|
55
|
+
export type TokenReach = {
|
|
56
|
+
kind: "github";
|
|
57
|
+
classic: boolean;
|
|
58
|
+
admin: boolean;
|
|
59
|
+
branchProtected: boolean;
|
|
60
|
+
} | {
|
|
61
|
+
kind: "gitlab";
|
|
62
|
+
scopes: string[];
|
|
63
|
+
accessLevel: number;
|
|
64
|
+
pushLevels: number[] | null;
|
|
65
|
+
} | {
|
|
66
|
+
kind: "forgejo";
|
|
67
|
+
login: string;
|
|
68
|
+
admin: boolean;
|
|
69
|
+
canPush: boolean;
|
|
70
|
+
};
|
|
71
|
+
/** Read what `token` reaches in `repo`, with GETs against the forge's REST base. Throws when a read the check needs is refused. */
|
|
72
|
+
export declare function readTokenReach(repo: RelayRepo, token: string, branch: string, fetchImpl?: ForgeFetch): Promise<TokenReach>;
|
|
73
|
+
/** The scopes an approve-only GitLab token may carry. */
|
|
74
|
+
export declare const RELAY_GITLAB_SCOPES: readonly string[];
|
|
75
|
+
/** The git user a token pushes as over https, before the forge names one. */
|
|
76
|
+
export declare const relayGitUser: (forge: ForgeKind) => string;
|
|
77
|
+
/**
|
|
78
|
+
* Why a token reaching `reach` is more than approve-only, or the git user
|
|
79
|
+
* name it pushes as.
|
|
80
|
+
*/
|
|
81
|
+
export declare function relayTokenVerdict(reach: TokenReach, repoPath: string, branch: string): {
|
|
82
|
+
user: string;
|
|
83
|
+
} | {
|
|
84
|
+
error: string;
|
|
85
|
+
};
|
|
86
|
+
/** Read what the token reaches through chant's forge client, and judge it. Never throws. */
|
|
87
|
+
export declare function checkRelayToken(repo: RelayRepo, token: string, branch: string, fetchImpl?: ForgeFetch): Promise<{
|
|
88
|
+
user: string;
|
|
89
|
+
} | {
|
|
90
|
+
error: string;
|
|
91
|
+
}>;
|
|
92
|
+
/**
|
|
93
|
+
* The git config that carries the token as a header, appended after any
|
|
94
|
+
* `GIT_CONFIG_*` entries `env` already has. Nothing is written to a file.
|
|
95
|
+
*/
|
|
96
|
+
export declare function relayGitAuthEnv(user: string, token: string, env?: NodeJS.ProcessEnv): Record<string, string>;
|
|
97
|
+
/** The default branch the remote at `url` names, or why it could not be read. */
|
|
98
|
+
export declare function relayDefaultBranch(url: string, env: NodeJS.ProcessEnv): Promise<{
|
|
99
|
+
branch: string;
|
|
100
|
+
} | {
|
|
101
|
+
error: string;
|
|
102
|
+
}>;
|
|
103
|
+
/** A checkout of `branch` of the repo at `url`, in a new directory under `parent` (default the system's temporary one). */
|
|
104
|
+
export declare function relayCheckout(url: string, branch: string, env: NodeJS.ProcessEnv, parent?: string): Promise<{
|
|
105
|
+
dir: string;
|
|
106
|
+
} | {
|
|
107
|
+
error: string;
|
|
108
|
+
}>;
|
|
109
|
+
/** What {@link prepareRelayCheckout} made: the checkout, its default branch, and the git config every later git call needs. */
|
|
110
|
+
export interface RelayCheckout {
|
|
111
|
+
dir: string;
|
|
112
|
+
branch: string;
|
|
113
|
+
repo: RelayRepo;
|
|
114
|
+
user: string;
|
|
115
|
+
gitEnv: Record<string, string>;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Check the token and clone the repo, in that order: a token that is more
|
|
119
|
+
* than approve-only is refused before anything is cloned with it.
|
|
120
|
+
*/
|
|
121
|
+
export declare function prepareRelayCheckout(o: {
|
|
122
|
+
url: string;
|
|
123
|
+
token: string;
|
|
124
|
+
forge?: string;
|
|
125
|
+
env?: NodeJS.ProcessEnv;
|
|
126
|
+
fetch?: ForgeFetch;
|
|
127
|
+
parent?: string;
|
|
128
|
+
}): Promise<RelayCheckout | {
|
|
129
|
+
error: string;
|
|
130
|
+
}>;
|
|
131
|
+
//# sourceMappingURL=notify-relay-repo.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"notify-relay-repo.d.ts","sourceRoot":"","sources":["../src/notify-relay-repo.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `chant notify relay --repo <url> --token-env <VAR>` (#4759): the relay
|
|
3
|
+
* clones the repository itself, so a product's relay container needs no
|
|
4
|
+
* entrypoint of its own.
|
|
5
|
+
*
|
|
6
|
+
* Before it serves, the relay:
|
|
7
|
+
*
|
|
8
|
+
* 1. refuses a token that is more than approve-only. An approve-only token
|
|
9
|
+
* reads the repo and pushes `chant/lifecycle`: it neither administers the
|
|
10
|
+
* repo nor may push to the default branch, and on GitLab it carries no
|
|
11
|
+
* scope beyond `read_api`, `read_repository` and `write_repository`
|
|
12
|
+
* ({@link relayTokenVerdict}). The forge is read with a few GETs of its
|
|
13
|
+
* own ({@link readTokenReach}), kept out of `./pr-forge.ts` so the forge
|
|
14
|
+
* client's bundles do not grow for the relay;
|
|
15
|
+
* 2. clones the repo into a checkout of its default branch. The token goes
|
|
16
|
+
* to git as an `http.extraHeader` in the environment
|
|
17
|
+
* (`GIT_CONFIG_COUNT`/`KEY`/`VALUE`), for this process's git calls only,
|
|
18
|
+
* and is never written to a file or into the remote's address.
|
|
19
|
+
*
|
|
20
|
+
* Each click then fetches the default branch before it reads the signers
|
|
21
|
+
* (`../cli/handlers/notify-relay.ts`), so a signer added since start counts
|
|
22
|
+
* without a restart.
|
|
23
|
+
*/
|
|
24
|
+
import { execFile } from "node:child_process";
|
|
25
|
+
import { mkdtempSync } from "node:fs";
|
|
26
|
+
import { tmpdir } from "node:os";
|
|
27
|
+
import { join } from "node:path";
|
|
28
|
+
/** The relay's clone address and forge, or why they do not name a repository. */
|
|
29
|
+
export function relayRepo(url, forge) {
|
|
30
|
+
const u = url.trim();
|
|
31
|
+
if (!/^https?:\/\//.test(u))
|
|
32
|
+
return { error: `--repo is the repository's https clone address, such as https://github.com/acme/infra.git (got ${JSON.stringify(url)})` };
|
|
33
|
+
if (/^https?:\/\/[^/]*@/.test(u))
|
|
34
|
+
return { error: "--repo must not carry a credential; the token goes in the variable --token-env names" };
|
|
35
|
+
const m = /^(https?):\/\/([^/:]+)(:\d+)?\/(.+?)(?:\.git)?\/?$/.exec(u);
|
|
36
|
+
if (!m || !m[4].includes("/"))
|
|
37
|
+
return { error: `--repo names no owner/name: ${u}` };
|
|
38
|
+
const host = m[2].toLowerCase();
|
|
39
|
+
const kind = forge === "github" || forge === "gitlab" || forge === "forgejo" ? forge : host === "github.com" ? "github" : host === "gitlab.com" ? "gitlab" : undefined;
|
|
40
|
+
if (forge !== undefined && forge !== "" && kind !== forge)
|
|
41
|
+
return { error: `--forge is github, gitlab or forgejo (got ${JSON.stringify(forge)})` };
|
|
42
|
+
if (!kind)
|
|
43
|
+
return { error: `${host} is not github.com or gitlab.com; pass --forge github, gitlab or forgejo` };
|
|
44
|
+
const web = `${m[1]}://${host}${m[3] ?? ""}`;
|
|
45
|
+
const apiBase = kind === "gitlab" ? `${web}/api/v4` : kind === "forgejo" ? `${web}/api/v1` : host === "github.com" ? "https://api.github.com" : `${web}/api/v3`;
|
|
46
|
+
return { forge: kind, host: `${host}${m[3] ?? ""}`, web, path: m[4], url: u, apiBase };
|
|
47
|
+
}
|
|
48
|
+
const isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
49
|
+
/** The header the forge takes a token in, as chant's forge clients send it. */
|
|
50
|
+
function authHeaders(forge, token) {
|
|
51
|
+
if (forge === "gitlab")
|
|
52
|
+
return { "PRIVATE-TOKEN": token, Accept: "application/json" };
|
|
53
|
+
if (forge === "forgejo")
|
|
54
|
+
return { Authorization: `token ${token}`, Accept: "application/json" };
|
|
55
|
+
return { Authorization: `Bearer ${token}`, Accept: "application/vnd.github+json" };
|
|
56
|
+
}
|
|
57
|
+
/** Read what `token` reaches in `repo`, with GETs against the forge's REST base. Throws when a read the check needs is refused. */
|
|
58
|
+
export async function readTokenReach(repo, token, branch, fetchImpl = globalThis.fetch) {
|
|
59
|
+
const headers = authHeaders(repo.forge, token);
|
|
60
|
+
const get = async (path, missing) => {
|
|
61
|
+
const url = `${repo.apiBase}${path}`;
|
|
62
|
+
const res = await fetchImpl(url, { method: "GET", headers });
|
|
63
|
+
const text = await res.text();
|
|
64
|
+
const header = (n) => res.headers?.get(n) ?? null;
|
|
65
|
+
if (res.status === 404 && missing)
|
|
66
|
+
return { body: null, header };
|
|
67
|
+
if (!res.ok)
|
|
68
|
+
throw new Error(`GET ${path} answered ${res.status}`);
|
|
69
|
+
return { body: text ? JSON.parse(text) : null, header };
|
|
70
|
+
};
|
|
71
|
+
const enc = (b) => b.split("/").map(encodeURIComponent).join("/");
|
|
72
|
+
if (repo.forge === "gitlab") {
|
|
73
|
+
const project = `/projects/${encodeURIComponent(repo.path)}`;
|
|
74
|
+
const self = (await get("/personal_access_tokens/self")).body;
|
|
75
|
+
const scopes = isObject(self) && Array.isArray(self.scopes) ? self.scopes.filter((x) => typeof x === "string") : [];
|
|
76
|
+
const got = (await get(project)).body;
|
|
77
|
+
const perms = isObject(got) && isObject(got.permissions) ? got.permissions : {};
|
|
78
|
+
const level = (k) => {
|
|
79
|
+
const v = perms[k];
|
|
80
|
+
return isObject(v) && typeof v.access_level === "number" ? v.access_level : 0;
|
|
81
|
+
};
|
|
82
|
+
const accessLevel = Math.max(level("project_access"), level("group_access"));
|
|
83
|
+
const pb = (await get(`${project}/protected_branches/${encodeURIComponent(branch)}`, "null")).body;
|
|
84
|
+
if (pb === null)
|
|
85
|
+
return { kind: "gitlab", scopes, accessLevel, pushLevels: null };
|
|
86
|
+
const push = isObject(pb) && Array.isArray(pb.push_access_levels) ? pb.push_access_levels : [];
|
|
87
|
+
const pushLevels = push.map((p) => (isObject(p) && typeof p.access_level === "number" ? p.access_level : undefined)).filter((n) => n !== undefined);
|
|
88
|
+
return { kind: "gitlab", scopes, accessLevel, pushLevels };
|
|
89
|
+
}
|
|
90
|
+
const at = `/repos/${repo.path}`;
|
|
91
|
+
const adminOf = (v) => isObject(v) && isObject(v.permissions) && v.permissions.admin === true;
|
|
92
|
+
if (repo.forge === "forgejo") {
|
|
93
|
+
const me = (await get("/user")).body;
|
|
94
|
+
const r = (await get(at)).body;
|
|
95
|
+
const b = (await get(`${at}/branches/${enc(branch)}`)).body;
|
|
96
|
+
// Anything but an explicit false counts as a push: a Forgejo that does not say is not trusted.
|
|
97
|
+
return { kind: "forgejo", login: isObject(me) && typeof me.login === "string" ? me.login : "", admin: adminOf(r), canPush: !(isObject(b) && b.user_can_push === false) };
|
|
98
|
+
}
|
|
99
|
+
const r = await get(at);
|
|
100
|
+
const b = (await get(`${at}/branches/${enc(branch)}`)).body;
|
|
101
|
+
return { kind: "github", classic: Boolean(r.header("x-oauth-scopes")), admin: adminOf(r.body), branchProtected: isObject(b) && b.protected === true };
|
|
102
|
+
}
|
|
103
|
+
/** The scopes an approve-only GitLab token may carry. */
|
|
104
|
+
export const RELAY_GITLAB_SCOPES = ["read_api", "read_repository", "write_repository"];
|
|
105
|
+
/** GitLab's Maintainer access level. */
|
|
106
|
+
const GITLAB_MAINTAINER = 40;
|
|
107
|
+
/** The git user a token pushes as over https, before the forge names one. */
|
|
108
|
+
export const relayGitUser = (forge) => (forge === "github" ? "x-access-token" : "oauth2");
|
|
109
|
+
/**
|
|
110
|
+
* Why a token reaching `reach` is more than approve-only, or the git user
|
|
111
|
+
* name it pushes as.
|
|
112
|
+
*/
|
|
113
|
+
export function relayTokenVerdict(reach, repoPath, branch) {
|
|
114
|
+
if (reach.kind === "gitlab") {
|
|
115
|
+
const extra = reach.scopes.filter((s) => !RELAY_GITLAB_SCOPES.includes(s));
|
|
116
|
+
if (extra.length > 0)
|
|
117
|
+
return { error: `the token has the scopes ${extra.join(", ")}; an approve-only token has write_repository and read_api only` };
|
|
118
|
+
if (reach.accessLevel >= GITLAB_MAINTAINER)
|
|
119
|
+
return { error: `the token's user is a Maintainer or Owner of ${repoPath}; give the relay a Developer` };
|
|
120
|
+
if (reach.pushLevels === null)
|
|
121
|
+
return { error: `${branch} is not a protected branch, so the token could push to it` };
|
|
122
|
+
if (reach.pushLevels.some((l) => l > 0 && l <= reach.accessLevel))
|
|
123
|
+
return { error: `the token's user may push to ${branch}` };
|
|
124
|
+
return { user: relayGitUser("gitlab") };
|
|
125
|
+
}
|
|
126
|
+
if (reach.kind === "github") {
|
|
127
|
+
if (reach.classic)
|
|
128
|
+
return { error: "a classic token reaches every repo its account can; give the relay a fine-grained token for this repo with Contents: Read and write, and nothing else" };
|
|
129
|
+
if (reach.admin)
|
|
130
|
+
return { error: `the token's account administers ${repoPath}` };
|
|
131
|
+
if (!reach.branchProtected)
|
|
132
|
+
return { error: `${branch} is not protected, so the token could push to it` };
|
|
133
|
+
return { user: relayGitUser("github") };
|
|
134
|
+
}
|
|
135
|
+
if (reach.admin)
|
|
136
|
+
return { error: `the token's user administers ${repoPath}` };
|
|
137
|
+
if (reach.canPush)
|
|
138
|
+
return { error: `the token's user may push to ${branch}; protect it so only reviewed merges reach it` };
|
|
139
|
+
return { user: reach.login || relayGitUser("forgejo") };
|
|
140
|
+
}
|
|
141
|
+
/** Read what the token reaches through chant's forge client, and judge it. Never throws. */
|
|
142
|
+
export async function checkRelayToken(repo, token, branch, fetchImpl) {
|
|
143
|
+
try {
|
|
144
|
+
return relayTokenVerdict(await readTokenReach(repo, token, branch, fetchImpl), repo.path, branch);
|
|
145
|
+
}
|
|
146
|
+
catch (e) {
|
|
147
|
+
return { error: `the forge could not be read with the token (${e.message})` };
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* The git config that carries the token as a header, appended after any
|
|
152
|
+
* `GIT_CONFIG_*` entries `env` already has. Nothing is written to a file.
|
|
153
|
+
*/
|
|
154
|
+
export function relayGitAuthEnv(user, token, env = {}) {
|
|
155
|
+
const n = Number(env.GIT_CONFIG_COUNT ?? "0");
|
|
156
|
+
const at = Number.isInteger(n) && n > 0 ? n : 0;
|
|
157
|
+
return {
|
|
158
|
+
GIT_CONFIG_COUNT: String(at + 1),
|
|
159
|
+
[`GIT_CONFIG_KEY_${at}`]: "http.extraHeader",
|
|
160
|
+
[`GIT_CONFIG_VALUE_${at}`]: `Authorization: Basic ${Buffer.from(`${user}:${token}`).toString("base64")}`,
|
|
161
|
+
GIT_TERMINAL_PROMPT: "0",
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/** Run git without blocking the process, so a forge served by the same process (a test's) can answer. */
|
|
165
|
+
const runGit = (args, env) => new Promise((done) => {
|
|
166
|
+
execFile("git", args, { encoding: "utf-8", env, maxBuffer: 16 * 1024 * 1024 }, (err, stdout, stderr) => done({ ok: !err, stdout: stdout ?? "", stderr: stderr ?? "" }));
|
|
167
|
+
});
|
|
168
|
+
/** The default branch the remote at `url` names, or why it could not be read. */
|
|
169
|
+
export async function relayDefaultBranch(url, env) {
|
|
170
|
+
const ls = await runGit(["ls-remote", "--symref", url, "HEAD"], env);
|
|
171
|
+
const branch = /^ref: refs\/heads\/(\S+)\s+HEAD/m.exec(ls.stdout)?.[1];
|
|
172
|
+
return branch ? { branch } : { error: ls.stderr.trim() || "the remote names no default branch" };
|
|
173
|
+
}
|
|
174
|
+
/** A checkout of `branch` of the repo at `url`, in a new directory under `parent` (default the system's temporary one). */
|
|
175
|
+
export async function relayCheckout(url, branch, env, parent = tmpdir()) {
|
|
176
|
+
const dir = mkdtempSync(join(parent, "chant-relay-"));
|
|
177
|
+
const r = await runGit(["clone", "-q", "--branch", branch, url, dir], env);
|
|
178
|
+
return r.ok ? { dir } : { error: `cannot clone ${url}: ${r.stderr.trim()}` };
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Check the token and clone the repo, in that order: a token that is more
|
|
182
|
+
* than approve-only is refused before anything is cloned with it.
|
|
183
|
+
*/
|
|
184
|
+
export async function prepareRelayCheckout(o) {
|
|
185
|
+
const env = o.env ?? process.env;
|
|
186
|
+
const repo = relayRepo(o.url, o.forge);
|
|
187
|
+
if ("error" in repo)
|
|
188
|
+
return repo;
|
|
189
|
+
if (!o.token)
|
|
190
|
+
return { error: "the token is empty: the relay pushes approvals with an approve-only token" };
|
|
191
|
+
const probe = await relayDefaultBranch(repo.url, { ...env, ...relayGitAuthEnv(relayGitUser(repo.forge), o.token, env) });
|
|
192
|
+
if ("error" in probe)
|
|
193
|
+
return { error: `cannot read ${repo.path} with the token: ${probe.error}` };
|
|
194
|
+
const checked = await checkRelayToken(repo, o.token, probe.branch, o.fetch);
|
|
195
|
+
if ("error" in checked)
|
|
196
|
+
return { error: `the relay holds only an approve-only token, and this one is more: ${checked.error}` };
|
|
197
|
+
const gitEnv = relayGitAuthEnv(checked.user, o.token, env);
|
|
198
|
+
const cloned = await relayCheckout(repo.url, probe.branch, { ...env, ...gitEnv }, o.parent);
|
|
199
|
+
if ("error" in cloned)
|
|
200
|
+
return cloned;
|
|
201
|
+
return { dir: cloned.dir, branch: probe.branch, repo, user: checked.user, gitEnv };
|
|
202
|
+
}
|
package/dist/notify-relay.d.ts
CHANGED
|
@@ -51,6 +51,15 @@ import type { GatedRequest } from "./lifecycle/gated-request-digest.js";
|
|
|
51
51
|
* its own (#4611). Products feature-detect on it.
|
|
52
52
|
*/
|
|
53
53
|
export declare const NOTIFY_RELAY_POINTS_FLOOR = "0.124.72";
|
|
54
|
+
/**
|
|
55
|
+
* The first chant whose `chant notify relay --repo <url> --token-env <VAR>`
|
|
56
|
+
* refuses a token that is more than approve-only and clones the repo itself,
|
|
57
|
+
* whose `--host` listens where asked, which acknowledges a Slack link button's
|
|
58
|
+
* click, and which reads the signers at the default branch's current tip
|
|
59
|
+
* (#4759). A product whose relay container ran its own entrypoint for these
|
|
60
|
+
* feature-detects on it.
|
|
61
|
+
*/
|
|
62
|
+
export declare const NOTIFY_RELAY_CLONE_FLOOR = "0.124.77";
|
|
54
63
|
/** The relay's settings, by environment variable. */
|
|
55
64
|
export declare const RELAY_ENV: {
|
|
56
65
|
/** The key the notify buttons are signed with: the variable `notify.relay` names on the sending side. */
|
|
@@ -66,6 +75,15 @@ export declare const RELAY_ENV: {
|
|
|
66
75
|
/** Comma-separated address prefixes a Slack response_url may have. */
|
|
67
76
|
readonly slackResponse: "CHANT_RELAY_SLACK_RESPONSE";
|
|
68
77
|
};
|
|
78
|
+
/** The variables that stand in for `--repo`, the default `--token-env`, and `--forge` (#4759). */
|
|
79
|
+
export declare const RELAY_REPO_ENV: {
|
|
80
|
+
/** The repository's https clone address. */
|
|
81
|
+
readonly repo: "CHANT_RELAY_REPO";
|
|
82
|
+
/** The variable `--token-env` reads by default: the approve-only token. */
|
|
83
|
+
readonly token: "CHANT_RELAY_TOKEN";
|
|
84
|
+
/** github, gitlab or forgejo, for a host other than github.com and gitlab.com. */
|
|
85
|
+
readonly forge: "CHANT_RELAY_FORGE";
|
|
86
|
+
};
|
|
69
87
|
/** Slack's own reply hosts: hooks.slack.com, and GovSlack's. */
|
|
70
88
|
export declare const SLACK_RESPONSE_PREFIXES: string[];
|
|
71
89
|
/** Where the relay listens by default: loopback, so it is reachable only through what the operator puts in front of it. */
|
|
@@ -92,9 +110,15 @@ export interface RelayClick {
|
|
|
92
110
|
responseUrl?: string;
|
|
93
111
|
thread?: string;
|
|
94
112
|
}
|
|
95
|
-
/**
|
|
113
|
+
/**
|
|
114
|
+
* A Slack `block_actions` payload (the form field `payload`), or why the
|
|
115
|
+
* relay does not read it. `ignore` is set for a click on a link button (one
|
|
116
|
+
* with a `url`, such as a product's Re-plan link on the same message): Slack
|
|
117
|
+
* posts it too, and the relay acknowledges it and does nothing (#4759).
|
|
118
|
+
*/
|
|
96
119
|
export declare function slackRelayClick(raw: string): RelayClick | {
|
|
97
120
|
error: string;
|
|
121
|
+
ignore?: true;
|
|
98
122
|
};
|
|
99
123
|
/** A Teams outgoing webhook's message, `@<relay> approve|decline <token>`, or why it is not one. */
|
|
100
124
|
export declare function teamsRelayClick(raw: string): RelayClick | {
|