@intentius/chant 0.124.62 → 0.124.64
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/cli/handlers/approve.d.ts +26 -0
- package/dist/cli/handlers/approve.d.ts.map +1 -1
- package/dist/cli/handlers/approve.js +167 -18
- package/dist/cli/handlers/plan.d.ts +10 -0
- package/dist/cli/handlers/plan.d.ts.map +1 -0
- package/dist/cli/handlers/plan.js +71 -0
- package/dist/cli/handlers/pr.d.ts.map +1 -1
- package/dist/cli/handlers/pr.js +2 -1
- package/dist/cli/handlers/release.d.ts +11 -7
- package/dist/cli/handlers/release.d.ts.map +1 -1
- package/dist/cli/handlers/release.js +93 -20
- package/dist/cli/main.d.ts.map +1 -1
- package/dist/cli/main.js +48 -7
- package/dist/cli/mcp/workspace-tools.d.ts.map +1 -1
- package/dist/cli/mcp/workspace-tools.js +2 -1
- package/dist/cli/parse-args.d.ts.map +1 -1
- package/dist/cli/parse-args.js +43 -0
- package/dist/cli/registry.d.ts +18 -2
- package/dist/cli/registry.d.ts.map +1 -1
- package/dist/config.d.ts +50 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +1 -0
- package/dist/gate-decision.d.ts +1 -1
- package/dist/gate-decision.d.ts.map +1 -1
- package/dist/gate-decision.js +4 -2
- package/dist/gate-statement.schema.json +46 -0
- package/dist/lifecycle/__fixtures__/publish-fakes.d.ts +20 -0
- package/dist/lifecycle/__fixtures__/publish-fakes.d.ts.map +1 -1
- package/dist/lifecycle/__fixtures__/publish-fakes.js +119 -3
- package/dist/lifecycle/gate-ledger.d.ts +28 -5
- package/dist/lifecycle/gate-ledger.d.ts.map +1 -1
- package/dist/lifecycle/gate-ledger.js +6 -0
- package/dist/lifecycle/gate-seal.d.ts +59 -7
- package/dist/lifecycle/gate-seal.d.ts.map +1 -1
- package/dist/lifecycle/gate-seal.js +68 -15
- package/dist/lifecycle/gate-statement.d.ts +95 -0
- package/dist/lifecycle/gate-statement.d.ts.map +1 -0
- package/dist/lifecycle/gate-statement.js +110 -0
- package/dist/lifecycle/gate-webauthn.d.ts +79 -0
- package/dist/lifecycle/gate-webauthn.d.ts.map +1 -0
- package/dist/lifecycle/gate-webauthn.js +105 -0
- package/dist/lifecycle/git.d.ts +1 -1
- package/dist/lifecycle/git.d.ts.map +1 -1
- package/dist/lifecycle/git.js +24 -19
- package/dist/lifecycle/ledger-remote.d.ts +37 -0
- package/dist/lifecycle/ledger-remote.d.ts.map +1 -0
- package/dist/lifecycle/ledger-remote.js +42 -0
- package/dist/lifecycle/plan-stale.d.ts +76 -0
- package/dist/lifecycle/plan-stale.d.ts.map +1 -0
- package/dist/lifecycle/plan-stale.js +120 -0
- package/dist/lifecycle/publish-credentials.d.ts +1 -1
- package/dist/lifecycle/publish-credentials.d.ts.map +1 -1
- package/dist/lifecycle/publish-credentials.js +2 -0
- package/dist/lifecycle/publish-declaration.d.ts +107 -0
- package/dist/lifecycle/publish-declaration.d.ts.map +1 -1
- package/dist/lifecycle/publish-declaration.js +110 -1
- package/dist/lifecycle/publish-forge.d.ts +5 -1
- package/dist/lifecycle/publish-forge.d.ts.map +1 -1
- package/dist/lifecycle/publish-forge.js +13 -4
- package/dist/lifecycle/publish-git-ssh.d.ts +82 -0
- package/dist/lifecycle/publish-git-ssh.d.ts.map +1 -0
- package/dist/lifecycle/publish-git-ssh.js +180 -0
- package/dist/lifecycle/publish-github.d.ts +69 -0
- package/dist/lifecycle/publish-github.d.ts.map +1 -0
- package/dist/lifecycle/publish-github.js +153 -0
- package/dist/lifecycle/publish-homebrew.d.ts +77 -0
- package/dist/lifecycle/publish-homebrew.d.ts.map +1 -0
- package/dist/lifecycle/publish-homebrew.js +265 -0
- package/dist/lifecycle/publish-marketplace.d.ts +85 -0
- package/dist/lifecycle/publish-marketplace.d.ts.map +1 -0
- package/dist/lifecycle/publish-marketplace.js +398 -0
- package/dist/lifecycle/publish-targets.d.ts +6 -0
- package/dist/lifecycle/publish-targets.d.ts.map +1 -1
- package/dist/lifecycle/publish-targets.js +15 -0
- package/dist/lifecycle/publish-tauri.d.ts +61 -0
- package/dist/lifecycle/publish-tauri.d.ts.map +1 -0
- package/dist/lifecycle/publish-tauri.js +181 -0
- package/dist/lifecycle/release-cut.d.ts +51 -5
- package/dist/lifecycle/release-cut.d.ts.map +1 -1
- package/dist/lifecycle/release-cut.js +165 -16
- package/dist/lifecycle/release-publish.d.ts +18 -2
- package/dist/lifecycle/release-publish.d.ts.map +1 -1
- package/dist/lifecycle/release-publish.js +5 -0
- package/dist/lifecycle/release-revocation.d.ts +4 -3
- package/dist/lifecycle/release-revocation.d.ts.map +1 -1
- package/dist/lifecycle/release-revocation.js +5 -4
- package/dist/lifecycle/release-store.d.ts +32 -0
- package/dist/lifecycle/release-store.d.ts.map +1 -1
- package/dist/lifecycle/release-store.js +11 -0
- package/dist/lifecycle/snapshot-store.d.ts.map +1 -1
- package/dist/lifecycle/snapshot-store.js +17 -9
- package/dist/lifecycle/test-passkey.d.ts +25 -0
- package/dist/lifecycle/test-passkey.d.ts.map +1 -0
- package/dist/lifecycle/test-passkey.js +39 -0
- package/dist/notify.d.ts +23 -3
- package/dist/notify.d.ts.map +1 -1
- package/dist/notify.js +8 -2
- package/dist/notify.schema.json +12 -3
- package/dist/op/gate.d.ts +1 -1
- package/dist/op/gate.d.ts.map +1 -1
- package/dist/op/gate.js +2 -1
- package/dist/pr-forge.d.ts +20 -0
- package/dist/pr-forge.d.ts.map +1 -1
- package/dist/pr-forge.js +87 -8
- package/dist/release-floors.d.ts +1 -1
- package/dist/release-floors.js +1 -1
- package/dist/wave-runtime.mjs +333 -331
- package/dist/workspace/ci-builds.d.ts +21 -0
- package/dist/workspace/ci-builds.d.ts.map +1 -1
- package/dist/workspace/ci-builds.js +14 -0
- package/dist/workspace/ci-green.d.ts +45 -2
- package/dist/workspace/ci-green.d.ts.map +1 -1
- package/dist/workspace/ci-green.js +123 -5
- package/dist/workspace/ci-quarantine-report.d.ts +80 -0
- package/dist/workspace/ci-quarantine-report.d.ts.map +1 -0
- package/dist/workspace/ci-quarantine-report.js +204 -0
- package/dist/workspace/ci-quarantine.d.ts +73 -0
- package/dist/workspace/ci-quarantine.d.ts.map +1 -0
- package/dist/workspace/ci-quarantine.js +170 -0
- package/dist/workspace/ci-serve.d.ts.map +1 -1
- package/dist/workspace/ci-serve.js +7 -4
- package/dist/workspace/class-data.d.ts +3 -1
- package/dist/workspace/class-data.d.ts.map +1 -1
- package/dist/workspace/class-data.js +4 -1
- package/dist/workspace/declaration.d.ts +45 -2
- package/dist/workspace/declaration.d.ts.map +1 -1
- package/dist/workspace/declaration.js +45 -2
- package/dist/workspace/declaration.schema.json +51 -3
- package/dist/workspace/forge-work.d.ts +76 -0
- package/dist/workspace/forge-work.d.ts.map +1 -0
- package/dist/workspace/forge-work.js +160 -0
- package/dist/workspace/identity-gate-rule.d.ts +3 -2
- package/dist/workspace/identity-gate-rule.d.ts.map +1 -1
- package/dist/workspace/identity-gate-rule.js +5 -7
- package/dist/workspace/identity.d.ts +3 -2
- package/dist/workspace/identity.d.ts.map +1 -1
- package/dist/workspace/identity.js +6 -3
- package/dist/workspace/intent-cli.d.ts +3 -2
- package/dist/workspace/intent-cli.d.ts.map +1 -1
- package/dist/workspace/intent-cli.js +26 -5
- package/dist/workspace/intent-record.d.ts +3 -0
- package/dist/workspace/intent-record.d.ts.map +1 -1
- package/dist/workspace/intent-record.schema.json +32 -0
- package/dist/workspace/intent.d.ts +9 -0
- package/dist/workspace/intent.d.ts.map +1 -1
- package/dist/workspace/intent.js +2 -0
- package/dist/workspace/intent.schema.json +32 -0
- package/dist/workspace/kind-readers.d.ts.map +1 -1
- package/dist/workspace/kind-readers.js +2 -1
- package/dist/workspace/kinds.d.ts +6 -1
- package/dist/workspace/kinds.d.ts.map +1 -1
- package/dist/workspace/kinds.js +8 -3
- package/dist/workspace/plan-stale.schema.json +286 -0
- package/dist/workspace/release-candidates.d.ts +42 -7
- package/dist/workspace/release-candidates.d.ts.map +1 -1
- package/dist/workspace/release-candidates.js +64 -23
- package/dist/workspace/release-candidates.schema.json +55 -2
- package/dist/workspace/releases.schema.json +47 -0
- package/dist/workspace/trust/policy.d.ts +10 -0
- package/dist/workspace/trust/policy.d.ts.map +1 -1
- package/dist/workspace/trust/policy.js +19 -1
- package/dist/workspace/trust/run-commits.d.ts.map +1 -1
- package/dist/workspace/trust/run-commits.js +2 -1
- package/dist/workspace/trust/seal.d.ts.map +1 -1
- package/dist/workspace/trust/seal.js +34 -3
- package/dist/workspace/trust/trust-files.d.ts.map +1 -1
- package/dist/workspace/trust/trust-files.js +6 -1
- package/package.json +2 -1
- package/src/cli/handlers/approve-external-signature.test.ts +328 -0
- package/src/cli/handlers/approve.ts +206 -18
- package/src/cli/handlers/plan.ts +74 -0
- package/src/cli/handlers/pr.ts +2 -1
- package/src/cli/handlers/release.ts +84 -20
- package/src/cli/main.ts +48 -7
- package/src/cli/mcp/workspace-tools.ts +2 -1
- package/src/cli/parse-args.ts +33 -0
- package/src/cli/registry.ts +18 -2
- package/src/config.ts +8 -0
- package/src/gate-decision-bundle.test.ts +4 -2
- package/src/gate-decision.ts +11 -2
- package/src/gate-statement.schema.json +46 -0
- package/src/lifecycle/__fixtures__/publish-fakes.ts +125 -5
- package/src/lifecycle/gate-ledger.ts +34 -5
- package/src/lifecycle/gate-seal.ts +86 -15
- package/src/lifecycle/gate-statement.ts +144 -0
- package/src/lifecycle/gate-webauthn.test.ts +77 -0
- package/src/lifecycle/gate-webauthn.ts +133 -0
- package/src/lifecycle/git.ts +23 -19
- package/src/lifecycle/ledger-remote.test.ts +183 -0
- package/src/lifecycle/ledger-remote.ts +46 -0
- package/src/lifecycle/plan-stale.test.ts +149 -0
- package/src/lifecycle/plan-stale.ts +172 -0
- package/src/lifecycle/publish-credentials.ts +2 -0
- package/src/lifecycle/publish-declaration.ts +116 -1
- package/src/lifecycle/publish-forge.ts +14 -4
- package/src/lifecycle/publish-git-ssh.ts +202 -0
- package/src/lifecycle/publish-github.ts +174 -0
- package/src/lifecycle/publish-homebrew.test.ts +358 -0
- package/src/lifecycle/publish-homebrew.ts +283 -0
- package/src/lifecycle/publish-marketplace.test.ts +406 -0
- package/src/lifecycle/publish-marketplace.ts +421 -0
- package/src/lifecycle/publish-resume.test.ts +40 -0
- package/src/lifecycle/publish-target-context.test.ts +2 -2
- package/src/lifecycle/publish-targets.test.ts +23 -0
- package/src/lifecycle/publish-targets.ts +15 -0
- package/src/lifecycle/publish-tauri.test.ts +163 -0
- package/src/lifecycle/publish-tauri.ts +198 -0
- package/src/lifecycle/release-cut-lane.test.ts +177 -0
- package/src/lifecycle/release-cut-override.test.ts +213 -0
- package/src/lifecycle/release-cut.ts +198 -17
- package/src/lifecycle/release-publish.ts +23 -3
- package/src/lifecycle/release-revocation.ts +5 -4
- package/src/lifecycle/release-store.ts +44 -0
- package/src/lifecycle/snapshot-store.ts +16 -9
- package/src/lifecycle/test-passkey.ts +55 -0
- package/src/notify-bundle.test.ts +3 -1
- package/src/notify.schema.json +12 -3
- package/src/notify.ts +20 -3
- package/src/op/gate.ts +2 -1
- package/src/override-gate-bundle.test.ts +2 -0
- package/src/pr-forge-bundle.test.ts +2 -2
- package/src/pr-forge.test.ts +38 -0
- package/src/pr-forge.ts +93 -11
- package/src/pr-requires.test.ts +9 -0
- package/src/release-floors.ts +1 -1
- package/src/stage-hooks-bundle.test.ts +2 -0
- package/src/wave-runtime-bundle.test.ts +3 -2
- package/src/workspace/ci-builds.ts +27 -0
- package/src/workspace/ci-green-stale.test.ts +193 -0
- package/src/workspace/ci-green.ts +137 -6
- package/src/workspace/ci-quarantine-report.test.ts +145 -0
- package/src/workspace/ci-quarantine-report.ts +234 -0
- package/src/workspace/ci-quarantine.test.ts +160 -0
- package/src/workspace/ci-quarantine.ts +188 -0
- package/src/workspace/ci-serve.ts +7 -4
- package/src/workspace/class-data.ts +5 -1
- package/src/workspace/declaration.schema.json +51 -3
- package/src/workspace/declaration.test.ts +1 -0
- package/src/workspace/declaration.ts +88 -5
- package/src/workspace/forge-work.test.ts +143 -0
- package/src/workspace/forge-work.ts +195 -0
- package/src/workspace/identity-gate-rule.ts +6 -8
- package/src/workspace/identity.ts +6 -3
- package/src/workspace/intent-cli.ts +24 -5
- package/src/workspace/intent-record.schema.json +32 -0
- package/src/workspace/intent-record.ts +3 -0
- package/src/workspace/intent.schema.json +32 -0
- package/src/workspace/intent.ts +9 -0
- package/src/workspace/kind-readers.ts +2 -1
- package/src/workspace/kinds.test.ts +13 -0
- package/src/workspace/kinds.ts +12 -3
- package/src/workspace/plan-stale.schema.json +286 -0
- package/src/workspace/release-candidates.schema.json +55 -2
- package/src/workspace/release-candidates.test.ts +77 -4
- package/src/workspace/release-candidates.ts +80 -23
- package/src/workspace/releases.schema.json +47 -0
- package/src/workspace/trust/gate-seal.test.ts +44 -0
- package/src/workspace/trust/policy.ts +24 -1
- package/src/workspace/trust/run-commits.ts +2 -1
- package/src/workspace/trust/seal.ts +35 -3
- package/src/workspace/trust/trust-files.test.ts +5 -0
- package/src/workspace/trust/trust-files.ts +15 -1
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #4676: the Homebrew tap target against a GitHub repository fake. The stage
|
|
3
|
+
* commits the rendered formula to a staging branch that brew does not read,
|
|
4
|
+
* and the tap branch moves only at the reveal. Nothing reaches a network:
|
|
5
|
+
* over SSH (#4699), git pushes to a bare repository through a fake ssh.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { afterAll, describe, expect, test } from "vitest";
|
|
9
|
+
import { createHash } from "node:crypto";
|
|
10
|
+
import { execFileSync } from "node:child_process";
|
|
11
|
+
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
12
|
+
import { tmpdir } from "node:os";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import { createRedactor } from "./publish-credentials";
|
|
15
|
+
import { PublishDeclarationSchema } from "./publish-declaration";
|
|
16
|
+
import { githubSshHost } from "./publish-git-ssh";
|
|
17
|
+
import { fillUrl, releaseVersion, renderFormula } from "./publish-homebrew";
|
|
18
|
+
import { createPublishTargets } from "./publish-targets";
|
|
19
|
+
import { PublishTargetError, type PublishContext } from "./release-publish";
|
|
20
|
+
import { makeRelease, type ReleaseArtifact } from "./release-store";
|
|
21
|
+
import { startFakeGitHubRepo } from "./__fixtures__/publish-fakes";
|
|
22
|
+
|
|
23
|
+
const dirs: string[] = [];
|
|
24
|
+
afterAll(() => {
|
|
25
|
+
for (const d of dirs) rmSync(d, { recursive: true, force: true });
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
function context(cwd: string, over: Partial<PublishContext> & { artifacts?: ReleaseArtifact[] } = {}): PublishContext & { lines: string[] } {
|
|
29
|
+
const env = over.env ?? { HOMEBREW_TAP_TOKEN: "ghp_taptokentap" };
|
|
30
|
+
const redactor = createRedactor(env);
|
|
31
|
+
const lines: string[] = [];
|
|
32
|
+
const release = makeRelease({ member: null, commit: "c0ffee".padEnd(40, "0"), artifactDigests: over.artifacts ?? [], targets: [{ env: "dev", params: [], templates: [] }] });
|
|
33
|
+
return { release, label: "v1.4.0", releaseTag: "release/v1.4.0", provenance: { oidc: false, ci: null, reason: "" }, cwd, tag: null, env, redactor, log: (l) => lines.push(redactor.redact(l)), ...over, lines };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function refused(p: Promise<unknown>): Promise<PublishTargetError> {
|
|
37
|
+
try {
|
|
38
|
+
await p;
|
|
39
|
+
} catch (err) {
|
|
40
|
+
if (err instanceof PublishTargetError) return err;
|
|
41
|
+
throw err;
|
|
42
|
+
}
|
|
43
|
+
throw new Error("expected a PublishTargetError");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const sha = (text: string) => createHash("sha256").update(text).digest("hex");
|
|
47
|
+
const API = "https://api.github.test";
|
|
48
|
+
const FILES = { "arugula-darwin-arm64.tar.gz": "mac arm", "arugula-darwin-amd64.tar.gz": "mac intel", "arugula-linux-arm64.tar.gz": "linux arm", "arugula-linux-amd64.tar.gz": "linux intel" };
|
|
49
|
+
const TEMPLATE = `class Arugula < Formula
|
|
50
|
+
desc "Arugula daemon"
|
|
51
|
+
homepage "https://arugula.example"
|
|
52
|
+
version "{{version}}"
|
|
53
|
+
|
|
54
|
+
on_macos do
|
|
55
|
+
on_arm do
|
|
56
|
+
url "{{url:arugula-darwin-arm64.tar.gz}}"
|
|
57
|
+
sha256 "{{sha256:arugula-darwin-arm64.tar.gz}}"
|
|
58
|
+
end
|
|
59
|
+
on_intel do
|
|
60
|
+
url "{{url:arugula-darwin-amd64.tar.gz}}"
|
|
61
|
+
sha256 "{{sha256:arugula-darwin-amd64.tar.gz}}"
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
on_linux do
|
|
66
|
+
on_arm do
|
|
67
|
+
url "{{url:arugula-linux-arm64.tar.gz}}"
|
|
68
|
+
sha256 "{{sha256:arugula-linux-arm64.tar.gz}}"
|
|
69
|
+
end
|
|
70
|
+
on_intel do
|
|
71
|
+
url "{{url:arugula-linux-amd64.tar.gz}}"
|
|
72
|
+
sha256 "{{sha256:arugula-linux-amd64.tar.gz}}"
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def install
|
|
77
|
+
bin.install "arugula"
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
`;
|
|
81
|
+
|
|
82
|
+
function member(template = TEMPLATE): string {
|
|
83
|
+
const dir = mkdtempSync(join(tmpdir(), "chant-publish-homebrew-"));
|
|
84
|
+
dirs.push(dir);
|
|
85
|
+
mkdirSync(join(dir, "dist"));
|
|
86
|
+
mkdirSync(join(dir, "packaging"));
|
|
87
|
+
for (const [name, bytes] of Object.entries(FILES)) writeFileSync(join(dir, "dist", name), bytes);
|
|
88
|
+
writeFileSync(join(dir, "packaging", "arugula.rb"), template);
|
|
89
|
+
return dir;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const declaration = (over: Record<string, unknown> = {}) =>
|
|
93
|
+
PublishDeclarationSchema.parse({
|
|
94
|
+
type: "homebrew",
|
|
95
|
+
tap: "arugula-salad/homebrew-tap",
|
|
96
|
+
formula: "Formula/arugula.rb",
|
|
97
|
+
template: "packaging/arugula.rb",
|
|
98
|
+
assets: Object.keys(FILES).map((n) => `dist/${n}`),
|
|
99
|
+
url: "https://github.com/arugula-salad/arugula/releases/download/{tag}/{name}",
|
|
100
|
+
api: API,
|
|
101
|
+
...over,
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe("homebrew tap (#4676)", () => {
|
|
105
|
+
test("renders the formula per platform, commits it to a staging branch brew does not read, and fast-forwards the tap branch only at the reveal", async () => {
|
|
106
|
+
const dir = member();
|
|
107
|
+
const tap = startFakeGitHubRepo(API, "arugula-salad/homebrew-tap", "main", { "Formula/arugula.rb": "old formula\n", "README.md": "tap\n" });
|
|
108
|
+
const [target] = await createPublishTargets([declaration()], { fetch: tap.fetch });
|
|
109
|
+
const ctx = context(dir);
|
|
110
|
+
const formula = TEMPLATE.replace("{{version}}", "1.4.0").replace(/\{\{(url|sha256):([^}]+)\}\}/g, (_, k: string, name: string) =>
|
|
111
|
+
k === "url" ? `https://github.com/arugula-salad/arugula/releases/download/release/v1.4.0/${name}` : sha(FILES[name as keyof typeof FILES]),
|
|
112
|
+
);
|
|
113
|
+
const digest = `sha256:${sha(formula)}`;
|
|
114
|
+
expect(await target!.plan(ctx)).toEqual([{ name: "Formula/arugula.rb", destination: "arugula-salad/homebrew-tap@the default branch:Formula/arugula.rb", digest }]);
|
|
115
|
+
expect(tap.calls).toEqual([]);
|
|
116
|
+
|
|
117
|
+
const main = tap.branches.get("main");
|
|
118
|
+
const staged = await target!.stage(ctx);
|
|
119
|
+
expect(staged.items).toEqual([{ name: "Formula/arugula.rb", destination: "arugula-salad/homebrew-tap@main:Formula/arugula.rb", digest }]);
|
|
120
|
+
expect(staged.state).toMatchObject({ branch: "main", staging: "chant-publish/arugula-1.4.0" });
|
|
121
|
+
expect(tap.branches.get("main")).toBe(main);
|
|
122
|
+
expect(tap.read("main", "Formula/arugula.rb")).toBe("old formula\n");
|
|
123
|
+
expect(tap.read("chant-publish/arugula-1.4.0", "Formula/arugula.rb")).toBe(formula);
|
|
124
|
+
expect(tap.calls[0]!.headers).toMatchObject({ authorization: "Bearer ghp_taptokentap" });
|
|
125
|
+
|
|
126
|
+
// A stage run again finds the staging branch holding the formula and commits nothing.
|
|
127
|
+
const n = tap.calls.length;
|
|
128
|
+
const again = await target!.stage(ctx);
|
|
129
|
+
expect(again.state).toEqual(staged.state);
|
|
130
|
+
expect(tap.calls.slice(n).map((c) => c.method)).toEqual(["GET", "GET", "GET", "GET", "GET"]);
|
|
131
|
+
|
|
132
|
+
await target!.reveal(ctx, staged);
|
|
133
|
+
expect(tap.read("main", "Formula/arugula.rb")).toBe(formula);
|
|
134
|
+
expect(tap.read("main", "README.md")).toBe("tap\n");
|
|
135
|
+
expect(tap.log("main")).toEqual(["init", "arugula 1.4.0"]);
|
|
136
|
+
expect(tap.branches.has("chant-publish/arugula-1.4.0")).toBe(false);
|
|
137
|
+
expect(ctx.lines.join("\n")).not.toContain("ghp_taptokentap");
|
|
138
|
+
|
|
139
|
+
// Revealed: a stage finds the tap holding the formula and stages nothing.
|
|
140
|
+
const done = await target!.stage(ctx);
|
|
141
|
+
expect(done.state).toEqual({ branch: "main", present: "true" });
|
|
142
|
+
expect(done.items[0]!.present).toBe(true);
|
|
143
|
+
expect(await target!.reveal(ctx, done)).toMatchObject({ items: [{ present: true }] });
|
|
144
|
+
expect(tap.log("main")).toEqual(["init", "arugula 1.4.0"]);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test("a tap branch that moved since the stage gets the formula as one commit on top", async () => {
|
|
148
|
+
const dir = member();
|
|
149
|
+
const tap = startFakeGitHubRepo(API, "arugula-salad/homebrew-tap", "main", { "Formula/arugula.rb": "old formula\n" });
|
|
150
|
+
const [target] = await createPublishTargets([declaration({ branch: "main" })], { fetch: tap.fetch });
|
|
151
|
+
const ctx = context(dir);
|
|
152
|
+
const staged = await target!.stage(ctx);
|
|
153
|
+
tap.push("main", { "Formula/other.rb": "other\n" });
|
|
154
|
+
await target!.reveal(ctx, staged);
|
|
155
|
+
expect(tap.log("main")).toEqual(["init", "someone else", "arugula 1.4.0"]);
|
|
156
|
+
expect(tap.read("main", "Formula/other.rb")).toBe("other\n");
|
|
157
|
+
expect(tap.read("main", "Formula/arugula.rb")).toContain('version "1.4.0"');
|
|
158
|
+
expect(tap.branches.has("chant-publish/arugula-1.4.0")).toBe(false);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test("refuses an asset that is not the release's, a placeholder it can't fill, a release with no version, and a missing token, before any request", async () => {
|
|
162
|
+
const tap = startFakeGitHubRepo(API, "arugula-salad/homebrew-tap", "main", {});
|
|
163
|
+
const make = async (over: Record<string, unknown> = {}) => (await createPublishTargets([declaration(over)], { fetch: tap.fetch }))[0]!;
|
|
164
|
+
const artifacts: ReleaseArtifact[] = [{ component: "daemon", kind: "asset", path: "dist/arugula-linux-amd64.tar.gz", digest: `sha256:${"0".repeat(64)}` }];
|
|
165
|
+
expect((await refused((await make()).plan(context(member(), { artifacts })))).code).toBe("publish-artifact-digest-mismatch");
|
|
166
|
+
expect((await refused((await make()).plan(context(member(TEMPLATE.replace("{{version}}", "{{revision}}")))))).code).toBe("publish-homebrew-template");
|
|
167
|
+
expect((await refused((await make()).plan(context(member(`url "{{url:arugula-windows.zip}}"`))))).message).toContain("no declared asset is named arugula-windows.zip");
|
|
168
|
+
expect((await refused((await make()).plan(context(member(), { label: null, releaseTag: null })))).code).toBe("publish-homebrew-no-version");
|
|
169
|
+
expect((await refused((await make({ tokenEnv: "TAP_PAT" })).plan(context(member())))).code).toBe("publish-credentials-missing");
|
|
170
|
+
expect(tap.calls).toEqual([]);
|
|
171
|
+
expect(() => declaration({ tap: "no-slash" })).toThrow();
|
|
172
|
+
expect(() => declaration({ token: "ghp_x" })).toThrow();
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
test("the version, URL and template helpers", () => {
|
|
176
|
+
expect(releaseVersion("v1.4.0")).toBe("1.4.0");
|
|
177
|
+
expect(releaseVersion("app-v2.0.0-rc.1")).toBe("2.0.0-rc.1");
|
|
178
|
+
expect(releaseVersion("latest")).toBeNull();
|
|
179
|
+
expect(fillUrl("https://x.test/{tag}/{name}?v={version}", { tag: "release/v1 0", label: "v1", version: "1", name: "a b.tgz" })).toBe("https://x.test/release/v1%200/a%20b.tgz?v=1");
|
|
180
|
+
expect(renderFormula('version "{{ version }}" # #{bin}', { version: "1.0", label: "v1.0", tag: "t" }, new Map())).toBe('version "1.0" # #{bin}');
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
describe("homebrew tap over SSH with a deploy key (#4699)", () => {
|
|
185
|
+
const KEY = "-----BEGIN OPENSSH PRIVATE KEY-----\nb3BlbnNzaC1rZXktdjEAAAAAfakedeploykeymaterial\n-----END OPENSSH PRIVATE KEY-----\n";
|
|
186
|
+
const HOST_KEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl";
|
|
187
|
+
const git = (cwd: string, args: string[]) =>
|
|
188
|
+
execFileSync("git", ["-c", "user.name=t", "-c", "user.email=t@example.com", "-c", "commit.gpgsign=false", ...args], { cwd, encoding: "utf-8", stdio: ["ignore", "pipe", "pipe"] }).trim();
|
|
189
|
+
|
|
190
|
+
/** A bare tap under `root`, as `git@github.com:<repo>.git` reaches it through the fake ssh, with one commit on main. */
|
|
191
|
+
function bareTap(files: Record<string, string>) {
|
|
192
|
+
const root = mkdtempSync(join(tmpdir(), "chant-tap-ssh-"));
|
|
193
|
+
dirs.push(root);
|
|
194
|
+
const bare = join(root, "arugula-salad", "homebrew-tap.git");
|
|
195
|
+
mkdirSync(bare, { recursive: true });
|
|
196
|
+
git(bare, ["init", "-q", "--bare", "-b", "main"]);
|
|
197
|
+
const work = join(root, "work");
|
|
198
|
+
mkdirSync(work);
|
|
199
|
+
git(work, ["init", "-q", "-b", "main"]);
|
|
200
|
+
const commit = (message: string, more: Record<string, string>) => {
|
|
201
|
+
for (const [p, text] of Object.entries(more)) {
|
|
202
|
+
mkdirSync(join(work, p, ".."), { recursive: true });
|
|
203
|
+
writeFileSync(join(work, p), text);
|
|
204
|
+
}
|
|
205
|
+
git(work, ["add", "-A"]);
|
|
206
|
+
git(work, ["commit", "-q", "-m", message]);
|
|
207
|
+
git(work, ["push", "-q", bare, "HEAD:refs/heads/main"]);
|
|
208
|
+
};
|
|
209
|
+
commit("init", files);
|
|
210
|
+
return {
|
|
211
|
+
root,
|
|
212
|
+
bare,
|
|
213
|
+
push: (more: Record<string, string>) => commit("someone else", more),
|
|
214
|
+
log: (branch: string) => git(bare, ["log", "--format=%s", branch]).split("\n").reverse(),
|
|
215
|
+
read: (branch: string, path: string) => git(bare, ["show", `${branch}:${path}`]) + "\n",
|
|
216
|
+
head: (branch: string) => git(bare, ["rev-parse", branch]),
|
|
217
|
+
branches: () => git(bare, ["for-each-ref", "--format=%(refname:short)", "refs/heads"]).split("\n").filter(Boolean),
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* An ssh that serves the bare taps under `root` when it is handed `KEY`
|
|
223
|
+
* and a known-hosts file holding `HOST_KEY` for the host, and logs each
|
|
224
|
+
* call's host and options.
|
|
225
|
+
*/
|
|
226
|
+
function fakeSsh(root: string) {
|
|
227
|
+
const bin = join(root, "ssh");
|
|
228
|
+
const log = join(root, "ssh.log");
|
|
229
|
+
writeFileSync(
|
|
230
|
+
bin,
|
|
231
|
+
`#!${process.execPath}
|
|
232
|
+
const fs = require("fs");
|
|
233
|
+
const { spawnSync } = require("child_process");
|
|
234
|
+
const args = process.argv.slice(2);
|
|
235
|
+
const opts = [];
|
|
236
|
+
let key = "";
|
|
237
|
+
for (let i = 0; i < args.length - 2; i++) {
|
|
238
|
+
if (args[i] === "-o") opts.push(args[++i]);
|
|
239
|
+
else if (args[i] === "-i") key = fs.readFileSync(args[++i], "utf-8");
|
|
240
|
+
else if (args[i] === "-F") i++;
|
|
241
|
+
}
|
|
242
|
+
const host = args[args.length - 2].replace(/^git@/, "");
|
|
243
|
+
const known = opts.find((o) => o.startsWith("UserKnownHostsFile="));
|
|
244
|
+
const hosts = known ? fs.readFileSync(known.slice("UserKnownHostsFile=".length), "utf-8") : "";
|
|
245
|
+
fs.appendFileSync(${JSON.stringify(log)}, JSON.stringify({ host, opts, mode: (fs.statSync(args[args.indexOf("-i") + 1]).mode & 0o777).toString(8) }) + "\\n");
|
|
246
|
+
if (!opts.includes("StrictHostKeyChecking=yes") || !hosts.split("\\n").includes(host + " " + ${JSON.stringify(HOST_KEY)})) { console.error("Host key verification failed."); process.exit(255); }
|
|
247
|
+
if (key !== ${JSON.stringify(KEY)}) { console.error("git@" + host + ": Permission denied (publickey)."); process.exit(255); }
|
|
248
|
+
const cmd = args[args.length - 1].replace(/'([^']+\\.git)'/, (_, p) => "'" + ${JSON.stringify(root)} + "/" + p + "'");
|
|
249
|
+
const r = spawnSync("sh", ["-c", cmd], { stdio: "inherit" });
|
|
250
|
+
process.exit(r.status === null ? 1 : r.status);
|
|
251
|
+
`,
|
|
252
|
+
);
|
|
253
|
+
chmodSync(bin, 0o755);
|
|
254
|
+
const calls = () => (existsSync(log) ? readFileSync(log, "utf-8").trim().split("\n").filter(Boolean).map((l) => JSON.parse(l) as { host: string; opts: string[]; mode: string }) : []);
|
|
255
|
+
return { bin, calls };
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** The API's /meta, which lists the host keys; nothing else is asked of it. */
|
|
259
|
+
function fakeMeta(keys: string[] = [HOST_KEY]) {
|
|
260
|
+
const calls: Array<{ url: string; headers: Record<string, string> }> = [];
|
|
261
|
+
const fetch = async (url: string, init: { method: string; headers: Record<string, string> }) => {
|
|
262
|
+
calls.push({ url, headers: init.headers });
|
|
263
|
+
const body = url === `${API}/meta` ? JSON.stringify({ ssh_keys: keys }) : "";
|
|
264
|
+
return { ok: body !== "", status: body ? 200 : 404, headers: { get: () => null }, text: async () => body };
|
|
265
|
+
};
|
|
266
|
+
return { fetch, calls };
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const sshCtx = (dir: string, env: NodeJS.ProcessEnv = { HOMEBREW_TAP_KEY: KEY, PATH: process.env.PATH }) => context(dir, { env });
|
|
270
|
+
|
|
271
|
+
test("stages the formula on a branch over SSH, and fast-forwards the tap branch at the reveal, with no token", async () => {
|
|
272
|
+
const dir = member();
|
|
273
|
+
const tap = bareTap({ "Formula/arugula.rb": "old formula\n", "README.md": "tap\n" });
|
|
274
|
+
const ssh = fakeSsh(tap.root);
|
|
275
|
+
const meta = fakeMeta();
|
|
276
|
+
const [target] = await createPublishTargets([declaration({ sshKeyEnv: "HOMEBREW_TAP_KEY" })], { fetch: meta.fetch, ssh: ssh.bin });
|
|
277
|
+
expect(target!.credentialEnv).toEqual(["HOMEBREW_TAP_KEY"]);
|
|
278
|
+
const ctx = sshCtx(dir);
|
|
279
|
+
await target!.plan(ctx);
|
|
280
|
+
expect(ssh.calls()).toEqual([]);
|
|
281
|
+
|
|
282
|
+
const main = tap.head("main");
|
|
283
|
+
const staged = await target!.stage(ctx);
|
|
284
|
+
expect(staged.state).toMatchObject({ branch: "main", staging: "chant-publish/arugula-1.4.0" });
|
|
285
|
+
expect(staged.state.commit).toBe(tap.head("chant-publish/arugula-1.4.0"));
|
|
286
|
+
expect(tap.head("main")).toBe(main);
|
|
287
|
+
expect(tap.read("main", "Formula/arugula.rb")).toBe("old formula\n");
|
|
288
|
+
expect(tap.read("chant-publish/arugula-1.4.0", "Formula/arugula.rb")).toContain('version "1.4.0"');
|
|
289
|
+
expect(new Set(ssh.calls().map((c) => c.host))).toEqual(new Set(["api.github.test"]));
|
|
290
|
+
expect(ssh.calls()[0]!.opts).toEqual(expect.arrayContaining(["IdentitiesOnly=yes", "StrictHostKeyChecking=yes", "BatchMode=yes"]));
|
|
291
|
+
expect(ssh.calls()[0]!.mode).toBe("600");
|
|
292
|
+
// The API is asked for the host keys only, without a token.
|
|
293
|
+
expect(meta.calls.map((c) => c.url)).toEqual([`${API}/meta`]);
|
|
294
|
+
expect(meta.calls[0]!.headers.authorization).toBeUndefined();
|
|
295
|
+
|
|
296
|
+
// A stage run again finds the staging branch holding the formula and pushes nothing.
|
|
297
|
+
const again = await target!.stage(ctx);
|
|
298
|
+
expect(again.state).toEqual(staged.state);
|
|
299
|
+
expect(tap.log("chant-publish/arugula-1.4.0")).toEqual(["init", "arugula 1.4.0"]);
|
|
300
|
+
|
|
301
|
+
// The reveal is a fast-forward to the staged commit, from a session of its own, as on a fresh clone.
|
|
302
|
+
await target!.reveal(ctx, staged);
|
|
303
|
+
expect(tap.head("main")).toBe(staged.state.commit);
|
|
304
|
+
expect(tap.log("main")).toEqual(["init", "arugula 1.4.0"]);
|
|
305
|
+
expect(tap.read("main", "README.md")).toBe("tap\n");
|
|
306
|
+
expect(tap.branches()).toEqual(["main"]);
|
|
307
|
+
expect(ctx.lines.join("\n")).not.toContain("fakedeploykeymaterial");
|
|
308
|
+
|
|
309
|
+
const done = await target!.stage(ctx);
|
|
310
|
+
expect(done.state).toEqual({ branch: "main", present: "true" });
|
|
311
|
+
expect(await target!.reveal(ctx, done)).toMatchObject({ items: [{ present: true }] });
|
|
312
|
+
expect(tap.log("main")).toEqual(["init", "arugula 1.4.0"]);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
test("a tap branch that moved since the stage gets the formula as one commit on top", async () => {
|
|
316
|
+
const dir = member();
|
|
317
|
+
const tap = bareTap({ "Formula/arugula.rb": "old formula\n" });
|
|
318
|
+
const [target] = await createPublishTargets([declaration({ sshKeyEnv: "HOMEBREW_TAP_KEY", branch: "main" })], { fetch: fakeMeta().fetch, ssh: fakeSsh(tap.root).bin });
|
|
319
|
+
const ctx = sshCtx(dir);
|
|
320
|
+
const staged = await target!.stage(ctx);
|
|
321
|
+
tap.push({ "Formula/other.rb": "other\n" });
|
|
322
|
+
await target!.reveal(ctx, staged);
|
|
323
|
+
expect(tap.log("main")).toEqual(["init", "someone else", "arugula 1.4.0"]);
|
|
324
|
+
expect(tap.read("main", "Formula/other.rb")).toBe("other\n");
|
|
325
|
+
expect(tap.read("main", "Formula/arugula.rb")).toContain('version "1.4.0"');
|
|
326
|
+
expect(tap.branches()).toEqual(["main"]);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
test("refuses a missing key, a key beside a token, a key the remote refuses, and a host whose key the API doesn't list", async () => {
|
|
330
|
+
const dir = member();
|
|
331
|
+
const tap = bareTap({ "Formula/arugula.rb": "old formula\n" });
|
|
332
|
+
const ssh = fakeSsh(tap.root);
|
|
333
|
+
const make = async (fetch = fakeMeta().fetch) => (await createPublishTargets([declaration({ sshKeyEnv: "HOMEBREW_TAP_KEY" })], { fetch, ssh: ssh.bin }))[0]!;
|
|
334
|
+
|
|
335
|
+
const missing = await refused((await make()).plan(sshCtx(dir, { HOMEBREW_TAP_TOKEN: "ghp_not_read" })));
|
|
336
|
+
expect(missing.code).toBe("publish-credentials-missing");
|
|
337
|
+
expect(missing.message).toContain("SSH key from HOMEBREW_TAP_KEY");
|
|
338
|
+
|
|
339
|
+
expect((await refused(createPublishTargets([declaration({ sshKeyEnv: "HOMEBREW_TAP_KEY", tokenEnv: "HOMEBREW_TAP_TOKEN" })]))).code).toBe("publish-homebrew-auth-invalid");
|
|
340
|
+
|
|
341
|
+
const wrongKey = await refused((await make()).stage(sshCtx(dir, { HOMEBREW_TAP_KEY: KEY.replace("fake", "other"), PATH: process.env.PATH })));
|
|
342
|
+
expect(wrongKey.code).toBe("publish-homebrew-failed");
|
|
343
|
+
expect(wrongKey.message).toContain("Permission denied (publickey)");
|
|
344
|
+
expect(wrongKey.message).not.toContain("otherdeploykeymaterial");
|
|
345
|
+
|
|
346
|
+
const otherHost = await refused((await make(fakeMeta(["ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIsomeoneelse"]).fetch)).stage(sshCtx(dir)));
|
|
347
|
+
expect(otherHost.message).toContain("Host key verification failed");
|
|
348
|
+
|
|
349
|
+
const noMeta = await refused((await make(fakeMeta([]).fetch)).stage(sshCtx(dir)));
|
|
350
|
+
expect(noMeta.message).toContain("gave no SSH host keys");
|
|
351
|
+
expect(tap.branches()).toEqual(["main"]);
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
test("the SSH host is github.com for api.github.com and the API's own host for GitHub Enterprise Server", () => {
|
|
355
|
+
expect(githubSshHost("https://api.github.com")).toBe("github.com");
|
|
356
|
+
expect(githubSshHost("https://ghe.acme.test/api/v3")).toBe("ghe.acme.test");
|
|
357
|
+
});
|
|
358
|
+
});
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Homebrew tap target (#4676): a formula in a tap repository on GitHub,
|
|
3
|
+
* rendered from a template in the member with the release's version and each
|
|
4
|
+
* asset's URL and SHA-256, and committed through GitHub's REST API with the
|
|
5
|
+
* token from `tokenEnv` (default `HOMEBREW_TAP_TOKEN`).
|
|
6
|
+
*
|
|
7
|
+
* The template is the formula as the tap holds it, with placeholders where a
|
|
8
|
+
* release changes it:
|
|
9
|
+
*
|
|
10
|
+
* - `{{version}}`: the release's version, its label from the first digit on
|
|
11
|
+
* (`v1.4.0` and `app-v1.4.0` are both `1.4.0`);
|
|
12
|
+
* - `{{url:<name>}}` and `{{sha256:<name>}}`: the URL and the SHA-256 in hex
|
|
13
|
+
* of the declared asset whose file name is `<name>`;
|
|
14
|
+
* - `{{label}}` and `{{tag}}`: the release's label and the tag the cut made.
|
|
15
|
+
*
|
|
16
|
+
* A placeholder chant does not know, or an asset name no declared asset has,
|
|
17
|
+
* is refused (`publish-homebrew-template`). Everything else is copied as it
|
|
18
|
+
* is, so `on_macos`, `on_linux`, `on_arm` and `on_intel` blocks each name
|
|
19
|
+
* their own asset and the formula's Ruby stays the member's. Each SHA-256 is
|
|
20
|
+
* of the file in the checkout, which must match the digest the release
|
|
21
|
+
* records for it.
|
|
22
|
+
*
|
|
23
|
+
* A tap is a branch people install from, with no draft, so the two steps are
|
|
24
|
+
* a branch and a fast-forward (#4048):
|
|
25
|
+
*
|
|
26
|
+
* - The stage commits the rendered formula to `chant-publish/<formula>-<version>`,
|
|
27
|
+
* a branch it makes at the tap branch's head (or moves there, when an
|
|
28
|
+
* earlier stage left it elsewhere). `brew` reads only the tap branch, so
|
|
29
|
+
* nothing is visible. A tap branch that already holds the formula as
|
|
30
|
+
* rendered stages nothing, and a staging branch that already holds it is
|
|
31
|
+
* not committed to again.
|
|
32
|
+
* - The reveal fast-forwards the tap branch to the staged commit, then
|
|
33
|
+
* deletes the staging branch. When the tap branch moved since the stage,
|
|
34
|
+
* the reveal commits the formula onto it instead, one commit either way,
|
|
35
|
+
* unless it already holds the formula as rendered.
|
|
36
|
+
*
|
|
37
|
+
* With `sshKeyEnv` (#4699), the same two steps run with git over SSH and the
|
|
38
|
+
* private key in that variable, such as a deploy key limited to the tap, in
|
|
39
|
+
* place of the API and a token (./publish-git-ssh.ts). The stage fetches the
|
|
40
|
+
* tap branch, commits the formula on its head and force-pushes that commit
|
|
41
|
+
* to the staging branch. The reveal fetches the staging branch and pushes
|
|
42
|
+
* its commit to the tap branch without force, which the remote takes only
|
|
43
|
+
* as a fast-forward; when the tap branch moved, it commits the formula onto
|
|
44
|
+
* the new head instead and pushes that, then deletes the staging branch.
|
|
45
|
+
*
|
|
46
|
+
* The asset URLs are the declaration's `url` pattern, so the files must be
|
|
47
|
+
* downloadable there by the time the formula is: declare the `forge` target
|
|
48
|
+
* that attaches them before this one, and its reveal runs first.
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
import { readFileSync } from "node:fs";
|
|
52
|
+
import { basename } from "node:path";
|
|
53
|
+
import { readCredential, type Credential } from "./publish-credentials";
|
|
54
|
+
import type { HomebrewPublishDeclaration } from "./publish-declaration";
|
|
55
|
+
import { SshGitRepo } from "./publish-git-ssh";
|
|
56
|
+
import { GitHubRepo, githubApi } from "./publish-github";
|
|
57
|
+
import { bytesDigest, checkReleaseDigest, memberFile, publishTargetKey } from "./publish-targets";
|
|
58
|
+
import { PublishTargetError, type PublishContext, type PublishDeps, type PublishFetch, type PublishItem, type PublishTarget } from "./release-publish";
|
|
59
|
+
import { NEXT_RELEASE } from "../release-floors";
|
|
60
|
+
|
|
61
|
+
/** The first chant that publishes a `homebrew` target (#4676). */
|
|
62
|
+
export const PUBLISH_HOMEBREW_FLOOR = "0.124.63";
|
|
63
|
+
|
|
64
|
+
/** The first chant that reads a homebrew target's `sshKeyEnv` (#4699). */
|
|
65
|
+
export const PUBLISH_HOMEBREW_SSH_KEY_FLOOR = NEXT_RELEASE;
|
|
66
|
+
|
|
67
|
+
/** The variable the tap token is read from when the declaration names none. */
|
|
68
|
+
export const HOMEBREW_TOKEN_ENV = "HOMEBREW_TAP_TOKEN";
|
|
69
|
+
|
|
70
|
+
/** A release's version for a package manager: its label from the first digit on, or null when the label has no digit. */
|
|
71
|
+
export function releaseVersion(label: string | null): string | null {
|
|
72
|
+
const at = label?.search(/\d/) ?? -1;
|
|
73
|
+
return label && at >= 0 ? label.slice(at) : null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** `pattern` with `{tag}`, `{label}`, `{version}` and `{name}` filled in, each URL-encoded except the tag's slashes. */
|
|
77
|
+
export function fillUrl(pattern: string, values: { tag: string; label: string; version: string; name: string }): string {
|
|
78
|
+
return pattern.replace(/\{(tag|label|version|name)\}/g, (_, k: keyof typeof values) =>
|
|
79
|
+
k === "tag" ? values.tag.split("/").map(encodeURIComponent).join("/") : encodeURIComponent(values[k]),
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Render a formula template, or name what it can't fill. */
|
|
84
|
+
export function renderFormula(
|
|
85
|
+
template: string,
|
|
86
|
+
values: { version: string; label: string; tag: string },
|
|
87
|
+
assets: ReadonlyMap<string, { url: string; sha256: string }>,
|
|
88
|
+
): string {
|
|
89
|
+
return template.replace(/\{\{\s*([^}]*?)\s*\}\}/g, (whole, inner: string) => {
|
|
90
|
+
if (inner === "version" || inner === "label" || inner === "tag") return values[inner];
|
|
91
|
+
const m = /^(url|sha256):(.+)$/.exec(inner);
|
|
92
|
+
const asset = m ? assets.get(m[2]!.trim()) : undefined;
|
|
93
|
+
if (m && asset) return m[1] === "url" ? asset.url : asset.sha256;
|
|
94
|
+
throw new PublishTargetError(
|
|
95
|
+
"publish-homebrew-template",
|
|
96
|
+
m ? `the formula template names ${whole}, and no declared asset is named ${m[2]!.trim()}` : `the formula template has ${whole}, which chant does not fill`,
|
|
97
|
+
`The placeholders are {{version}}, {{label}}, {{tag}}, and {{url:<name>}} and {{sha256:<name>}} for an asset in assets: ${[...assets.keys()].join(", ")}.`,
|
|
98
|
+
);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function homebrewTarget(decl: HomebrewPublishDeclaration, deps: PublishDeps): PublishTarget {
|
|
103
|
+
const fetchImpl: PublishFetch = deps.fetch ?? ((url, init) => fetch(url, init as RequestInit));
|
|
104
|
+
if (decl.sshKeyEnv && decl.tokenEnv) {
|
|
105
|
+
throw new PublishTargetError("publish-homebrew-auth-invalid", `the homebrew target for ${decl.tap} names both tokenEnv and sshKeyEnv`, "Keep one: sshKeyEnv pushes with git over SSH, tokenEnv commits through the API.");
|
|
106
|
+
}
|
|
107
|
+
const ssh = decl.sshKeyEnv !== undefined;
|
|
108
|
+
const tokenNames = [decl.sshKeyEnv ?? decl.tokenEnv ?? HOMEBREW_TOKEN_ENV];
|
|
109
|
+
const formulaName = basename(decl.formula).replace(/\.rb$/, "");
|
|
110
|
+
const setup = (ctx: PublishContext) => {
|
|
111
|
+
const version = releaseVersion(ctx.label);
|
|
112
|
+
if (!version || !ctx.label || !ctx.releaseTag) {
|
|
113
|
+
throw new PublishTargetError(
|
|
114
|
+
"publish-homebrew-no-version",
|
|
115
|
+
`a formula's version is the release's label from its first digit, and release ${ctx.release.releaseDigest} has ${ctx.label ? `the label ${ctx.label}, which has no digit` : "no label"}`,
|
|
116
|
+
"Cut the release with a label such as v1.4.0.",
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
const token = readCredential(ctx.env, tokenNames, ctx.redactor);
|
|
120
|
+
if (!token) {
|
|
121
|
+
throw new PublishTargetError(
|
|
122
|
+
"publish-credentials-missing",
|
|
123
|
+
`the homebrew target reads its ${ssh ? "SSH key" : "token"} from ${tokenNames[0]}, and it is not set`,
|
|
124
|
+
ssh ? `Set ${tokenNames[0]} to a private key that may push to ${decl.tap}, such as a deploy key with write access.` : `Set ${tokenNames[0]} to a token that may push to ${decl.tap}.`,
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
const assets = new Map<string, { url: string; sha256: string }>();
|
|
128
|
+
for (const p of decl.assets) {
|
|
129
|
+
const abs = memberFile(ctx.cwd, p);
|
|
130
|
+
const digest = bytesDigest(new Uint8Array(readFileSync(abs)));
|
|
131
|
+
checkReleaseDigest(ctx.release, "asset", ctx.cwd, p, [digest]);
|
|
132
|
+
const name = basename(p);
|
|
133
|
+
if (assets.has(name)) throw new PublishTargetError("publish-homebrew-asset-name-taken", `two assets are named ${name}, and the template names an asset by its file name`);
|
|
134
|
+
const url = fillUrl(decl.url, { tag: ctx.releaseTag, label: ctx.label, version, name });
|
|
135
|
+
assets.set(name, { url, sha256: digest.slice("sha256:".length) });
|
|
136
|
+
}
|
|
137
|
+
const template = readFileSync(memberFile(ctx.cwd, decl.template), "utf-8");
|
|
138
|
+
const bytes = new Uint8Array(Buffer.from(renderFormula(template, { version, label: ctx.label, tag: ctx.releaseTag }, assets), "utf-8"));
|
|
139
|
+
return { version, token, assets, bytes, digest: bytesDigest(bytes) };
|
|
140
|
+
};
|
|
141
|
+
const where = (branch: string) => `${decl.tap}@${branch}:${decl.formula}`;
|
|
142
|
+
const target: PublishTarget = {
|
|
143
|
+
kind: "homebrew",
|
|
144
|
+
name: `homebrew formula ${formulaName} in ${decl.tap}`,
|
|
145
|
+
destination: `github ${decl.tap}`,
|
|
146
|
+
credentialEnv: tokenNames,
|
|
147
|
+
key: publishTargetKey(decl),
|
|
148
|
+
async plan(ctx) {
|
|
149
|
+
const { digest } = setup(ctx);
|
|
150
|
+
return [{ name: decl.formula, destination: where(decl.branch ?? "the default branch"), digest }];
|
|
151
|
+
},
|
|
152
|
+
async stage(ctx) {
|
|
153
|
+
const { version, token, assets, bytes, digest } = setup(ctx);
|
|
154
|
+
ctx.log(`using the ${ssh ? "SSH key" : "token"} from ${token.from}`);
|
|
155
|
+
for (const [name, a] of assets) ctx.log(`${name}: ${a.url} sha256 ${a.sha256}`);
|
|
156
|
+
if (ssh) return withSsh(ctx, token, (tap) => stageOverSsh(ctx, tap, version, bytes, digest));
|
|
157
|
+
const tap = new GitHubRepo(githubApi(decl.api, ctx.env), decl.tap, token, fetchImpl, ctx, "publish-homebrew-failed");
|
|
158
|
+
const branch = decl.branch ?? (await tap.defaultBranch());
|
|
159
|
+
const head = await tap.branchHead(branch);
|
|
160
|
+
if (!head) throw new PublishTargetError("publish-homebrew-failed", `${decl.tap} has no branch ${branch}`, "Set branch to the branch brew installs from.");
|
|
161
|
+
const item: PublishItem = { name: decl.formula, destination: where(branch), digest };
|
|
162
|
+
const done = (state: Record<string, string>, present: boolean) => ({ target: target.name, destination: target.destination, items: [{ ...item, ...(present ? { present: true } : {}) }], state: { branch, ...state } });
|
|
163
|
+
|
|
164
|
+
const onTap = await tap.file(decl.formula, branch);
|
|
165
|
+
if (onTap && onTap.bytes.equals(Buffer.from(bytes))) {
|
|
166
|
+
ctx.log(`${branch} already has ${decl.formula} for ${version}`);
|
|
167
|
+
return done({ present: "true" }, true);
|
|
168
|
+
}
|
|
169
|
+
const staging = `chant-publish/${formulaName}-${version}`;
|
|
170
|
+
const stagingHead = await tap.branchHead(staging);
|
|
171
|
+
if (stagingHead) {
|
|
172
|
+
const onStaging = await tap.file(decl.formula, staging);
|
|
173
|
+
if (onStaging && onStaging.bytes.equals(Buffer.from(bytes))) {
|
|
174
|
+
ctx.log(`${staging} already has ${decl.formula} for ${version}`);
|
|
175
|
+
return done({ staging, commit: stagingHead }, true);
|
|
176
|
+
}
|
|
177
|
+
await tap.moveRef(`heads/${staging}`, head, true);
|
|
178
|
+
} else {
|
|
179
|
+
await tap.createBranch(staging, head);
|
|
180
|
+
}
|
|
181
|
+
ctx.log(`committing ${decl.formula} for ${version} to ${staging}, at ${branch}'s head ${head.slice(0, 12)}`);
|
|
182
|
+
const commit = await tap.putFile(decl.formula, staging, bytes, `${formulaName} ${version}`, onTap?.sha ?? null);
|
|
183
|
+
return done({ staging, commit }, false);
|
|
184
|
+
},
|
|
185
|
+
async reveal(ctx, staged) {
|
|
186
|
+
const items = staged.items.map((i) => ({ name: i.name, destination: i.destination, ...(i.digest ? { digest: i.digest } : {}) }));
|
|
187
|
+
const published = { target: target.name, destination: target.destination, items };
|
|
188
|
+
if (staged.state.present === "true") return { ...published, items: items.map((i) => ({ ...i, present: true })) };
|
|
189
|
+
const { version, token, bytes } = setup(ctx);
|
|
190
|
+
const { branch, staging, commit } = staged.state;
|
|
191
|
+
if (!branch || !staging || !commit) throw new PublishTargetError("publish-homebrew-failed", `the staged formula for ${decl.tap} has no staging commit to reveal`);
|
|
192
|
+
if (ssh) {
|
|
193
|
+
await withSsh(ctx, token, (tap) => revealOverSsh(ctx, tap, { branch, staging, commit }, version, bytes));
|
|
194
|
+
return published;
|
|
195
|
+
}
|
|
196
|
+
const tap = new GitHubRepo(githubApi(decl.api, ctx.env), decl.tap, token, fetchImpl, ctx, "publish-homebrew-failed");
|
|
197
|
+
ctx.log(`fast-forwarding ${branch} to ${commit.slice(0, 12)}`);
|
|
198
|
+
if (!(await tap.moveRef(`heads/${branch}`, commit, false))) {
|
|
199
|
+
// The tap moved since the stage: one commit of the formula onto it, unless it already holds it.
|
|
200
|
+
const onTap = await tap.file(decl.formula, branch);
|
|
201
|
+
if (onTap && onTap.bytes.equals(Buffer.from(bytes))) {
|
|
202
|
+
ctx.log(`${branch} moved since the stage and already has ${decl.formula} for ${version}`);
|
|
203
|
+
} else {
|
|
204
|
+
ctx.log(`${branch} moved since the stage; committing ${decl.formula} for ${version} onto it`);
|
|
205
|
+
await tap.putFile(decl.formula, branch, bytes, `${formulaName} ${version}`, onTap?.sha ?? null);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
await tap.deleteRef(`heads/${staging}`);
|
|
209
|
+
return published;
|
|
210
|
+
},
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
async function withSsh<T>(ctx: PublishContext, key: Credential, step: (tap: SshGitRepo) => Promise<T>): Promise<T> {
|
|
214
|
+
const tap = await SshGitRepo.open({ api: githubApi(decl.api, ctx.env), repo: decl.tap, key, ctx, fetch: fetchImpl, code: "publish-homebrew-failed", git: deps.git, ssh: deps.ssh });
|
|
215
|
+
try {
|
|
216
|
+
return await step(tap);
|
|
217
|
+
} finally {
|
|
218
|
+
tap.close();
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async function stageOverSsh(ctx: PublishContext, tap: SshGitRepo, version: string, bytes: Uint8Array, digest: string) {
|
|
223
|
+
const { defaultBranch, heads } = await tap.heads();
|
|
224
|
+
const branch = decl.branch ?? defaultBranch;
|
|
225
|
+
const head = branch ? heads.get(branch) : undefined;
|
|
226
|
+
if (!branch || !head) throw new PublishTargetError("publish-homebrew-failed", `${tap.remote} has no branch ${branch ?? "HEAD points at"}`, "Set branch to the branch brew installs from.");
|
|
227
|
+
const item: PublishItem = { name: decl.formula, destination: where(branch), digest };
|
|
228
|
+
const done = (state: Record<string, string>, present: boolean) => ({ target: target.name, destination: target.destination, items: [{ ...item, ...(present ? { present: true } : {}) }], state: { branch, ...state } });
|
|
229
|
+
ctx.log(`reading ${branch} of ${tap.remote} over SSH`);
|
|
230
|
+
await tap.fetch(branch);
|
|
231
|
+
const onTap = await tap.file(head, decl.formula);
|
|
232
|
+
if (onTap && onTap.equals(Buffer.from(bytes))) {
|
|
233
|
+
ctx.log(`${branch} already has ${decl.formula} for ${version}`);
|
|
234
|
+
return done({ present: "true" }, true);
|
|
235
|
+
}
|
|
236
|
+
const staging = `chant-publish/${formulaName}-${version}`;
|
|
237
|
+
const stagingHead = heads.get(staging);
|
|
238
|
+
if (stagingHead) {
|
|
239
|
+
await tap.fetch(staging);
|
|
240
|
+
const onStaging = await tap.file(stagingHead, decl.formula);
|
|
241
|
+
if (onStaging && onStaging.equals(Buffer.from(bytes))) {
|
|
242
|
+
ctx.log(`${staging} already has ${decl.formula} for ${version}`);
|
|
243
|
+
return done({ staging, commit: stagingHead }, true);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
ctx.log(`committing ${decl.formula} for ${version} to ${staging}, at ${branch}'s head ${head.slice(0, 12)}, and pushing it over SSH`);
|
|
247
|
+
const commit = await tap.commitFile(head, decl.formula, bytes, `${formulaName} ${version}`);
|
|
248
|
+
await tap.push(commit, staging, true);
|
|
249
|
+
return done({ staging, commit }, false);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
async function revealOverSsh(ctx: PublishContext, tap: SshGitRepo, state: { branch: string; staging: string; commit: string }, version: string, bytes: Uint8Array): Promise<void> {
|
|
253
|
+
const { branch, staging, commit } = state;
|
|
254
|
+
const { heads } = await tap.heads();
|
|
255
|
+
// The staged commit is on the staging branch: fetch it, since this run may be a resume on a fresh clone.
|
|
256
|
+
// Depth 2: the staged commit and the head it was made on, so git sees the push as a fast-forward.
|
|
257
|
+
const stagingHead = heads.get(staging) === commit ? await tap.fetch(staging, 2) : null;
|
|
258
|
+
let moved = true;
|
|
259
|
+
if (stagingHead === commit) {
|
|
260
|
+
ctx.log(`fast-forwarding ${branch} to ${commit.slice(0, 12)} over SSH`);
|
|
261
|
+
moved = (await tap.push(commit, branch, false)) === "rejected";
|
|
262
|
+
} else {
|
|
263
|
+
ctx.log(`${staging} is not at the staged commit ${commit.slice(0, 12)} any more`);
|
|
264
|
+
}
|
|
265
|
+
if (moved) {
|
|
266
|
+
// The tap moved since the stage, or the staged commit is gone: one commit of the formula onto it, unless it already holds it.
|
|
267
|
+
const head = await tap.fetch(branch);
|
|
268
|
+
const onTap = await tap.file(head, decl.formula);
|
|
269
|
+
if (onTap && onTap.equals(Buffer.from(bytes))) {
|
|
270
|
+
ctx.log(`${branch} already has ${decl.formula} for ${version}`);
|
|
271
|
+
} else {
|
|
272
|
+
ctx.log(`committing ${decl.formula} for ${version} onto ${branch}'s head ${head.slice(0, 12)}`);
|
|
273
|
+
const onto = await tap.commitFile(head, decl.formula, bytes, `${formulaName} ${version}`);
|
|
274
|
+
if ((await tap.push(onto, branch, false)) === "rejected") {
|
|
275
|
+
throw new PublishTargetError("publish-homebrew-failed", `${branch} of ${tap.remote} moved again while ${decl.formula} was committed onto it`, "Run the same publish again: it commits the formula onto the branch's new head.");
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
await tap.deleteBranch(staging);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
return target;
|
|
283
|
+
}
|