// Copyright (C) 2026 Zaher Salman // SPDX-License-Identifier: GPL-3.0-or-later async function checkPackageRun(env = process.env, request = fetch) { const { GITEA_SERVER_URL, GITEA_REPOSITORY, PACKAGE_RUN_ID, ACTIONS_TOKEN } = env; if (!/^[1-9][0-9]*$/.test(PACKAGE_RUN_ID || '')) throw new Error('A numeric packaging workflow run ID is required'); if (!/^[\w.-]+\/[\w.-]+$/.test(GITEA_REPOSITORY || '')) throw new Error('Expected owner/repository'); if (!ACTIONS_TOKEN) throw new Error('An Actions read token is required'); const server = new URL(GITEA_SERVER_URL); if (server.protocol !== 'https:' || server.username || server.password || server.search || server.hash) { throw new Error('Expected an HTTPS Gitea server URL without credentials, query, or fragment'); } const base = `${server.href.replace(/\/$/, '')}/api/v1/repos/${GITEA_REPOSITORY}/actions/runs/${PACKAGE_RUN_ID}`; const headers = { Authorization: `token ${ACTIONS_TOKEN}` }; const response = await request(base, { headers, redirect: 'error', signal: AbortSignal.timeout(60000) }); if (!response.ok) throw new Error(`Cannot inspect packaging run: HTTP ${response.status}`); const run = await response.json(); const workflow = String(run.path || '').split('@')[0].split('/').pop(); const platform = env.PACKAGE_PLATFORM || 'linux'; if (!['linux', 'windows'].includes(platform)) throw new Error('Expected linux or windows package platform'); const expectedWorkflow = platform === 'windows' ? 'build-windows.yml' : 'build-and-package.yml'; const artifactName = platform === 'windows' ? 'musrflow-windows-x64' : 'musrflow-linux-packages'; if (workflow !== expectedWorkflow) { throw new Error(`Run ${PACKAGE_RUN_ID} is not ${platform === 'windows' ? 'Build Windows installer' : 'Build and package'} (${run.path || 'unknown workflow'}). Select the matching platform and packaging run ID, not the runtime run ID`); } if (!['success', 'failure'].includes(run.conclusion)) { throw new Error('The packaging run must finish before publishing its artifacts'); } // A registry failure marks the whole combined run failed, but its package // artifact was uploaded only after tests, builds, and collection succeeded. const artifactResponse = await request(`${base}/artifacts?name=${artifactName}`, { headers, redirect: 'error', signal: AbortSignal.timeout(60000) }); if (!artifactResponse.ok) throw new Error(`Cannot inspect package artifacts: HTTP ${artifactResponse.status}`); const listing = await artifactResponse.json(); const matches = (listing.artifacts || []).filter(artifact => artifact.name === artifactName); if (matches.length !== 1) throw new Error(`Run ${PACKAGE_RUN_ID} must contain one ${artifactName} artifact; check its upload step`); const artifact = matches[0]; if (artifact.expired || (artifact.expires_at && Date.parse(artifact.expires_at) <= Date.now())) { throw new Error(`The package artifact for run ${PACKAGE_RUN_ID} has expired`); } console.log(`Selected packaging run ${PACKAGE_RUN_ID}, artifact ${artifact.id}: ${artifact.name}`); if (run.conclusion === 'failure') console.log('Retrying publication from the saved package artifact; no rebuild needed'); return artifact; } module.exports = { checkPackageRun }; if (require.main === module) { checkPackageRun().catch(error => { console.error(error.message); process.exitCode = 1; }); }