72 lines
2.6 KiB
JavaScript
72 lines
2.6 KiB
JavaScript
// Create (or reuse) the Gitea release for a tag and upload files to it.
|
|
//
|
|
// node gitea-release.mjs <tag> <file>...
|
|
//
|
|
// Reads GITHUB_SERVER_URL, GITHUB_REPOSITORY and GITHUB_SHA (set by Gitea
|
|
// Actions) and GITEA_TOKEN. Safe to re-run: an existing release is reused and
|
|
// assets with the same name are replaced.
|
|
import { openAsBlob } from "node:fs";
|
|
import { basename } from "node:path";
|
|
|
|
function fail(msg) {
|
|
console.error(`::error::${msg}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
const [tag, ...files] = process.argv.slice(2);
|
|
if (!tag || files.length === 0) fail("usage: gitea-release.mjs <tag> <file>...");
|
|
|
|
const { GITHUB_SERVER_URL: server, GITHUB_REPOSITORY: repo, GITHUB_SHA: sha, GITEA_TOKEN: token } =
|
|
process.env;
|
|
if (!server || !repo || !token) fail("GITHUB_SERVER_URL, GITHUB_REPOSITORY and GITEA_TOKEN must be set");
|
|
|
|
const api = `${server.replace(/\/+$/, "")}/api/v1/repos/${repo}`;
|
|
|
|
async function call(method, path, { json, form, allow404 = false } = {}) {
|
|
const headers = { Authorization: `token ${token}`, Accept: "application/json" };
|
|
let body;
|
|
if (json) {
|
|
headers["Content-Type"] = "application/json";
|
|
body = JSON.stringify(json);
|
|
} else if (form) {
|
|
body = form;
|
|
}
|
|
const res = await fetch(`${api}${path}`, { method, headers, body });
|
|
if (allow404 && res.status === 404) return null;
|
|
if (!res.ok) fail(`${method} ${path} failed: HTTP ${res.status}: ${await res.text()}`);
|
|
return res.status === 204 ? null : res.json();
|
|
}
|
|
|
|
const archive = files.map((f) => basename(f)).find((n) => n.endsWith(".tar.gz"));
|
|
const notes = [
|
|
"Static Linux binary with the web UI built in.",
|
|
sha ? `\nBuilt from ${sha}.` : "",
|
|
archive ? `\nCheck the download with \`sha256sum -c ${archive}.sha256\`.` : "",
|
|
].join("");
|
|
|
|
let release = await call("GET", `/releases/tags/${encodeURIComponent(tag)}`, { allow404: true });
|
|
if (release) {
|
|
console.log(`Reusing release ${tag} (id ${release.id})`);
|
|
} else {
|
|
release = await call("POST", "/releases", {
|
|
json: { tag_name: tag, name: tag, body: notes, draft: false, prerelease: tag.includes("-") },
|
|
});
|
|
console.log(`Created release ${tag} (id ${release.id})`);
|
|
}
|
|
|
|
for (const file of files) {
|
|
const name = basename(file);
|
|
for (const old of release.assets ?? []) {
|
|
if (old.name === name) {
|
|
await call("DELETE", `/releases/${release.id}/assets/${old.id}`);
|
|
console.log(`Removed the old ${name}`);
|
|
}
|
|
}
|
|
const form = new FormData();
|
|
form.append("attachment", await openAsBlob(file), name);
|
|
await call("POST", `/releases/${release.id}/assets?name=${encodeURIComponent(name)}`, { form });
|
|
console.log(`Uploaded ${name}`);
|
|
}
|
|
|
|
console.log(release.html_url ?? `${server}/${repo}/releases/tag/${tag}`);
|