80 lines · 4.1 KBRaw
| 1 | import { now } from "./auth.ts"; |
| 2 | import { indexCommit } from "./code-index/build.ts"; |
| 3 | import { claimBuild, failCodeMap, readCodeMap, readCodeMapSummary, saveCodeMap } from "./code-index/store.ts"; |
| 4 | import type { ChangeFile, ChangeSet, CodeChange, CodeMapState, CodeMapSummary } from "./contract/views.ts"; |
| 5 | import { diffRows, rememberReads } from "./diff.ts"; |
| 6 | import type { RefUpdate } from "./git.ts"; |
| 7 | import { openPulls } from "./pulls.ts"; |
| 8 | import type { RepoRecord } from "./repos.ts"; |
| 9 | |
| 10 | /** At most this many branch tips are indexed for one push, so a push of many branches stays cheap. */ |
| 11 | const MAX_TIPS_PER_PUSH = 3; |
| 12 | |
| 13 | /** |
| 14 | * The code map of a commit. When there is none yet, a build starts after the response and the caller gets |
| 15 | * `indexing`; the next request finds the finished map. |
| 16 | */ |
| 17 | export async function codeMapAt(env: Env, ctx: Pick<ExecutionContext, "waitUntil">, repoId: string, handle: ArtifactsRepo, commit: string): Promise<CodeMapState> { |
| 18 | const at = now(); |
| 19 | const known = await readCodeMap(env.DB, repoId, commit, at); |
| 20 | if (known) return known; |
| 21 | if (await claimBuild(env.DB, repoId, commit, at)) ctx.waitUntil(buildCodeMap(env.DB, repoId, handle, commit)); |
| 22 | return { kind: "indexing", commit }; |
| 23 | } |
| 24 | |
| 25 | /** The state of a commit's map without the map itself. Starts a build like `codeMapAt` when there is no map. */ |
| 26 | export async function codeMapSummaryAt(env: Env, ctx: Pick<ExecutionContext, "waitUntil">, repoId: string, handle: ArtifactsRepo, commit: string): Promise<CodeMapSummary> { |
| 27 | const at = now(); |
| 28 | const known = await readCodeMapSummary(env.DB, repoId, commit, at); |
| 29 | if (known) return known; |
| 30 | if (await claimBuild(env.DB, repoId, commit, at)) ctx.waitUntil(buildCodeMap(env.DB, repoId, handle, commit)); |
| 31 | return { kind: "indexing", commit }; |
| 32 | } |
| 33 | |
| 34 | /** Builds and stores one commit's map. A failure is stored too, so readers see it and a later request tries again. */ |
| 35 | export async function buildCodeMap(db: D1Database, repoId: string, handle: ArtifactsRepo, commit: string): Promise<void> { |
| 36 | try { |
| 37 | const graph = await indexCommit(handle, commit); |
| 38 | await saveCodeMap(db, repoId, graph, now()); |
| 39 | } catch (error) { |
| 40 | console.error(`code map failed for repo ${repoId} at ${commit}`, error); |
| 41 | await failCodeMap(db, repoId, commit, now()); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** Indexes the new tips of the default branch and of open pull requests' branches, so their maps are ready before anyone opens them. */ |
| 46 | export async function indexPushedTips(env: Env, record: RepoRecord, updates: readonly RefUpdate[]): Promise<void> { |
| 47 | const moved = updates.filter((update) => update.to && update.ref.startsWith("refs/heads/")); |
| 48 | if (!moved.length) return; |
| 49 | const heads = new Set((await openPulls(env.DB, record.id)).flatMap((pull) => [pull.head_ref, pull.base_ref])); |
| 50 | const wanted = moved.filter((update) => { |
| 51 | const branch = update.ref.slice("refs/heads/".length); |
| 52 | return branch === record.default_branch || heads.has(branch); |
| 53 | }); |
| 54 | if (!wanted.length) return; |
| 55 | const handle = rememberReads(await env.ARTIFACTS.get(record.artifacts_name), record.id); |
| 56 | for (const update of wanted.slice(0, MAX_TIPS_PER_PUSH)) { |
| 57 | if (await claimBuild(env.DB, record.id, update.to!, now())) await buildCodeMap(env.DB, record.id, handle, update.to!); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** What a pull request changed in each file, as line numbers of the head version, so the map can mark the changed declarations. */ |
| 62 | export function codeChanges(changes: ChangeSet): CodeChange[] { |
| 63 | return changes.files.map(codeChange); |
| 64 | } |
| 65 | |
| 66 | function codeChange(file: ChangeFile): CodeChange { |
| 67 | const base = { path: file.path, status: file.status, additions: file.additions, deletions: file.deletions }; |
| 68 | if (file.kind !== "text" || file.after === null) return { ...base, added: [], removed: [] }; |
| 69 | const rows = diffRows(file.before ?? "", file.after); |
| 70 | if (!rows) return { ...base, added: [], removed: [] }; |
| 71 | const added: number[] = []; |
| 72 | const removed: { after: number; text: string }[] = []; |
| 73 | let last = 0; |
| 74 | for (const row of rows) { |
| 75 | if (row.kind === "add") added.push(row.newLine!); |
| 76 | if (row.kind === "delete") removed.push({ after: last, text: row.text }); |
| 77 | if (row.newLine !== null) last = row.newLine; |
| 78 | } |
| 79 | return { ...base, added, removed }; |
| 80 | } |