121 lines
5.1 KiB
JavaScript
121 lines
5.1 KiB
JavaScript
// Publishes a Gitea release for the current tag: creates a DRAFT, uploads every asset, then publishes.
|
|
// Any failed request aborts and deletes the draft, so a partial release is never visible.
|
|
// The token is read only from the environment (TOKEN) and never printed or passed as an argument.
|
|
//
|
|
// TOKEN=… API=https://host/api/v1/repos/owner/repo TAG=v0.1.0 node scripts/publish-release.mjs <asset-dir> [notes-file]
|
|
// TOKEN=… API=… TAG=v0.1.2 node scripts/publish-release.mjs --append <asset-dir>
|
|
//
|
|
// --append adds assets (e.g. the locally built Windows packages) to the already published release of TAG;
|
|
// it refuses to replace an existing asset name.
|
|
//
|
|
// Update feeds (*.yml) are checked before anything is uploaded: every package a feed references must be in
|
|
// the same directory with the exact name, size and sha512 the feed declares, and the feed version must equal
|
|
// the tag. Feeds are uploaded after all packages, so clients never see a feed whose package is not there yet.
|
|
// The release is marked prerelease unless PRERELEASE=false is set and the tag has no prerelease part.
|
|
import { readdirSync, readFileSync, statSync } from 'node:fs';
|
|
import path from 'node:path';
|
|
import { checkFeed } from './update-feed-check.mjs';
|
|
|
|
const { TOKEN, API, TAG } = process.env;
|
|
const args = process.argv.slice(2);
|
|
const append = args[0] === '--append';
|
|
if (append) args.shift();
|
|
const assetDir = args[0];
|
|
const notesFile = args[1];
|
|
for (const [name, value] of Object.entries({ TOKEN, API, TAG, assetDir })) {
|
|
if (!value) {
|
|
console.error(`missing ${name}`);
|
|
process.exit(2);
|
|
}
|
|
}
|
|
const version = TAG.replace(/^v/, '');
|
|
const prerelease = process.env.PRERELEASE !== 'false' || version.includes('-');
|
|
|
|
function collect(dir) {
|
|
const files = [];
|
|
for (const entry of readdirSync(dir)) {
|
|
const full = path.join(dir, entry);
|
|
if (statSync(full).isDirectory()) files.push(...collect(full));
|
|
else files.push(full);
|
|
}
|
|
return files;
|
|
}
|
|
|
|
async function request(method, url, body, headers = {}) {
|
|
const res = await fetch(url, { method, headers: { Authorization: `token ${TOKEN}`, ...headers }, body });
|
|
const text = await res.text();
|
|
if (!res.ok) throw new Error(`${method} ${url.replace(API, '')} → HTTP ${res.status}: ${text.slice(0, 300)}`);
|
|
return text ? JSON.parse(text) : null;
|
|
}
|
|
|
|
const isFeed = (f) => f.endsWith('.yml');
|
|
// Packages first, feeds last.
|
|
const files = collect(assetDir).sort((a, b) => isFeed(a) - isFeed(b));
|
|
if (files.length === 0) {
|
|
console.error(`no assets in ${assetDir}`);
|
|
process.exit(1);
|
|
}
|
|
const names = new Set();
|
|
for (const f of files) {
|
|
const n = path.basename(f);
|
|
if (names.has(n)) {
|
|
console.error(`duplicate asset name ${n}`);
|
|
process.exit(1);
|
|
}
|
|
names.add(n);
|
|
}
|
|
for (const feed of files.filter(isFeed)) {
|
|
try {
|
|
const referenced = await checkFeed(feed, { version });
|
|
console.log(`feed ${path.basename(feed)} ok: ${referenced.join(', ')}`);
|
|
} catch (err) {
|
|
console.error(`feed check failed: ${err.message}`);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
async function upload(releaseId, file) {
|
|
const form = new FormData();
|
|
form.append('attachment', new Blob([readFileSync(file)]), path.basename(file));
|
|
const asset = await request('POST', `${API}/releases/${releaseId}/assets?name=${encodeURIComponent(path.basename(file))}`, form);
|
|
if (asset.name !== path.basename(file) || asset.size !== statSync(file).size) {
|
|
throw new Error(`server stored ${asset.name} (${asset.size} bytes) for ${path.basename(file)} (${statSync(file).size} bytes)`);
|
|
}
|
|
console.log(`uploaded ${asset.name} (${asset.size} bytes)`);
|
|
}
|
|
|
|
if (append) {
|
|
try {
|
|
const release = await request('GET', `${API}/releases/tags/${encodeURIComponent(TAG)}`);
|
|
if (release.draft) throw new Error(`release ${TAG} is a draft; publish it first`);
|
|
const existing = new Set((release.assets || []).map((a) => a.name));
|
|
const clash = files.map((f) => path.basename(f)).filter((n) => existing.has(n));
|
|
if (clash.length > 0) throw new Error(`release ${TAG} already has ${clash.join(', ')}`);
|
|
for (const file of files) await upload(release.id, file);
|
|
console.log(`appended ${files.length} assets to ${TAG}`);
|
|
} catch (err) {
|
|
console.error(`append failed: ${err.message}`);
|
|
process.exit(1);
|
|
}
|
|
process.exit(0);
|
|
}
|
|
|
|
const body = notesFile ? readFileSync(notesFile, 'utf8') : '';
|
|
let release = null;
|
|
try {
|
|
release = await request('POST', `${API}/releases`, JSON.stringify({ tag_name: TAG, name: `TeamsPeach ${TAG}`, body, draft: true, prerelease }), { 'Content-Type': 'application/json' });
|
|
console.log(`draft release ${release.id} created for ${TAG} (prerelease: ${prerelease})`);
|
|
for (const file of files) await upload(release.id, file);
|
|
await request('PATCH', `${API}/releases/${release.id}`, JSON.stringify({ draft: false }), { 'Content-Type': 'application/json' });
|
|
console.log(`release ${TAG} published with ${files.length} assets`);
|
|
} catch (err) {
|
|
console.error(`release failed: ${err.message}`);
|
|
if (release) {
|
|
await request('DELETE', `${API}/releases/${release.id}`).then(
|
|
() => console.error(`draft ${release.id} deleted`),
|
|
(e) => console.error(`could not delete draft ${release.id}: ${e.message}`),
|
|
);
|
|
}
|
|
process.exit(1);
|
|
}
|