staging-tester / ocean-landing-serve / src/ lib/ mirror.ts

export type HostId = 'github' | 'gitlab' | 'bitbucket' | 'codeberg' | 'git';
export type Visibility = 'public' | 'private';

export type Host = {
	id: HostId;
	name: string;
	domain: string | null;
	// What the host's consent screen will list, in plain words, shown before anyone leaves Sailfork.
	asks: string[];
	never: string;
};

export const hosts: Host[] = [
	{
		id: 'github',
		name: 'GitHub',
		domain: 'github.com',
		asks: ['Push to the repositories you pick on GitHub', 'Create a repository when the one you name does not exist yet'],
		never: 'It cannot read your issues, pull requests or any repository you leave unpicked.'
	},
	{
		id: 'gitlab',
		name: 'GitLab',
		domain: 'gitlab.com',
		asks: ['Push to projects you can write to', 'Create a project when the one you name does not exist yet'],
		never: 'It never reads issues, merge requests or pipelines.'
	},
	{
		id: 'bitbucket',
		name: 'Bitbucket',
		domain: 'bitbucket.org',
		asks: ['Push to repositories in your workspaces', 'Create a repository when the one you name does not exist yet'],
		never: 'It never reads pull requests, pipelines or workspace members.'
	},
	{
		id: 'codeberg',
		name: 'Codeberg',
		domain: 'codeberg.org',
		asks: ['Push to your repositories and your organizations’ repositories', 'Create a repository when the one you name does not exist yet'],
		never: 'It never reads issues, pull requests or your other settings.'
	},
	{
		id: 'git',
		name: 'Any git server',
		domain: null,
		asks: [],
		never: 'Sailfork only pushes; it never fetches from the server.'
	}
];

export const hostOf = (id: HostId): Host => {
	const host = hosts.find((h) => h.id === id);
	if (!host) throw new Error(`Unknown host ${id}`);
	return host;
};

export type RemoteRepo = { name: string; visibility: Visibility; commits: number };
export type Namespace = { name: string; kind: 'user' | 'org'; repos: RemoteRepo[] };

export type Connection = {
	host: Exclude<HostId, 'git'>;
	account: string;
	authorized: string;
	expired: string | null;
	namespaces: Namespace[];
};
export type Grant = Omit<Connection, 'authorized' | 'expired'>;

export type Ref = { name: string; kind: 'branch' | 'tag'; sha: string; subject: string; at: string };

export type RefChoice = 'all' | 'default' | 'chosen';
// include and exclude are globs on full ref names; a short one like `release/*` means a branch.
export type Send = { choice: RefChoice; chosen: string[]; prune: boolean; include?: string[]; exclude?: string[] };

export type Auth = { kind: 'app'; account: string } | { kind: 'deploy-key'; fingerprint: string } | { kind: 'token'; user: string };

export type Push = { at: string; ok: boolean; text: string };

export type Mirror = {
	id: string;
	host: HostId;
	url: string;
	label: string;
	auth: Auth;
	send: Send;
	paused: boolean;
	// What the destination holds after the last push; a ref absent here was never pushed.
	there: Record<string, string>;
	// Commits that exist only on the destination, found when a push was refused.
	aheadThere: { ref: string; commits: number; by: string; author: string; committer: string } | null;
	pushes: Push[];
};