@intentius/chant 0.124.87 → 0.124.88
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/action-pins.d.ts +1 -0
- package/dist/action-pins.d.ts.map +1 -1
- package/dist/action-pins.js +2 -0
- package/dist/actor.d.ts +12 -0
- package/dist/actor.d.ts.map +1 -0
- package/dist/actor.js +18 -0
- package/dist/bundle-check.d.ts +20 -5
- package/dist/bundle-check.d.ts.map +1 -1
- package/dist/bundle-check.js +20 -5
- package/dist/chant-install.d.ts +98 -0
- package/dist/chant-install.d.ts.map +1 -1
- package/dist/chant-install.js +204 -2
- package/dist/ci-commit.d.ts +1 -1
- package/dist/ci-commit.d.ts.map +1 -1
- package/dist/ci-commit.js +1 -2
- package/dist/ci-control.d.ts +1 -1
- package/dist/ci-control.d.ts.map +1 -1
- package/dist/ci-control.js +1 -1
- package/dist/ci-evidence-claims.d.ts +59 -0
- package/dist/ci-evidence-claims.d.ts.map +1 -0
- package/dist/ci-evidence-claims.js +87 -0
- package/dist/ci-evidence-from-job.d.ts +103 -0
- package/dist/ci-evidence-from-job.d.ts.map +1 -0
- package/dist/ci-evidence-from-job.js +162 -0
- package/dist/ci-evidence-jobs.d.ts +39 -4
- package/dist/ci-evidence-jobs.d.ts.map +1 -1
- package/dist/ci-evidence-jobs.js +38 -4
- package/dist/ci-handover.d.ts +12 -0
- package/dist/ci-handover.d.ts.map +1 -1
- package/dist/ci-handover.js +15 -3
- package/dist/ci-identity.d.ts +32 -6
- package/dist/ci-identity.d.ts.map +1 -1
- package/dist/ci-identity.js +40 -8
- package/dist/ci-image.d.ts +47 -9
- package/dist/ci-image.d.ts.map +1 -1
- package/dist/ci-image.js +45 -18
- package/dist/ci-jobs.d.ts +1 -1
- package/dist/ci-jobs.d.ts.map +1 -1
- package/dist/ci-jobs.js +7 -2
- package/dist/ci-login.d.ts +75 -13
- package/dist/ci-login.d.ts.map +1 -1
- package/dist/ci-login.js +89 -37
- package/dist/ci-product-words.d.ts +20 -1
- package/dist/ci-product-words.d.ts.map +1 -1
- package/dist/ci-product-words.js +27 -1
- package/dist/ci-product.d.ts +21 -2
- package/dist/ci-product.d.ts.map +1 -1
- package/dist/ci-product.js +21 -2
- package/dist/cli/handlers/approve-confirm.d.ts +14 -0
- package/dist/cli/handlers/approve-confirm.d.ts.map +1 -1
- package/dist/cli/handlers/approve-confirm.js +46 -0
- 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 +22 -1
- package/dist/cli/handlers/ci-check.d.ts.map +1 -1
- package/dist/cli/handlers/ci-check.js +21 -1
- package/dist/cli/handlers/ci-evidence.d.ts.map +1 -1
- package/dist/cli/handlers/ci-evidence.js +227 -18
- package/dist/cli/handlers/ci-forge-check.d.ts.map +1 -1
- package/dist/cli/handlers/ci-forge-check.js +34 -17
- package/dist/cli/handlers/generated-check.d.ts +3 -0
- package/dist/cli/handlers/generated-check.d.ts.map +1 -0
- package/dist/cli/handlers/generated-check.js +41 -0
- package/dist/cli/handlers/lifecycle.d.ts.map +1 -1
- package/dist/cli/handlers/lifecycle.js +3 -0
- package/dist/cli/handlers/local-chant.d.ts +13 -0
- package/dist/cli/handlers/local-chant.d.ts.map +1 -1
- package/dist/cli/handlers/local-chant.js +42 -1
- package/dist/cli/handlers/pr.d.ts.map +1 -1
- package/dist/cli/handlers/pr.js +42 -5
- package/dist/cli/handlers/run-wave.js +8 -0
- package/dist/cli/main.d.ts.map +1 -1
- package/dist/cli/main.js +38 -4
- package/dist/cli/parse-args.d.ts.map +1 -1
- package/dist/cli/parse-args.js +66 -0
- package/dist/cli/registry.d.ts +24 -0
- package/dist/cli/registry.d.ts.map +1 -1
- package/dist/components/check-hooks.d.ts +10 -0
- package/dist/components/check-hooks.d.ts.map +1 -1
- package/dist/components/check-hooks.js +18 -0
- package/dist/components/pr-checks.d.ts +105 -0
- package/dist/components/pr-checks.d.ts.map +1 -0
- package/dist/components/pr-checks.js +119 -0
- package/dist/components/pr-pipeline.d.ts +34 -0
- package/dist/components/pr-pipeline.d.ts.map +1 -1
- package/dist/components/pr-pipeline.js +14 -1
- package/dist/components/product-pipeline.d.ts +16 -1
- package/dist/components/product-pipeline.d.ts.map +1 -1
- package/dist/components/product-pipeline.js +17 -2
- package/dist/components/product-plan.d.ts +257 -4
- package/dist/components/product-plan.d.ts.map +1 -1
- package/dist/components/product-plan.js +241 -11
- package/dist/config.d.ts +10 -6
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +11 -6
- package/dist/control/consumers.d.ts +6 -0
- package/dist/control/consumers.d.ts.map +1 -1
- package/dist/control/consumers.js +6 -0
- package/dist/dead-guards.d.ts.map +1 -1
- package/dist/dead-guards.js +9 -6
- package/dist/discovery/files.d.ts +1 -1
- package/dist/discovery/files.js +1 -1
- package/dist/drift-issue.d.ts +20 -3
- package/dist/drift-issue.d.ts.map +1 -1
- package/dist/drift-issue.js +16 -0
- package/dist/drift-record.d.ts +45 -5
- package/dist/drift-record.d.ts.map +1 -1
- package/dist/drift-record.js +27 -6
- package/dist/floor-table.d.ts +6 -6
- package/dist/floor-table.d.ts.map +1 -1
- package/dist/floor-table.data.d.ts.map +1 -1
- package/dist/floor-table.data.js +58 -21
- package/dist/floor-table.js +10 -7
- package/dist/forge-check/contract.d.ts +8 -0
- package/dist/forge-check/contract.d.ts.map +1 -1
- package/dist/forge-check/contract.js +4 -1
- package/dist/forge-check/index.d.ts +2 -0
- package/dist/forge-check/index.d.ts.map +1 -1
- package/dist/forge-check/index.js +2 -0
- package/dist/forge-flags.d.ts +10 -0
- package/dist/forge-flags.d.ts.map +1 -1
- package/dist/forge-flags.js +31 -0
- package/dist/forge-remote.d.ts +47 -0
- package/dist/forge-remote.d.ts.map +1 -0
- package/dist/forge-remote.js +69 -0
- package/dist/generated-check.d.ts +74 -0
- package/dist/generated-check.d.ts.map +1 -0
- package/dist/generated-check.js +216 -0
- package/dist/lexicon.d.ts +16 -0
- package/dist/lexicon.d.ts.map +1 -1
- 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 +49 -3
- package/dist/lifecycle/release-images.d.ts +22 -1
- package/dist/lifecycle/release-images.d.ts.map +1 -1
- package/dist/lifecycle/release-images.js +27 -3
- package/dist/lifecycle/release-ledger.d.ts +4 -3
- package/dist/lifecycle/release-ledger.d.ts.map +1 -1
- package/dist/lifecycle/release-ledger.js +3 -2
- package/dist/local-chant.d.ts +57 -3
- package/dist/local-chant.d.ts.map +1 -1
- package/dist/local-chant.js +122 -13
- package/dist/op/op-waves-evidence-jobs.d.ts +92 -0
- package/dist/op/op-waves-evidence-jobs.d.ts.map +1 -0
- package/dist/op/op-waves-evidence-jobs.js +112 -0
- package/dist/op/op-waves-evidence.d.ts +12 -1
- package/dist/op/op-waves-evidence.d.ts.map +1 -1
- package/dist/op/op-waves-evidence.js +23 -2
- package/dist/op/op-waves-run.d.ts +12 -1
- package/dist/op/op-waves-run.d.ts.map +1 -1
- package/dist/op/op-waves-run.js +54 -3
- package/dist/op/op-waves.d.ts +8 -0
- package/dist/op/op-waves.d.ts.map +1 -1
- package/dist/op/op-waves.js +10 -0
- package/dist/op/schedule-overdue.d.ts +39 -2
- package/dist/op/schedule-overdue.d.ts.map +1 -1
- package/dist/op/schedule-overdue.js +89 -7
- package/dist/pr-forge-reads.d.ts +21 -1
- package/dist/pr-forge-reads.d.ts.map +1 -1
- package/dist/pr-forge-reads.js +19 -1
- package/dist/pr-forge.d.ts +7 -1
- package/dist/pr-forge.d.ts.map +1 -1
- package/dist/pr-forge.js +7 -2
- package/dist/pr-plan-note.d.ts +24 -0
- package/dist/pr-plan-note.d.ts.map +1 -1
- package/dist/pr-plan-note.js +11 -1
- package/dist/pr-plan-record.d.ts +47 -0
- package/dist/pr-plan-record.d.ts.map +1 -0
- package/dist/pr-plan-record.js +89 -0
- package/dist/pr-post.d.ts +40 -0
- package/dist/pr-post.d.ts.map +1 -1
- package/dist/pr-post.js +79 -6
- package/dist/pr-requires.d.ts +8 -0
- package/dist/pr-requires.d.ts.map +1 -1
- package/dist/pr-requires.js +3 -1
- package/dist/ref-gate-init.d.ts +76 -0
- package/dist/ref-gate-init.d.ts.map +1 -0
- package/dist/ref-gate-init.js +144 -0
- package/dist/ref-gate-rules.d.ts +19 -0
- package/dist/ref-gate-rules.d.ts.map +1 -0
- package/dist/ref-gate-rules.js +17 -0
- package/dist/release-floors.d.ts +64 -16
- package/dist/release-floors.d.ts.map +1 -1
- package/dist/release-floors.js +65 -17
- package/dist/state-migration.d.ts +24 -1
- package/dist/state-migration.d.ts.map +1 -1
- package/dist/state-migration.js +39 -3
- package/dist/state-unlock.d.ts +32 -1
- package/dist/state-unlock.d.ts.map +1 -1
- package/dist/state-unlock.js +31 -0
- package/dist/wave-runtime.mjs +292 -285
- package/dist/workspace/ci-green-cli.d.ts.map +1 -1
- package/dist/workspace/ci-green-cli.js +13 -7
- package/dist/workspace/drift-cli.d.ts +1 -1
- package/dist/workspace/drift-cli.d.ts.map +1 -1
- package/dist/workspace/drift-cli.js +5 -12
- package/dist/workspace/forge-work.d.ts +3 -2
- package/dist/workspace/forge-work.d.ts.map +1 -1
- package/dist/workspace/forge-work.js +7 -17
- package/dist/workspace/lexicon-outputs.js +2 -2
- package/dist/workspace/release-workflow.d.ts +26 -3
- package/dist/workspace/release-workflow.d.ts.map +1 -1
- package/dist/workspace/release-workflow.js +48 -6
- package/dist/workspace/trust/check-evidence-report.d.ts +18 -0
- package/dist/workspace/trust/check-evidence-report.d.ts.map +1 -1
- package/dist/workspace/trust/check-evidence-report.js +13 -0
- package/dist/workspace/trust/check-evidence.d.ts +13 -1
- package/dist/workspace/trust/check-evidence.d.ts.map +1 -1
- package/dist/workspace/trust/check-evidence.js +8 -3
- package/dist/workspace/trust/evidence.d.ts +5 -0
- package/dist/workspace/trust/evidence.d.ts.map +1 -1
- package/dist/workspace/trust/evidence.js +5 -0
- package/dist/workspace/trust/open-evidence.d.ts +67 -0
- package/dist/workspace/trust/open-evidence.d.ts.map +1 -0
- package/dist/workspace/trust/open-evidence.js +92 -0
- package/dist/workspace/trust/ref-gate-cli.d.ts.map +1 -1
- package/dist/workspace/trust/ref-gate-cli.js +65 -3
- package/dist/workspace/trust/ref-gate.d.ts +61 -2
- package/dist/workspace/trust/ref-gate.d.ts.map +1 -1
- package/dist/workspace/trust/ref-gate.js +72 -7
- package/package.json +1 -1
- package/src/action-pins.ts +2 -0
- package/src/actor.test.ts +12 -0
- package/src/actor.ts +19 -0
- package/src/bundle-check.test.ts +22 -0
- package/src/bundle-check.ts +33 -5
- package/src/chant-install.test.ts +109 -1
- package/src/chant-install.ts +243 -2
- package/src/ci-commit.test.ts +51 -5
- package/src/ci-commit.ts +1 -2
- package/src/ci-control.ts +1 -0
- package/src/ci-evidence-claims.ts +102 -0
- package/src/ci-evidence-from-job.test.ts +147 -0
- package/src/ci-evidence-from-job.ts +195 -0
- package/src/ci-evidence-jobs.ts +41 -4
- package/src/ci-handover.ts +16 -3
- package/src/ci-identity.test.ts +3 -3
- package/src/ci-identity.ts +51 -11
- package/src/ci-image.test.ts +60 -10
- package/src/ci-image.ts +78 -19
- package/src/ci-jobs.ts +7 -2
- package/src/ci-login.test.ts +131 -3
- package/src/ci-login.token-drop.test.ts +204 -0
- package/src/ci-login.ts +137 -39
- package/src/ci-product-words.ts +30 -0
- package/src/ci-product.ts +26 -1
- package/src/cli/commands/__fixtures__/source-dir-root/migrations/0001-init/backfill.ts +3 -0
- package/src/cli/handlers/approve-confirm.test.ts +35 -0
- package/src/cli/handlers/approve-confirm.ts +44 -0
- package/src/cli/handlers/build-source-dir.test.ts +4 -0
- package/src/cli/handlers/build.ts +20 -2
- package/src/cli/handlers/ci-check.ts +19 -1
- package/src/cli/handlers/ci-evidence-open.test.ts +164 -0
- package/src/cli/handlers/ci-evidence.ts +210 -16
- package/src/cli/handlers/ci-forge-check.ts +22 -9
- package/src/cli/handlers/generated-check.ts +39 -0
- package/src/cli/handlers/lifecycle.test.ts +20 -0
- package/src/cli/handlers/lifecycle.ts +4 -0
- package/src/cli/handlers/local-chant.ts +39 -1
- package/src/cli/handlers/pr.ts +43 -5
- package/src/cli/handlers/run-wave.ts +7 -0
- package/src/cli/main.ts +38 -4
- package/src/cli/parse-args.ts +46 -0
- package/src/cli/registry.ts +24 -0
- package/src/components/check-hooks.ts +22 -0
- package/src/components/check-report-summary.test.ts +78 -0
- package/src/components/pr-checks.ts +159 -0
- package/src/components/pr-pipeline.ts +41 -1
- package/src/components/product-pipeline.ts +25 -3
- package/src/components/product-plan.test.ts +102 -1
- package/src/components/product-plan.ts +416 -15
- package/src/config.ts +13 -8
- package/src/control/consumers.ts +7 -0
- package/src/dead-guards.test.ts +27 -5
- package/src/dead-guards.ts +9 -6
- package/src/discovery/files.ts +1 -1
- package/src/drift-issue.test.ts +29 -1
- package/src/drift-issue.ts +28 -3
- package/src/drift-record.test.ts +25 -0
- package/src/drift-record.ts +55 -6
- package/src/floor-table.data.ts +58 -21
- package/src/floor-table.ts +12 -9
- package/src/forge-check/contract.ts +11 -1
- package/src/forge-check/forge-check.test.ts +8 -0
- package/src/forge-check/index.ts +2 -0
- package/src/forge-flags.test.ts +25 -1
- package/src/forge-flags.ts +34 -0
- package/src/forge-remote.test.ts +37 -0
- package/src/forge-remote.ts +79 -0
- package/src/generated-check.e2e.test.ts +98 -0
- package/src/generated-check.test.ts +104 -0
- package/src/generated-check.ts +269 -0
- package/src/lexicon.ts +17 -0
- package/src/lifecycle/publish-npm.ts +56 -3
- package/src/lifecycle/release-catalog.test.ts +2 -2
- package/src/lifecycle/release-images-dockerfile.test.ts +20 -0
- package/src/lifecycle/release-images.test.ts +44 -0
- package/src/lifecycle/release-images.ts +39 -3
- package/src/lifecycle/release-ledger.ts +4 -3
- package/src/local-chant.e2e.test.ts +81 -6
- package/src/local-chant.test.ts +22 -3
- package/src/local-chant.ts +130 -13
- package/src/op/op-waves-evidence-jobs.ts +134 -0
- package/src/op/op-waves-evidence.test.ts +36 -3
- package/src/op/op-waves-evidence.ts +23 -3
- package/src/op/op-waves-run.ts +57 -2
- package/src/op/op-waves.ts +11 -0
- package/src/op/schedule-overdue.test.ts +83 -1
- package/src/op/schedule-overdue.ts +107 -9
- package/src/pr-forge-reads-compare.test.ts +2 -1
- package/src/pr-forge-reads.ts +37 -1
- package/src/pr-forge.ts +14 -3
- package/src/pr-plan-note.ts +32 -3
- package/src/pr-plan-record.test.ts +75 -0
- package/src/pr-plan-record.ts +101 -0
- package/src/pr-post-plan-note.test.ts +60 -0
- package/src/pr-post.test.ts +30 -0
- package/src/pr-post.ts +110 -6
- package/src/pr-requires.test.ts +20 -0
- package/src/pr-requires.ts +10 -1
- package/src/ref-gate-init.ts +189 -0
- package/src/ref-gate-rules.ts +26 -0
- package/src/release-floors.ts +86 -17
- package/src/state-migration-record.test.ts +90 -0
- package/src/state-migration.test.ts +1 -1
- package/src/state-migration.ts +52 -3
- package/src/state-unlock.test.ts +33 -1
- package/src/state-unlock.ts +49 -1
- package/src/workspace/ci-green-cli.ts +12 -6
- package/src/workspace/drift-cli.ts +6 -18
- package/src/workspace/forge-work.ts +7 -14
- package/src/workspace/lexicon-outputs.ts +2 -2
- package/src/workspace/release-workflow.test.ts +70 -0
- package/src/workspace/release-workflow.ts +56 -8
- package/src/workspace/trust/check-evidence-report.ts +24 -0
- package/src/workspace/trust/check-evidence.ts +17 -5
- package/src/workspace/trust/evidence.ts +5 -0
- package/src/workspace/trust/open-evidence.ts +129 -0
- package/src/workspace/trust/ref-gate-cli.ts +59 -3
- package/src/workspace/trust/ref-gate-pieces.test.ts +175 -0
- package/src/workspace/trust/ref-gate.e2e.test.ts +17 -0
- package/src/workspace/trust/ref-gate.ts +87 -6
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `chant ci evidence sign --open` (#4914): the GitLab signing job, in a
|
|
3
|
+
* pipeline of the target branch, signs the checks of every open merge
|
|
4
|
+
* request into the branch from its newest pipeline's job.
|
|
5
|
+
*
|
|
6
|
+
* A merge request pipeline is built from the merge request's own files, so
|
|
7
|
+
* a job there that holds the runner key (or asks for a signing token) runs
|
|
8
|
+
* whatever the change wrote. The plan notes pipeline (#4328) is the target
|
|
9
|
+
* branch's: its jobs come from the branch's files, and its protected
|
|
10
|
+
* variables reach it. So the signing moves there, as the note posting did:
|
|
11
|
+
* for each open merge request GitLab names the newest pipeline's `job`
|
|
12
|
+
* (`plan`, `<target>-plan`, `check`), the report is read from that job's
|
|
13
|
+
* artifacts as data, the head is fetched, and the checks are signed for
|
|
14
|
+
* the head as `sign --from-report` signs them. What was signed and why the
|
|
15
|
+
* rest were not is kept as `<out>/<iid>/evidence.json`, with the merge
|
|
16
|
+
* request and the head, which the note job reads (`pr-post --open
|
|
17
|
+
* --evidence-dir`, ../../pr-post.ts) for the note's "Checks:" line.
|
|
18
|
+
*
|
|
19
|
+
* A job that has not settled is left for the next pipeline. A merge request
|
|
20
|
+
* whose report does not read, whose head does not fetch, or that the forge
|
|
21
|
+
* refuses is a warning, and the others go on.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
25
|
+
import { join } from "node:path";
|
|
26
|
+
import type { PrForge } from "../../pr-forge";
|
|
27
|
+
import { CHECK_EVIDENCE_FILE, type CheckEvidenceNote, type OpenEvidenceFile } from "./check-evidence-report";
|
|
28
|
+
|
|
29
|
+
/** The report files a job's artifact is read for, in order: a plan report, a product's plan note hand-off, then a check job's report. */
|
|
30
|
+
export const OPEN_PLAN_FILES = ["pr-plan.json", "pr-plan-note.json"] as const;
|
|
31
|
+
export const OPEN_CHECK_FILE = "check-hooks.json";
|
|
32
|
+
|
|
33
|
+
/** What {@link signOpenReports} signs for. */
|
|
34
|
+
export interface OpenEvidenceInput {
|
|
35
|
+
/**
|
|
36
|
+
* A GitLab client with {@link PrForge.mergeRequestJobs} and
|
|
37
|
+
* {@link PrForge.jobArtifact}, and with `noForks` {@link PrForge.openPullRequests},
|
|
38
|
+
* which says which merge requests come from a fork. The fork test is read
|
|
39
|
+
* here, not in `mergeRequestJobs`, to keep it out of the pull request loop's
|
|
40
|
+
* bundle (its ceiling, pr-loop-bundle.test.ts).
|
|
41
|
+
*/
|
|
42
|
+
forge: Pick<PrForge, "kind" | "mergeRequestJobs" | "jobArtifact" | "openPullRequests">;
|
|
43
|
+
/** The branch the merge requests merge into: the pipeline's own. */
|
|
44
|
+
branch: string;
|
|
45
|
+
/** The job whose report is signed: `plan`, `<target>-plan` or `check`. */
|
|
46
|
+
job: string;
|
|
47
|
+
/** The report directory from the repository root, as the job kept it as its artifact. */
|
|
48
|
+
artifactDir: string;
|
|
49
|
+
/** Where each merge request's report and `evidence.json` go: `<out>/<iid>/`. */
|
|
50
|
+
out: string;
|
|
51
|
+
/** Sign for no merge request from a fork, as a product plan with `forks: "skip"` plans none. */
|
|
52
|
+
noForks?: boolean;
|
|
53
|
+
/** Fetch merge request `pr`'s head into this checkout; true once the commit is here. */
|
|
54
|
+
fetchHead: (pr: number, head: string) => boolean;
|
|
55
|
+
/** Sign the checks of the report in `dir` for `head`, given the job's result, as `sign --from-report` does. */
|
|
56
|
+
sign: (dir: string, head: string, result: "success" | "failure" | "cancelled") => Promise<CheckEvidenceNote>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** What {@link signOpenReports} did for one merge request. */
|
|
60
|
+
export interface OpenEvidence {
|
|
61
|
+
pr: number;
|
|
62
|
+
head: string;
|
|
63
|
+
/** What was signed and what was not, as kept in `evidence.json`. */
|
|
64
|
+
note?: CheckEvidenceNote;
|
|
65
|
+
/** Why nothing was signed: the job has not settled, left no report, or its head did not fetch. */
|
|
66
|
+
skipped?: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Sign every open merge request's settled `job` into `branch` (#4914). */
|
|
70
|
+
export async function signOpenReports(input: OpenEvidenceInput): Promise<{ done: OpenEvidence[]; warnings: string[] }> {
|
|
71
|
+
const { forge } = input;
|
|
72
|
+
if (!forge.mergeRequestJobs || !forge.jobArtifact) {
|
|
73
|
+
throw new Error(`ci evidence sign --open reads the jobs of open merge requests, which ${forge.kind} cannot say; it is for GitLab`);
|
|
74
|
+
}
|
|
75
|
+
const done: OpenEvidence[] = [];
|
|
76
|
+
const warnings: string[] = [];
|
|
77
|
+
const at = input.artifactDir.replace(/\/+$/, "");
|
|
78
|
+
let forks = new Set<number>();
|
|
79
|
+
if (input.noForks) {
|
|
80
|
+
if (!forge.openPullRequests) throw new Error(`ci evidence sign --open --no-forks reads which merge requests come from a fork, which ${forge.kind} cannot say`);
|
|
81
|
+
forks = new Set((await forge.openPullRequests(input.branch)).filter((p) => p.fork).map((p) => p.number));
|
|
82
|
+
}
|
|
83
|
+
for (const job of await forge.mergeRequestJobs(input.branch, input.job)) {
|
|
84
|
+
if (forks.has(job.pr)) {
|
|
85
|
+
done.push({ pr: job.pr, head: job.head, skipped: "the merge request is from a fork" });
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const result = job.status === "success" ? "success" : job.status === "failed" ? "failure" : job.status === "canceled" ? "cancelled" : undefined;
|
|
89
|
+
if (!result) {
|
|
90
|
+
done.push({ pr: job.pr, head: job.head, skipped: `the ${input.job} job is ${job.status}` });
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
const dir = join(input.out, String(job.pr));
|
|
95
|
+
mkdirSync(dir, { recursive: true });
|
|
96
|
+
// A plan report wins over a check report, as `sign --from-report` reads a directory.
|
|
97
|
+
let found = false;
|
|
98
|
+
for (const file of OPEN_PLAN_FILES) {
|
|
99
|
+
const text = await forge.jobArtifact(job.id, `${at}/${file}`);
|
|
100
|
+
if (text === undefined) continue;
|
|
101
|
+
writeFileSync(join(dir, file), text);
|
|
102
|
+
found = true;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
if (!found) {
|
|
106
|
+
const text = await forge.jobArtifact(job.id, `${at}/${OPEN_CHECK_FILE}`);
|
|
107
|
+
if (text !== undefined) {
|
|
108
|
+
writeFileSync(join(dir, OPEN_CHECK_FILE), text);
|
|
109
|
+
found = true;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (!found) {
|
|
113
|
+
done.push({ pr: job.pr, head: job.head, skipped: `the ${input.job} job left no report under ${at}` });
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (!input.fetchHead(job.pr, job.head)) {
|
|
117
|
+
done.push({ pr: job.pr, head: job.head, skipped: `the head ${job.head.slice(0, 12)} did not fetch` });
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
const note = await input.sign(dir, job.head, result);
|
|
121
|
+
const kept: OpenEvidenceFile = { pr: job.pr, head: job.head, ...note };
|
|
122
|
+
writeFileSync(join(dir, CHECK_EVIDENCE_FILE), JSON.stringify(kept, null, 2) + "\n");
|
|
123
|
+
done.push({ pr: job.pr, head: job.head, note });
|
|
124
|
+
} catch (err) {
|
|
125
|
+
warnings.push(`!${job.pr}: ${err instanceof Error ? err.message : String(err)}`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return { done, warnings };
|
|
129
|
+
}
|
|
@@ -48,6 +48,7 @@ const OVERRIDE_REQUEST_HOURS = 72;
|
|
|
48
48
|
const USAGE =
|
|
49
49
|
"chant workspace ref-gate check --ref <ref> [--at <rev>] [--base <rev>] [--dry-run] [--ledger <file>] [--json]\n" +
|
|
50
50
|
" chant workspace ref-gate status --ref <ref> [--at <rev>] [--base <rev>] [--dry-run] [--ledger <file>] [--json]\n" +
|
|
51
|
+
" chant workspace ref-gate status --open --ref refs/heads/<branch> [--base <rev>] [--dry-run] [--ledger <file>] [--json]\n" +
|
|
51
52
|
" chant workspace ref-gate pre-receive|pre-push [--ledger <file>] [--dry-run] [--json] (git hook input on stdin)\n" +
|
|
52
53
|
" chant workspace ref-gate attach --envelope <file> [--at <rev>]\n" +
|
|
53
54
|
" chant workspace ref-gate install --hook pre-receive|pre-push [--git-dir <dir>] [--chant <command>]";
|
|
@@ -82,7 +83,7 @@ function repoHere(): { repo: string; bare: boolean } | null {
|
|
|
82
83
|
return { repo: top, bare: false };
|
|
83
84
|
}
|
|
84
85
|
|
|
85
|
-
function print(json: boolean | undefined, decisions: RefDecision[], ledger: string | undefined, stream: "out" | "err"): void {
|
|
86
|
+
function print(json: boolean | undefined, decisions: RefDecision[], ledger: string | undefined, stream: "out" | "err", site?: RefGateSite): void {
|
|
86
87
|
const ok = decisions.every((d) => d.decision === "admit");
|
|
87
88
|
if (json) {
|
|
88
89
|
console.log(JSON.stringify({ ...head(), ok, ...(ledger ? { ledger } : {}), decisions }, null, 2));
|
|
@@ -90,7 +91,7 @@ function print(json: boolean | undefined, decisions: RefDecision[], ledger: stri
|
|
|
90
91
|
}
|
|
91
92
|
for (const d of decisions) {
|
|
92
93
|
if (d.status === null && d.checks.length === 0 && !d.error) continue;
|
|
93
|
-
const text = describeDecision(d).join("\n");
|
|
94
|
+
const text = describeDecision(d, site ? { site } : {}).join("\n");
|
|
94
95
|
if (stream === "err" || d.decision === "refuse") console.error(text);
|
|
95
96
|
else console.log(text);
|
|
96
97
|
}
|
|
@@ -161,7 +162,7 @@ export async function runWorkspaceRefGate(ctx: CommandContext): Promise<number>
|
|
|
161
162
|
try {
|
|
162
163
|
if (sub === "pre-receive") {
|
|
163
164
|
const r = runPreReceive(here.repo, stdin, { ...(args.ledger ? { ledger: args.ledger } : {}), now });
|
|
164
|
-
print(args.json, r.decisions, r.ledger, "err");
|
|
165
|
+
print(args.json, r.decisions, r.ledger, "err", "pre-receive");
|
|
165
166
|
return r.ok ? 0 : 1;
|
|
166
167
|
}
|
|
167
168
|
const r = runPrePush(here.repo, stdin, { ...(args.ledger ? { ledger: args.ledger } : {}), now, base: (repo) => resolveBase(repo).commit });
|
|
@@ -176,6 +177,10 @@ export async function runWorkspaceRefGate(ctx: CommandContext): Promise<number>
|
|
|
176
177
|
|
|
177
178
|
// check and status: one update, from a checkout or a CI job.
|
|
178
179
|
if (!args.ref || !args.ref.startsWith("refs/")) return usage("--ref <ref> is required, as a full ref name such as refs/heads/main");
|
|
180
|
+
if (args.open) {
|
|
181
|
+
if (sub !== "status") return usage("--open sets the status of every open pull request, so it goes with status");
|
|
182
|
+
return statusOpen(here.repo, args.ref, args.base, args.ledger, args.dryRun === true, args.json, now);
|
|
183
|
+
}
|
|
179
184
|
const at = commitOf(here.repo, args.at ?? "HEAD");
|
|
180
185
|
if (!at) return fail(args.json, "revision-unknown", `--at ${args.at ?? "HEAD"} names no commit`);
|
|
181
186
|
const resolved = resolveBase(here.repo, args.base);
|
|
@@ -203,6 +208,57 @@ export async function runWorkspaceRefGate(ctx: CommandContext): Promise<number>
|
|
|
203
208
|
return d.decision === "admit" ? 0 : 1;
|
|
204
209
|
}
|
|
205
210
|
|
|
211
|
+
/**
|
|
212
|
+
* `status --open --ref refs/heads/<branch>` (#4935): the status of every open
|
|
213
|
+
* pull request into the branch, from a pipeline of the branch. GitLab sets a
|
|
214
|
+
* commit status only with the protected token a target-branch pipeline holds
|
|
215
|
+
* (#4328), so its generated `ref-gate` job runs here, beside the plan notes.
|
|
216
|
+
* Each head is fetched and decided as `status --at <head>` decides it, against
|
|
217
|
+
* the policy at `base` (default this checkout's `HEAD`, the branch's commit).
|
|
218
|
+
* A head that does not fetch, or a forge that refuses one status, is said
|
|
219
|
+
* and the others go on. Exits 0 once each is set, whatever it decided: each
|
|
220
|
+
* pull request's status says it.
|
|
221
|
+
*/
|
|
222
|
+
async function statusOpen(repo: string, ref: string, baseRev: string | undefined, ledgerFlag: string | undefined, dryRun: boolean, json: boolean | undefined, now: string): Promise<number> {
|
|
223
|
+
if (!ref.startsWith("refs/heads/")) return usage("--open names the branch the pull requests merge into: --ref refs/heads/<branch>");
|
|
224
|
+
const base = commitOf(repo, baseRev ?? "HEAD");
|
|
225
|
+
if (!base) return fail(json, "revision-unknown", `--base ${baseRev ?? "HEAD"} names no commit`);
|
|
226
|
+
const { detectForge, forgeFromEnv } = await import("../../pr-forge");
|
|
227
|
+
const kind = detectForge();
|
|
228
|
+
if (!kind && !dryRun) {
|
|
229
|
+
console.error(formatError({ message: "no forge detected, so no status was set", hint: "run it in a GitHub, GitLab or Forgejo job, or pass --dry-run" }));
|
|
230
|
+
return 1;
|
|
231
|
+
}
|
|
232
|
+
const forge = kind ? forgeFromEnv(kind) : undefined;
|
|
233
|
+
if (!forge?.openPullRequests) return fail(json, "ref-update-invalid", `--open lists the open pull requests, which ${kind ?? "no"} forge client here can`);
|
|
234
|
+
const branch = ref.slice("refs/heads/".length);
|
|
235
|
+
const ledger = ledgerFlag ?? refGateLedgerPath(repo);
|
|
236
|
+
const decisions: RefDecision[] = [];
|
|
237
|
+
for (const pr of await forge.openPullRequests(branch)) {
|
|
238
|
+
const fetched = spawnSync("git", ["fetch", "--quiet", "--no-tags", "origin", pr.headSha], { cwd: repo, encoding: "utf-8" });
|
|
239
|
+
if (!commitOf(repo, pr.headSha)) {
|
|
240
|
+
console.error(`chant ref gate: #${pr.number}'s head ${pr.headSha.slice(0, 8)} did not fetch${fetched.stderr ? ` (${fetched.stderr.trim().split("\n")[0]})` : ""}, so its status is left as it is`);
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
const spent = spentOverrides(readRefGateLedger(ledger));
|
|
244
|
+
const d = decideRefUpdate({ repo, update: { ref, old: base, new: pr.headSha }, base, spent, now });
|
|
245
|
+
decisions.push(d);
|
|
246
|
+
if (dryRun) {
|
|
247
|
+
if (d.status !== null) console.error(`chant ref gate: would set ${d.status} to ${d.decision === "admit" ? "success" : "failure"} on #${pr.number} at ${pr.headSha.slice(0, 8)}`);
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
appendRefGateLedger(ledger, [{ ...d, version: 1, kind: "ref-update", site: "status", timestamp: now }]);
|
|
251
|
+
try {
|
|
252
|
+
await postRefGateStatus(forge, d);
|
|
253
|
+
} catch (err) {
|
|
254
|
+
console.error(`chant ref gate: could not set the status on #${pr.number}: ${err instanceof Error ? err.message : String(err)}`);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
print(json, decisions, dryRun ? undefined : ledger, "err");
|
|
258
|
+
if (decisions.length === 0) console.error(`chant ref gate: no open pull request into ${branch} to set a status on`);
|
|
259
|
+
return 0;
|
|
260
|
+
}
|
|
261
|
+
|
|
206
262
|
/** The commit status a forge requires for `d`'s ref: `success` when it is admitted, `failure` naming the missing checks when not. */
|
|
207
263
|
export async function postRefGateStatus(forge: { setStatus(sha: string, status: { context: string; state: "success" | "failure"; description: string }): Promise<void> }, d: RefDecision): Promise<void> {
|
|
208
264
|
if (d.status === null || d.new === null) return;
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #4913: the ref gate's pieces a product uses without code of its own. The
|
|
3
|
+
* rules read at base (`@intentius/chant/ref-gate-rules`), the status a forge
|
|
4
|
+
* check requires, the init pieces that write `.chant/trust.json`, the hook
|
|
5
|
+
* and the runner key hint (`@intentius/chant/ref-gate-init`), and a
|
|
6
|
+
* pre-receive refusal that prints the command recording the request for an
|
|
7
|
+
* override from the checkout.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { existsSync, readFileSync, realpathSync, statSync, writeFileSync } from "node:fs";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
import { describe, expect, test } from "vitest";
|
|
13
|
+
import { refGateInitPlan, RefGateInitError, writeRefGateInit } from "../../ref-gate-init";
|
|
14
|
+
import { REF_GATE_STATUS, refGateRulesAt, refGateStatusFor } from "../../ref-gate-rules";
|
|
15
|
+
import { decideRefUpdate, describeDecision, requestCommand } from "./ref-gate";
|
|
16
|
+
import { REF_GATE_HOOK_MARK } from "./ref-gate-cli";
|
|
17
|
+
import { GateRepos, RUNNER, runnerKey, trustJson } from "./ref-gate-fixture";
|
|
18
|
+
|
|
19
|
+
const CHECKS = [
|
|
20
|
+
{ check: "plan", command: "chant ci run plan" },
|
|
21
|
+
{ check: "lint", action: "warn" as const, command: "npm run lint" },
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
describe("the ref gate's rules as a library (#4913)", () => {
|
|
25
|
+
test("rules for a ref come from the policy at base, and a change that drops them does not count", () => {
|
|
26
|
+
const r = new GateRepos("4913-rules");
|
|
27
|
+
const ci = runnerKey();
|
|
28
|
+
r.write(".chant/trust.json", trustJson(ci, CHECKS, {}));
|
|
29
|
+
const base = r.commit("policy");
|
|
30
|
+
r.write(".chant/trust.json", JSON.stringify({ schema: 1 }));
|
|
31
|
+
r.commit("drop the rules");
|
|
32
|
+
|
|
33
|
+
const atBase = refGateRulesAt(r.work, { ref: "refs/heads/main", base });
|
|
34
|
+
expect(atBase).toEqual({
|
|
35
|
+
ref: "refs/heads/main",
|
|
36
|
+
base,
|
|
37
|
+
matched: true,
|
|
38
|
+
status: REF_GATE_STATUS,
|
|
39
|
+
checks: [
|
|
40
|
+
{ check: "plan", action: "block", command: "chant ci run plan" },
|
|
41
|
+
{ check: "lint", action: "warn", command: "npm run lint" },
|
|
42
|
+
],
|
|
43
|
+
problems: [],
|
|
44
|
+
});
|
|
45
|
+
expect(refGateRulesAt(r.work, { ref: "refs/heads/release/2", base }).matched).toBe(true);
|
|
46
|
+
expect(refGateRulesAt(r.work, { ref: "refs/heads/feature", base })).toMatchObject({ matched: false, status: null, checks: [], problems: [] });
|
|
47
|
+
// At the change itself the rules are gone, which is why a product reads them at base.
|
|
48
|
+
expect(refGateRulesAt(r.work, { ref: "refs/heads/main", base: "HEAD" }).matched).toBe(false);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("a base that names no commit, or a policy that can't be read, is a problem", () => {
|
|
52
|
+
const r = new GateRepos("4913-problems");
|
|
53
|
+
r.write(".chant/trust.json", "{ not json");
|
|
54
|
+
const base = r.commit("broken policy");
|
|
55
|
+
expect(refGateRulesAt(r.work, { ref: "refs/heads/main", base: "nope" }).problems[0]).toContain("--base nope names no commit");
|
|
56
|
+
const broken = refGateRulesAt(r.work, { ref: "refs/heads/main", base });
|
|
57
|
+
expect(broken.matched).toBe(false);
|
|
58
|
+
expect(broken.problems[0]).toContain(".chant/trust.json is not JSON");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("the status a forge check requires: the policy's own name, at origin/<default> when the checkout has it", () => {
|
|
62
|
+
const r = new GateRepos("4913-status");
|
|
63
|
+
const ci = runnerKey();
|
|
64
|
+
r.write(".chant/trust.json", JSON.stringify({ schema: 1, runners: [{ principal: RUNNER, class: "runner", key: ci.pub }], refs: [{ ref: "refs/heads/main", checks: CHECKS, status: "acme/gate" }] }));
|
|
65
|
+
r.commit("policy");
|
|
66
|
+
r.push("main");
|
|
67
|
+
r.git(["fetch", "-q", "origin"]);
|
|
68
|
+
// A local change that drops the rule does not move what the forge's default branch requires.
|
|
69
|
+
r.write(".chant/trust.json", JSON.stringify({ schema: 1 }));
|
|
70
|
+
r.commit("drop locally");
|
|
71
|
+
expect(refGateStatusFor(r.work, "main")).toEqual({ status: "acme/gate" });
|
|
72
|
+
expect(refGateStatusFor(r.work, "trunk").note).toContain("matches refs/heads/trunk, so the ref-gate job sets no status there");
|
|
73
|
+
const bare = new GateRepos("4913-status-head");
|
|
74
|
+
bare.write(".chant/trust.json", "[]");
|
|
75
|
+
bare.commit("not a policy");
|
|
76
|
+
expect(refGateStatusFor(bare.work, "main").problem).toContain("the trust policy can't be read");
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe("the ref gate's init pieces (#4913)", () => {
|
|
81
|
+
test("a checkout with no trust policy gets one with the rule, the pre-push hook and the runner key hint", () => {
|
|
82
|
+
const r = new GateRepos("4913-init-new");
|
|
83
|
+
r.write("README.md", "hi\n");
|
|
84
|
+
r.commit("start");
|
|
85
|
+
const plan = refGateInitPlan(r.work, { checks: CHECKS, chant: "npx chant", runner: "ci:acme/infra", runnerKeySecret: "CHANT_RUNNER_KEY" });
|
|
86
|
+
const trust = plan.files.find((f) => f.path.endsWith(".chant/trust.json"))!;
|
|
87
|
+
expect(trust.status).toBe("created");
|
|
88
|
+
expect(JSON.parse(trust.content)).toEqual({ schema: 1, refs: [{ ref: "refs/heads/main", checks: [{ check: "plan", command: "chant ci run plan" }, { check: "lint", action: "warn", command: "npm run lint" }] }] });
|
|
89
|
+
const hook = plan.files.find((f) => f.path.endsWith("pre-push"))!;
|
|
90
|
+
expect(hook).toMatchObject({ status: "created", mode: 0o755, local: true });
|
|
91
|
+
expect(hook.path.startsWith(join(realpathSync(r.work), ".git"))).toBe(true);
|
|
92
|
+
expect(hook.content).toContain(REF_GATE_HOOK_MARK);
|
|
93
|
+
expect(hook.content).toContain("exec npx chant workspace ref-gate pre-push");
|
|
94
|
+
const hint = plan.notes.find((n) => n.includes("no runner key"))!;
|
|
95
|
+
expect(hint).toContain("chant workspace trust runner add ci:acme/infra --key runner.pem");
|
|
96
|
+
expect(hint).toContain("the CHANT_RUNNER_KEY secret");
|
|
97
|
+
expect(plan.notes.some((n) => n.includes("protected write"))).toBe(true);
|
|
98
|
+
|
|
99
|
+
writeRefGateInit(plan);
|
|
100
|
+
expect(statSync(hook.path).mode & 0o777).toBe(0o755);
|
|
101
|
+
const base = r.commit("ref gate");
|
|
102
|
+
// What init wrote is what the gate reads.
|
|
103
|
+
expect(refGateRulesAt(r.work, { ref: "refs/heads/main", base })).toMatchObject({ matched: true, status: REF_GATE_STATUS, checks: [{ check: "plan", action: "block" }, { check: "lint", action: "warn" }] });
|
|
104
|
+
const again = refGateInitPlan(r.work, { checks: CHECKS, chant: "npx chant" });
|
|
105
|
+
expect(again.files.map((f) => f.status)).toEqual(["unchanged", "unchanged"]);
|
|
106
|
+
expect(again.notes.some((n) => n.includes("already has a refs rule for refs/heads/main"))).toBe(true);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("an existing policy keeps its text and gets the rule added; one with a runner gets no hint", () => {
|
|
110
|
+
const r = new GateRepos("4913-init-edit");
|
|
111
|
+
const ci = runnerKey();
|
|
112
|
+
const before = `{\n "schema": 1,\n "runners": [{ "principal": "${RUNNER}", "class": "runner", "key": "${ci.pub}" }]\n}\n`;
|
|
113
|
+
r.write(".chant/trust.json", before);
|
|
114
|
+
r.commit("runner only");
|
|
115
|
+
const plan = refGateInitPlan(r.work, { branch: "trunk", checks: [{ check: "plan", command: "chant ci run plan" }], status: "acme/gate", prePush: false });
|
|
116
|
+
expect(plan.files).toHaveLength(1);
|
|
117
|
+
const trust = plan.files[0]!;
|
|
118
|
+
expect(trust.status).toBe("updated");
|
|
119
|
+
expect(trust.content.startsWith(`{\n "schema": 1,\n "runners": [{ "principal": "${RUNNER}"`)).toBe(true);
|
|
120
|
+
expect(JSON.parse(trust.content).refs).toEqual([{ ref: "refs/heads/trunk", checks: [{ check: "plan", command: "chant ci run plan" }], status: "acme/gate" }]);
|
|
121
|
+
expect(plan.notes.some((n) => n.includes("no runner key"))).toBe(false);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test("a rule that covers the branch, a policy that does not read, and a hook that is not chant's are left as they are", () => {
|
|
125
|
+
const r = new GateRepos("4913-init-leave");
|
|
126
|
+
r.write(".chant/trust.json", JSON.stringify({ schema: 1, refs: [{ ref: "refs/heads/*", checks: [{ check: "lint", command: "npm run lint" }] }] }));
|
|
127
|
+
r.commit("a wide rule");
|
|
128
|
+
const hooks = join(r.work, ".git", "hooks");
|
|
129
|
+
writeFileSync(join(hooks, "pre-push"), "#!/bin/sh\necho mine\n");
|
|
130
|
+
const plan = refGateInitPlan(r.work, { checks: CHECKS });
|
|
131
|
+
expect(plan.files.map((f) => [f.path.endsWith(".chant/trust.json"), f.status])).toEqual([[true, "unchanged"]]);
|
|
132
|
+
expect(plan.notes.some((n) => n.includes("already has a refs rule for refs/heads/main (refs/heads/*)"))).toBe(true);
|
|
133
|
+
expect(plan.notes.some((n) => n.includes("is not chant's ref gate hook"))).toBe(true);
|
|
134
|
+
expect(readFileSync(join(hooks, "pre-push"), "utf-8")).toContain("echo mine");
|
|
135
|
+
|
|
136
|
+
r.write(".chant/trust.json", "{ not json");
|
|
137
|
+
const bad = refGateInitPlan(r.work, { checks: CHECKS, prePush: false });
|
|
138
|
+
expect(bad.files).toEqual([]);
|
|
139
|
+
expect(bad.notes[0]).toContain("does not read as chant's trust policy");
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("options no rule can hold, or a directory outside git, are refused", () => {
|
|
143
|
+
const r = new GateRepos("4913-init-refuse");
|
|
144
|
+
r.commit("start");
|
|
145
|
+
expect(() => refGateInitPlan(r.work, { checks: [] })).toThrow(RefGateInitError);
|
|
146
|
+
expect(() => refGateInitPlan(r.work, { checks: [{ check: "has space", command: "x" }] })).toThrow("is not a check name");
|
|
147
|
+
expect(() => refGateInitPlan(r.work, { branch: "release/*", checks: CHECKS })).toThrow(RefGateInitError);
|
|
148
|
+
expect(() => refGateInitPlan(r.home, { checks: CHECKS })).toThrow("is not in a git checkout");
|
|
149
|
+
expect(existsSync(join(r.work, ".chant/trust.json"))).toBe(false);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe("the request for an override from a push the remote refused (#4913)", () => {
|
|
154
|
+
test("the pre-receive refusal prints the exact command that records it in the checkout, then the override and the push", () => {
|
|
155
|
+
const r = new GateRepos("4913-request");
|
|
156
|
+
const ci = runnerKey();
|
|
157
|
+
r.write(".chant/trust.json", trustJson(ci, CHECKS));
|
|
158
|
+
const base = r.commit("policy");
|
|
159
|
+
r.write("app/a.txt", "change\n");
|
|
160
|
+
const head = r.commit("a change");
|
|
161
|
+
const d = decideRefUpdate({ repo: r.work, update: { ref: "refs/heads/main", old: base, new: head }, base, lifecycle: null });
|
|
162
|
+
expect(d.decision).toBe("refuse");
|
|
163
|
+
expect(requestCommand(d)).toBe(`chant workspace ref-gate check --ref refs/heads/main --at ${head} --base ${base}`);
|
|
164
|
+
|
|
165
|
+
const remote = describeDecision(d, { site: "pre-receive" }).join("\n");
|
|
166
|
+
expect(remote).toContain("record the request for an override in your checkout (this remote can't)");
|
|
167
|
+
expect(remote).toContain(` chant workspace ref-gate check --ref refs/heads/main --at ${head} --base ${base}`);
|
|
168
|
+
expect(remote).toContain(`chant approve policy-override ref-gate-${head.slice(0, 12)}-plan --rule plan`);
|
|
169
|
+
expect(remote).toContain(`git push origin chant/lifecycle ${head}:refs/heads/main`);
|
|
170
|
+
expect(remote).not.toContain("lint --rule");
|
|
171
|
+
|
|
172
|
+
const local = describeDecision(d).join("\n");
|
|
173
|
+
expect(local).toContain(`the pre-push hook records it, or chant workspace ref-gate check --ref refs/heads/main --at ${head} --base ${base}`);
|
|
174
|
+
});
|
|
175
|
+
});
|
|
@@ -60,6 +60,23 @@ describe("the ref gate's hooks", () => {
|
|
|
60
60
|
expect(r.run(r.origin, ["rev-parse", "main"]).trim()).toBe(head);
|
|
61
61
|
}, 60_000);
|
|
62
62
|
|
|
63
|
+
test("a push only the remote refused prints the command that records the request for an override, and it does from the checkout (#4913)", () => {
|
|
64
|
+
const { r, base, head } = setup("e2e-request");
|
|
65
|
+
const installed = chant(r, r.work, ["workspace", "ref-gate", "install", "--hook", "pre-receive", "--git-dir", r.origin, "--chant", CHANT]);
|
|
66
|
+
expect(installed.status, installed.stderr).toBe(0);
|
|
67
|
+
const refused = push(r, `${head}:refs/heads/main`);
|
|
68
|
+
expect(refused.status).not.toBe(0);
|
|
69
|
+
const printed = refused.stderr.split("\n").map((l) => l.replace(/^remote:\s*/, "").trim()).find((l) => l.startsWith("chant workspace ref-gate check "));
|
|
70
|
+
expect(printed).toBe(`chant workspace ref-gate check --ref refs/heads/main --at ${head} --base ${base}`);
|
|
71
|
+
|
|
72
|
+
const recorded = chant(r, r.work, printed!.split(" ").slice(1));
|
|
73
|
+
expect(recorded.status).toBe(1);
|
|
74
|
+
expect(recorded.stderr).toContain("is recorded on chant/lifecycle");
|
|
75
|
+
const ledger = r.git(["show", "chant/lifecycle:_gates/policy-override.jsonl"]);
|
|
76
|
+
expect(ledger).toContain(`"gate":"ref-gate-${head.slice(0, 12)}-plan"`);
|
|
77
|
+
expect(ledger).toContain(`"gate":"ref-gate-${head.slice(0, 12)}-policy"`);
|
|
78
|
+
}, 60_000);
|
|
79
|
+
|
|
63
80
|
test("chant's pre-push hook refuses the same push before the network", () => {
|
|
64
81
|
const { r, ci, base, head } = setup("e2e-push");
|
|
65
82
|
const installed = chant(r, r.work, ["workspace", "ref-gate", "install", "--hook", "pre-push", "--chant", CHANT]);
|
|
@@ -37,7 +37,7 @@ import { appendFileSync, existsSync, mkdirSync, readFileSync } from "node:fs";
|
|
|
37
37
|
import { dirname, isAbsolute, join } from "node:path";
|
|
38
38
|
import type { ReasonCode } from "../reason-codes";
|
|
39
39
|
import { verifyEvidence, type EvidenceErrorCode } from "./evidence";
|
|
40
|
-
import { policyAtBase } from "./provenance";
|
|
40
|
+
import { policyAtBase, resolveBase } from "./provenance";
|
|
41
41
|
import { runnersAt, type RefRule, type TrustPolicy } from "./policy";
|
|
42
42
|
import {
|
|
43
43
|
OVERRIDE_APPLIED,
|
|
@@ -172,6 +172,63 @@ export function rulesFor(policy: Pick<TrustPolicy, "refs">, ref: string): { matc
|
|
|
172
172
|
};
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
+
/** The ref gate's rules for one ref, read at base, as a product reads them (#4913). */
|
|
176
|
+
export interface RefGateRules {
|
|
177
|
+
ref: string;
|
|
178
|
+
/** The commit the policy was read at, or null when there is none. */
|
|
179
|
+
base: string | null;
|
|
180
|
+
/** A rule of the policy at base matches the ref. */
|
|
181
|
+
matched: boolean;
|
|
182
|
+
/** The commit status the ref gate's status job sets for the ref, which the forge should require; null when no rule matches. */
|
|
183
|
+
status: string | null;
|
|
184
|
+
/** The checks an update of the ref needs current evidence for, each once, as {@link rulesFor} merges them. */
|
|
185
|
+
checks: RefRule["checks"];
|
|
186
|
+
overrides?: "ledger" | "sealed";
|
|
187
|
+
/**
|
|
188
|
+
* Why the policy at base cannot be read. The gate then refuses every
|
|
189
|
+
* update of the ref and sets no status, so a product should refuse too.
|
|
190
|
+
*/
|
|
191
|
+
problems: string[];
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* The ref gate's rules for `ref` from the policy at `base` (#4913), the same
|
|
196
|
+
* read {@link decideRefUpdate} makes, for a product that acts on them
|
|
197
|
+
* without deciding an update: an apply that waits for the status the gate
|
|
198
|
+
* sets (`@intentius/chant/ref-gate-rules`), or a forge check that requires
|
|
199
|
+
* it. `base` defaults as `check` and `status` default it: `origin/HEAD`,
|
|
200
|
+
* then `main`, then `master`. A base that names no commit is a problem; no
|
|
201
|
+
* base at all is no policy, so nothing is required.
|
|
202
|
+
*/
|
|
203
|
+
export function refGateRulesAt(repo: string, opts: { ref: string; base?: string }): RefGateRules {
|
|
204
|
+
const resolved = resolveBase(repo, opts.base);
|
|
205
|
+
const none = { ref: opts.ref, base: resolved.commit, matched: false, status: null, checks: [] };
|
|
206
|
+
if (!resolved.commit) return { ...none, problems: opts.base !== undefined && resolved.problem ? [resolved.problem] : [] };
|
|
207
|
+
const policy = policyAtBase(repo, resolved);
|
|
208
|
+
if (policy.problems.length > 0) return { ...none, problems: [...policy.problems] };
|
|
209
|
+
const rules = rulesFor(policy, opts.ref);
|
|
210
|
+
if (!rules.matched) return { ...none, problems: [] };
|
|
211
|
+
return { ref: opts.ref, base: resolved.commit, matched: true, status: rules.status, checks: rules.checks, ...(rules.overrides ? { overrides: rules.overrides } : {}), problems: [] };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* The status a forge's default branch should require for the ref gate
|
|
216
|
+
* (#4913), for a forge check whose pipeline renders the `ref-gate` job: the
|
|
217
|
+
* policy's status for `refs/heads/<defaultBranch>`, read at
|
|
218
|
+
* `origin/<defaultBranch>` when the checkout has it, else at `HEAD`. With
|
|
219
|
+
* no rule for the branch the job sets no status, so none is required and
|
|
220
|
+
* `note` says so; a policy that can't be read is a `problem`.
|
|
221
|
+
*/
|
|
222
|
+
export function refGateStatusFor(repo: string, defaultBranch: string): { status?: string; note?: string; problem?: string } {
|
|
223
|
+
const remote = `refs/remotes/origin/${defaultBranch}`;
|
|
224
|
+
const rules = refGateRulesAt(repo, { ref: `refs/heads/${defaultBranch}`, base: commitOf(repo, remote) ? remote : "HEAD" });
|
|
225
|
+
if (rules.problems.length > 0) return { problem: `the trust policy can't be read, so the ref gate sets no status to require: ${rules.problems.join("; ")}` };
|
|
226
|
+
if (!rules.matched || !rules.status) {
|
|
227
|
+
return { note: `no refs rule in .chant/trust.json${rules.base ? ` at ${rules.base.slice(0, 8)}` : ""} matches refs/heads/${defaultBranch}, so the ref-gate job sets no status there and none is required` };
|
|
228
|
+
}
|
|
229
|
+
return { status: rules.status };
|
|
230
|
+
}
|
|
231
|
+
|
|
175
232
|
// ── evidence notes ───────────────────────────────────────────────────────
|
|
176
233
|
|
|
177
234
|
/** The envelopes in a note's text: one per line, or the whole note as one envelope or an array of them. */
|
|
@@ -510,8 +567,24 @@ export function runPrePush(repo: string, stdin: string, opts: { ledger?: string;
|
|
|
510
567
|
|
|
511
568
|
const short = (s: string | null): string => (s ? s.slice(0, 8) : "(none)");
|
|
512
569
|
|
|
513
|
-
/**
|
|
514
|
-
|
|
570
|
+
/**
|
|
571
|
+
* The command that records the request for an override of `d`'s blocked
|
|
572
|
+
* checks from a checkout (#4913): `check` for the same ref, commit and base
|
|
573
|
+
* the remote decided, so it reaches the same decision and records the
|
|
574
|
+
* request on `chant/lifecycle`, which a pre-receive hook can't move.
|
|
575
|
+
*/
|
|
576
|
+
export function requestCommand(d: Pick<RefDecision, "ref" | "new" | "base">): string {
|
|
577
|
+
return `chant workspace ref-gate check --ref ${d.ref} --at ${d.new ?? "HEAD"}${d.base ? ` --base ${d.base}` : ""}`;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
/**
|
|
581
|
+
* What the gate says about one decision: each missing check and its command,
|
|
582
|
+
* and how to override a blocked one. At `pre-receive` (#4913) the request
|
|
583
|
+
* can't be recorded where the decision is made, so the refusal prints the
|
|
584
|
+
* exact command that records it from the checkout, then the override and
|
|
585
|
+
* the push that spends it.
|
|
586
|
+
*/
|
|
587
|
+
export function describeDecision(d: RefDecision, opts: { site?: RefGateSite } = {}): string[] {
|
|
515
588
|
const out: string[] = [];
|
|
516
589
|
const verb = d.decision === "admit" ? "admitted" : "refused";
|
|
517
590
|
out.push(`chant ref gate: ${verb} ${d.ref} ${short(d.old)}..${short(d.new)}`);
|
|
@@ -531,9 +604,17 @@ export function describeDecision(d: RefDecision): string[] {
|
|
|
531
604
|
const blocked = d.checks.filter((c) => c.action === "block" && (c.status === "missing" || c.status === "stale"));
|
|
532
605
|
if (blocked.length > 0) {
|
|
533
606
|
out.push(` Attach each check's evidence to ${short(d.new)} (chant workspace ref-gate attach --envelope <file> --at ${short(d.new)}) and push ${EVIDENCE_NOTES_REF} with the ref.`);
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
607
|
+
if (opts.site === "pre-receive") {
|
|
608
|
+
out.push(` To let ${short(d.new)} through without a check, record the request for an override in your checkout (this remote can't):`);
|
|
609
|
+
out.push(` ${requestCommand(d)}`);
|
|
610
|
+
out.push(` then a person listed in ${OVERRIDERS_PATH} at base overrides each check:`);
|
|
611
|
+
for (const c of blocked) out.push(` ${overrideCommand(c.subject, [c.check])}`);
|
|
612
|
+
out.push(` and the push goes again with chant/lifecycle: git push origin chant/lifecycle ${d.new ?? "HEAD"}:${d.ref}`);
|
|
613
|
+
} else {
|
|
614
|
+
out.push(` To let ${short(d.new)} through without a check, a person listed in ${OVERRIDERS_PATH} at base can override it once the request is recorded`);
|
|
615
|
+
out.push(` (the pre-push hook records it, or ${requestCommand(d)}):`);
|
|
616
|
+
for (const c of blocked) out.push(` ${overrideCommand(c.subject, [c.check])}`);
|
|
617
|
+
}
|
|
537
618
|
}
|
|
538
619
|
return out;
|
|
539
620
|
}
|