Something went wrong. Try again.
Tangled Spindle built on Cloudflare Workers
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312import type { JetstreamEvent } from "./jetstream";import { admitRepo, defaultTargets, isObjectId, loadWorkflows, newRuns, readCommit, repository, resolveKnot,} from "./notify";import type { Manifest } from "./notify";import type { NewPipeline } from "./store";import { type Pull, ticketEvents, type Webhooks } from "./webhooks";import { matchesPullRequest } from "./workflow";import { getRecord, isDid, isObject, upstream, upstreamError, XrpcError, xrpcUrl } from "./xrpc";
export const patchCollection = "org.tangled.review.patch";export const ticketCollection = "org.tangled.track.ticket";
export type Action = "opened" | "synchronize";
export interface Patch { head: string; ticket: string; sender: string; source: { repo: string; branch: string } | undefined;}
export interface PullStore { pushPipeline(pushKey: string): string | undefined; // Records the first patch of a ticket. action(repo: string, ticket: string, patch: string): Action; insert(pipeline: NewPipeline, status: "pending" | "blocked"): void; webhooks(repo: string): DurableObjectStub<Webhooks>;}
export function parsePatch(record: Record<string, unknown>): Patch | string { const { head, ticket, source, "x-tngl-editor": sender } = record; const oid = isObject(head) ? head["oid"] : undefined; if (!isObjectId(oid)) { return "patch has no head commit"; } if (typeof ticket !== "string" || ticket === "") { return "patch names no ticket"; } if (!isDid(sender)) { return "patch names no editor"; } return { head: oid, ticket, sender, source: isObject(source) && isDid(source["repo"]) && typeof source["branch"] === "string" ? { repo: source["repo"], branch: source["branch"] } : undefined, };}
// Returns the target branch of an open pull request.export function parseTicket(record: Record<string, unknown>): string | undefined { const { state, targetBranch } = record; return (state === undefined || state === "open") && typeof targetBranch === "string" && targetBranch !== "" ? targetBranch : undefined;}
function pullKey(repo: string, patch: string): string { return `pull ${repo} ${patch}`;}
export function pullPipeline( repo: string, rkey: string, patch: Patch, targetBranch: string, action: Action,): Omit<NewPipeline, "repo" | "runs"> { const { source } = patch; const sourceBranch = source?.repo === repo ? source.branch : undefined; const branch = sourceBranch ?? `pull/${patch.ticket}`; const trigger = { $type: "org.tangled.event.pullRequest", action, pull: `at://${repo}/${ticketCollection}/${patch.ticket}`, ...(source === undefined || sourceBranch !== undefined ? {} : { sourceRepo: source.repo }), ...(sourceBranch === undefined ? {} : { sourceBranch }), targetBranch, sourceSha: patch.head, // Retry builds the branch of the ref, and fork builds must not match the repo's branch filters. ref: `refs/heads/${branch}`, }; return { trigger, pushKey: pullKey(repo, rkey), commit: patch.head, branch, sender: patch.sender, };}
export function isPermanent(error: unknown): boolean { return error instanceof XrpcError && error.status < 500;}
interface Served extends Manifest { knot: string;}
// Returns the knot and manifest of each repo that this spindle serves, once per tick.export function admitter(env: Env): (repo: string) => Promise<Served | undefined> { const served = new Map<string, Promise<Served | undefined>>(); return async (repo) => { let found = served.get(repo); if (found === undefined) { found = resolveKnot(repo).then(async (knot) => { const manifest = await admitRepo(env, repo, knot); return manifest === undefined ? undefined : { ...manifest, knot }; }); served.set(repo, found); } return await found; };}
async function ticketAuthor(knot: string, uri: string): Promise<unknown> { const url = xrpcUrl(knot, "org.tangled.repo.listRecordVersions", { uri, limit: "1", order: "asc", }); const response = await upstream(url, 500); if (!response.ok) { throw upstreamError(url, 500, `HTTP ${response.status}`); } const body: unknown = await response.json().catch(() => null); const versions = isObject(body) ? body["versions"] : undefined; const first: unknown = Array.isArray(versions) ? versions[0] : undefined; return isObject(first) ? first["editor"] : undefined;}
function markup(value: unknown): string { return isObject(value) && typeof value["text"] === "string" ? value["text"] : "";}
function pull( env: Env, served: Served, ticket: Record<string, unknown>, targetBranch: string, author: unknown, sender: string,): Pull { const { createdAt } = ticket; return { title: markup(ticket["title"]), body: markup(ticket["body"]), targetBranch, author: typeof author === "string" ? author : "", createdAt: typeof createdAt === "string" ? createdAt : "", sender, repository: repository(env, served.knot, served), };}
// Throws only when a later tick may succeed.export async function handleTicket( env: Env, store: PullStore, event: JetstreamEvent, admit: (repo: string) => Promise<Served | undefined>,): Promise<void> { const { did: repo, rkey } = event; if (event.collection !== ticketCollection || event.operation === "delete") { return; } try { const served = await admit(repo); if (served === undefined) { return; } const ticket = await getRecord(served.knot, repo, ticketCollection, rkey, 500); const state = ticket?.["state"] ?? "open"; const targetBranch = ticket?.["targetBranch"]; if ( ticket === undefined || typeof targetBranch !== "string" || targetBranch === "" || typeof state !== "string" || !Object.hasOwn(ticketEvents, state) ) { return; } const hooks = store.webhooks(repo); const editor = ticket["x-tngl-editor"]; const events = Object.values(ticketEvents).map((change) => change.event); const payload = (await hooks.subscribed(events)) ? pull( env, served, ticket, targetBranch, await ticketAuthor(served.knot, `at://${repo}/${ticketCollection}/${rkey}`).catch( () => "", ), isDid(editor) ? editor : "", ) : undefined; await hooks.ticket(rkey, state, payload); } catch (error) { if (!isPermanent(error)) { throw error; } console.log("ticket skipped", { repo, rkey, reason: String(error) }); }}
// Throws only when a later tick may succeed.export async function handlePatch( env: Env, store: PullStore, event: JetstreamEvent, admit: (repo: string) => Promise<Served | undefined>,): Promise<void> { const { did: repo, rkey } = event; if ( event.collection !== patchCollection || event.operation !== "create" || store.pushPipeline(pullKey(repo, rkey)) !== undefined ) { return; } const skip = (reason: string): void => { console.log("pull request skipped", { repo, rkey, reason }); }; try { const served = await admit(repo); if (served === undefined) { skip("repo is not served"); return; } const { knot, name } = served; const record = await getRecord(knot, repo, patchCollection, rkey, 500); const patch = record === undefined ? "patch not found" : parsePatch(record); if (typeof patch === "string") { skip(patch); return; } const ticket = await getRecord(knot, repo, ticketCollection, patch.ticket, 500); const targetBranch = ticket === undefined ? undefined : parseTicket(ticket); if (targetBranch === undefined) { skip("ticket is not an open pull request"); return; } const action = store.action(repo, patch.ticket, rkey); let author: Promise<unknown> | undefined; const ticketBy = (): Promise<unknown> => (author ??= ticketAuthor(knot, `at://${repo}/${ticketCollection}/${patch.ticket}`)); const hooks = store.webhooks(repo); const head = { rkey, head: patch.head, branch: patch.source?.branch ?? "", repo: patch.source?.repo ?? "", }; const payload = (await hooks.subscribed([ "pull_request:created", "pull_request:resubmitted", ])) ? pull( env, served, ticket ?? {}, targetBranch, await ticketBy().catch(() => ""), patch.sender, ) : undefined; await hooks.patch(patch.ticket, head, payload); const [all, info] = await Promise.all([ loadWorkflows(knot, repo, patch.head, name), readCommit(knot, repo, patch.head), ]); const workflows = all.filter((workflow) => matchesPullRequest(workflow, targetBranch, action), ); if (workflows.length === 0) { skip("no workflows matched"); return; } const pipeline = pullPipeline(repo, rkey, patch, targetBranch, action); const trusted = patch.sender === env.OWNER_DID && (await ticketBy()) === env.OWNER_DID; store.insert( { ...pipeline, ...info, repo, runs: newRuns(env, await defaultTargets(knot, repo, workflows), name), }, trusted ? "pending" : "blocked", ); } catch (error) { if (!isPermanent(error)) { throw error; } skip(String(error)); }}