diff --git a/scripts/release-mirror-prepare.mjs b/scripts/release-mirror-prepare.mjs index 66f5729e2..146fa5a49 100644 --- a/scripts/release-mirror-prepare.mjs +++ b/scripts/release-mirror-prepare.mjs @@ -4,17 +4,18 @@ import { execFileSync } from "node:child_process"; import { appendFileSync } from "node:fs"; import { pathToFileURL } from "node:url"; -import { mirrorReadiness, readMirror, validateMirrorSource, validateMirrorRelease } from "./release-mirror-ready.mjs"; +import { mirrorReadiness, validateMirrorSource, validateMirrorRelease } from "./release-mirror-ready.mjs"; async function requestMirror(path, method = "GET", body) { - if (method === "GET") return readMirror(path); const token = process.env.ATOMGIT_TOKEN?.trim(); if (!token) throw new Error("ATOMGIT_TOKEN is required to prepare the mirror before downloading artifacts"); const response = await fetch(`https://api.atomgit.com/api/v5/repos/TNTcraftHIM/Piik${path}`, { method, redirect: "error", signal: AbortSignal.timeout(30_000), - headers: { Accept: "application/json", "PRIVATE-TOKEN": token, "Content-Type": "application/json" }, - body: JSON.stringify(body), + headers: { Accept: "application/json", "PRIVATE-TOKEN": token, + ...(body ? { "Content-Type": "application/json" } : {}) }, + body: body ? JSON.stringify(body) : undefined, }); + if (response.status === 404 && method === "GET") return null; if (!response.ok) { const detail = await response.json().catch(() => null); const reason = detail?.message ?? detail?.error_message ?? detail?.error; diff --git a/tests/release-mirror-ready.test.ts b/tests/release-mirror-ready.test.ts index 6e4ca7b3c..18c90d62d 100644 --- a/tests/release-mirror-ready.test.ts +++ b/tests/release-mirror-ready.test.ts @@ -83,6 +83,28 @@ test("preparation reports the platform rejection without logging its token or re } finally { vi.unstubAllGlobals(); vi.unstubAllEnvs(); } }); +test("preparation authenticates the repository GET while public readiness stays anonymous", async () => { + vi.stubEnv("ATOMGIT_TOKEN", "fixture-private-token"); + const origin = "https://api.atomgit.com/api/v5/repos/TNTcraftHIM/Piik"; + const requests: { url: string; token: string | null }[] = []; + vi.stubGlobal("fetch", vi.fn(async (url: string, options?: RequestInit) => { + const token = new Headers(options?.headers).get("PRIVATE-TOKEN"); + requests.push({ url, token }); + if (url === origin) { + return token === "fixture-private-token" ? Response.json({ private: false, default_branch: "main" }) + : Response.json({ message: "Authentication required" }, { status: 401 }); + } + if (options?.method === "POST") return Response.json({ ...mirror, prerelease: true, assets: [] }); + return new Response(null, { status: 404 }); + })); + try { + expect(await mirrorReadiness(source)).toEqual({ status: "ready", version, revision }); + expect(requests).toEqual([{ url: `${origin}/releases/tags/${version}`, token: null }]); + expect(await prepareMirror(source)).toEqual({ status: "ready", version, revision }); + expect(requests.find(request => request.url === origin)?.token).toBe("fixture-private-token"); + } finally { vi.unstubAllGlobals(); vi.unstubAllEnvs(); } +}); + test("a missing attachment or conflicting source marker fails rather than claiming a completed mirror", async () => { await expect(mirrorReadiness(source, async () => ({ ...mirror, assets: [] }))).rejects.toThrow("incomplete"); await expect(mirrorReadiness(source, async () => ({ ...mirror, body: `` }))).rejects.toThrow("different or unknown source revision");