staging-tester / ocean-landing-serve / src/ lib/ live/ migration.ts

import type { GithubOrg, Move, SourceRepo } from '../migration.js';
import { request } from './http.js';
import { installUrl, loadInstallations, type Installation } from './github.js';

// GitHub's repository list leaves out some of what the picker shows; the server names those in `unknown`.
type ServerRepo = {
	org: string;
	name: string;
	visibility: 'public' | 'private';
	sizeMb: number;
	lfsMb: number | null;
	largest: { path: string; mb: number } | null;
	issues: number;
	pulls: number | null;
	releases: number | null;
	wiki: boolean;
	archived: boolean;
	forkOf: string | null;
	pushedAt: string;
	people: string[];
	behind: { webhooks: number; deployKeys: number; workflows: number } | null;
	unknown: string[];
};

type ServerOrgs = { account: string | null; orgs: GithubOrg[] };

export const sourceRepo = (r: ServerRepo, installation: number): SourceRepo => ({
	...r,
	lfsMb: r.lfsMb ?? 0,
	largest: r.largest ?? { path: '', mb: 0 },
	pulls: r.pulls ?? 0,
	releases: r.releases ?? 0,
	behind: r.behind ?? { webhooks: 0, deployKeys: 0, workflows: 0 },
	installation
});

export type GithubSide = { installations: Installation[]; account: string | null; orgs: GithubOrg[]; repos: SourceRepo[] };

// Every installation the signed-in user can reach, merged: one picker lists them all.
export async function loadGithub(): Promise<GithubSide> {
	const installations = (await loadInstallations()).filter((i) => !i.suspended);
	const parts = await Promise.all(
		installations.map(async (i) => {
			const q = `installation=${i.id}`;
			const [repos, orgs] = await Promise.all([request<ServerRepo[]>('GET', `/api/migrate/github/repos?${q}`), request<ServerOrgs>('GET', `/api/migrate/github/orgs?${q}`)]);
			return { repos: repos.map((r) => sourceRepo(r, i.id)), orgs };
		})
	);
	const orgs = new Map<string, GithubOrg>();
	for (const p of parts) for (const o of p.orgs.orgs) if (!orgs.has(o.name) || orgs.get(o.name)?.access === 'removed') orgs.set(o.name, o);
	return {
		installations,
		account: parts.map((p) => p.orgs.account).find((a): a is string => !!a) ?? null,
		orgs: [...orgs.values()],
		repos: parts.flatMap((p) => p.repos)
	};
}

export const loadMoves = () => request<Move[]>('GET', '/api/migrate/moves');

export type NewMoves = {
	installation: number;
	repos: { org: string; name: string; as?: string }[];
	visibility: 'keep' | 'private';
	brings: { items: boolean; releases: boolean; wiki: boolean };
	people: Record<string, string>;
	// The Sailfork org the repositories land in; the server defaults to the signed-in user.
	target?: string;
};

export const createMoves = (body: NewMoves) => request<Move[]>('POST', '/api/migrate/moves', body);
export const retryMove = (id: string) => request<Move>('POST', `/api/migrate/moves/${id}/retry`);
export const switchMove = (id: string, after: string) => request<Move>('POST', `/api/migrate/moves/${id}/switch`, { after });
export const cancelMove = (id: string) => request<void>('DELETE', `/api/migrate/moves/${id}`);

// GitHub's own page for installing the app; the person comes back by focusing this tab again.
export const openInstall = async () => {
	// Opened before the request so the click still counts as the person's own.
	const tab = window.open('about:blank', '_blank');
	try {
		const url = await installUrl();
		if (tab) tab.location.href = url;
		else location.assign(url);
	} catch (err) {
		tab?.close();
		throw err;
	}
};