github-to-rithub/bin/github-to-rithub.js
caelum0x 8af41526c5 feat: github-to-rithub CLI — mirror GitHub repos to Rithub with dry-run/resume/report
Zero-dependency Node CLI that lists a user's GitHub repos and migrates
selected ones to Rithub via Rithub's own (Gitea) /repos/migrate API,
carrying code, branches, tags, issues, labels, milestones, releases and
wiki. Adds dry-run, a resumable state file, and JSON/Markdown reports.
Includes 3 honest migration guides (suspended-account recovery, GitHub
Actions -> Rithub CI, keeping a GitHub mirror).

Real-tested 2026-11-17: migrated 3 live public GitHub repos
(basic-rag, defi-social-app, house-price-prediction) to a new
github-migration-test org on rithub.dev; verified branches/clone over
the live Rithub API; confirmed --resume skip and existing-repo
collision skip both work for real. Fixed a real bug found during
testing: repeated --repo flags were overwriting each other instead of
accumulating.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 18:23:16 +03:00

170 lines
6.5 KiB
JavaScript

#!/usr/bin/env node
// github-to-rithub — mirror GitHub repos to Rithub (or any Gitea-API
// compatible server) using your own tokens. Zero dependencies.
// See README.md for full usage.
import { writeFile } from "node:fs/promises";
import { listGithubRepos, getGithubRepo } from "../src/github.js";
import { getRithubAuthedUser, rithubRepoIsFree, migrateRepo } from "../src/rithub.js";
import { resolveStatePath, loadState, saveState, markCompleted, markFailed, isAlreadyDone } from "../src/state.js";
import { buildReport, formatReportMarkdown } from "../src/report.js";
function parseArgs(argv) {
const args = { _: [] };
for (let i = 0; i < argv.length; i += 1) {
const token = argv[i];
if (token.startsWith("--")) {
const key = token.slice(2);
const next = argv[i + 1];
if (next === undefined || next.startsWith("--")) {
args[key] = true;
} else if (key in args) {
args[key] = [...(Array.isArray(args[key]) ? args[key] : [args[key]]), next];
i += 1;
} else {
args[key] = next;
i += 1;
}
} else {
args._.push(token);
}
}
return args;
}
function requireEnv(name) {
const value = process.env[name];
if (!value) {
console.error(`Missing required environment variable: ${name}`);
process.exit(1);
}
return value;
}
function printUsage() {
console.log(`github-to-rithub — move your repos from GitHub to Rithub
Usage:
github-to-rithub list --github-user <user>
github-to-rithub migrate --github-user <user> --rithub-owner <owner> [--repo <name> ...] [--all]
[--rithub-url https://rithub.dev] [--dry-run] [--resume] [--state <path>]
[--report <path.json>] [--report-md <path.md>]
Environment:
GITHUB_TOKEN GitHub personal access token (read scope is enough: repo, read:org)
RITHUB_TOKEN Rithub personal access token for the target account/org
Commands:
list List the repos your GitHub token can see (no writes anywhere).
migrate Migrate selected repos to Rithub via Rithub's own /repos/migrate API.
`);
}
async function cmdList(args) {
const githubToken = requireEnv("GITHUB_TOKEN");
const repos = await listGithubRepos(githubToken);
for (const repo of repos) {
console.log(`${repo.full_name}\t${repo.private ? "private" : "public"}\t${repo.html_url}`);
}
console.log(`\n${repos.length} repo(s) visible to this token.`);
}
function selectRepos(allRepos, args) {
if (args.all) return allRepos;
const requested = Array.isArray(args.repo) ? args.repo : args.repo ? [args.repo] : [];
if (requested.length === 0) {
console.error("Pass --repo <name> one or more times, or --all. Refusing to migrate nothing/everything by accident.");
process.exit(1);
}
return allRepos.filter((repo) => requested.includes(repo.name) || requested.includes(repo.full_name));
}
async function migrateOne({ repo, args, githubToken, rithubToken, rithubUrl, rithubOwner, state }) {
const fullName = repo.full_name;
if (args.resume && isAlreadyDone(state, fullName)) {
return { repo: fullName, status: "skipped-already-done" };
}
const targetName = repo.name;
const free = await rithubRepoIsFree(rithubUrl, rithubToken, rithubOwner, targetName);
if (!free) {
return { repo: fullName, status: "skipped-existing", rithubUrl: `${rithubUrl}/${rithubOwner}/${targetName}` };
}
if (args["dry-run"]) {
return { repo: fullName, status: "dry-run", rithubUrl: `${rithubUrl}/${rithubOwner}/${targetName} (not created)` };
}
try {
const detail = await getGithubRepo(repo.owner.login, repo.name, githubToken);
await migrateRepo(rithubUrl, rithubToken, {
cloneUrl: detail.clone_url,
githubToken,
repoOwner: rithubOwner,
repoName: targetName,
isPrivate: detail.private,
description: detail.description,
});
return { repo: fullName, status: "migrated", rithubUrl: `${rithubUrl}/${rithubOwner}/${targetName}` };
} catch (error) {
return { repo: fullName, status: "failed", error: error instanceof Error ? error.message : String(error) };
}
}
async function cmdMigrate(args) {
const githubToken = requireEnv("GITHUB_TOKEN");
const rithubToken = requireEnv("RITHUB_TOKEN");
const rithubUrl = String(args["rithub-url"] || "https://rithub.dev").replace(/\/$/, "");
const rithubOwner = args["rithub-owner"];
if (!rithubOwner) {
console.error("Pass --rithub-owner <account or org on Rithub>");
process.exit(1);
}
const authedUser = await getRithubAuthedUser(rithubUrl, rithubToken);
console.log(`Authenticated to ${rithubUrl} as ${authedUser.login}. Target namespace: ${rithubOwner}.`);
const githubUser = args["github-user"];
const allRepos = await listGithubRepos(githubToken);
const scoped = githubUser ? allRepos.filter((r) => r.owner.login === githubUser) : allRepos;
const selected = selectRepos(scoped, args);
const statePath = resolveStatePath(args.state);
let state = await loadState(statePath);
console.log(`\nMigrating ${selected.length} repo(s) from GitHub to ${rithubUrl}/${rithubOwner} ${args["dry-run"] ? "(DRY RUN)" : ""}`);
const startedAt = new Date().toISOString();
const results = [];
for (const repo of selected) {
console.log(`-> ${repo.full_name}`);
const result = await migrateOne({ repo, args, githubToken, rithubToken, rithubUrl, rithubOwner, state });
results.push(result);
console.log(` ${result.status}${result.rithubUrl ? " " + result.rithubUrl : ""}${result.error ? " " + result.error : ""}`);
if (result.status === "migrated") state = markCompleted(state, repo.full_name, { rithubUrl: result.rithubUrl });
if (result.status === "failed") state = markFailed(state, repo.full_name, result.error);
if (!args["dry-run"]) await saveState(statePath, state);
}
const finishedAt = new Date().toISOString();
const report = buildReport({ startedAt, finishedAt, baseUrl: rithubUrl, rithubOwner, results });
console.log(`\n${JSON.stringify(report.totals, null, 2)}`);
if (args.report) await writeFile(args.report, JSON.stringify(report, null, 2));
if (args["report-md"]) await writeFile(args["report-md"], formatReportMarkdown(report));
}
async function main() {
const args = parseArgs(process.argv.slice(2));
const command = args._[0];
if (command === "list") return cmdList(args);
if (command === "migrate") return cmdMigrate(args);
printUsage();
process.exit(command ? 1 : 0);
}
main().catch((error) => {
console.error(error instanceof Error ? error.message : error);
process.exit(1);
});