feat(build): bootstrap Purpur Carry plugin
Release / release (push) Successful in 3m26s

This commit is contained in:
dmg committed 2026-09-22 21:32:51 -04:00
commit 23964f7695
24 files changed
+1126

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
#!/bin/sh
# Git consumes this protocol over a pipe; never run interactively with a real credential.
set +x
[ "${1-}" = get ] || exit 0
protocol= host= path= username=
while IFS= read -r line && [ -n "$line" ]; do
case "$line" in
protocol=*) protocol=${line#protocol=} ;;
host=*) host=${line#host=} ;;
path=*) path=${line#path=} ;;
username=*) username=${line#username=} ;;
esac
done
[ "$protocol" = https ] || exit 1
[ "$host" = git.garvis.dev ] || exit 1
[ "$path" = dmg/purpur-carry.git ] || exit 1
[ -z "$username" ] || [ "$username" = dmg ] || exit 1
[ -n "${RELEASE_CREDENTIAL-}" ] || exit 1
LC_ALL=C
export LC_ALL
case "$RELEASE_CREDENTIAL" in *[![:graph:]]*) exit 1 ;; esac
printf '%s\n' 'username=dmg'
printf 'password=%s\n\n' "$RELEASE_CREDENTIAL"
+69
View File
@@ -0,0 +1,69 @@
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { resolve } from "node:path";
import { test } from "node:test";
const fixture = "synthetic-git-protocol-fixture";
const helper = resolve("scripts/git-credential.sh");
const request = "protocol=https\nhost=git.garvis.dev\npath=dmg/purpur-carry.git\n\n";
const environment = {
PATH: process.env.PATH,
RELEASE_CREDENTIAL: fixture,
LC_ALL: "C",
GIT_ASKPASS: "echo",
GIT_TERMINAL_PROMPT: "0",
GIT_CONFIG_NOSYSTEM: "1",
GIT_CONFIG_GLOBAL: "/dev/null",
};
test("real Git obtains only the scoped in-memory credential", () => {
const child = spawnSync("git", [
"-c", "credential.helper=",
"-c", `credential.helper=${helper}`,
"-c", "credential.useHttpPath=true",
"credential", "fill",
], { encoding: "utf8", input: request, env: environment });
assert.equal(child.status, 0);
assert.ok(child.stdout.split("\n").includes("username=dmg"));
assert.ok(child.stdout.split("\n").includes(`password=${fixture}`));
assert.equal(child.stderr, "");
});
for (const input of [
request.replace("https", "http"),
request.replace("git.garvis.dev", "evil.invalid"),
request.replace("purpur-carry.git", "another-project.git"),
request.replace("\n\n", "\nusername=other\n\n"),
]) {
test(`refuses mismatched credential scope: ${JSON.stringify(input)}`, () => {
const child = spawnSync("/bin/sh", [helper, "get"], {
encoding: "utf8", input, env: environment,
});
assert.notEqual(child.status, 0);
assert.equal(child.stdout, "");
assert.equal(child.stderr, "");
});
}
for (const action of ["store", "erase"]) {
test(`ignores ${action} without persistence`, () => {
const child = spawnSync("/bin/sh", [helper, action], {
encoding: "utf8", input: request, env: environment,
});
assert.equal(child.status, 0);
assert.equal(child.stdout, "");
assert.equal(child.stderr, "");
});
}
for (const value of ["", "bad\npassword=injected", "trailing-space "]) {
test(`rejects malformed credential input of length ${value.length}`, () => {
const child = spawnSync("/bin/sh", [helper, "get"], {
encoding: "utf8", input: request,
env: { ...environment, RELEASE_CREDENTIAL: value },
});
assert.notEqual(child.status, 0);
assert.equal(child.stdout, "");
assert.equal(child.stderr, "");
});
}
+22
View File
@@ -0,0 +1,22 @@
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 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");
});
}
+24
View File
@@ -0,0 +1,24 @@
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 ?? "";
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-carry-${version}.jar`);
await publishRelease({
credential,
version,
commit,
jar,
notes: process.env.RELEASE_NOTES ?? "See the source history for changes.",
}, fetch);
console.log(`Published purpur-carry v${version}.`);
} catch {
console.error("Release publication failed; inspect the release state before retrying.");
process.exitCode = 1;
}
+94
View File
@@ -0,0 +1,94 @@
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-carry-1.2.3.jar",
size: input.jar.length,
});
}
test("publishes exact bytes through a fixed-repository draft without redirects", async () => {
const calls: { url: string; init: RequestInit }[] = [];
const id = await publishRelease(input, async (url, init) => {
calls.push({ url, init });
return success(init);
});
assert.equal(id, 42);
assert.equal(calls.length, 3);
assert.equal(calls[0]!.url, "https://git.garvis.dev/api/v1/repos/dmg/purpur-carry/releases");
assert.deepEqual(JSON.parse(String(calls[0]!.init.body)), {
tag_name: "v1.2.3",
target_commitish: input.commit,
name: "v1.2.3",
body: input.notes,
draft: true,
prerelease: false,
});
assert.equal(calls[1]!.url,
"https://git.garvis.dev/api/v1/repos/dmg/purpur-carry/releases/42/assets?name=purpur-carry-1.2.3.jar");
assert.deepEqual(new Uint8Array(await new Response(calls[1]!.init.body).arrayBuffer()), input.jar);
assert.equal(calls[2]!.url, "https://git.garvis.dev/api/v1/repos/dmg/purpur-carry/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 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);
});
}
}
test("sanitizes transport errors", 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 network access`, 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);
});
}
+99
View File
@@ -0,0 +1,99 @@
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-carry/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 assetName = `purpur-carry-${input.version}.jar`;
const assetResponse = await send(`${releases}/${release.id}/assets?name=${assetName}`, {
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 !== assetName || 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>;
}