## Problem and outcome / 问题与结果
Preparing the missing v2.7.0 mirror fetched AtomGit repository metadata
anonymously and failed with HTTP 401 before reaching tag creation.
Publication preparation now uses the configured token for its GET
requests, including the repository endpoint, while the public read-only
readiness helper remains anonymous.
GET 404 still means absent. Preparation uses one HTTP owner, and
mutation errors are not retried.
## Verification / 验证
- The new mocked endpoint regression reproduces the original 401 before
the change and passes afterward; it also checks that public readiness
remains anonymous.
- Publisher, preparation/readiness and source tests: 65 passed.
TypeScript, diff and repository hygiene checks passed.
- Actual changed paths are release tooling and tests only:
`hasProductChanges(e25bd009, HEAD)` is false. With the published v2.7.0
tag fetched, the planner returns `publish: false`; the existing v2.7.0
packages are not rebuilt or replaced.
## Impact / 影响
No App/Server behavior, dependencies, wire or data format changes.
Recovery continues from the original v2.7.0 CI artifacts and source
marker. AtomGit's separate Pull-mirror tag restriction may remain the
next platform blocker; this repair does not bypass it.
68 lines
3.8 KiB
JavaScript
68 lines
3.8 KiB
JavaScript
// Prepare the attachment container before downloading original build artifacts.
|
|
// The local tag locates attachments; their original source
|
|
// identity comes from the GitHub marker and verified package digests.
|
|
import { execFileSync } from "node:child_process";
|
|
import { appendFileSync } from "node:fs";
|
|
import { pathToFileURL } from "node:url";
|
|
import { mirrorReadiness, validateMirrorSource, validateMirrorRelease } from "./release-mirror-ready.mjs";
|
|
|
|
async function requestMirror(path, method = "GET", body) {
|
|
const token = process.env.ATOMGIT_TOKEN?.trim();
|
|
if (!token) throw new Error("ATOMGIT_TOKEN is required to prepare the mirror before downloading artifacts");
|
|
const response = await fetch(`https://api.atomgit.com/api/v5/repos/TNTcraftHIM/Piik${path}`, {
|
|
method, redirect: "error", signal: AbortSignal.timeout(30_000),
|
|
headers: { Accept: "application/json", "PRIVATE-TOKEN": token,
|
|
...(body ? { "Content-Type": "application/json" } : {}) },
|
|
body: body ? JSON.stringify(body) : undefined,
|
|
});
|
|
if (response.status === 404 && method === "GET") return null;
|
|
if (!response.ok) {
|
|
const detail = await response.json().catch(() => null);
|
|
const reason = detail?.message ?? detail?.error_message ?? detail?.error;
|
|
const message = typeof reason === "string" ? reason.replaceAll(token, "[redacted]").slice(0, 500) : "";
|
|
throw new Error(`AtomGit preparation ${method} ${path}: HTTP ${response.status}${message ? `: ${message}` : ""}. Mirror publication remains incomplete.`);
|
|
}
|
|
return response.json();
|
|
}
|
|
|
|
export function mirrorReleaseBody(source) {
|
|
const revision = source.target_commitish;
|
|
return `${source.body ?? ""}\n\n此镜像分发 GitHub 的原始安装包;源码以 GitHub 为准。 / This mirror distributes the original GitHub packages; GitHub owns their source.\n\nSource: https://github.com/TNTcraftHIM/Piik/commit/${revision}\n\n<!-- piik-source: ${revision} -->\n`;
|
|
}
|
|
|
|
export async function prepareMirrorRelease(source, request = requestMirror, existing) {
|
|
validateMirrorSource(source);
|
|
const version = source.tag_name;
|
|
let release = existing === undefined ? await request(`/releases/tags/${encodeURIComponent(version)}`) : existing;
|
|
if (!release) {
|
|
const repo = await request("");
|
|
if (!repo || repo.private || !repo.default_branch) throw new Error("A public initialized AtomGit mirror is required");
|
|
release = await request("/releases", "POST", {
|
|
tag_name: version, name: source.name || `Piik ${version}`,
|
|
body: mirrorReleaseBody(source),
|
|
target_commitish: repo.default_branch, release_status: "pre",
|
|
});
|
|
if (release?.prerelease !== true) throw new Error("AtomGit did not confirm the prepared prerelease; mirror publication remains incomplete");
|
|
}
|
|
validateMirrorRelease(source, release);
|
|
return release;
|
|
}
|
|
|
|
export async function prepareMirror(source, request = requestMirror) {
|
|
const result = await mirrorReadiness(source, path => request(path));
|
|
if (result.status !== "complete") await prepareMirrorRelease(source, request);
|
|
return result;
|
|
}
|
|
|
|
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
const source = JSON.parse(execFileSync("gh", ["api", "repos/TNTcraftHIM/Piik/releases/latest"], {
|
|
encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], timeout: 30_000,
|
|
}));
|
|
const result = await prepareMirror(source);
|
|
if (process.env.GITHUB_OUTPUT) appendFileSync(process.env.GITHUB_OUTPUT, `status=${result.status}\n`);
|
|
if (process.env.GITHUB_STEP_SUMMARY) appendFileSync(process.env.GITHUB_STEP_SUMMARY,
|
|
result.status === "complete" ? `Mirror ${result.version} is already stable with the complete published attachment inventory.\n`
|
|
: `Mirror ${result.version} is prepared to resume from original CI artifacts; it remains incomplete until all packages pass verification.\n`);
|
|
console.log(JSON.stringify(result));
|
|
}
|