@intentius/chant 0.124.76 → 0.124.78
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__fixtures__/agent-guard/plywood.d.ts +8 -0
- package/dist/__fixtures__/agent-guard/plywood.d.ts.map +1 -0
- package/dist/__fixtures__/agent-guard/plywood.js +19 -0
- package/dist/__fixtures__/agent-guard/terragucci.d.ts +8 -0
- package/dist/__fixtures__/agent-guard/terragucci.d.ts.map +1 -0
- package/dist/__fixtures__/agent-guard/terragucci.js +30 -0
- package/dist/__fixtures__/gated-ledger.d.ts +10 -0
- package/dist/__fixtures__/gated-ledger.d.ts.map +1 -0
- package/dist/__fixtures__/gated-ledger.js +51 -0
- package/dist/__fixtures__/notify-messages/events.d.ts +9 -0
- package/dist/__fixtures__/notify-messages/events.d.ts.map +1 -0
- package/dist/__fixtures__/notify-messages/events.js +21 -0
- package/dist/__fixtures__/product-words.d.ts +14 -0
- package/dist/__fixtures__/product-words.d.ts.map +1 -1
- package/dist/__fixtures__/product-words.js +11 -0
- package/dist/__fixtures__/terragucci-notify.d.ts +66 -0
- package/dist/__fixtures__/terragucci-notify.d.ts.map +1 -0
- package/dist/__fixtures__/terragucci-notify.js +133 -0
- package/dist/action-pins.d.ts +8 -0
- package/dist/action-pins.d.ts.map +1 -1
- package/dist/action-pins.js +9 -0
- package/dist/agent-guard.d.ts +143 -0
- package/dist/agent-guard.d.ts.map +1 -0
- package/dist/agent-guard.js +455 -0
- package/dist/ci-commit.d.ts +120 -5
- package/dist/ci-commit.d.ts.map +1 -1
- package/dist/ci-commit.js +214 -23
- package/dist/ci-own-jobs.d.ts +81 -0
- package/dist/ci-own-jobs.d.ts.map +1 -0
- package/dist/ci-own-jobs.js +233 -0
- package/dist/ci-product-words.d.ts +18 -4
- package/dist/ci-product-words.d.ts.map +1 -1
- package/dist/ci-product-words.js +26 -6
- package/dist/ci-product.d.ts +40 -1
- package/dist/ci-product.d.ts.map +1 -1
- package/dist/ci-product.js +40 -1
- package/dist/cli/handlers/approve.d.ts +5 -1
- package/dist/cli/handlers/approve.d.ts.map +1 -1
- package/dist/cli/handlers/approve.js +17 -11
- package/dist/cli/handlers/build.d.ts +1 -1
- package/dist/cli/handlers/build.d.ts.map +1 -1
- package/dist/cli/handlers/build.js +88 -2
- package/dist/cli/handlers/commit.d.ts.map +1 -1
- package/dist/cli/handlers/commit.js +3 -1
- package/dist/cli/handlers/notify-relay.d.ts +18 -1
- package/dist/cli/handlers/notify-relay.d.ts.map +1 -1
- package/dist/cli/handlers/notify-relay.js +90 -14
- package/dist/cli/handlers/pr.d.ts.map +1 -1
- package/dist/cli/handlers/pr.js +3 -1
- package/dist/cli/handlers/release.d.ts +5 -1
- package/dist/cli/handlers/release.d.ts.map +1 -1
- package/dist/cli/handlers/release.js +21 -9
- package/dist/cli/handlers/run-generate.d.ts.map +1 -1
- package/dist/cli/handlers/run-generate.js +2 -1
- package/dist/cli/main.d.ts.map +1 -1
- package/dist/cli/main.js +21 -1
- package/dist/cli/parse-args.d.ts.map +1 -1
- package/dist/cli/parse-args.js +60 -4
- package/dist/cli/registry.d.ts +16 -0
- package/dist/cli/registry.d.ts.map +1 -1
- package/dist/components/pr-env-components.d.ts +65 -0
- package/dist/components/pr-env-components.d.ts.map +1 -0
- package/dist/components/pr-env-components.js +162 -0
- package/dist/components/pr-policy.d.ts +15 -0
- package/dist/components/pr-policy.d.ts.map +1 -1
- package/dist/components/pr-policy.js +24 -1
- package/dist/components/release-compile.d.ts +15 -3
- package/dist/components/release-compile.d.ts.map +1 -1
- package/dist/components/release-compile.js +66 -15
- package/dist/config.d.ts +14 -8
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +16 -1
- package/dist/gate-policy.d.ts +14 -1
- package/dist/gate-policy.d.ts.map +1 -1
- package/dist/gate-policy.js +16 -6
- package/dist/lexicon.d.ts +25 -1
- package/dist/lexicon.d.ts.map +1 -1
- package/dist/lexicon.js +10 -0
- package/dist/lifecycle/gated-op.d.ts +95 -0
- package/dist/lifecycle/gated-op.d.ts.map +1 -0
- package/dist/lifecycle/gated-op.js +96 -0
- package/dist/lifecycle/git.d.ts.map +1 -1
- package/dist/lifecycle/git.js +83 -2
- package/dist/lifecycle/release-images.d.ts +77 -3
- package/dist/lifecycle/release-images.d.ts.map +1 -1
- package/dist/lifecycle/release-images.js +132 -6
- package/dist/notify-product.d.ts +11 -2
- package/dist/notify-product.d.ts.map +1 -1
- package/dist/notify-product.js +16 -4
- package/dist/notify-relay-repo.d.ts +131 -0
- package/dist/notify-relay-repo.d.ts.map +1 -0
- package/dist/notify-relay-repo.js +202 -0
- package/dist/notify-relay.d.ts +25 -1
- package/dist/notify-relay.d.ts.map +1 -1
- package/dist/notify-relay.js +27 -2
- package/dist/notify.d.ts +36 -4
- package/dist/notify.d.ts.map +1 -1
- package/dist/notify.js +31 -21
- package/dist/notify.schema.json +1 -0
- package/dist/op/activities/review-product.d.ts +111 -0
- package/dist/op/activities/review-product.d.ts.map +1 -0
- package/dist/op/activities/review-product.js +116 -0
- package/dist/op/op-waves-capability.js +2 -2
- package/dist/policy-engine.d.ts +95 -0
- package/dist/policy-engine.d.ts.map +1 -0
- package/dist/policy-engine.js +157 -0
- package/dist/pr-forge-reads.d.ts +124 -0
- package/dist/pr-forge-reads.d.ts.map +1 -0
- package/dist/pr-forge-reads.js +322 -0
- package/dist/pr-forge.d.ts +14 -0
- package/dist/pr-forge.d.ts.map +1 -1
- package/dist/pr-forge.js +21 -13
- package/dist/pr-plan-note.d.ts +293 -0
- package/dist/pr-plan-note.d.ts.map +1 -0
- package/dist/pr-plan-note.js +362 -0
- package/dist/release-floors.d.ts +9 -1
- package/dist/release-floors.d.ts.map +1 -1
- package/dist/release-floors.js +9 -1
- package/dist/state-export.d.ts +126 -0
- package/dist/state-export.d.ts.map +1 -0
- package/dist/state-export.js +156 -0
- package/dist/state-migration.d.ts +148 -0
- package/dist/state-migration.d.ts.map +1 -0
- package/dist/state-migration.js +146 -0
- package/dist/state-unlock.d.ts +167 -0
- package/dist/state-unlock.d.ts.map +1 -0
- package/dist/state-unlock.js +196 -0
- package/dist/wave-runtime.mjs +332 -332
- package/dist/workspace/declaration.schema.json +1 -1
- package/dist/workspace/release-workflow.d.ts +10 -0
- package/dist/workspace/release-workflow.d.ts.map +1 -1
- package/dist/workspace/release-workflow.js +10 -4
- package/package.json +1 -1
- package/src/__fixtures__/agent-guard/plywood.ts +27 -0
- package/src/__fixtures__/agent-guard/terragucci.ts +39 -0
- package/src/__fixtures__/gated-ledger.ts +58 -0
- package/src/__fixtures__/notify-messages/events.ts +23 -0
- package/src/__fixtures__/notify-messages/messages.json +50 -0
- package/src/__fixtures__/pr-loop/terragucci-plan.note.golden.md +53 -0
- package/src/__fixtures__/pr-loop/terragucci-plan.note.json +83 -0
- package/src/__fixtures__/product-words.ts +12 -0
- package/src/__fixtures__/review-agent/terragucci-review.json +94 -0
- package/src/__fixtures__/terragucci-notify.ts +170 -0
- package/src/action-pins.test.ts +12 -1
- package/src/action-pins.ts +11 -0
- package/src/agent-guard.test.ts +319 -0
- package/src/agent-guard.ts +547 -0
- package/src/ci-commit.test.ts +213 -2
- package/src/ci-commit.ts +258 -27
- package/src/ci-own-jobs.test.ts +108 -0
- package/src/ci-own-jobs.ts +252 -0
- package/src/ci-product-words.ts +35 -7
- package/src/ci-product.ts +81 -1
- package/src/cli/handlers/approve-cwd.test.ts +112 -0
- package/src/cli/handlers/approve.ts +22 -11
- package/src/cli/handlers/build-own-jobs.test.ts +80 -0
- package/src/cli/handlers/build-pr-loop-control.test.ts +87 -0
- package/src/cli/handlers/build-run-group.test.ts +59 -0
- package/src/cli/handlers/build.ts +83 -3
- package/src/cli/handlers/commit.ts +3 -0
- package/src/cli/handlers/notify-relay.ts +99 -17
- package/src/cli/handlers/pr.ts +3 -1
- package/src/cli/handlers/release.ts +20 -10
- package/src/cli/handlers/run-generate.ts +2 -1
- package/src/cli/main.ts +21 -1
- package/src/cli/parse-args.ts +45 -3
- package/src/cli/registry.ts +16 -0
- package/src/components/pr-env-components.test.ts +137 -0
- package/src/components/pr-env-components.ts +213 -0
- package/src/components/pr-policy.test.ts +41 -1
- package/src/components/pr-policy.ts +25 -1
- package/src/components/release-compile.test.ts +47 -0
- package/src/components/release-compile.ts +70 -13
- package/src/config.ts +23 -3
- package/src/gate-policy-bundle.test.ts +14 -0
- package/src/gate-policy.test.ts +7 -0
- package/src/gate-policy.ts +22 -7
- package/src/lexicon.ts +28 -1
- package/src/lifecycle/gated-op.ts +158 -0
- package/src/lifecycle/git.test.ts +92 -0
- package/src/lifecycle/git.ts +71 -2
- package/src/lifecycle/release-images-dockerfile.test.ts +221 -0
- package/src/lifecycle/release-images.ts +167 -8
- package/src/notify-message-words.test.ts +186 -0
- package/src/notify-product.ts +17 -4
- package/src/notify-relay-repo.test.ts +284 -0
- package/src/notify-relay-repo.ts +234 -0
- package/src/notify-relay.test.ts +12 -0
- package/src/notify-relay.ts +29 -3
- package/src/notify.schema.json +1 -0
- package/src/notify.ts +59 -19
- package/src/op/activities/review-product.test.ts +116 -0
- package/src/op/activities/review-product.ts +200 -0
- package/src/op/op-waves-capability.ts +2 -2
- package/src/op/schedule-overdue.test.ts +26 -0
- package/src/policy-engine.test.ts +162 -0
- package/src/policy-engine.ts +200 -0
- package/src/pr-forge-reads.test.ts +285 -0
- package/src/pr-forge-reads.ts +424 -0
- package/src/pr-forge.ts +30 -16
- package/src/pr-plan-note.test.ts +105 -0
- package/src/pr-plan-note.ts +521 -0
- package/src/release-floors.ts +13 -1
- package/src/state-export.test.ts +124 -0
- package/src/state-export.ts +227 -0
- package/src/state-migration.test.ts +133 -0
- package/src/state-migration.ts +236 -0
- package/src/state-unlock.test.ts +165 -0
- package/src/state-unlock.ts +293 -0
- package/src/workspace/declaration.schema.json +1 -1
- package/src/workspace/release-workflow.test.ts +34 -0
- package/src/workspace/release-workflow.ts +19 -4
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slack and Teams messages a product can word (#4818): labels, a drift
|
|
3
|
+
* notice's re-run button, and the relay's name in the Teams reply.
|
|
4
|
+
*
|
|
5
|
+
* Two proofs. With no words, every message is the bytes chant 0.124.77
|
|
6
|
+
* rendered (`__fixtures__/notify-messages/messages.json`). With terragucci's
|
|
7
|
+
* words, chant's messages are the bytes terragucci's own renderers write
|
|
8
|
+
* (`__fixtures__/terragucci-notify.ts`, copied from terragucci), over the
|
|
9
|
+
* notices terragucci's notify tests build.
|
|
10
|
+
*/
|
|
11
|
+
import { describe, expect, test } from "vitest";
|
|
12
|
+
import { makeNotifyEvent, sendNotify, slackMessage, teamsMessage, type NotifyEvent, type NotifyFetch, type NotifyMessageWords } from "./notify";
|
|
13
|
+
import { driftNotifyFields, NOTIFY_MESSAGE_WORDS_FLOOR, productSendOptions } from "./notify-product";
|
|
14
|
+
import { assertProductWords } from "./ci-product-words";
|
|
15
|
+
import { NEXT_RELEASE } from "./release-floors";
|
|
16
|
+
import { NOTIFY_MESSAGE_EVENTS } from "./__fixtures__/notify-messages/events";
|
|
17
|
+
import recorded from "./__fixtures__/notify-messages/messages.json";
|
|
18
|
+
import * as tg from "./__fixtures__/terragucci-notify";
|
|
19
|
+
import schema from "./notify.schema.json";
|
|
20
|
+
import { contract } from "./workspace/__fixtures__/contract-repo";
|
|
21
|
+
|
|
22
|
+
const { expectValid } = contract(schema);
|
|
23
|
+
const NOW = new Date("2026-11-08T12:00:00.000Z");
|
|
24
|
+
const relay = { token: "signed-ref" };
|
|
25
|
+
|
|
26
|
+
describe("a message given no words (#4818)", () => {
|
|
27
|
+
test.each(Object.keys(NOTIFY_MESSAGE_EVENTS))("%s is the bytes chant 0.124.77 wrote, with and without relay buttons", (key) => {
|
|
28
|
+
const e = NOTIFY_MESSAGE_EVENTS[key]!;
|
|
29
|
+
const want = (recorded as Record<string, Record<string, string>>)[key]!;
|
|
30
|
+
for (const words of [undefined, {}]) {
|
|
31
|
+
expect(JSON.stringify(slackMessage(e, undefined, words))).toBe(want.slack);
|
|
32
|
+
expect(JSON.stringify(teamsMessage(e, undefined, words))).toBe(want.teams);
|
|
33
|
+
expect(JSON.stringify(slackMessage(e, relay, words))).toBe(want.slackRelay);
|
|
34
|
+
expect(JSON.stringify(teamsMessage(e, relay, words))).toBe(want.teamsRelay);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("chant's words, and a product's words with no message slot, leave sendNotify's options as they were", () => {
|
|
39
|
+
expect(productSendOptions(assertProductWords({}), { attempts: 1 })).toEqual({ attempts: 1 });
|
|
40
|
+
expect(productSendOptions(assertProductWords({ product: "plywood", noun: ["stack", "stacks"] }))).toEqual({ prefix: "x-plywood" });
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// ── terragucci ───────────────────────────────────────────────────────────
|
|
45
|
+
|
|
46
|
+
/** terragucci's message words, as it would pass them in its CiProductWords' notify slot. */
|
|
47
|
+
const TERRAGUCCI_MESSAGE: NotifyMessageWords = {
|
|
48
|
+
targets: "Roots",
|
|
49
|
+
noTargets: "see the run",
|
|
50
|
+
reason: "Outcome",
|
|
51
|
+
again: "run the wave again",
|
|
52
|
+
wave: "Wave",
|
|
53
|
+
teamsReport: false,
|
|
54
|
+
rerun: { label: "Re-plan", action: "terragucci-replan", block: "terragucci-drift" },
|
|
55
|
+
};
|
|
56
|
+
const words = assertProductWords({ product: "terragucci", noun: ["root", "roots"], notify: { source: "terragucci", wave: "wave {k}", headers: "x-terragucci", message: TERRAGUCCI_MESSAGE } });
|
|
57
|
+
|
|
58
|
+
/** The chat event for a wave notice: what terragucci's messages show, as chant's event fields. */
|
|
59
|
+
function waveEvent(n: tg.WaveNotice): NotifyEvent {
|
|
60
|
+
return makeNotifyEvent(
|
|
61
|
+
{
|
|
62
|
+
source: "terragucci",
|
|
63
|
+
event: n.event,
|
|
64
|
+
summary: tg.summary(n),
|
|
65
|
+
project: n.project,
|
|
66
|
+
wave: { number: n.wave, gate: `wave-${n.wave}` },
|
|
67
|
+
targets: n.roots,
|
|
68
|
+
...(n.digest ? { digest: n.digest } : {}),
|
|
69
|
+
approve: { command: n.approve, ...(n.review ? { review: n.review } : {}) },
|
|
70
|
+
...(n.outcome ? { reason: n.outcome } : {}),
|
|
71
|
+
run: n.run ? { url: n.run } : {},
|
|
72
|
+
...(n.report ? { reportUrl: n.report } : {}),
|
|
73
|
+
},
|
|
74
|
+
{ env: {}, now: NOW },
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The drift event: chant's drift fields under terragucci's words, its headline as the summary, and the Re-plan page. */
|
|
79
|
+
function driftEvent(n: tg.DriftNotice): NotifyEvent {
|
|
80
|
+
const fields = driftNotifyFields(words, { changed: n.roots, failed: n.failed, project: n.project, ...(n.report ? { reportUrl: n.report } : {}) });
|
|
81
|
+
return makeNotifyEvent({ ...fields, summary: tg.driftHeadline(n).slice("terragucci: ".length), ...(n.replan ? { rerunUrl: n.replan } : {}), run: n.run ? { url: n.run } : {} }, { env: {}, now: NOW });
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// The notices terragucci's test/notify.test.ts builds (ENV is acme/infra, run 42).
|
|
85
|
+
const RUN = "https://github.com/acme/infra/actions/runs/42";
|
|
86
|
+
const PROJECT = "github.com/acme/infra";
|
|
87
|
+
const waiting: tg.WaveNotice = { event: "waiting", wave: 2, project: PROJECT, roots: ["envs/prod/app", "envs/prod/db"], approve: "npx terragucci approve wave-2 --plan jcs1-sha256:ab", digest: "jcs1-sha256:ab", run: RUN };
|
|
88
|
+
const WAVES: Record<string, tg.WaveNotice> = {
|
|
89
|
+
waiting,
|
|
90
|
+
"waiting with a report": { ...waiting, report: "https://reports.example/acme/42" },
|
|
91
|
+
"under pr-review": { ...waiting, review: { pr: 7, url: "https://github.com/acme/infra/pull/7/files" } },
|
|
92
|
+
"waiting with the relay": { ...waiting, relay: "terragucci" },
|
|
93
|
+
refused: { event: "refused", wave: 3, project: PROJECT, roots: ["envs/prod/app"], approve: "read the plans that moved, then npx terragucci approve wave-3 --plan jcs1-sha256:dd, or revert", outcome: "wave 3 changed after approval: envs/prod/app", run: RUN },
|
|
94
|
+
"failed, no roots": { event: "failed", wave: 1, project: "this project", roots: [], approve: "nothing to approve: the apply failed; read the job log" },
|
|
95
|
+
"failed, policy denied": { event: "failed", wave: 1, project: PROJECT, roots: ["envs/prod/app"], approve: "nothing to approve: the policy denied it; an override command is in the job log", outcome: "policy denied envs/prod/app", run: RUN, report: "https://reports.example/acme/43" },
|
|
96
|
+
};
|
|
97
|
+
const DRIFTS: Record<string, tg.DriftNotice> = {
|
|
98
|
+
"drift with a Re-plan page": { project: PROJECT, roots: ["app"], failed: ["db"], run: RUN, replan: "https://github.com/acme/infra/actions/workflows/terragucci.yml" },
|
|
99
|
+
"drift on GitLab, with a report": { project: "acme/infra", roots: ["app", "net"], failed: [], run: "https://gitlab.com/acme/infra/-/jobs/9", report: "https://reports.example/acme/44", replan: "https://gitlab.com/acme/infra/-/pipeline_schedules" },
|
|
100
|
+
"drift with no Re-plan page": { project: PROJECT, roots: [], failed: ["db"] },
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
describe("terragucci's messages, rendered by chant under its words (#4818)", () => {
|
|
104
|
+
test.each(Object.keys(WAVES))("a wave %s: Slack and Teams are terragucci's bytes", (key) => {
|
|
105
|
+
const n = WAVES[key]!;
|
|
106
|
+
const e = waveEvent(n);
|
|
107
|
+
const message = { ...TERRAGUCCI_MESSAGE, ...(n.relay ? { relayName: n.relay } : {}) };
|
|
108
|
+
expect(JSON.stringify(slackMessage(e, undefined, message))).toBe(JSON.stringify(tg.slackMessage(n)));
|
|
109
|
+
expect(JSON.stringify(teamsMessage(e, undefined, message))).toBe(JSON.stringify(tg.teamsMessage(n)));
|
|
110
|
+
if (n.relay) {
|
|
111
|
+
expect(JSON.stringify(slackMessage(e, relay, message))).toBe(JSON.stringify(tg.slackMessage(n, relay)));
|
|
112
|
+
expect(JSON.stringify(teamsMessage(e, relay, message))).toBe(JSON.stringify(tg.teamsMessage(n, relay)));
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test.each(Object.keys(DRIFTS))("%s: Slack and Teams are terragucci's bytes", (key) => {
|
|
117
|
+
const n = DRIFTS[key]!;
|
|
118
|
+
const e = driftEvent(n);
|
|
119
|
+
expect(JSON.stringify(slackMessage(e, undefined, TERRAGUCCI_MESSAGE))).toBe(JSON.stringify(tg.slackDrift(n)));
|
|
120
|
+
expect(JSON.stringify(teamsMessage(e, undefined, TERRAGUCCI_MESSAGE))).toBe(JSON.stringify(tg.teamsDrift(n)));
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("terragucci's test lines hold: the review line, the relay reply, the Re-plan button", () => {
|
|
124
|
+
const review = slackMessage(waveEvent(WAVES["under pr-review"]!), undefined, TERRAGUCCI_MESSAGE).text.split("\n");
|
|
125
|
+
expect(review[1]).toBe("Review and approve: <https://github.com/acme/infra/pull/7/files|pull request 7>, then run the wave again");
|
|
126
|
+
const card = (teamsMessage(waveEvent(WAVES["waiting with the relay"]!), relay, { ...TERRAGUCCI_MESSAGE, relayName: "terragucci" }) as any).attachments[0].content;
|
|
127
|
+
expect(card.body.at(-1).text).toBe("Approve here: reply @terragucci approve signed-ref. Decline: reply @terragucci decline signed-ref.");
|
|
128
|
+
const n = DRIFTS["drift with a Re-plan page"]!;
|
|
129
|
+
const slack = slackMessage(driftEvent(n), undefined, TERRAGUCCI_MESSAGE);
|
|
130
|
+
expect(slack.text.split("\n")).toEqual(["*terragucci: drift in github.com/acme/infra: 1 root changed outside Terraform, 1 could not be refreshed*", "Drifted: app", "Not refreshed: db", `Run: <${RUN}>`]);
|
|
131
|
+
expect((slack.blocks?.[1] as any).elements).toEqual([{ type: "button", action_id: "terragucci-replan", text: { type: "plain_text", text: "Re-plan" }, url: n.replan }]);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("sendNotify posts terragucci's drift bodies with the words and no render of its own", async () => {
|
|
135
|
+
const n = DRIFTS["drift with a Re-plan page"]!;
|
|
136
|
+
const posted: Record<string, string> = {};
|
|
137
|
+
const fetch: NotifyFetch = async (url, init) => {
|
|
138
|
+
posted[url] = init.body;
|
|
139
|
+
return { ok: true, status: 200 };
|
|
140
|
+
};
|
|
141
|
+
const env = { S: "https://hooks.slack.test/a", T: "https://teams.test/b" };
|
|
142
|
+
const out = await sendNotify({ slack: "S", teams: "T" }, driftEvent(n), productSendOptions(words, { env, fetch, attempts: 1 }));
|
|
143
|
+
expect(out.map((d) => d.ok)).toEqual([true, true]);
|
|
144
|
+
expect(posted[env.S]).toBe(JSON.stringify(tg.slackDrift(n)));
|
|
145
|
+
expect(posted[env.T]).toBe(JSON.stringify(tg.teamsDrift(n)));
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("sendNotify names the relay in the Teams reply from its message options", async () => {
|
|
149
|
+
const n = WAVES["waiting with the relay"]!;
|
|
150
|
+
const posted: string[] = [];
|
|
151
|
+
const fetch: NotifyFetch = async (_url, init) => {
|
|
152
|
+
posted.push(init.body);
|
|
153
|
+
return { ok: true, status: 200 };
|
|
154
|
+
};
|
|
155
|
+
const options = productSendOptions(words, { env: { T: "https://teams.test/b", RK: "k" }, fetch, attempts: 1, message: { relayName: "terragucci" } });
|
|
156
|
+
await sendNotify({ teams: "T", relay: "RK" }, waveEvent(n), options);
|
|
157
|
+
const card = JSON.parse(posted[0]!).attachments[0].content;
|
|
158
|
+
expect(card.body.at(-1).text).toMatch(/^Approve here: reply @terragucci approve \S+\. Decline: reply @terragucci decline \S+\.$/);
|
|
159
|
+
expect(card.body[1].facts[0]).toEqual({ title: "Wave", value: "2" });
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
describe("chant's own words for the re-run button and the relay (#4818)", () => {
|
|
164
|
+
const drift = NOTIFY_MESSAGE_EVENTS.drift!;
|
|
165
|
+
const e = { ...drift, rerunUrl: "https://github.com/acme/db/actions/workflows/drift.yml" };
|
|
166
|
+
|
|
167
|
+
test("a re-run page gives a Slack button and a Teams action before the run's", () => {
|
|
168
|
+
const slack = slackMessage(e);
|
|
169
|
+
expect(slack.blocks?.[1]).toEqual({ type: "actions", elements: [{ type: "button", action_id: "chant-rerun", text: { type: "plain_text", text: "Run again" }, url: e.rerunUrl }] });
|
|
170
|
+
const card = (teamsMessage(e) as any).attachments[0].content;
|
|
171
|
+
expect(card.actions).toEqual([
|
|
172
|
+
{ type: "Action.OpenUrl", title: "Run again", url: e.rerunUrl },
|
|
173
|
+
{ type: "Action.OpenUrl", title: "Open the run", url: drift.run!.url },
|
|
174
|
+
]);
|
|
175
|
+
expectValid(JSON.parse(JSON.stringify(e)));
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test("the relay's name turns the Teams fact into the full replies", () => {
|
|
179
|
+
const waitingEvent = NOTIFY_MESSAGE_EVENTS.waiting!;
|
|
180
|
+
const card = (teamsMessage(waitingEvent, relay, { relayName: "chant-relay" }) as any).attachments[0].content;
|
|
181
|
+
expect(card.body[1].facts.map((f: { title: string }) => f.title)).not.toContain("Approve in chat");
|
|
182
|
+
expect(card.body[2]).toEqual({ type: "TextBlock", wrap: true, text: "Approve here: reply @chant-relay approve signed-ref. Decline: reply @chant-relay decline signed-ref." });
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test("the floor names the next release", () => expect(NOTIFY_MESSAGE_WORDS_FLOOR).toBe(NEXT_RELEASE));
|
|
186
|
+
});
|
package/src/notify-product.ts
CHANGED
|
@@ -14,6 +14,15 @@
|
|
|
14
14
|
|
|
15
15
|
import { CHANT_WORDS, isChantWords, type CiProductNotifyWords, type CiProductWords } from "./ci-product-words";
|
|
16
16
|
import type { NotifyFields, SendNotifyOptions } from "./notify";
|
|
17
|
+
import { NEXT_RELEASE } from "./release-floors";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The first chant whose Slack and Teams messages take a product's words, a
|
|
21
|
+
* drift event's `rerunUrl` and the relay's name (#4818): `notify.message` in
|
|
22
|
+
* the words, `message` in sendNotify's options, and the third argument of
|
|
23
|
+
* `slackMessage` and `teamsMessage`.
|
|
24
|
+
*/
|
|
25
|
+
export const NOTIFY_MESSAGE_WORDS_FLOOR = NEXT_RELEASE;
|
|
17
26
|
|
|
18
27
|
/** The notify slots of `words`, chant's when a caller built words before #4610. */
|
|
19
28
|
export function productNotifyWords(words: CiProductWords): CiProductNotifyWords {
|
|
@@ -26,12 +35,16 @@ export function productNotifySource(words: CiProductWords): string {
|
|
|
26
35
|
}
|
|
27
36
|
|
|
28
37
|
/**
|
|
29
|
-
* `options` with the product's header prefix, unless the caller names one
|
|
30
|
-
*
|
|
38
|
+
* `options` with the product's header prefix, unless the caller names one,
|
|
39
|
+
* and its message words (`notify.message`, #4818) under the caller's
|
|
40
|
+
* `message`. chant's own words with no message words leave `options` as they
|
|
41
|
+
* are, so chant sends `x-chant`.
|
|
31
42
|
*/
|
|
32
43
|
export function productSendOptions(words: CiProductWords, options: SendNotifyOptions = {}): SendNotifyOptions {
|
|
33
|
-
|
|
34
|
-
|
|
44
|
+
const own = productNotifyWords(words);
|
|
45
|
+
const out = own.message ? { ...options, message: { ...own.message, ...options.message } } : options;
|
|
46
|
+
if (options.prefix !== undefined || isChantWords(words)) return out;
|
|
47
|
+
return { ...out, prefix: own.headers };
|
|
35
48
|
}
|
|
36
49
|
|
|
37
50
|
/** What a drift notice concerns. */
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #4759: `chant notify relay --repo <url> --token-env <VAR>` checks its token
|
|
3
|
+
* and clones the repo itself. The forge is a loopback server that answers the
|
|
4
|
+
* forge's REST calls and serves a local bare repository over git's smart
|
|
5
|
+
* HTTP protocol (`git http-backend`), so the clone, the fetch before each
|
|
6
|
+
* click and the ledger push all carry the token as a header. No real forge.
|
|
7
|
+
*/
|
|
8
|
+
import { spawn } from "node:child_process";
|
|
9
|
+
import { readdirSync, readFileSync } from "node:fs";
|
|
10
|
+
import { createServer, type IncomingMessage, type Server } from "node:http";
|
|
11
|
+
import type { AddressInfo } from "node:net";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import { afterAll, afterEach, describe, expect, test } from "vitest";
|
|
14
|
+
import { CHANT_WORDS } from "./ci-product-words";
|
|
15
|
+
import { checkoutRelayDeps } from "./cli/handlers/notify-relay";
|
|
16
|
+
import { parseArgs } from "./cli/parse-args";
|
|
17
|
+
import { relayToken } from "./notify";
|
|
18
|
+
import { decideRelayClick } from "./notify-relay";
|
|
19
|
+
import { checkRelayToken, prepareRelayCheckout, relayGitAuthEnv, relayRepo, type RelayRepo } from "./notify-relay-repo";
|
|
20
|
+
import { cleanScratch, git, scratchDir, writeFiles } from "./workspace/__fixtures__/contract-repo";
|
|
21
|
+
|
|
22
|
+
afterAll(cleanScratch);
|
|
23
|
+
|
|
24
|
+
const KEY = "AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl";
|
|
25
|
+
const TOKEN = "relay-token-for-tests";
|
|
26
|
+
const RELAY_KEY = "relay-key-for-tests";
|
|
27
|
+
const D = `jcs1-sha256:${"a".repeat(64)}`;
|
|
28
|
+
|
|
29
|
+
/** A route's answer: the JSON body, and any response headers. */
|
|
30
|
+
type Route = { body: unknown; headers?: Record<string, string> };
|
|
31
|
+
|
|
32
|
+
interface LoopbackForge {
|
|
33
|
+
url: string;
|
|
34
|
+
/** REST routes by path, as the client asks them (encoded). Anything else answers 404. */
|
|
35
|
+
routes: Map<string, Route>;
|
|
36
|
+
/** Every request's method, path and Authorization header. */
|
|
37
|
+
seen: Array<{ method: string; path: string; auth: string | undefined }>;
|
|
38
|
+
close(): Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const readBody = (req: IncomingMessage): Promise<Buffer> =>
|
|
42
|
+
new Promise((done) => {
|
|
43
|
+
const chunks: Buffer[] = [];
|
|
44
|
+
req.on("data", (c: Buffer) => chunks.push(c));
|
|
45
|
+
req.on("end", () => done(Buffer.concat(chunks)));
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
/** One loopback server: the forge's REST API, and `<owner>/<name>.git` from `gitRoot` through git http-backend. */
|
|
49
|
+
async function loopbackForge(gitRoot?: string): Promise<LoopbackForge> {
|
|
50
|
+
const routes = new Map<string, Route>();
|
|
51
|
+
const seen: LoopbackForge["seen"] = [];
|
|
52
|
+
const server: Server = createServer((req, res) => {
|
|
53
|
+
void (async () => {
|
|
54
|
+
const [path, query = ""] = (req.url ?? "/").split("?");
|
|
55
|
+
seen.push({ method: req.method ?? "GET", path: path!, auth: req.headers.authorization });
|
|
56
|
+
const body = await readBody(req);
|
|
57
|
+
if (gitRoot && /\.git\//.test(path!)) {
|
|
58
|
+
const cgi = spawn("git", ["http-backend"], {
|
|
59
|
+
env: {
|
|
60
|
+
...process.env,
|
|
61
|
+
GIT_PROJECT_ROOT: gitRoot,
|
|
62
|
+
GIT_HTTP_EXPORT_ALL: "1",
|
|
63
|
+
// http-backend serves a push only to an authenticated user.
|
|
64
|
+
REMOTE_USER: "relay",
|
|
65
|
+
PATH_INFO: decodeURIComponent(path!),
|
|
66
|
+
QUERY_STRING: query,
|
|
67
|
+
REQUEST_METHOD: req.method ?? "GET",
|
|
68
|
+
CONTENT_TYPE: req.headers["content-type"] ?? "",
|
|
69
|
+
CONTENT_LENGTH: String(body.length),
|
|
70
|
+
...(req.headers["git-protocol"] ? { GIT_PROTOCOL: String(req.headers["git-protocol"]) } : {}),
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
const out: Buffer[] = [];
|
|
74
|
+
cgi.stdout.on("data", (c: Buffer) => out.push(c));
|
|
75
|
+
cgi.stdin.end(body);
|
|
76
|
+
await new Promise((done) => cgi.on("close", done));
|
|
77
|
+
const all = Buffer.concat(out);
|
|
78
|
+
const split = all.indexOf("\r\n\r\n");
|
|
79
|
+
const head = all.subarray(0, split).toString("utf-8");
|
|
80
|
+
const headers: Record<string, string> = {};
|
|
81
|
+
let status = 200;
|
|
82
|
+
for (const line of head.split("\r\n")) {
|
|
83
|
+
const at = line.indexOf(":");
|
|
84
|
+
const name = line.slice(0, at).trim();
|
|
85
|
+
const value = line.slice(at + 1).trim();
|
|
86
|
+
if (name.toLowerCase() === "status") status = Number(value.split(" ")[0]);
|
|
87
|
+
else headers[name] = value;
|
|
88
|
+
}
|
|
89
|
+
res.writeHead(status, headers);
|
|
90
|
+
res.end(all.subarray(split + 4));
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const hit = routes.get(path!);
|
|
94
|
+
res.writeHead(hit ? 200 : 404, { "content-type": "application/json", ...(hit?.headers ?? {}) });
|
|
95
|
+
res.end(JSON.stringify(hit ? hit.body : { message: "Not Found" }));
|
|
96
|
+
})();
|
|
97
|
+
});
|
|
98
|
+
await new Promise<void>((done) => server.listen(0, "127.0.0.1", done));
|
|
99
|
+
return {
|
|
100
|
+
url: `http://127.0.0.1:${(server.address() as AddressInfo).port}`,
|
|
101
|
+
routes,
|
|
102
|
+
seen,
|
|
103
|
+
close: () => new Promise<void>((done) => server.close(() => done())),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const forges: LoopbackForge[] = [];
|
|
108
|
+
afterEach(async () => {
|
|
109
|
+
for (const f of forges.splice(0)) await f.close();
|
|
110
|
+
});
|
|
111
|
+
async function forge(routes: Record<string, Route | unknown> = {}, gitRoot?: string): Promise<LoopbackForge> {
|
|
112
|
+
const f = await loopbackForge(gitRoot);
|
|
113
|
+
for (const [p, r] of Object.entries(routes)) f.routes.set(p, r && typeof r === "object" && "body" in r ? (r as Route) : { body: r });
|
|
114
|
+
forges.push(f);
|
|
115
|
+
return f;
|
|
116
|
+
}
|
|
117
|
+
const repoAt = (f: LoopbackForge, kind: "github" | "gitlab" | "forgejo"): RelayRepo => {
|
|
118
|
+
const r = relayRepo(`${f.url}/acme/infra.git`, kind);
|
|
119
|
+
if ("error" in r) throw new Error(r.error);
|
|
120
|
+
return r;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
describe("the relay's token (#4759)", () => {
|
|
124
|
+
test("on Forgejo, takes a token whose user neither administers the repo nor may push to the default branch, and pushes as that user", async () => {
|
|
125
|
+
const ok = await forge({ "/api/v1/user": { login: "relay-bot" }, "/api/v1/repos/acme/infra": { permissions: { admin: false, push: true } }, "/api/v1/repos/acme/infra/branches/main": { user_can_push: false } });
|
|
126
|
+
expect(await checkRelayToken(repoAt(ok, "forgejo"), TOKEN, "main")).toEqual({ user: "relay-bot" });
|
|
127
|
+
expect(ok.seen.every((s) => s.auth === `token ${TOKEN}`)).toBe(true);
|
|
128
|
+
const admin = await forge({ "/api/v1/user": { login: "root" }, "/api/v1/repos/acme/infra": { permissions: { admin: true } }, "/api/v1/repos/acme/infra/branches/main": { user_can_push: true } });
|
|
129
|
+
expect(await checkRelayToken(repoAt(admin, "forgejo"), TOKEN, "main")).toEqual({ error: "the token's user administers acme/infra" });
|
|
130
|
+
const pusher = await forge({ "/api/v1/user": { login: "dev" }, "/api/v1/repos/acme/infra": { permissions: { admin: false } }, "/api/v1/repos/acme/infra/branches/main": { user_can_push: true } });
|
|
131
|
+
expect(await checkRelayToken(repoAt(pusher, "forgejo"), TOKEN, "main")).toMatchObject({ error: expect.stringMatching(/may push to main/) });
|
|
132
|
+
// A branch read that does not say counts as a push.
|
|
133
|
+
const silent = await forge({ "/api/v1/user": { login: "dev" }, "/api/v1/repos/acme/infra": { permissions: { admin: false } }, "/api/v1/repos/acme/infra/branches/main": {} });
|
|
134
|
+
expect(await checkRelayToken(repoAt(silent, "forgejo"), TOKEN, "main")).toMatchObject({ error: expect.stringMatching(/may push to main/) });
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("on GitHub, refuses a classic token, an admin, and an unprotected default branch", async () => {
|
|
138
|
+
const routes = { "/api/v3/repos/acme/infra": { permissions: { admin: false, push: true } }, "/api/v3/repos/acme/infra/branches/main": { protected: true } };
|
|
139
|
+
const ok = await forge(routes);
|
|
140
|
+
expect(await checkRelayToken(repoAt(ok, "github"), TOKEN, "main")).toEqual({ user: "x-access-token" });
|
|
141
|
+
expect(ok.seen.every((s) => s.auth === `Bearer ${TOKEN}`)).toBe(true);
|
|
142
|
+
const classic = await forge({ ...routes, "/api/v3/repos/acme/infra": { body: { permissions: { admin: false } }, headers: { "x-oauth-scopes": "repo" } } });
|
|
143
|
+
expect(await checkRelayToken(repoAt(classic, "github"), TOKEN, "main")).toMatchObject({ error: expect.stringMatching(/classic token/) });
|
|
144
|
+
const admin = await forge({ ...routes, "/api/v3/repos/acme/infra": { permissions: { admin: true } } });
|
|
145
|
+
expect(await checkRelayToken(repoAt(admin, "github"), TOKEN, "main")).toEqual({ error: "the token's account administers acme/infra" });
|
|
146
|
+
const open = await forge({ ...routes, "/api/v3/repos/acme/infra/branches/main": { protected: false } });
|
|
147
|
+
expect(await checkRelayToken(repoAt(open, "github"), TOKEN, "main")).toMatchObject({ error: expect.stringMatching(/not protected/) });
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
test("on GitLab, refuses the api scope, a Maintainer, an unprotected branch, and a push level the user holds", async () => {
|
|
151
|
+
const base = {
|
|
152
|
+
"/api/v4/personal_access_tokens/self": { scopes: ["write_repository", "read_api"] },
|
|
153
|
+
"/api/v4/projects/acme%2Finfra": { permissions: { project_access: { access_level: 30 } } },
|
|
154
|
+
"/api/v4/projects/acme%2Finfra/protected_branches/main": { push_access_levels: [{ access_level: 40 }] },
|
|
155
|
+
};
|
|
156
|
+
const ok = await forge(base);
|
|
157
|
+
expect(await checkRelayToken(repoAt(ok, "gitlab"), TOKEN, "main")).toEqual({ user: "oauth2" });
|
|
158
|
+
expect(ok.seen.length).toBe(3);
|
|
159
|
+
const api = await forge({ ...base, "/api/v4/personal_access_tokens/self": { scopes: ["api"] } });
|
|
160
|
+
expect(await checkRelayToken(repoAt(api, "gitlab"), TOKEN, "main")).toMatchObject({ error: expect.stringMatching(/scopes api/) });
|
|
161
|
+
const maintainer = await forge({ ...base, "/api/v4/projects/acme%2Finfra": { permissions: { project_access: { access_level: 30 }, group_access: { access_level: 40 } } } });
|
|
162
|
+
expect(await checkRelayToken(repoAt(maintainer, "gitlab"), TOKEN, "main")).toMatchObject({ error: expect.stringMatching(/Maintainer/) });
|
|
163
|
+
const unprotected = await forge({ "/api/v4/personal_access_tokens/self": base["/api/v4/personal_access_tokens/self"], "/api/v4/projects/acme%2Finfra": base["/api/v4/projects/acme%2Finfra"] });
|
|
164
|
+
expect(await checkRelayToken(repoAt(unprotected, "gitlab"), TOKEN, "main")).toMatchObject({ error: expect.stringMatching(/not a protected branch/) });
|
|
165
|
+
const developers = await forge({ ...base, "/api/v4/projects/acme%2Finfra/protected_branches/main": { push_access_levels: [{ access_level: 30 }] } });
|
|
166
|
+
expect(await checkRelayToken(repoAt(developers, "gitlab"), TOKEN, "main")).toMatchObject({ error: expect.stringMatching(/may push to main/) });
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test("a forge that will not answer refuses the token", async () => {
|
|
170
|
+
const none = await forge();
|
|
171
|
+
expect(await checkRelayToken(repoAt(none, "github"), TOKEN, "main")).toMatchObject({ error: expect.stringMatching(/could not be read with the token .*answered 404/) });
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("refuses a clone address that carries a credential or names no forge, and keeps the token out of every file", () => {
|
|
175
|
+
expect(relayRepo("https://bot:t@github.com/acme/infra.git")).toMatchObject({ error: expect.stringMatching(/must not carry a credential/) });
|
|
176
|
+
expect(relayRepo("git@github.com:acme/infra.git")).toMatchObject({ error: expect.stringMatching(/https clone address/) });
|
|
177
|
+
expect(relayRepo("https://git.example.com/acme/infra.git")).toMatchObject({ error: expect.stringMatching(/pass --forge/) });
|
|
178
|
+
expect(relayRepo("https://gitlab.com/acme/platform/infra.git")).toMatchObject({ forge: "gitlab", path: "acme/platform/infra", apiBase: "https://gitlab.com/api/v4" });
|
|
179
|
+
expect(relayRepo("https://github.com/acme/infra")).toMatchObject({ forge: "github", path: "acme/infra", apiBase: "https://api.github.com" });
|
|
180
|
+
const basic = `Authorization: Basic ${Buffer.from("relay-bot:tok").toString("base64")}`;
|
|
181
|
+
expect(relayGitAuthEnv("relay-bot", "tok")).toEqual({ GIT_CONFIG_COUNT: "1", GIT_CONFIG_KEY_0: "http.extraHeader", GIT_CONFIG_VALUE_0: basic, GIT_TERMINAL_PROMPT: "0" });
|
|
182
|
+
// Entries the environment already has are kept.
|
|
183
|
+
expect(relayGitAuthEnv("relay-bot", "tok", { GIT_CONFIG_COUNT: "2" })).toEqual({ GIT_CONFIG_COUNT: "3", GIT_CONFIG_KEY_2: "http.extraHeader", GIT_CONFIG_VALUE_2: basic, GIT_TERMINAL_PROMPT: "0" });
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
describe("the relay's flags (#4759)", () => {
|
|
188
|
+
test("--repo takes a clone address for the relay and owner/name for ci serve", () => {
|
|
189
|
+
expect(parseArgs(["notify", "relay", "--repo", "https://github.com/acme/infra.git", "--token-env", "RELAY_TOKEN", "--host", "0.0.0.0", "--port", "8080"])).toMatchObject({ repoUrl: "https://github.com/acme/infra.git", tokenEnv: "RELAY_TOKEN", host: "0.0.0.0", port: ["8080"] });
|
|
190
|
+
expect(parseArgs(["ci", "serve", "--repo", "acme/infra"])).toMatchObject({ ciRepo: "acme/infra" });
|
|
191
|
+
expect(() => parseArgs(["notify", "relay", "--token-env", "not a name"])).toThrow(/--token-env needs a variable's name/);
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
describe("the relay's own checkout (#4759)", () => {
|
|
196
|
+
const SIGNERS = `alice@acme.com,slack:T1/U1 ssh-ed25519 ${KEY}\nbob@acme.com ssh-ed25519 ${KEY}\n`;
|
|
197
|
+
const T = (h: number): string => new Date(Date.now() - 3600_000 + h * 3600_000).toISOString();
|
|
198
|
+
const pending = { version: 1, kind: "pending", op: "tf-apply", gate: "wave-1", timestamp: T(1), expiresAt: T(49), planDigest: D };
|
|
199
|
+
|
|
200
|
+
/** A bare `acme/infra.git` under a scratch root: main with the signers file, and chant/lifecycle with one pending fact. */
|
|
201
|
+
function bareRepo(): { root: string; bare: string; work: string } {
|
|
202
|
+
const root = scratchDir("chant-relay-forge-");
|
|
203
|
+
const bare = join(root, "acme", "infra.git");
|
|
204
|
+
git(root, "init", "-q", "--bare", "-b", "main", bare);
|
|
205
|
+
const work = join(root, "work");
|
|
206
|
+
git(root, "init", "-q", "-b", "main", work);
|
|
207
|
+
writeFiles(work, { ".chant/allowed_signers": SIGNERS });
|
|
208
|
+
git(work, "add", "-A");
|
|
209
|
+
git(work, "commit", "-q", "-m", "main");
|
|
210
|
+
git(work, "push", "-q", bare, "main");
|
|
211
|
+
git(work, "checkout", "-q", "--orphan", "chant/lifecycle");
|
|
212
|
+
git(work, "rm", "-rq", "--cached", ".");
|
|
213
|
+
writeFiles(work, { "_gates/tf-apply.jsonl": `${JSON.stringify(pending)}\n` });
|
|
214
|
+
git(work, "add", "_gates");
|
|
215
|
+
git(work, "commit", "-q", "-m", "ledger");
|
|
216
|
+
git(work, "push", "-q", bare, "chant/lifecycle");
|
|
217
|
+
git(work, "checkout", "-q", "-f", "main");
|
|
218
|
+
return { root, bare, work };
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const githubRoutes = { "/api/v3/repos/acme/infra": { permissions: { admin: false, push: true } }, "/api/v3/repos/acme/infra/branches/main": { protected: true } };
|
|
222
|
+
|
|
223
|
+
test("refuses an over-scoped token before it clones anything", async () => {
|
|
224
|
+
const { root } = bareRepo();
|
|
225
|
+
const f = await forge({ ...githubRoutes, "/api/v3/repos/acme/infra/branches/main": { protected: false } }, root);
|
|
226
|
+
const parent = scratchDir();
|
|
227
|
+
const made = await prepareRelayCheckout({ url: `${f.url}/acme/infra.git`, token: TOKEN, forge: "github", parent });
|
|
228
|
+
expect(made).toMatchObject({ error: expect.stringMatching(/^the relay holds only an approve-only token, and this one is more: main is not protected/) });
|
|
229
|
+
// Only the default branch was asked (ls-remote); nothing was cloned.
|
|
230
|
+
expect(readdirSync(parent)).toEqual([]);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
test("clones the default branch with the token as a header only, reads a signer added since at the tip, and pushes the approval", async () => {
|
|
234
|
+
const { root, bare, work } = bareRepo();
|
|
235
|
+
const f = await forge(githubRoutes, root);
|
|
236
|
+
const made = await prepareRelayCheckout({ url: `${f.url}/acme/infra.git`, token: TOKEN, forge: "github", env: { ...process.env, GIT_CONFIG_COUNT: "0" }, parent: scratchDir() });
|
|
237
|
+
if ("error" in made) throw new Error(made.error);
|
|
238
|
+
expect(made).toMatchObject({ branch: "main", user: "x-access-token" });
|
|
239
|
+
// Every git request carried the token as Basic auth; the checkout's files do not hold it.
|
|
240
|
+
const gitCalls = f.seen.filter((s) => s.path.includes(".git/"));
|
|
241
|
+
expect(gitCalls.length).toBeGreaterThan(0);
|
|
242
|
+
expect(gitCalls.every((s) => s.auth === `Basic ${Buffer.from(`x-access-token:${TOKEN}`).toString("base64")}`)).toBe(true);
|
|
243
|
+
expect(readFileSync(join(made.dir, ".git", "config"), "utf-8")).not.toContain(TOKEN);
|
|
244
|
+
expect(readFileSync(join(made.dir, ".git", "config"), "utf-8")).not.toContain(Buffer.from(`x-access-token:${TOKEN}`).toString("base64"));
|
|
245
|
+
expect(git(made.dir, "remote", "get-url", "origin")).toBe(`${f.url}/acme/infra.git`);
|
|
246
|
+
|
|
247
|
+
// A signer added on main after the clone.
|
|
248
|
+
writeFiles(work, { ".chant/allowed_signers": `${SIGNERS}carol@acme.com,slack:T1/U3 ssh-ed25519 ${KEY}\n` });
|
|
249
|
+
git(work, "commit", "-qam", "add carol");
|
|
250
|
+
git(work, "push", "-q", bare, "main");
|
|
251
|
+
|
|
252
|
+
const prev = { env: { ...process.env }, cwd: process.cwd() };
|
|
253
|
+
Object.assign(process.env, made.gitEnv, { GIT_AUTHOR_NAME: "t", GIT_AUTHOR_EMAIL: "t@t", GIT_COMMITTER_NAME: "t", GIT_COMMITTER_EMAIL: "t@t" });
|
|
254
|
+
// As chant notify relay runs it: in the checkout, since chant approve writes the ledger of the process's directory (#4760).
|
|
255
|
+
process.chdir(made.dir);
|
|
256
|
+
try {
|
|
257
|
+
const deps = checkoutRelayDeps(made.dir, "chant-relay");
|
|
258
|
+
expect(deps.words).toEqual(CHANT_WORDS);
|
|
259
|
+
const click = (user: string) => ({ app: "slack" as const, chatUser: user, display: user, action: "approve" as const, token: relayToken(RELAY_KEY, { id: "e1", source: "chant", at: new Date().toISOString(), gate: "wave-1", digest: D }) });
|
|
260
|
+
const a = await decideRelayClick(click("slack:T1/U3"), { key: RELAY_KEY }, deps);
|
|
261
|
+
expect(a).toMatchObject({ ok: true, outcome: "approved", principal: "carol@acme.com" });
|
|
262
|
+
const ledger = git(root, "--git-dir", bare, "show", "chant/lifecycle:_gates/tf-apply.jsonl");
|
|
263
|
+
expect(ledger).toMatch(/"resolvedBy":"carol@acme\.com"/);
|
|
264
|
+
expect(ledger).toMatch(/"relayedBy":"chant-relay"/);
|
|
265
|
+
} finally {
|
|
266
|
+
process.chdir(prev.cwd);
|
|
267
|
+
for (const k of Object.keys(process.env)) if (!(k in prev.env)) delete process.env[k];
|
|
268
|
+
Object.assign(process.env, prev.env);
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
test("a click whose fetch of the default branch fails records nothing", async () => {
|
|
273
|
+
const { root } = bareRepo();
|
|
274
|
+
const f = await forge(githubRoutes, root);
|
|
275
|
+
const made = await prepareRelayCheckout({ url: `${f.url}/acme/infra.git`, token: TOKEN, forge: "github", parent: scratchDir() });
|
|
276
|
+
if ("error" in made) throw new Error(made.error);
|
|
277
|
+
// Without the header, the loopback forge still serves git, so point origin somewhere that answers nothing.
|
|
278
|
+
git(made.dir, "remote", "set-url", "origin", "http://127.0.0.1:9/acme/infra.git");
|
|
279
|
+
const deps = checkoutRelayDeps(made.dir, "chant-relay");
|
|
280
|
+
const token = relayToken(RELAY_KEY, { id: "e1", source: "chant", at: new Date().toISOString(), gate: "wave-1", digest: D });
|
|
281
|
+
const a = await decideRelayClick({ app: "slack", chatUser: "slack:T1/U1", display: "alice", action: "approve", token }, { key: RELAY_KEY }, deps);
|
|
282
|
+
expect(a).toMatchObject({ ok: false, outcome: "refused", text: expect.stringMatching(/^Not recorded: cannot fetch main to read the signers at its tip/) });
|
|
283
|
+
});
|
|
284
|
+
});
|