#!/usr/bin/env zx // vim: set filetype=javascript : import { URL } from "url"; import { Codeberg } from "./zx/codeberg.mjs"; import { GitLab } from "./zx/gitlab.mjs"; $.verbose = false; const ask_bool = async (prompt) => { return await question(`${prompt} [y/N] `).then((answer) => { return answer.toLowerCase() == "y"; }); }; const allowed_default_branches = ["main", "master"]; async function get_repo_info() { let ret; ret = await $`git branch --show-current`; const current_branch = ret.stdout.trim(); if (!allowed_default_branches.includes(current_branch)) { throw new Error( `Current branch: ${current_branch}, expected: ${allowed_default_branches.join( "/", )}`, ); } ret = await $`git remote`; const remote_names = ret.stdout.trim().split("\n"); const remote = {}; function parseRemoteUrl(url) { if (url.startsWith("git@")) { url = url.replace(/^git@([^:]+):/, "https://$1/"); } try { return new URL(url); } catch { return null; } } for (const remote_name of remote_names) { remote[remote_name] = {}; ret = await $`git remote get-url --all ${remote_name}`; remote[remote_name]["fetch"] = ret.stdout .trim() .split("\n") .map(parseRemoteUrl) .filter(Boolean); ret = await $`git remote get-url --all --push ${remote_name}`; remote[remote_name]["push"] = ret.stdout .trim() .split("\n") .map(parseRemoteUrl) .filter(Boolean); remote[remote_name].url = remote[remote_name]["fetch"][0]; } const project = remote.origin.fetch[0].pathname .replace(/^\//, "") .replace(/\.git$/, ""); const [owner, name] = project.split("/"); return { project, owner, name, remote, current_branch, }; } async function refresh_repo_info(repo) { Object.assign(repo, await get_repo_info()); } async function ensure_git_mirror_remote(repo, name, remote_url) { const is_ssh = remote_url.startsWith("git@"); const host = is_ssh ? remote_url.match(/^git@([^:]+):/)?.[1] : new URL(remote_url).host; const url = is_ssh ? remote_url : new URL(remote_url).href; if (repo.remote.origin.url.host === host) { return; } if (repo.remote[`origin-${name}`]) { if (repo.remote[`origin-${name}`].url == url) { return; } await $`git remote remove origin-${name}`; } await $`git remote add origin-${name} ${url}`; await refresh_repo_info(repo); } async function init_codeberg(repo) { const codeberg = await new Codeberg({ id: "git-mirror" }).init(); const get_mirror_project_settings = () => ({ description: `Mirroring: ${repo.remote.origin.url.href}`, issues: false, labels: false, lfs: false, milestones: false, pull_requests: false, releases: false, service: "git", wiki: false, }); async function create_mirror({ try_import = true, is_public = null } = {}) { codeberg.log.info(`Creating project: ${repo.project}...`); if (typeof is_public !== "boolean") { is_public = await ask_bool("Make project public?"); } const settings = get_mirror_project_settings(repo); const res = try_import ? await codeberg.api("POST /repos/migrate", { body: { ...settings, repo_name: repo.name, clone_addr: repo.remote.origin.url.href, private: !is_public, }, }) : await codeberg.api("POST /user/repos", { body: { auto_init: false, description: settings.description, name: repo.name, private: !is_public, }, }); if (res.ok) { codeberg.log.info(`Created project: ${repo.project}...`); return res.data; } else if (try_import && res.status === 422) { codeberg.log.error(`Failed to create project: ${repo.project}...`); codeberg.log.error(` Reason: ${res.data.message}`); await codeberg.api(`DELETE /repos/${repo.project}`); codeberg.log.info(`Trying again w/o initial import...`); return create_mirror({ try_import: false, is_public }); } else { codeberg.log.error(`Failed to create project: ${repo.project}...`); throw res; } } async function update_mirror(project) { const settings = get_mirror_project_settings(); const res = await codeberg.api( `PATCH /repos/${project.owner.login}/${project.name}`, { body: { description: settings.description, has_actions: settings.actions ?? false, has_issues: settings.issues, has_packages: settings.packages ?? false, has_projects: settings.projects ?? false, has_pull_requests: settings.pull_requests, has_releases: settings.releases, has_wiki: settings.wiki, }, }, ); if (!res.ok) { throw res; } } async function ensure_mirror() { const res = await codeberg.api(`/repos/${repo.project}`); let project; if (res.ok) { codeberg.log.info(`Project Found: ${repo.project}`); project = res.data; } else { project = await create_mirror(); } await update_mirror(project); codeberg.log.info(`Synced Settings...`); } await ensure_mirror(); const remote_url = `https://codeberg.org/${repo.project}.git`; await ensure_git_mirror_remote(repo, "codeberg", remote_url); } async function init_gitlab(repo) { const gitlab = await new GitLab({ id: "git-mirror" }).init(); const get_mirror_project_settings = () => ({ description: `Mirroring: ${repo.remote.origin.url.href}`, analytics_access_level: "disabled", builds_access_level: "disabled", container_registry_access_level: "disabled", feature_flags_access_level: "disabled", forking_access_level: "disabled", infrastructure_access_level: "disabled", issues_access_level: "disabled", merge_requests_access_level: "disabled", monitor_access_level: "disabled", operations_access_level: "disabled", packages_enabled: false, pages_access_level: "disabled", requirements_access_level: "disabled", security_and_compliance_access_level: "disabled", snippets_access_level: "disabled", wiki_access_level: "disabled", }); async function create_mirror({ try_import = true, is_public = null } = {}) { gitlab.log.info(`Creating project: ${repo.project}...`); if (typeof is_public !== "boolean") { is_public = await ask_bool("Make project public?"); } const import_url = try_import ? repo.remote.origin.url.href : null; const res = await gitlab.api("POST /projects", { body: { name: repo.name, import_url, visibility: is_public ? "public" : "private", ...get_mirror_project_settings(repo), }, }); if (res.ok) { gitlab.log.info(`Created project: ${repo.project}...`); } else if (try_import && res.status === 422) { gitlab.log.error(`Failed to create project: ${repo.project}...`); gitlab.log.error(` Reason: ${res.data.message}`); gitlab.log.info(`Trying again w/o initial import...`); return create_mirror({ try_import: false, is_public }); } else { gitlab.log.error(`Failed to create project: ${repo.project}...`); throw res; } } async function update_mirror(project) { const res = await gitlab.api(`PUT /projects/${project.id}`, { body: get_mirror_project_settings(), }); if (!res.ok) { throw res; } } async function ensure_mirror() { const res = await gitlab.api( `/projects/${encodeURIComponent(repo.project)}`, ); if (res.ok) { await update_mirror(res.data); gitlab.log.info(`Project Found: ${repo.project}`); return; } await create_mirror(); } async function remove_mirror_branch_protection() { const res = await gitlab.api( `/projects/${encodeURIComponent(repo.project)}/protected_branches`, ); for (const item of res.data) { await gitlab.api( `DELETE /projects/${encodeURIComponent( repo.project, )}/protected_branches/${item.name}`, ); gitlab.log.info(`Removed branch protection: ${item.name}`); } } await ensure_mirror(); await remove_mirror_branch_protection(); const remote_url = `https://gitlab.com/${repo.project}.git`; await ensure_git_mirror_remote(repo, "gitlab", remote_url); } async function init_tangled(repo) { const handle = "muniftanjim.dev"; const remote_url = `git@tangled.org:${handle}/${repo.name}`; await ensure_git_mirror_remote(repo, "tangled", remote_url); } const REMOTE_SERVICE = { codeberg: { init: init_codeberg, has_auto_sync: false, }, gitlab: { init: init_gitlab, has_auto_sync: false, }, tangled: { init: init_tangled, has_auto_sync: false, opt_in: true, }, }; async function init(repo) { for (const remote of Object.values(enabled_services)) { await remote.init(repo); } } async function sync(repo) { $.verbose = true; for (const remote of Object.keys(enabled_services)) { if ( !repo.remote[`origin-${remote}`] || enabled_services[remote].has_auto_sync ) { continue; } await $`git push origin-${remote} --force --all --follow-tags`; // push non-annotated tags await $`git push origin-${remote} --force --tags`; } $.verbose = false; } const repo = await get_repo_info(); // opt-in services are only included when their `--` flag is passed // or their `origin-` remote already exists const enabled_services = Object.fromEntries( Object.entries(REMOTE_SERVICE).filter( ([name, service]) => !service.opt_in || argv[name] || repo.remote[`origin-${name}`], ), ); switch (argv._[0]) { case "init": { await init(repo); break; } case "sync": { await sync(repo); break; } default: { if ( Object.keys(enabled_services).some( (remote) => !repo.remote[`origin-${remote}`], ) ) { await init(repo); } await sync(repo); } }