| 1 | import { spawnSync } from "node:child_process"; |
| 2 | import { copyFileSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | import { tmpdir } from "node:os"; |
| 4 | import { join } from "node:path"; |
| 5 | import { afterEach, describe, expect, it } from "vitest"; |
| 6 | |
| 7 | /** |
| 8 | * The GitHub App of GitHub runners on deploy: render-config.mjs takes its IDs from the environment |
| 9 | * (the Deploy workflow maps the DIFFINERY_GITHUB_APP_* repository variables to them), so the |
| 10 | * repository's wrangler.jsonc names no instance's app. |
| 11 | */ |
| 12 | |
| 13 | const repo = new URL("../../../../", import.meta.url).pathname; |
| 14 | const APP = { GITHUB_APP_ID: "1234567", GITHUB_APP_SLUG: "diffinery-runners", GITHUB_APP_CLIENT_ID: "Iv23liAbCdEf", GITHUB_APP_URL: "https://ghe.example.com" }; |
| 15 | |
| 16 | let dir = ""; |
| 17 | afterEach(() => { |
| 18 | if (dir) rmSync(dir, { recursive: true, force: true }); |
| 19 | dir = ""; |
| 20 | }); |
| 21 | |
| 22 | function tree(wranglerVars: Record<string, string> = {}): void { |
| 23 | dir = mkdtempSync(join(tmpdir(), "github-app-")); |
| 24 | for (const d of ["scripts", "apps/diffinery"]) mkdirSync(join(dir, d), { recursive: true }); |
| 25 | for (const f of ["jsonc.mjs", "render-config.mjs"]) copyFileSync(join(repo, "scripts", f), join(dir, "scripts", f)); |
| 26 | const cfg = readFileSync(join(repo, "apps/diffinery/wrangler.jsonc"), "utf8"); |
| 27 | const vars = Object.entries(wranglerVars).map(([k, v]) => `\n ${JSON.stringify(k)}: ${JSON.stringify(v)},`).join(""); |
| 28 | writeFileSync(join(dir, "apps/diffinery/wrangler.jsonc"), cfg.replace(/"vars": \{/, `"vars": {${vars}`)); |
| 29 | } |
| 30 | |
| 31 | function render(env: Record<string, string> = {}) { |
| 32 | const r = spawnSync("node", [join(dir, "scripts/render-config.mjs"), "--d1", "db-id", "--public-url", "https://git.example.com"], { |
| 33 | cwd: dir, |
| 34 | encoding: "utf8", |
| 35 | env: { PATH: process.env.PATH, HOME: dir, ...env }, |
| 36 | }); |
| 37 | const cfg = r.status === 0 ? (JSON.parse(readFileSync(join(dir, "apps/diffinery/wrangler.deploy.jsonc"), "utf8").split("\n").slice(1).join("\n")) as { vars: Record<string, string> }) : null; |
| 38 | return { status: r.status, stderr: r.stderr, vars: cfg?.vars ?? null }; |
| 39 | } |
| 40 | |
| 41 | describe("scripts/render-config.mjs and the GitHub App", () => { |
| 42 | it("sets the app's IDs from the environment, trimmed", () => { |
| 43 | tree(); |
| 44 | const { vars } = render({ ...APP, GITHUB_APP_SLUG: " diffinery-runners\n" }); |
| 45 | expect(vars).toMatchObject(APP); |
| 46 | }); |
| 47 | |
| 48 | it("leaves them out without the environment: the repository's wrangler.jsonc has none, so the feature is off", () => { |
| 49 | tree(); |
| 50 | const { vars } = render({ GITHUB_APP_ID: "", GITHUB_APP_SLUG: "" }); |
| 51 | for (const key of Object.keys(APP)) expect(vars).not.toHaveProperty(key); |
| 52 | }); |
| 53 | |
| 54 | it("keeps a copy's own wrangler.jsonc values when the environment has none, and the environment wins over them", () => { |
| 55 | tree({ GITHUB_APP_ID: "42", GITHUB_APP_SLUG: "own-app" }); |
| 56 | expect(render().vars).toMatchObject({ GITHUB_APP_ID: "42", GITHUB_APP_SLUG: "own-app" }); |
| 57 | expect(render({ GITHUB_APP_ID: "7" }).vars).toMatchObject({ GITHUB_APP_ID: "7", GITHUB_APP_SLUG: "own-app" }); |
| 58 | }); |
| 59 | |
| 60 | it("refuses a value of the wrong shape without printing it", () => { |
| 61 | tree(); |
| 62 | // Put together at run time, so secret scanners (diffinery.com's push protection among them) don't take the fake key for a real one. |
| 63 | const pem = ["BEGIN", "END"].map((edge) => `-----${edge} RSA PRIVATE KEY-----`).join("\nMIIEsecret\n"); |
| 64 | for (const [key, value, message] of [ |
| 65 | ["GITHUB_APP_ID", pem, "GITHUB_APP_ID must be the App ID, a number"], |
| 66 | ["GITHUB_APP_SLUG", "Diffinery Runners", "GITHUB_APP_SLUG must be the slug"], |
| 67 | ["GITHUB_APP_CLIENT_ID", "Iv23li with space", "GITHUB_APP_CLIENT_ID must be the Client ID"], |
| 68 | ["GITHUB_APP_URL", "https://ghe.example.com/api/v3", "GITHUB_APP_URL must be an https origin"], |
| 69 | ] as const) { |
| 70 | const r = render({ [key]: value }); |
| 71 | expect(r.status).not.toBe(0); |
| 72 | expect(r.stderr).toContain(message); |
| 73 | expect(r.stderr).not.toContain(value); |
| 74 | } |
| 75 | }); |
| 76 | }); |