build(fantasy-stick): bootstrap tested plugin and release tools
This commit is contained in:
commit
05f9627cf9
24 files changed
+1128
No files matched your search
Executable
+13
@@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
# Git captures stdout through its askpass pipe. Never invoke with a credential argument.
|
||||
set +x
|
||||
case "${1-}" in
|
||||
"Username for 'https://git.garvis.dev':"|"Username for 'https://git.garvis.dev': ")
|
||||
printf '%s\n' dmg
|
||||
;;
|
||||
"Password for 'https://dmg@git.garvis.dev':"|"Password for 'https://dmg@git.garvis.dev': ")
|
||||
test -n "${RELEASE_CREDENTIAL-}" || exit 1
|
||||
printf '%s\n' "$RELEASE_CREDENTIAL"
|
||||
;;
|
||||
*) exit 1 ;;
|
||||
esac
|
||||
@@ -0,0 +1,41 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { test } from "node:test";
|
||||
|
||||
const credential = "synthetic-askpass-fixture";
|
||||
function ask(prompt: string, value = credential) {
|
||||
return spawnSync("/bin/sh", ["scripts/git-askpass.sh", prompt], {
|
||||
encoding: "utf8", env: { RELEASE_CREDENTIAL: value, LC_ALL: "C" },
|
||||
});
|
||||
}
|
||||
|
||||
test("gives Git its username and password over captured stdout, never credential argv", () => {
|
||||
for (const suffix of ["", " "]) {
|
||||
const username = ask(`Username for 'https://git.garvis.dev':${suffix}`);
|
||||
const password = ask(`Password for 'https://dmg@git.garvis.dev':${suffix}`);
|
||||
assert.equal(username.status, 0);
|
||||
assert.equal(username.stdout, "dmg\n");
|
||||
assert.equal(password.status, 0);
|
||||
assert.equal(password.stdout, `${credential}\n`);
|
||||
assert.equal(password.stderr, "");
|
||||
}
|
||||
});
|
||||
|
||||
for (const prompt of [
|
||||
"Password for 'https://dmg@git.garvis.dev.evil.invalid': ",
|
||||
"Password for 'http://dmg@git.garvis.dev': ",
|
||||
"Password for 'https://other@git.garvis.dev': ", "unexpected prompt",
|
||||
]) {
|
||||
test(`refuses non-approved authentication prompts: ${prompt}`, () => {
|
||||
const result = ask(prompt);
|
||||
assert.notEqual(result.status, 0);
|
||||
assert.equal(result.stdout, "");
|
||||
assert.equal(result.stderr, "");
|
||||
});
|
||||
}
|
||||
|
||||
test("missing release credential fails without falling back to another secret", () => {
|
||||
const result = ask("Password for 'https://dmg@git.garvis.dev': ", "");
|
||||
assert.notEqual(result.status, 0);
|
||||
assert.equal(result.stdout, "");
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { test } from "node:test";
|
||||
|
||||
for (const environment of [
|
||||
{ VERSION: "1.2.3", RELEASE_COMMIT: "a".repeat(40) },
|
||||
{ VERSION: "../../outside", RELEASE_CREDENTIAL: "synthetic-cli-fixture", RELEASE_COMMIT: "a".repeat(40) },
|
||||
]) {
|
||||
test(`CLI fails closed on invalid input without echoing environment: ${Object.keys(environment)}`, () => {
|
||||
const child = spawnSync(process.execPath, ["build/release-tools/publish-release.js"], {
|
||||
encoding: "utf8", env: environment,
|
||||
});
|
||||
assert.equal(child.status, 1);
|
||||
assert.equal(child.stdout, "");
|
||||
assert.equal(child.stderr, "Release publication failed; inspect the release state before retrying.\n");
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { publishRelease } from "./release-client.js";
|
||||
|
||||
try {
|
||||
const version = process.env.VERSION ?? "";
|
||||
const credential = process.env.RELEASE_CREDENTIAL ?? "";
|
||||
const commit = process.env.RELEASE_COMMIT ?? "";
|
||||
// Check path components and required inputs before opening an artifact or contacting Gitea.
|
||||
if (!/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/.test(version)
|
||||
|| !credential || !/^[a-f0-9]{40}$/.test(commit)) throw new Error("Invalid input");
|
||||
const jar = await readFile(`build/libs/purpur-fantasy-stick-${version}.jar`);
|
||||
await publishRelease({
|
||||
credential, version, commit, jar,
|
||||
notes: process.env.RELEASE_NOTES ?? "See the source history for changes.",
|
||||
}, fetch);
|
||||
console.log(`Published purpur-fantasy-stick v${version}.`);
|
||||
} catch {
|
||||
// Never echo credentials, upstream error bodies, transport diagnostics or the environment.
|
||||
console.error("Release publication failed; inspect the release state before retrying.");
|
||||
process.exitCode = 1;
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { publishRelease, type ReleaseInput } from "./release-client.js";
|
||||
|
||||
const input: ReleaseInput = {
|
||||
credential: "synthetic-test-value-not-a-real-credential",
|
||||
version: "1.2.3",
|
||||
commit: "a".repeat(40),
|
||||
notes: "Verified changes",
|
||||
jar: new Uint8Array([0x50, 0x4b, 0x03, 0x04]),
|
||||
};
|
||||
|
||||
function success(init: RequestInit): Response {
|
||||
return Response.json({
|
||||
id: 42, draft: init.method !== "PATCH", tag_name: "v1.2.3",
|
||||
name: "purpur-fantasy-stick-1.2.3.jar", size: input.jar.length,
|
||||
});
|
||||
}
|
||||
|
||||
test("creates a draft on the fixed repository and exact source commit, without following redirects", async () => {
|
||||
const calls: { url: string; init: RequestInit }[] = [];
|
||||
const id = await publishRelease(input, async (url, init) => {
|
||||
calls.push({ url, init });
|
||||
return success(init);
|
||||
});
|
||||
assert.ok(calls.length >= 1);
|
||||
const first = calls[0]!;
|
||||
assert.equal(first.url, "https://git.garvis.dev/api/v1/repos/dmg/purpur-fantasy-stick/releases");
|
||||
assert.equal(first.init.method, "POST");
|
||||
assert.equal(first.init.redirect, "error");
|
||||
assert.equal(new Headers(first.init.headers).get("authorization"), `token ${input.credential}`);
|
||||
assert.deepEqual(JSON.parse(String(first.init.body)), {
|
||||
tag_name: "v1.2.3", target_commitish: input.commit, name: "v1.2.3",
|
||||
body: input.notes, draft: true, prerelease: false,
|
||||
});
|
||||
assert.equal(id, 42);
|
||||
});
|
||||
|
||||
test("uploads the exact versioned bytes before publishing the draft", async () => {
|
||||
const calls: { url: string; init: RequestInit }[] = [];
|
||||
await publishRelease(input, async (url, init) => {
|
||||
calls.push({ url, init });
|
||||
return success(init);
|
||||
});
|
||||
assert.equal(calls.length, 3);
|
||||
assert.equal(calls[1]!.url,
|
||||
"https://git.garvis.dev/api/v1/repos/dmg/purpur-fantasy-stick/releases/42/assets?name=purpur-fantasy-stick-1.2.3.jar");
|
||||
assert.equal(calls[1]!.init.method, "POST");
|
||||
assert.deepEqual(new Uint8Array(await new Response(calls[1]!.init.body).arrayBuffer()), input.jar);
|
||||
assert.equal(new Headers(calls[1]!.init.headers).get("content-type"), "application/octet-stream");
|
||||
assert.equal(calls[2]!.init.method, "PATCH");
|
||||
assert.equal(calls[2]!.url, "https://git.garvis.dev/api/v1/repos/dmg/purpur-fantasy-stick/releases/42");
|
||||
assert.deepEqual(JSON.parse(String(calls[2]!.init.body)), { draft: false });
|
||||
for (const call of calls) {
|
||||
assert.equal(call.init.redirect, "error");
|
||||
assert.equal(new Headers(call.init.headers).get("authorization"), `token ${input.credential}`);
|
||||
assert.ok(!call.url.includes(input.credential));
|
||||
}
|
||||
});
|
||||
|
||||
for (const failureStep of [1, 2, 3]) {
|
||||
for (const status of [302, 401, 409, 500]) {
|
||||
test(`stops at step ${failureStep} on HTTP ${status} without leaking API error bodies`, async () => {
|
||||
let calls = 0;
|
||||
await assert.rejects(publishRelease(input, async (_url, init) => {
|
||||
calls++;
|
||||
if (calls === failureStep) return new Response(input.credential, { status });
|
||||
return success(init);
|
||||
}), (error: unknown) => {
|
||||
assert.ok(error instanceof Error);
|
||||
assert.ok(error.message.includes(String(status)));
|
||||
assert.ok(!error.message.includes(input.credential));
|
||||
return true;
|
||||
});
|
||||
assert.equal(calls, failureStep, "Do not retry, overwrite an asset or publish after a failed step");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
test("sanitizes transport errors and does not follow a redirect with the credential", async () => {
|
||||
await assert.rejects(publishRelease(input, async () => {
|
||||
throw new Error(`diagnostic containing ${input.credential}`);
|
||||
}), { message: "Release API request failed." });
|
||||
});
|
||||
|
||||
for (const invalid of [
|
||||
{ credential: "" }, { credential: "header\r\ninjection" }, { version: "../../latest" },
|
||||
{ version: "1.0.0-SNAPSHOT" }, { commit: "main" }, { jar: new Uint8Array() },
|
||||
]) {
|
||||
test(`rejects invalid ${Object.keys(invalid)[0]} before any network request`, async () => {
|
||||
let calls = 0;
|
||||
await assert.rejects(publishRelease({ ...input, ...invalid }, async () => {
|
||||
calls++;
|
||||
return Response.json({ id: 42 });
|
||||
}), { message: "Invalid release input." });
|
||||
assert.equal(calls, 0);
|
||||
});
|
||||
}
|
||||
|
||||
for (const body of ["not JSON", "null", JSON.stringify({ id: -1 }), JSON.stringify({ id: "../other" }),
|
||||
JSON.stringify({ id: 42, draft: false, tag_name: "v1.2.3" })]) {
|
||||
test(`rejects malformed or already-public draft response: ${body}`, async () => {
|
||||
let calls = 0;
|
||||
await assert.rejects(publishRelease(input, async () => {
|
||||
calls++;
|
||||
return new Response(body);
|
||||
}), { message: "Invalid draft release response." });
|
||||
assert.equal(calls, 1);
|
||||
});
|
||||
}
|
||||
|
||||
for (const [failureStep, body] of [
|
||||
[2, {}], [2, { name: "other.jar", size: 4 }],
|
||||
[2, { name: "purpur-fantasy-stick-1.2.3.jar", size: 0 }],
|
||||
[3, { id: 42, draft: true, tag_name: "v1.2.3" }],
|
||||
[3, { id: 77, draft: false, tag_name: "v1.2.3" }],
|
||||
[3, { id: 42, draft: false, tag_name: "v8.8.8" }],
|
||||
] as const) {
|
||||
test(`requires a matching asset and completed publication: ${JSON.stringify(body)}`, async () => {
|
||||
let calls = 0;
|
||||
await assert.rejects(publishRelease(input, async (_url, init) => {
|
||||
calls++;
|
||||
return calls === failureStep ? Response.json(body) : success(init);
|
||||
}), { message: failureStep === 2 ? "Invalid release asset response." : "Invalid published release response." });
|
||||
assert.equal(calls, failureStep);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
export interface ReleaseInput {
|
||||
credential: string;
|
||||
version: string;
|
||||
commit: string;
|
||||
notes: string;
|
||||
jar: Uint8Array;
|
||||
}
|
||||
|
||||
export type Request = (url: string, init: RequestInit) => Promise<Response>;
|
||||
|
||||
const releases = "https://git.garvis.dev/api/v1/repos/dmg/purpur-fantasy-stick/releases";
|
||||
|
||||
export async function publishRelease(input: ReleaseInput, request: Request): Promise<number> {
|
||||
if (!/^[\x21-\x7e]+$/.test(input.credential)
|
||||
|| !/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/.test(input.version)
|
||||
|| !/^[a-f0-9]{40}$/.test(input.commit) || input.jar.length === 0) {
|
||||
throw new Error("Invalid release input.");
|
||||
}
|
||||
const send: Request = async (url, init) => {
|
||||
let response: Response;
|
||||
try {
|
||||
response = await request(url, { ...init, signal: AbortSignal.timeout(60_000) });
|
||||
} catch {
|
||||
throw new Error("Release API request failed.");
|
||||
}
|
||||
if (!response.ok) throw new Error(`Release API request failed (HTTP ${response.status}).`);
|
||||
return response;
|
||||
};
|
||||
const response = await send(releases, {
|
||||
method: "POST",
|
||||
redirect: "error",
|
||||
headers: { authorization: `token ${input.credential}`, "content-type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
tag_name: `v${input.version}`, target_commitish: input.commit, name: `v${input.version}`,
|
||||
body: input.notes, draft: true, prerelease: false,
|
||||
}),
|
||||
});
|
||||
const release = await readObject(response, "Invalid draft release response.");
|
||||
if (typeof release.id !== "number" || !Number.isSafeInteger(release.id) || release.id <= 0
|
||||
|| release.draft !== true || release.tag_name !== `v${input.version}`) {
|
||||
throw new Error("Invalid draft release response.");
|
||||
}
|
||||
const assetResponse = await send(`${releases}/${release.id}/assets?name=purpur-fantasy-stick-${input.version}.jar`, {
|
||||
method: "POST", redirect: "error",
|
||||
headers: { authorization: `token ${input.credential}`, "content-type": "application/octet-stream" },
|
||||
body: new Uint8Array(input.jar).buffer,
|
||||
});
|
||||
const asset = await readObject(assetResponse, "Invalid release asset response.");
|
||||
if (asset.name !== `purpur-fantasy-stick-${input.version}.jar` || asset.size !== input.jar.length) {
|
||||
throw new Error("Invalid release asset response.");
|
||||
}
|
||||
const publishedResponse = await send(`${releases}/${release.id}`, {
|
||||
method: "PATCH", redirect: "error",
|
||||
headers: { authorization: `token ${input.credential}`, "content-type": "application/json" },
|
||||
body: JSON.stringify({ draft: false }),
|
||||
});
|
||||
const published = await readObject(publishedResponse, "Invalid published release response.");
|
||||
if (published.id !== release.id || published.draft !== false || published.tag_name !== `v${input.version}`) {
|
||||
throw new Error("Invalid published release response.");
|
||||
}
|
||||
return release.id;
|
||||
}
|
||||
|
||||
async function readObject(response: Response, message: string): Promise<Record<string, unknown>> {
|
||||
let value: unknown;
|
||||
try {
|
||||
value = await response.json();
|
||||
} catch {
|
||||
throw new Error(message);
|
||||
}
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error(message);
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
Reference in new issue
Block a user