import { now } from "./auth.ts"; import { indexCommit } from "./code-index/build.ts"; import { claimBuild, failCodeMap, readCodeMap, readCodeMapSummary, saveCodeMap } from "./code-index/store.ts"; import type { ChangeFile, ChangeSet, CodeChange, CodeMapState, CodeMapSummary } from "./contract/views.ts"; import { diffRows, rememberReads } from "./diff.ts"; import type { RefUpdate } from "./git.ts"; import { openPulls } from "./pulls.ts"; import type { RepoRecord } from "./repos.ts"; /** At most this many branch tips are indexed for one push, so a push of many branches stays cheap. */ const MAX_TIPS_PER_PUSH = 3; /** * The code map of a commit. When there is none yet, a build starts after the response and the caller gets * `indexing`; the next request finds the finished map. */ export async function codeMapAt(env: Env, ctx: Pick, repoId: string, handle: ArtifactsRepo, commit: string): Promise { const at = now(); const known = await readCodeMap(env.DB, repoId, commit, at); if (known) return known; if (await claimBuild(env.DB, repoId, commit, at)) ctx.waitUntil(buildCodeMap(env.DB, repoId, handle, commit)); return { kind: "indexing", commit }; } /** The state of a commit's map without the map itself. Starts a build like `codeMapAt` when there is no map. */ export async function codeMapSummaryAt(env: Env, ctx: Pick, repoId: string, handle: ArtifactsRepo, commit: string): Promise { const at = now(); const known = await readCodeMapSummary(env.DB, repoId, commit, at); if (known) return known; if (await claimBuild(env.DB, repoId, commit, at)) ctx.waitUntil(buildCodeMap(env.DB, repoId, handle, commit)); return { kind: "indexing", commit }; } /** Builds and stores one commit's map. A failure is stored too, so readers see it and a later request tries again. */ export async function buildCodeMap(db: D1Database, repoId: string, handle: ArtifactsRepo, commit: string): Promise { try { const graph = await indexCommit(handle, commit); await saveCodeMap(db, repoId, graph, now()); } catch (error) { console.error(`code map failed for repo ${repoId} at ${commit}`, error); await failCodeMap(db, repoId, commit, now()); } } /** Indexes the new tips of the default branch and of open pull requests' branches, so their maps are ready before anyone opens them. */ export async function indexPushedTips(env: Env, record: RepoRecord, updates: readonly RefUpdate[]): Promise { const moved = updates.filter((update) => update.to && update.ref.startsWith("refs/heads/")); if (!moved.length) return; const heads = new Set((await openPulls(env.DB, record.id)).flatMap((pull) => [pull.head_ref, pull.base_ref])); const wanted = moved.filter((update) => { const branch = update.ref.slice("refs/heads/".length); return branch === record.default_branch || heads.has(branch); }); if (!wanted.length) return; const handle = rememberReads(await env.ARTIFACTS.get(record.artifacts_name), record.id); for (const update of wanted.slice(0, MAX_TIPS_PER_PUSH)) { if (await claimBuild(env.DB, record.id, update.to!, now())) await buildCodeMap(env.DB, record.id, handle, update.to!); } } /** What a pull request changed in each file, as line numbers of the head version, so the map can mark the changed declarations. */ export function codeChanges(changes: ChangeSet): CodeChange[] { return changes.files.map(codeChange); } function codeChange(file: ChangeFile): CodeChange { const base = { path: file.path, status: file.status, additions: file.additions, deletions: file.deletions }; if (file.kind !== "text" || file.after === null) return { ...base, added: [], removed: [] }; const rows = diffRows(file.before ?? "", file.after); if (!rows) return { ...base, added: [], removed: [] }; const added: number[] = []; const removed: { after: number; text: string }[] = []; let last = 0; for (const row of rows) { if (row.kind === "add") added.push(row.newLine!); if (row.kind === "delete") removed.push({ after: last, text: row.text }); if (row.newLine !== null) last = row.newLine; } return { ...base, added, removed }; }