// Creates a release's page on a forge, or updates it in place when the run is // repeated, so a re-run never fails on the release it made the first time. // // node ci/releases.mjs github // the public repository's GitHub Release, with PUBLIC_PUSH_TOKEN. The tag // must already be pushed there. // node ci/releases.mjs forge // this repository's release on its own forge, with FORGE_TOKEN, tagging // when the tag does not exist yet. // node ci/releases.mjs forge-probe // whether FORGE_TOKEN may write releases, without writing one: an update // of a release that does not exist answers 404 to a token that may, and // 403 to one that may not. // // Prints the release's page. import { readFileSync } from 'node:fs'; const PUBLIC_REPO = 'Fabled-Sword/minstrel'; const [kind, tag, bodyFile, commit] = process.argv.slice(2); function need(name) { const value = process.env[name]; if (!value) throw new Error(`${name} is empty or unset`); return value; } async function call(method, url, headers, body) { const res = await fetch(url, { method, headers: { 'Content-Type': 'application/json', ...headers }, body: body === undefined ? undefined : JSON.stringify(body) }); const text = await res.text(); return { status: res.status, json: text ? safeJSON(text) : null, text }; } function safeJSON(text) { try { return JSON.parse(text); } catch { return null; } } function ok(res, what) { if (res.status < 200 || res.status >= 300) { throw new Error(`${what}: HTTP ${res.status} ${res.text.slice(0, 300)}`); } return res.json; } // The same steps on either forge: find the release by its tag, then update it // or create it. async function upsert(api, headers, fields) { const found = await call('GET', `${api}/releases/tags/${encodeURIComponent(tag)}`, headers); if (found.status === 200) { return ok(await call('PATCH', `${api}/releases/${found.json.id}`, headers, fields), 'update'); } if (found.status !== 404) ok(found, 'look up'); return ok(await call('POST', `${api}/releases`, headers, { tag_name: tag, ...fields }), 'create'); } async function main() { if (kind === 'forge-probe') { const res = await call( 'PATCH', `${need('GITHUB_SERVER_URL')}/api/v1/repos/${need('GITHUB_REPOSITORY')}/releases/0`, { Authorization: `token ${need('FORGE_TOKEN')}` }, {} ); if (res.status !== 404) throw new Error(`the token may not write releases: HTTP ${res.status} ${res.text.slice(0, 200)}`); console.log('the forge token may write releases'); return; } if (!tag || !bodyFile) throw new Error('usage: node ci/releases.mjs github|forge []'); const body = readFileSync(bodyFile, 'utf8'); const name = `Fabled Minstrel ${tag}`; let release; if (kind === 'github') { release = await upsert( `https://api.github.com/repos/${PUBLIC_REPO}`, { Authorization: `Bearer ${need('PUBLIC_PUSH_TOKEN')}`, Accept: 'application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28' }, { name, body } ); } else if (kind === 'forge') { if (!commit) throw new Error('forge needs the commit to tag'); release = await upsert( `${need('GITHUB_SERVER_URL')}/api/v1/repos/${need('GITHUB_REPOSITORY')}`, { Authorization: `token ${need('FORGE_TOKEN')}` }, { name, body, target_commitish: commit } ); } else { throw new Error(`unknown forge ${kind}`); } console.log(release.html_url); } main().catch((err) => { console.error(`releases: ${err.message}`); process.exit(1); });