// Repairs the release-please state that its Gitea fork cannot recover from. // // Why this exists (evidence in OKF_Nuxt/decisions/release-engineering.md): // // Gitea rewrites a merged PR's `head.ref` to `refs/pull//head` when the // head branch is deleted. The fork parses that field as the release branch, // logs `Bad branch name`, creates no tag - and still exits successfully. // The merged PR keeps `autorelease: pending`, so every later run aborts with // `There are untagged, merged release PRs outstanding` and opens no new // release PR. That is a permanent deadlock, not a transient failure. // // This script runs before the action and reconciles the state: // // * a merged pending PR whose merge commit already has the expected // `v` tag -> swap `autorelease: pending` for // `autorelease: tagged` (the release exists, only the label is stale); // * a merged pending PR whose head branch is gone and has no expected tag // -> create the tag from the version recorded in the manifest at that // merge commit. This is exactly the tag release-please would have made if // the branch had survived; without it the PR blocks every future release; // * a merged pending PR whose head branch still exists -> leave it alone, // release-please tags it in the same run. // // Pending PRs are looked up by the label itself, not by paging through every // closed PR: the set is tiny (normally zero or one) and cannot race with PRs // closing while the pages are being read. // // Run locally against the public API with: // DRY_RUN=1 node scripts/reconcile-release-please.mjs // Reads work unauthenticated for a public repository; writes are skipped. const API_URL = process.env.GITHUB_API_URL const REPOSITORY = process.env.GITHUB_REPOSITORY const TOKEN = process.env.GITEA_TOKEN const DRY_RUN = process.env.DRY_RUN === '1' || process.argv.includes('--dry-run') const PENDING = 'autorelease: pending' const TAGGED = 'autorelease: tagged' const MANIFEST = '.release-please-manifest.json' if (!API_URL || !REPOSITORY) { console.error('::error::GITHUB_API_URL and GITHUB_REPOSITORY must be set') process.exit(1) } if (!TOKEN && !DRY_RUN) { console.error('::error::GITEA_TOKEN is required (or set DRY_RUN=1)') process.exit(1) } const repoUrl = `${API_URL}/repos/${REPOSITORY}` const api = async (path, init = {}) => { const response = await fetch(`${repoUrl}${path}`, { ...init, headers: { Accept: 'application/json', ...(TOKEN ? { Authorization: `token ${TOKEN}` } : {}), ...(init.headers ?? {}), }, }) const body = await response.text() if (!response.ok) { throw new Error(`${init.method ?? 'GET'} ${path} -> HTTP ${response.status}: ${body.slice(0, 300)}`) } return body ? JSON.parse(body) : null } // Gitea caps page size at 50. The label-filtered issue list is normally one // page; loop anyway so a backlog of stale pending PRs is still reconciled. const listAll = async (path, params = {}) => { const items = [] for (let page = 1; ; page++) { const query = new URLSearchParams({ ...params, limit: '50', page: String(page) }) const batch = await api(`${path}?${query}`) items.push(...batch) if (batch.length < 50) return items } } const replaceLabels = async (number, labels) => { if (DRY_RUN) { console.log(`[dry-run] #${number}: labels -> ${labels.join(', ')}`) return } await api(`/issues/${number}/labels`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ labels }), }) } const createTag = async (name, sha) => { if (DRY_RUN) { console.log(`[dry-run] create tag ${name} at ${sha}`) return } await api('/tags', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ tag_name: name, target: sha, message: `Release ${name}` }), }) } const versionAt = async (sha) => { const file = await api(`/contents/${MANIFEST}?ref=${sha}`) if (!file?.content) throw new Error(`no manifest content at ${sha}`) const manifest = JSON.parse(Buffer.from(file.content, 'base64').toString('utf8')) // Single-package repository: the root path is ".". Keep a fallback so a // future second package cannot silently produce `vundefined`. const version = manifest['.'] ?? Object.values(manifest)[0] if (!version) throw new Error(`manifest at ${sha} has no version`) return version } const run = async () => { // One tag can point at the merge commit for a non-release reason (CI // tooling, a manual annotation). Only the `v` tag proves // the release exists; every other tag at that commit is ignored. const tags = await listAll('/tags') const tagsAtSha = new Map() const tagSha = new Map() for (const tag of tags) { const sha = tag.commit?.sha if (!sha) continue if (!tagsAtSha.has(sha)) tagsAtSha.set(sha, []) tagsAtSha.get(sha).push(tag.name) tagSha.set(tag.name, sha) } const pendingIssues = await listAll('/issues', { state: 'closed', type: 'pulls', labels: PENDING, }) if (pendingIssues.length === 0) { console.log('No merged release PR is pending - nothing to reconcile.') return } const failures = [] const versionCache = new Map() for (const issue of pendingIssues) { const pull = await api(`/pulls/${issue.number}`) if (!pull.merged_at) continue const mergeSha = pull.merge_commit_sha if (!mergeSha) { failures.push(`#${pull.number}: merged PR has no merge_commit_sha`) continue } const headRef = pull.head?.ref ?? '' const branchGone = headRef.startsWith('refs/pull/') || headRef === '' const labels = (pull.labels ?? []).map(label => label.name) if (!versionCache.has(mergeSha)) versionCache.set(mergeSha, await versionAt(mergeSha)) const expectedTag = `v${versionCache.get(mergeSha)}` const tagsHere = tagsAtSha.get(mergeSha) ?? [] if (tagsHere.includes(expectedTag)) { console.log(`#${pull.number}: ${expectedTag} already points at merge commit ${mergeSha} - marking as tagged`) const next = labels.filter(name => name !== PENDING) if (!next.includes(TAGGED)) next.push(TAGGED) await replaceLabels(pull.number, next) continue } if (tagsHere.length > 0) { console.log(`#${pull.number}: ignoring non-release tag(s) at merge commit: ${tagsHere.join(', ')}`) } if (!branchGone) { console.log(`#${pull.number}: head branch ${headRef} still exists - leaving it for release-please to tag`) continue } // The branch is gone and the expected tag is missing, so release-please // will never get past `Bad branch name` for this PR. Recreate the tag it // should have made, using the version the merged release PR wrote into the // manifest. try { if (tagSha.has(expectedTag)) { throw new Error(`${expectedTag} already exists at ${tagSha.get(expectedTag)}, not at merge commit ${mergeSha}; refusing to move a release tag`) } console.log(`#${pull.number}: head branch is gone (${headRef}) - creating ${expectedTag} at ${mergeSha}`) await createTag(expectedTag, mergeSha) const next = labels.filter(name => name !== PENDING) if (!next.includes(TAGGED)) next.push(TAGGED) await replaceLabels(pull.number, next) } catch (error) { failures.push(`#${pull.number}: ${error.message}`) } } if (failures.length > 0) { for (const failure of failures) console.error(`::error::${failure}`) console.error('::error::release-please state is unrecoverable automatically; repair the PR/tag by hand before the next release') process.exit(1) } } run().catch((error) => { console.error(`::error::${error.message}`) process.exit(1) })