import { untrack } from 'svelte';
import { bindList, createApi, HttpError, paths, writable, type Commit, type Compare, type FileDiff, type Issue, type PullData, type Readable, type ThreadData } from '@sailfork/client';
import type { Branch } from '../branches.js';
import type { FileEntry, InboxItem, Repo } from '../org-types.js';
import type { PullDetail, PullEvent, PullFile, PullThread } from '../pulls-types.js';
import { api, sync } from '../sailfork.js';
import { request } from './http.js';
import { identity } from './identity.svelte.js';
import { org } from './org.svelte.js';
// The repository the route is in; openRepo moves every store below to another one.
export const current = $state({ owner: '', name: '' });
let OWNER = '';
let NAME = '';
export const repoId = () => `${current.owner}/${current.name}`;
// The open repository as the pages read it: pulls and issues are the live rows from the sync store, edited in place
// so components keep their object identities.
export const ledger = $state({
repo: { name: '', about: '', visibility: 'public', archived: false, language: '', openPulls: 0, openIssues: 0, pushedAt: '' } as Repo,
branch: '',
commits: 0,
lastCommit: { author: '', agent: false, message: '', sha: '', at: '' },
files: [] as FileEntry[],
readme: { title: '', intro: '', quickstart: '', notes: [] as string[] },
// No language statistics exist on the server yet.
languages: [] as { name: string; share: number }[],
// The newest tag by commit time.
release: null as { tag: string; at: string } | null,
pulls: [] as PullData[],
issues: [] as Issue[]
});
export const pullDetails = $state<PullDetail[]>([]);
// The repository as the server describes it, plus the branches worked out from real commits.
export const repoInfo = $state({ cloneUrl: '', branches: [] as string[], loaded: false });
export const branches = $state<Branch[]>([]);
export const recentBranches = $state<Branch[]>([]);
export const inbox = $state<InboxItem[]>([]);
// Items decided this session stay in the queue so their outcome and Undo remain on screen.
const held = $state<string[]>([]);
export const holdInbox = (id: string) => {
if (!held.includes(id)) held.push(id);
};
// The signed-in handle from the one identity module; the server's authors and reviewers are handles, never display names.
export const me = {
get handle() {
return identity.me?.handle ?? '';
}
};
const blankReadme = (name: string) => ({ title: name, intro: '', quickstart: '', notes: [] as string[] });
export const issueOf = (number: number) => ledger.issues.find((i) => i.number === number) ?? null;
// A pull request's diff from the server: live for an open one, the recorded range and totals for a merged one. A
// separate cache from the shared `api`, which is rooted at /api.
const compareApi = createApi({ base: '', dbName: 'sailfork-compare' });
type PullCompare = Compare & { stats: { added: number; removed: number; files: number }; merged: boolean };
// Owner and repository names are letters, digits, '-', '_' and '.', so `owner/name` needs no escaping.
const pullComparePath = (repo: string, number: number) => `/api/repos/${repo}/pulls/${number}/compare`;
const uniqueByNumber = <T extends { number: number }>(source: Readable<T[]>): Readable<T[]> => ({
// An optimistic row and its server twin share a number for an instant; list keys must stay unique.
subscribe: (run) => source.subscribe((list) => run(list.filter((x, i) => list.findIndex((y) => y.number === x.number) === i)))
});
const where = <T>(source: Readable<T[]>, keep: (t: T) => boolean): Readable<T[]> => ({
subscribe: (run) => source.subscribe((list) => run(list.filter(keep)))
});
const touches = (files: PullFile[]) => ({
migrations: files.some((f) => /(^|\/)migrations?\//.test(f.path)),
secrets: files.some((f) => /(^|\/)(\.env[^/]*|[^/]*\.(pem|key)|id_(rsa|ed25519))$/.test(f.path))
});
const toFile = (f: FileDiff): PullFile => ({ path: f.path, status: f.status, from: f.from, hunks: f.hunks });
function track(number: number, repo: string): () => void {
const pr = ledger.pulls.find((p) => p.number === number);
if (!pr) return () => undefined;
pullDetails.push({
number,
branch: pr.branch,
body: [],
closes: null,
behind: 0,
touches: { migrations: false, secrets: false },
approvals: [],
commits: [],
checks: [],
files: [],
threads: [],
events: []
});
const live = pullDetails[pullDetails.length - 1];
if (!live) return () => undefined;
const notes: PullThread[] = $state([]);
const known = new Map<string, boolean>();
const threadStore = sync.threads(repo, number);
const anchored = where<ThreadData>(threadStore, (t) => t.path !== '');
const stopKnown = anchored.subscribe((list) => {
for (const t of list) known.set(t.id, t.resolved);
});
const strip = (t: ThreadData): PullThread => ({ id: t.id, path: t.path, line: t.line, resolved: t.resolved, comments: t.comments });
const stopThreads = bindList(live.threads, anchored, (t) => t.id, strip);
// A review body has no file anchor on the server; it becomes a note on the pull request's history.
const stopNotes = bindList(notes, where<ThreadData>(threadStore, (t) => t.path === ''), (t) => t.id, strip);
return $effect.root(() => {
$effect(() => {
live.branch = pr.branch;
live.body = [...pr.body];
live.closes = ledger.issues.find((i) => i.pull === number)?.number ?? null;
live.approvals = pr.review === 'approved' && pr.reviewer ? [{ by: pr.reviewer, agent: false, at: pr.at }] : [];
const events: PullEvent[] = [{ at: pr.at, actor: pr.author, agent: pr.agent, text: live.closes === null ? 'opened this' : `opened this and linked #${live.closes}` }];
for (const n of notes) for (const c of n.comments) events.push({ at: c.at, actor: c.author, agent: c.agent, text: `commented: ${c.body}` });
const last = events.reduce((a, e) => (e.at > a ? e.at : a), pr.at);
if (pr.review !== 'none' && pr.reviewer) events.push({ at: last, actor: pr.reviewer, agent: false, text: pr.review === 'approved' ? 'approved' : 'asked for changes' });
if (pr.state === 'merged') events.push({ at: last, actor: pr.reviewer ?? pr.author, agent: false, text: `merged into ${pr.base}` });
live.events = events.sort((a, b) => a.at.localeCompare(b.at));
});
$effect(() => {
// An open pull request whose head branch is gone (deleted, or force-pushed away) has nothing to compare; asking
// would only 404. A merged one is served from the range recorded when it merged, whatever became of the branch.
const merged = pr.state === 'merged';
if (!merged && !repoInfo.branches.includes(pr.branch)) return;
return compareApi.resource<PullCompare>(pullComparePath(repo, number)).subscribe((r) =>
untrack(() => {
const c = r.data;
if (!c) {
if (r.error instanceof HttpError && r.error.status === 404) {
live.files = [];
live.commits = [];
live.behind = 0;
live.stats = null;
live.missing = merged ? 'This pull request merged before Sailfork recorded merge diffs, so its changes cannot be shown.' : null;
}
return;
}
const files = c.files.map(toFile);
live.behind = c.behind;
live.files = files;
live.stats = c.stats;
live.missing = null;
live.touches = touches(files);
live.commits = c.commits.map((k) => ({ sha: k.short, subject: k.subject, author: k.author, agent: false, at: k.at })).reverse();
})
);
});
$effect(() => {
// Ticking a thread resolved in the markup writes the thread directly; send what changed.
for (const t of live.threads) {
const resolved = t.resolved;
if (known.get(t.id) === resolved) continue;
known.set(t.id, resolved);
sync.mutate([{ kind: 'thread.resolve', repo, pull: number, thread: t.id, resolved }]).catch((e: unknown) => console.error('resolve failed', e));
}
});
return () => {
stopKnown();
stopThreads();
stopNotes();
};
});
}
const wait = (ms: number) => new Promise<void>((r) => setTimeout(r, ms));
async function until<T>(read: () => T | undefined, ms: number): Promise<T | undefined> {
const end = Date.now() + ms;
for (let v = read(); ; v = read()) {
if (v !== undefined || Date.now() > end) return v;
await wait(20);
}
}
// Resolves once the pull request is in the store (from IndexedDB, or the first sync after a cold start) and its diff
// is loaded, with plain copies for the page to fall back on. Null when the repository has no such pull request.
export async function openPull(number: number): Promise<{ pr: PullData; detail: PullDetail } | null> {
await sync.ready;
const pr = await until(() => ledger.pulls.find((p) => p.number === number), 4000);
if (!pr) return null;
// The branch list arrives with the repository; a pull whose head branch is gone has no diff to load.
await until(() => (repoInfo.loaded ? true : undefined), 3000);
if (pr.state === 'merged' || repoInfo.branches.includes(pr.branch)) await compareApi.load(pullComparePath(repoId(), number)).catch(() => undefined);
const detail = await until(() => pullDetails.find((d) => d.number === number), 1000);
if (!detail) return null;
await wait(0);
return { pr: $state.snapshot(pr), detail: $state.snapshot(detail) };
}
export async function tipOf(branch: string): Promise<Commit | null> {
const res = await fetch(paths.commits(OWNER, NAME, { ref: branch, limit: 1 }), { credentials: 'include' });
if (!res.ok) return null;
const list: Commit[] = await res.json();
return list[0] ?? null;
}
export const itemById = (id: string) => inbox.find((i) => i.id === id);
// The queue item for a pull request of the open repository; only an org's repositories have a queue.
export const queued = (number: number) => (current.owner === org.handle ? inbox.find((i) => i.repo === current.name && i.number === number) : undefined);
// Review (and merge, when the item was offered as approve-and-merge) from the queue. False when the server refused.
export async function submitReview(id: string, decision: 'approved' | 'changes' | 'read', body = ''): Promise<boolean> {
holdInbox(id);
const item = itemById(id);
if (!item || decision === 'read') return true;
try {
const repo = `${org.handle}/${item.repo}`;
await sync.mutate([{ kind: 'pull.review', repo, number: item.number, verdict: decision === 'approved' ? 'approve' : 'changes', body }]);
if (decision === 'approved' && item.ask === 'approve') await sync.mutate([{ kind: 'pull.merge', repo, number: item.number }]);
return true;
} catch (e) {
console.error('review failed', e);
return false;
}
}
// A pull request of the open repository has its diff and threads loaded; any other repository's is offered for review.
const toItem = (repo: string, pr: PullData): InboxItem => {
const d = repo === NAME && org.handle === OWNER ? pullDetails.find((x) => x.number === pr.number) : undefined;
const lines = d?.files.flatMap((f) => f.hunks.flatMap((h) => h.lines)) ?? [];
return {
id: `${repo}-${pr.number}`,
repo,
number: pr.number,
title: pr.title,
agent: pr.author,
// Only a change that nothing holds back is offered as "approve and merge".
ask: pr.checks === 'passing' && d && d.threads.every((t) => t.resolved) && d.behind === 0 && !d.touches.migrations ? 'approve' : 'review',
checks: pr.checks,
added: lines.filter((l) => l.kind === 'add').length,
removed: lines.filter((l) => l.kind === 'del').length,
files: d?.files.map((f) => f.path) ?? [],
why: pr.body[0] ?? pr.title,
at: pr.at
};
};
// A README, boiled down to the pieces the overview shows: heading, opening paragraph, first code block, first list.
function readmeParts(text: string, fallbackTitle: string) {
const clean = (t: string) => t.replace(/\[([^\]]+)\]\([^)]*\)/g, '$1').replace(/[`*_]/g, '').trim();
const lines = text.split('\n');
const title = clean(lines.find((l) => /^#\s+\S/.test(l))?.replace(/^#\s+/, '') ?? fallbackTitle);
const intro: string[] = [];
let seenTitle = false;
for (const l of lines) {
if (/^#\s/.test(l)) {
seenTitle = true;
continue;
}
if (!seenTitle && lines.some((x) => /^#\s/.test(x))) continue;
if (l.trim() === '' || /^(#|```|[-*]\s|\||<|!\[|\[!)/.test(l.trim())) {
if (intro.length > 0) break;
continue;
}
intro.push(l.trim());
}
const fence = text.match(/```[^\n]*\n([\s\S]*?)```/);
const notes = lines.filter((l) => /^\s*[-*]\s+\S/.test(l)).slice(0, 4).map((l) => clean(l.replace(/^\s*[-*]\s+/, '')));
return { title, intro: clean(intro.join(' ')), quickstart: (fence?.[1] ?? '').trim().split('\n').slice(0, 4).join('\n'), notes };
}
// Reads the repository from the cached API (stale first, refreshed in the background) into `ledger`, `branches` and
// `recentBranches`: clone URL, files, README, commit count, and each branch's head commit and distance from main.
// The newest tag. A repository nothing was pushed to answers 404, which is the same as having no tag.
let releaseAsk = 0;
async function loadRelease(owner: string, name: string) {
const ask = ++releaseAsk;
const newest = await request<{ name: string; at: string }[]>('GET', `${paths.repo(owner, name)}/tags`).then(
(tags) => tags[0] ?? null,
() => null
);
if (ask === releaseAsk && owner === OWNER && name === NAME) ledger.release = newest ? { tag: newest.name, at: newest.at } : null;
}
function watchRepository(repoOwner: string, repoName: string): () => void {
const raw = $state<Record<string, { head: Commit | null; ahead: number; behind: number }>>({});
const subs = new Map<string, () => void>();
let defaultBranch = '';
const stops: (() => void)[] = [];
const branchStore = writable<Branch[]>([]);
const recentStore = writable<Branch[]>([]);
stops.push(bindList(branches, branchStore, (b) => b.name), bindList(recentBranches, recentStore, (b) => b.name));
let content: () => void = () => undefined;
const followContent = (branch: string) => {
const off = [
api.tree(repoOwner, repoName, branch).subscribe((r) => {
const t = r.data;
if (!t) return;
ledger.files = [...t.entries]
.sort((a, b) => Number(b.dir) - Number(a.dir) || a.name.localeCompare(b.name))
.map((e) => ({ name: e.name, dir: e.dir, message: e.commit?.subject ?? '', at: e.commit?.at ?? '' }));
}),
api.blob(repoOwner, repoName, branch, 'README.md').subscribe((r) => {
if (r.data?.text) ledger.readme = readmeParts(r.data.text, repoName);
})
];
return () => off.forEach((f) => f());
};
const follow = (name: string) => {
const off: (() => void)[] = [];
off.push(
api.commits(repoOwner, repoName, { ref: name, limit: name === defaultBranch ? 1000 : 1 }).subscribe((r) => {
const list = r.data;
if (!list) return;
raw[name] = { head: list[0] ?? null, ahead: raw[name]?.ahead ?? 0, behind: raw[name]?.behind ?? 0 };
if (name !== defaultBranch) return;
ledger.commits = list.length;
const c = list[0];
if (c) ledger.lastCommit = { author: c.author, agent: false, message: c.subject, sha: c.short, at: c.at };
})
);
if (name !== defaultBranch) {
off.push(
api.compare(repoOwner, repoName, defaultBranch, name).subscribe((r) => {
const c = r.data;
if (c) raw[name] = { head: raw[name]?.head ?? null, ahead: c.ahead, behind: c.behind };
else if (r.error instanceof HttpError && r.error.status === 404) delete raw[name];
})
);
}
return () => off.forEach((f) => f());
};
stops.push(
api.repo(repoOwner, repoName).subscribe((r) => {
const info = r.data;
if (!info) return;
repoInfo.cloneUrl = info.cloneUrl;
repoInfo.branches = info.branches;
repoInfo.loaded = true;
void loadRelease(repoOwner, repoName);
ledger.repo.about = info.about;
ledger.repo.visibility = info.visibility;
ledger.branch = info.defaultBranch;
if (defaultBranch !== info.defaultBranch) {
for (const stop of subs.values()) stop();
subs.clear();
content();
defaultBranch = info.defaultBranch;
content = followContent(defaultBranch);
}
for (const name of info.branches) if (!subs.has(name)) subs.set(name, follow(name));
for (const [name, stop] of subs) {
if (info.branches.includes(name)) continue;
stop();
subs.delete(name);
delete raw[name];
}
})
);
const root = $effect.root(() => {
$effect(() => {
const list: Branch[] = Object.entries(raw).flatMap(([name, d]) => {
if (!d.head) return [];
const pull = ledger.pulls.find((p) => p.branch === name);
const main = name === defaultBranch;
return [
{
name,
owner: d.head.author,
agent: false,
head: d.head.short,
subject: d.head.subject,
pushedAt: d.head.at,
ahead: d.ahead,
behind: d.behind,
pull: pull?.number ?? null,
merged: !main && (pull?.state === 'merged' || d.ahead === 0)
}
];
});
list.sort((a, b) => b.pushedAt.localeCompare(a.pushedAt));
branchStore.set(list);
recentStore.set(list.filter((b) => b.name !== defaultBranch && !b.merged).slice(0, 4));
});
});
return () => {
root();
content();
stops.forEach((f) => f());
subs.forEach((f) => f());
};
}
const inboxStore = writable<InboxItem[]>([]);
let closeRepo: () => void = () => undefined;
// Points every store at owner/name: pulls and issues are the sync rows of that repository only, and the branches,
// files and README come from its API. Opening the repository already open does nothing.
export function openRepo(owner: string, name: string) {
if (owner === OWNER && name === NAME) return;
closeRepo();
OWNER = owner;
NAME = name;
current.owner = owner;
current.name = name;
const repo = `${owner}/${name}`;
Object.assign(ledger, {
repo: { ...ledger.repo, name, about: '', openPulls: 0, openIssues: 0 },
branch: '',
commits: 0,
lastCommit: { author: '', agent: false, message: '', sha: '', at: '' },
files: [],
readme: blankReadme(name),
languages: [],
release: null
});
repoInfo.cloneUrl = '';
repoInfo.branches = [];
repoInfo.loaded = false;
pullDetails.splice(0);
const tracked = new Map<number, () => void>();
const stops = [bindList(ledger.pulls, uniqueByNumber(sync.pulls(repo)), (p) => p.number), bindList(ledger.issues, uniqueByNumber(sync.issues(repo)), (i) => i.number), watchRepository(owner, name)];
const root = $effect.root(() => {
$effect(() => {
ledger.repo.openPulls = ledger.pulls.filter((p) => p.state !== 'merged').length;
ledger.repo.openIssues = ledger.issues.length;
});
$effect(() => {
const numbers = ledger.pulls.map((p) => p.number);
untrack(() => {
for (const n of numbers) if (!tracked.has(n)) tracked.set(n, track(n, repo));
for (const [n, stop] of tracked) {
if (numbers.includes(n)) continue;
stop();
tracked.delete(n);
const at = pullDetails.findIndex((d) => d.number === n);
if (at >= 0) pullDetails.splice(at, 1);
}
});
});
});
closeRepo = () => {
root();
stops.forEach((f) => f());
tracked.forEach((f) => f());
};
}
// The queue spans every repository of the org the shell shows.
function watchInbox(): () => void {
const rows = $state<Record<string, PullData[]>>({});
const subs = new Map<string, () => void>();
let orgHandle = '';
return $effect.root(() => {
$effect(() => {
const handle = org.handle;
const names = org.repos.map((r) => r.name);
untrack(() => {
if (handle !== orgHandle) {
subs.forEach((f) => f());
subs.clear();
for (const k of Object.keys(rows)) delete rows[k];
orgHandle = handle;
}
for (const name of names) {
if (subs.has(name)) continue;
subs.set(name, uniqueByNumber(sync.pulls(`${handle}/${name}`)).subscribe((list) => (rows[name] = list)));
}
for (const [name, stop] of subs) {
if (names.includes(name)) continue;
stop();
subs.delete(name);
delete rows[name];
}
});
});
$effect(() => {
const mine = me.handle;
inboxStore.set(
Object.entries(rows).flatMap(([repo, pulls]) =>
pulls
.filter((p) => held.includes(`${repo}-${p.number}`) || (p.state === 'open' && p.author !== mine && !(p.review !== 'none' && p.reviewer === mine)))
.map((p) => toItem(repo, p))
)
);
});
return () => subs.forEach((f) => f());
});
}
if (typeof window !== 'undefined') {
bindList(inbox, inboxStore, (i) => i.id);
watchInbox();
}