73 lines
2.8 KiB
JavaScript
73 lines
2.8 KiB
JavaScript
// Create (or reuse) the Gitea release for a tag and upload files to it.
|
|
//
|
|
// node gitea-release.mjs <tag> <notes.md> <file>...
|
|
//
|
|
// The release notes are the contents of <notes.md>. Reads GITHUB_SERVER_URL
|
|
// and GITHUB_REPOSITORY (set by Gitea Actions) and GITEA_TOKEN. Safe to
|
|
// re-run: an existing release is reused, its notes are updated, and assets
|
|
// with the same name are replaced.
|
|
import { openAsBlob, readFileSync } from "node:fs";
|
|
import { basename } from "node:path";
|
|
|
|
function fail(msg) {
|
|
console.error(`::error::${msg}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
const [tag, notesFile, ...files] = process.argv.slice(2);
|
|
if (!tag || !notesFile || files.length === 0) {
|
|
fail("usage: gitea-release.mjs <tag> <notes.md> <file>...");
|
|
}
|
|
const notes = readFileSync(notesFile, "utf8").trim();
|
|
if (!notes) fail(`${notesFile} is empty`);
|
|
|
|
const { GITHUB_SERVER_URL: server, GITHUB_REPOSITORY: repo, 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();
|
|
}
|
|
|
|
let release = await call("GET", `/releases/tags/${encodeURIComponent(tag)}`, { allow404: true });
|
|
if (release) {
|
|
console.log(`Reusing release ${tag} (id ${release.id})`);
|
|
if ((release.body ?? "").trim() !== notes) {
|
|
release = await call("PATCH", `/releases/${release.id}`, { json: { body: notes } });
|
|
console.log("Updated the release notes");
|
|
}
|
|
} 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}`);
|