import {pathToFileURL} from 'node:url'; const API='https://www.dunerealms.com/git/api/v1/repos/unimatrix/web'; const LIVE='https://www.dunerealms.com/ci-build.json'; const SHA=/^[0-9a-f]{40}$/; // The deployment trigger is asynchronous. Never prune on its acknowledgement: // the exact compiled build must first be served by the public website. export async function prunePublishedArtifacts({sha,runId,token,request=fetch,wait=ms=>new Promise(resolve=>setTimeout(resolve,ms)),attempts=40}){ if(!SHA.test(sha||'')||!/^\d+$/.test(String(runId))||!token)throw Error('Missing CI publication identity or token'); let live=false; for(let i=0;i{ const response=await request(API+path,{method,headers:{Authorization:'token '+token,Accept:'application/json'},signal:AbortSignal.timeout(20000)}); if(!response.ok)throw Error('CI artifact API returned '+response.status); return method==='DELETE'?null:response.json(); }; const run=await api('/actions/runs/'+runId); if(run.commit_sha!==sha||run.prettyref!=='main')throw Error('Refusing cleanup for a different commit or branch'); const artifacts=[]; for(let page=1;;page++){ if(page>100)throw Error('Artifact pagination exceeded safety bound'); const rows=await api('/actions/artifacts?limit=50&page='+page); if(!Array.isArray(rows))throw Error('Unexpected artifact inventory'); artifacts.push(...rows); if(!rows.length)break; } const current=artifacts.filter(a=>a.name==='DuneRealms-Website'&&String(a.run_id)===String(runId)); if(!current.length)throw Error('Current publication artifact not found; keeping previous builds'); const cutoff=Math.min(...current.map(a=>Date.parse(a.created_at))); if(!Number.isFinite(cutoff))throw Error('Invalid current artifact date'); const previous=artifacts.filter(a=>a.name==='DuneRealms-Website'&&String(a.run_id)!==String(runId)&&Date.parse(a.created_at)