#!/usr/bin/env node // Release helpers for CI (no dependencies). // // node scripts/release-notes.mjs notes print the CHANGELOG section for // node scripts/release-notes.mjs publish create the Gitea release for $GITHUB_REF_NAME // // `publish` uses the Gitea Actions environment: GITHUB_SERVER_URL, GITHUB_REPOSITORY, // GITHUB_REF_NAME (the tag), IMAGE (registry/owner/name) and TOKEN (API token). import { readFileSync } from "node:fs"; export function changelogSection(markdown, version) { const lines = markdown.split(/\r?\n/); const start = lines.findIndex((l) => l.startsWith(`## [${version}]`)); if (start === -1) return null; let end = lines.findIndex((l, i) => i > start && l.startsWith("## ")); if (end === -1) end = lines.length; return lines .slice(start + 1, end) .join("\n") .trim(); } async function publish() { const { GITHUB_SERVER_URL: server, GITHUB_REPOSITORY: repo, GITHUB_REF_NAME: tag, IMAGE, TOKEN } = process.env; if (!server || !repo || !tag || !IMAGE || !TOKEN) throw new Error("missing CI environment variables"); const version = tag.replace(/^v/, ""); const notes = changelogSection(readFileSync("CHANGELOG.md", "utf8"), version); if (!notes) throw new Error(`CHANGELOG.md has no section for ${version}`); const body = `${notes}\n\n### Docker image\n\n\`\`\`\n${IMAGE}:${version}\n\`\`\`\n`; const res = await fetch(`${server}/api/v1/repos/${repo}/releases`, { method: "POST", headers: { Authorization: `token ${TOKEN}`, "Content-Type": "application/json" }, body: JSON.stringify({ tag_name: tag, name: tag, body, draft: false, prerelease: false }), }); if (res.status === 409) { console.log(`Release ${tag} already exists, leaving it as is.`); return; } if (!res.ok) throw new Error(`Creating release failed: ${res.status} ${await res.text()}`); console.log(`Created release ${tag}.`); } const [cmd, arg] = process.argv.slice(2); if (cmd === "notes") { const section = changelogSection(readFileSync("CHANGELOG.md", "utf8"), arg); if (!section) { console.error(`No CHANGELOG section for ${arg}`); process.exit(1); } console.log(section); } else if (cmd === "publish") { publish().catch((err) => { console.error(err.message); process.exit(1); }); }