95 lines
2.9 KiB
JavaScript
95 lines
2.9 KiB
JavaScript
// --to rithub: mirror straight from GitHub into a Rithub account using
|
|
// Rithub's own migrate API (POST /api/v1/repos/migrate, HTTP Basic auth
|
|
// with a Rithub access token as the password). Same request shape proven
|
|
// live on rithub.dev: service "github", mirror: true, auth_token is the
|
|
// GitHub token (omit for public repos).
|
|
|
|
function authHeader(rithubUser, rithubToken) {
|
|
const basic = Buffer.from(`${rithubUser}:${rithubToken}`).toString("base64");
|
|
return { Authorization: `Basic ${basic}`, "User-Agent": "github-backup-cli" };
|
|
}
|
|
|
|
async function rithubRequest(host, method, path, rithubUser, rithubToken, body) {
|
|
const res = await fetch(`https://${host}${path}`, {
|
|
method,
|
|
headers: {
|
|
...authHeader(rithubUser, rithubToken),
|
|
...(body ? { "Content-Type": "application/json" } : {}),
|
|
},
|
|
body: body ? JSON.stringify(body) : undefined,
|
|
});
|
|
const text = await res.text().catch(() => "");
|
|
return { status: res.status, text };
|
|
}
|
|
|
|
export async function repoExistsOnRithub({ host, rithubUser, rithubToken, name }) {
|
|
const { status } = await rithubRequest(
|
|
host,
|
|
"GET",
|
|
`/api/v1/repos/${rithubUser}/${name}`,
|
|
rithubUser,
|
|
rithubToken,
|
|
);
|
|
return status === 200;
|
|
}
|
|
|
|
// disableActionsForMirror: best-effort. Gitea-family migrate has no
|
|
// create-time flag to skip Actions, and a mirrored repo with many tags
|
|
// can queue a CI run per historical ref (seen live on railyard-box — see
|
|
// plans/railyard.md). Never throws: a failed PATCH just leaves Actions on.
|
|
async function disableActionsForMirror({ host, rithubUser, rithubToken, name }) {
|
|
try {
|
|
await rithubRequest(
|
|
host,
|
|
"PATCH",
|
|
`/api/v1/repos/${rithubUser}/${name}`,
|
|
rithubUser,
|
|
rithubToken,
|
|
{ has_actions: false },
|
|
);
|
|
} catch {
|
|
// intentionally swallowed — cosmetic cleanup only
|
|
}
|
|
}
|
|
|
|
// migrateRepoToRithub: one repo, one migrate call. Returns a short status
|
|
// string for the caller to print; never throws on an ordinary API error
|
|
// so one failed repo doesn't abort the whole run.
|
|
export async function migrateRepoToRithub({
|
|
repo,
|
|
host,
|
|
rithubUser,
|
|
rithubToken,
|
|
githubToken,
|
|
}) {
|
|
const body = {
|
|
clone_addr: repo.clone_url,
|
|
repo_name: repo.name,
|
|
repo_owner: rithubUser,
|
|
service: "github",
|
|
mirror: true,
|
|
mirror_interval: "8h",
|
|
private: Boolean(repo.private),
|
|
wiki: true,
|
|
issues: true,
|
|
pull_requests: true,
|
|
releases: true,
|
|
labels: true,
|
|
milestones: true,
|
|
...(githubToken ? { auth_token: githubToken } : {}),
|
|
};
|
|
const { status, text } = await rithubRequest(
|
|
host,
|
|
"POST",
|
|
"/api/v1/repos/migrate",
|
|
rithubUser,
|
|
rithubToken,
|
|
body,
|
|
);
|
|
if (status >= 200 && status < 300) {
|
|
await disableActionsForMirror({ host, rithubUser, rithubToken, name: repo.name });
|
|
return { ok: true, status };
|
|
}
|
|
return { ok: false, status, detail: text.slice(0, 300) };
|
|
}
|