diff --git a/.gitea/scripts/publish-releases.js b/.gitea/scripts/publish-releases.js new file mode 100644 index 0000000000..881a2306d2 --- /dev/null +++ b/.gitea/scripts/publish-releases.js @@ -0,0 +1,267 @@ +// Publish a `v` git tag and Gitea release for every container image +// tag in the registry that has no release yet. Idempotent; safe to run every +// CI cycle. Runs on plain Node 22 (the image base) with no dependencies: +// docker run --rm -i -e ... node:22-bookworm-slim node - < publish-releases.js +// +// Env: GITEA (default https://gitea.milesward.dev), OWNER, REPO (owner/name), +// IMAGE (owner/name in the registry), GITEA_TOKEN (repo write), +// REG_TOKEN (read:package), MAIN_SHA, RUN_NUMBER, DRY_RUN=1 (print only). +// +// Provenance: the release points at the image's org.opencontainers.image.revision +// label (the main commit CI built from). Images built before that label existed +// fall back to a pre-existing tag's commit, then to MAIN_SHA — and say so. + +'use strict'; + +const env = (k, d) => { + const v = process.env[k]; + if (v !== undefined && v !== '') return v; + if (d !== undefined) return d; + throw new Error(`missing env ${k}`); +}; + +const GITEA = env('GITEA', 'https://gitea.milesward.dev').replace(/\/$/, ''); +const OWNER = env('OWNER'); +const REPO = env('REPO'); +const IMAGE = env('IMAGE'); +const GITEA_TOKEN = env('GITEA_TOKEN'); +const REG_TOKEN = env('REG_TOKEN'); +const MAIN_SHA = env('MAIN_SHA'); +const RUN_NUMBER = env('RUN_NUMBER', ''); +const DRY_RUN = process.env.DRY_RUN === '1'; +const REGISTRY = new URL(GITEA).host; +const UPSTREAM = 'deepseek-ai/deepseek-harness'; +const UA = 'deepseek-harness-release-bot/1.0'; + +const MANIFEST_ACCEPT = [ + 'application/vnd.docker.distribution.manifest.v2+json', + 'application/vnd.oci.image.manifest.v1+json', +].join(','); + +async function gitea(method, path, body) { + const res = await fetch(`${GITEA}/api/v1${path}`, { + method, + headers: { + Authorization: `token ${GITEA_TOKEN}`, + 'Content-Type': 'application/json', + 'User-Agent': UA, + }, + body: body === undefined ? undefined : JSON.stringify(body), + }); + const text = await res.text(); + let json = null; + try { json = text ? JSON.parse(text) : null; } catch { /* non-JSON error body */ } + return { status: res.status, json, text }; +} + +async function registry(path, accept) { + const auth = Buffer.from(`${OWNER}:${REG_TOKEN}`).toString('base64'); + return fetch(`https://${REGISTRY}/v2/${IMAGE}/${path}`, { + headers: { Authorization: `Basic ${auth}`, Accept: accept, 'User-Agent': UA }, + }); +} + +// --- versions ------------------------------------------------------------- + +/** Sort key for "0.1.5-rc.2" / "0.1.5-alpha.1" / "0.1.1-rc.2-r57". */ +function versionKey(tag) { + const m = /^(\d+)\.(\d+)\.(\d+)(?:-(alpha|beta|rc)\.(\d+))?(?:-r(\d+))?$/.exec(tag); + if (!m) return [Infinity]; + const pre = { alpha: 0, beta: 1, rc: 2 }[m[4]] ?? 3; + return [+m[1], +m[2], +m[3], pre, +(m[5] ?? 0), +(m[6] ?? 0)]; +} +const compareVersions = (a, b) => { + const ka = versionKey(a), kb = versionKey(b); + for (let i = 0; i < Math.max(ka.length, kb.length); i++) { + const d = (ka[i] ?? 0) - (kb[i] ?? 0); + if (d) return d; + } + return 0; +}; +const dshVersionOf = (tag) => tag.replace(/-r\d+$/, ''); +const isRevision = (tag) => /-r\d+$/.test(tag); +const atLeast = (ver, target) => compareVersions(ver.split('-')[0], target) >= 0; + +// --- upstream release notes ------------------------------------------------ + +async function upstreamNotes(ver) { + const res = await fetch(`https://api.github.com/repos/${UPSTREAM}/releases/tags/dsh-v${ver}`, { + headers: { 'User-Agent': UA, Accept: 'application/vnd.github+json' }, + }); + if (!res.ok) return null; + const rel = await res.json(); + const body = rel.body || ''; + // Notes are bilingual; keep the English half, demote its headings under ours. + // Upstream mixes HTML

(the anchored first heading) with markdown ###. + const i = body.search(/