import { execFileSync } from "node:child_process"; import { appendFileSync } from "node:fs"; import { pathToFileURL } from "node:url"; import { isReleaseVersion } from "./release-version.mjs"; const repository = "TNTcraftHIM/Piik"; const gh = (path) => JSON.parse(execFileSync("gh", ["api", `repos/${repository}/${path}`], { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], timeout: 30_000, })); export function resolveMirrorSource(runId, automatic = false) { if (!runId && automatic) { const latest = gh("releases/latest"); if (latest.draft !== false || latest.prerelease !== false || !isReleaseVersion(latest.tag_name) || !/^[a-f0-9]{40}$/.test(latest.target_commitish)) throw new Error("A published stable GitHub release with a full source SHA is required"); const runs = gh(`actions/workflows/repository-hygiene.yml/runs?event=push&branch=main&status=completed&head_sha=${latest.target_commitish}&per_page=100`); const source = runs.workflow_runs?.find(run => run.head_sha === latest.target_commitish); if (!source) throw new Error("The published release's original completed CI run is unavailable; do not rebuild its packages"); return resolveMirrorSource(String(source.id), true); } if (!/^[1-9]\d*$/.test(runId) || !Number.isSafeInteger(Number(runId))) { throw new Error("Provide the original CI run's numeric ID"); } const run = gh(`actions/runs/${runId}`); if (run.id !== Number(runId) || run.repository?.full_name !== repository || run.head_repository?.full_name !== repository || run.event !== "push" || run.head_branch !== "main" || run.path !== ".github/workflows/repository-hygiene.yml" || run.status !== "completed" || !/^[a-f0-9]{40}$/.test(run.head_sha)) { throw new Error("Mirror artifacts must come from a completed main push in this repository's CI workflow"); } // A later publication step can fail after GitHub has published the packages. // The matching stable release owns mirror identity, not the CI aggregate result. const release = gh("releases/latest"); if (release.draft !== false || release.prerelease !== false || !isReleaseVersion(release.tag_name) || !/^[a-f0-9]{40}$/.test(release.target_commitish)) { throw new Error("A published stable GitHub release with a full source SHA is required"); } if (release.target_commitish !== run.head_sha) { if (automatic) return null; throw new Error("This CI run does not match the latest GitHub release; use its original publishing run"); } return { run_id: String(run.id), version: release.tag_name, revision: run.head_sha }; } if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { const source = resolveMirrorSource(process.argv[2], ["workflow_run", "schedule"].includes(process.env.GITHUB_EVENT_NAME) || !process.argv[2]); const output = { mirror: source !== null, ...source }; if (process.env.GITHUB_OUTPUT) { appendFileSync(process.env.GITHUB_OUTPUT, Object.entries(output).map(([key, value]) => `${key}=${value}\n`).join("")); } if (process.env.GITHUB_STEP_SUMMARY) { appendFileSync(process.env.GITHUB_STEP_SUMMARY, source ? `Mirror ${source.version} from original [CI run ${source.run_id}](https://github.com/${repository}/actions/runs/${source.run_id}).\n` : "This CI run did not publish the current GitHub release; no mirror work is needed.\n"); } console.log(JSON.stringify(output)); }