staging-org / ocean-landing-serve / src/ lib/ merge-gate.ts

import type { PullCheck } from './pulls-types.js';

export type MergePolicy = { approvals: number; dismissStale: boolean; upToDate: boolean; threadsResolved: boolean };

export type Approval = { by: string; agent: boolean; at: string };

export type GateInput = {
	draft: boolean;
	author: string;
	checks: PullCheck[];
	approvals: Approval[];
	changesBy: string | null;
	threads: { resolved: boolean }[];
	behind: number;
	touchesMigrations: boolean;
	lastPushAt: string | null;
	me: string;
};

export type GateId = 'draft' | 'checks' | 'approval' | 'migration' | 'changes' | 'threads' | 'behind';

export type Gate = { id: GateId; label: string; state: 'ok' | 'wait' | 'block'; detail: string };

const people = (n: number) => `${n} ${n === 1 ? 'person' : 'people'}`;

export const mergeGate = (pr: GateInput, policy: MergePolicy): Gate[] => {
	const failing = pr.checks.filter((c) => c.state === 'fail');
	const running = pr.checks.filter((c) => c.state === 'running');
	const humans = pr.approvals.filter((a) => !a.agent && a.by !== pr.author);
	const last = pr.lastPushAt;
	const stale = policy.dismissStale && last ? humans.filter((a) => a.at < last) : [];
	const counted = humans.filter((a) => !stale.includes(a));
	const agentApprovals = pr.approvals.filter((a) => a.agent);
	const need = policy.approvals;
	const open = pr.threads.filter((t) => !t.resolved);
	const items: Gate[] = [];

	if (pr.draft) items.push({ id: 'draft', label: 'Marked ready', state: 'block', detail: `Still a draft. ${pr.author} marks it ready for review.` });
	items.push(
		failing.length > 0
			? { id: 'checks', label: 'Checks pass', state: 'block', detail: `${failing.map((c) => c.name).join(', ')} failed.` }
			: running.length > 0
				? { id: 'checks', label: 'Checks pass', state: 'wait', detail: `${pr.checks.length - running.length} of ${pr.checks.length} done, ${running.map((c) => c.name).join(' and ')} still running.` }
				: pr.checks.length === 0
					? { id: 'checks', label: 'Checks pass', state: 'ok', detail: 'No checks have run on this branch.' }
					: { id: 'checks', label: 'Checks pass', state: 'ok', detail: `All ${pr.checks.length} passed.` }
	);

	const label = need === 1 ? 'Approved by a person' : `Approved by ${need} people`;
	if (counted.length >= need) {
		items.push({ id: 'approval', label, state: 'ok', detail: `${counted.map((a) => a.by).join(' and ')} approved.` });
	} else {
		const notes: string[] = [];
		if (counted.length > 0) notes.push(`${counted.map((a) => a.by).join(' and ')} approved; ${people(need - counted.length)} more needed.`);
		if (stale.length > 0) notes.push(`${stale.map((a) => a.by).join(' and ')} approved before the last push, so it no longer counts.`);
		if (agentApprovals.length > 0) notes.push(`${agentApprovals.map((a) => a.by).join(', ')} approved, but an agent's approval never counts toward merge.`);
		if (notes.length === 0) notes.push('Nobody has approved yet.');
		items.push({ id: 'approval', label, state: 'wait', detail: notes.join(' ') });
	}

	if (pr.touchesMigrations)
		items.push(
			counted.some((a) => a.by === pr.me)
				? { id: 'migration', label: 'Migration reviewed by you', state: 'ok', detail: 'You approved the schema change.' }
				: { id: 'migration', label: 'Migration reviewed by you', state: 'wait', detail: 'It adds a migration, and a migration never merges without a person.' }
		);
	items.push(
		pr.changesBy
			? { id: 'changes', label: 'Changes requested', state: 'block', detail: `${pr.changesBy} asked for changes.` }
			: { id: 'changes', label: 'No changes requested', state: 'ok', detail: 'Nobody is asking for changes.' }
	);
	if (policy.threadsResolved)
		items.push(
			open.length > 0
				? { id: 'threads', label: 'Threads resolved', state: 'wait', detail: `${open.length} open ${open.length === 1 ? 'thread' : 'threads'} on the changes.` }
				: { id: 'threads', label: 'Threads resolved', state: 'ok', detail: pr.threads.length > 0 ? `All ${pr.threads.length} resolved.` : 'No threads.' }
		);
	if (policy.upToDate)
		items.push(
			pr.behind > 0
				? { id: 'behind', label: 'Up to date with main', state: 'wait', detail: `${pr.behind} ${pr.behind === 1 ? 'commit' : 'commits'} behind main.` }
				: { id: 'behind', label: 'Up to date with main', state: 'ok', detail: 'Includes everything on main.' }
		);
	return items;
};