#!/usr/bin/env node // Publish a Gitea release for the current tag and attach the plugin bundle. // // Required environment (set by Gitea Actions): // GITHUB_SERVER_URL e.g. https://git.example.com // GITHUB_REPOSITORY e.g. owner/repo // GITHUB_REF_NAME tag name, e.g. v1.1.6 // GITEA_TOKEN job token (${{ secrets.GITEA_TOKEN }}), needs contents: write // // The run is idempotent: an existing release for the tag is reused and assets // with the same names are replaced instead of duplicated. import { readFile } from 'node:fs/promises'; const ASSETS = ['main.js', 'manifest.json', 'styles.css']; const server = required('GITHUB_SERVER_URL').replace(/\/+$/, ''); const repository = required('GITHUB_REPOSITORY'); const tag = required('GITHUB_REF_NAME'); const token = required('GITEA_TOKEN'); const api = `${server}/api/v1/repos/${repository}`; async function request(method, path, body, headers = {}) { const response = await fetch(`${api}${path}`, { method, headers: { Authorization: `token ${token}`, ...headers }, body, }); const text = await response.text(); return { status: response.status, text }; } function parse(text) { return text ? JSON.parse(text) : {}; } function fail(message) { console.error(`::error::${message}`); process.exit(1); } function required(name) { const value = process.env[name]; if (!value) fail(`missing environment variable ${name}`); return value; } // Gitea expects the file as multipart form-data (`attachment`); `name` in the // query string sets the asset name explicitly. function toFormData(name, data) { const form = new FormData(); form.append('attachment', new Blob([data], { type: 'application/octet-stream' }), name); return form; } async function ensureRelease() { const found = await request('GET', `/releases/tags/${encodeURIComponent(tag)}`); if (found.status === 200) { const release = parse(found.text); console.log(`release ${tag} already exists (id ${release.id})`); return release; } const payload = JSON.stringify({ tag_name: tag, name: tag, body: `Release ${tag}`, }); const created = await request('POST', '/releases', payload, { 'Content-Type': 'application/json', }); if (created.status !== 201) { fail(`creating release ${tag} failed: HTTP ${created.status} ${created.text}`); } const release = parse(created.text); console.log(`release ${tag} created (id ${release.id})`); return release; } async function existingAssets(releaseId) { const response = await request('GET', `/releases/${releaseId}/assets`); if (response.status !== 200) { fail(`listing assets failed: HTTP ${response.status} ${response.text}`); } return new Map(parse(response.text).map((asset) => [asset.name, asset.id])); } async function replaceAsset(releaseId, name, assets) { let data; try { data = await readFile(name); } catch { fail(`asset ${name} is missing — did the build run?`); } if (data.length === 0) fail(`asset ${name} is empty`); const previous = assets.get(name); if (previous !== undefined) { const removed = await request('DELETE', `/releases/${releaseId}/assets/${previous}`); if (removed.status !== 204 && removed.status !== 200) { fail(`removing old ${name} failed: HTTP ${removed.status} ${removed.text}`); } } const uploaded = await request( 'POST', `/releases/${releaseId}/assets?name=${encodeURIComponent(name)}`, toFormData(name, data), ); if (uploaded.status !== 201) { fail(`uploading ${name} failed: HTTP ${uploaded.status} ${uploaded.text}`); } console.log(`uploaded ${name} (${data.length} bytes)`); } const release = await ensureRelease(); const assets = await existingAssets(release.id); for (const name of ASSETS) { await replaceAsset(release.id, name, assets); } console.log(`release ${tag} published with ${ASSETS.join(', ')}`);