justintsus-experiment Public
// Running a list search (api.ts) against D1 for the Issues and Pull requests pages: one page of rows// in the query's order, and the counts the state tabs show (the query with its state terms removed,// by state). Issues come from `issues`; pull requests from `pull_index`, the RepoDO's D1 copy, which// may lag a few seconds; each row's details (comments, the author's kind) then come from the RepoDO,// which stays the source of truth, falling back to what pull_index has.//// Pages are numbered (?page=): an order other than by number has no cursor that a number could be.import { log } from "@duragit/shared/log.ts";import type { PrState } from "@duragit/shared/pulls.ts";import type { PrListItem } from "../do/repo/pulls.ts";import type { WebEnv } from "../env.ts";import { viewOf, type IssueRow, type IssueView } from "../issues/service.ts";import { repoStub } from "../repo/do-client.ts";import type { PullListRow } from "../ui/pulls.ts";import { searchHref, type Pager } from "../ui/search.ts";import { serializeQuery, tabOf, toSql, withState, type ListState, type SearchQuery, type SqlCtx, type Target } from "./api.ts";export const PAGE_SIZE = 25;export const MAX_PAGE = 400;export function pageOf(raw: string | null): number { const n = Number(raw ?? ""); return Number.isSafeInteger(n) && n >= 1 ? Math.min(n, MAX_PAGE) : 1;}// What the list searches for: ?q= when the URL has it (an empty q is the empty query: everything),// otherwise the default is:open, with the old links' state= and label= turned into terms.export function inputOf(p: URLSearchParams, target: Target): string { const q = p.get("q"); if (q !== null) return q; const s = p.get("state"); const terms: string[] = []; if (s === "closed" || (s === "merged" && target === "pulls")) terms.push(`is:${s}`); else if (s !== "all") terms.push("is:open"); const label = p.get("label"); if (target === "issues" && label) terms.push(`label:"${label.replace(/[\\"]/g, (c) => `\\${c}`)}"`); return terms.join(" ");}// The links a list page makes from its query, all with canonical q values.export type SearchLinks = { canonical: string; // the tab the query is on: one is: state, "all" (no state term), or null (a mix no tab stands for) tab: ListState | "all" | null; // the query has terms besides its state filtered: boolean; tabHref: (state: ListState | null) => string; pages: Pager; // the default list, when the query is something else clear: string | null;};export const DEFAULT_QUERY = "is:open";export function linksOf(base: string, query: SearchQuery, n: number, more: boolean): SearchLinks { const canonical = serializeQuery(query); const stateless = serializeQuery(withState(query, null)); const tab = tabOf(query); return { canonical, tab: tab ?? (stateless === canonical ? "all" : null), filtered: stateless !== "", tabHref: (s) => searchHref(base, serializeQuery(withState(query, s))), pages: { prev: n > 1 ? searchHref(base, canonical, n - 1) : null, next: more ? searchHref(base, canonical, n + 1) : null }, clear: canonical === DEFAULT_QUERY ? null : searchHref(base, DEFAULT_QUERY), };}// The links of a page whose query has errors: the tabs and Clear lead to plain state queries.export function linksOfInvalid(base: string): SearchLinks { return { canonical: "", tab: null, filtered: true, tabHref: (s) => searchHref(base, s ? `is:${s}` : ""), pages: { prev: null, next: null }, clear: searchHref(base, DEFAULT_QUERY), };}export type ListPage<T> = { items: T[]; more: boolean };async function page<R>(env: Pick<WebEnv, "DB">, table: "issues" | "pull_index", cols: string, query: SearchQuery, ctx: SqlCtx, n: number): Promise<ListPage<R>> { const s = toSql(query, ctx); const rows = await env.DB.prepare(`SELECT ${cols} FROM ${table} WHERE ${s.where} ORDER BY ${s.orderBy} LIMIT ? OFFSET ?`) .bind(...s.params, PAGE_SIZE + 1, (n - 1) * PAGE_SIZE).all<R>(); const list = rows.results ?? []; return { items: list.slice(0, PAGE_SIZE), more: list.length > PAGE_SIZE };}async function countsOf(env: Pick<WebEnv, "DB">, table: "issues" | "pull_index", query: SearchQuery, ctx: SqlCtx): Promise<Map<string, number>> { const s = toSql(withState(query, null), ctx); const rows = await env.DB.prepare(`SELECT state, COUNT(*) AS n FROM ${table} WHERE ${s.where} GROUP BY state`).bind(...s.params).all<{ state: string; n: number }>(); return new Map((rows.results ?? []).map((r) => [r.state, r.n]));}const ISSUE_COLS = "repo_id, number, org_id, author_id, title, body, state, labels, comments, created_at, updated_at, closed_at, closed_by";export async function searchIssues(env: Pick<WebEnv, "DB">, query: SearchQuery, ctx: SqlCtx, n: number): Promise<ListPage<IssueView> & { counts: { open: number; closed: number } }> { const [rows, counts] = await Promise.all([page<IssueRow>(env, "issues", ISSUE_COLS, query, ctx, n), countsOf(env, "issues", query, ctx)]); return { items: rows.items.map(viewOf), more: rows.more, counts: { open: counts.get("open") ?? 0, closed: counts.get("closed") ?? 0 } };}type PullIndexRow = { number: number; author_id: string; owner_id: string | null; state: string; draft: number; title: string; base_ref: string; head_ref: string; created_at: number; updated_at: number; merged_at: number | null; closed_at: number | null;};const PULL_COLS = "number, author_id, owner_id, state, draft, title, base_ref, head_ref, created_at, updated_at, merged_at, closed_at";function stateOf(s: string): PrState { return s === "merged" || s === "closed" ? s : "open";}function rowOf(r: PullIndexRow, live: PrListItem | undefined): PullListRow { if (live) { return { number: live.number, title: live.title, state: live.state, draft: live.draft, authorId: live.authorId, ownerId: live.ownerId, baseRef: live.baseRef, headRef: live.headRef, createdAt: live.createdAt, mergedAt: live.mergedAt, closedAt: live.closedAt, authorKind: live.authorKind, comments: live.comments, }; } return { number: r.number, title: r.title, state: stateOf(r.state), draft: r.draft === 1, authorId: r.author_id, ownerId: r.owner_id, baseRef: r.base_ref, headRef: r.head_ref, createdAt: r.created_at, mergedAt: r.merged_at, closedAt: r.closed_at, authorKind: r.owner_id ? "agent" : "user", comments: 0, };}export async function searchPulls(env: Pick<WebEnv, "DB" | "REPO">, query: SearchQuery, ctx: SqlCtx, n: number): Promise<ListPage<PullListRow> & { counts: Record<PrState, number> }> { const [rows, counts] = await Promise.all([page<PullIndexRow>(env, "pull_index", PULL_COLS, query, ctx, n), countsOf(env, "pull_index", query, ctx)]); let live = new Map<number, PrListItem>(); if (rows.items.length) { try { const got = await repoStub(env, ctx.repoId).prList({ state: "all", numbers: rows.items.map((r) => r.number), limit: 100 }); live = new Map(got.items.map((x) => [x.number, x])); } catch (e) { log("search.pulls_hydrate_failed", { error: e }, "warn"); } } return { items: rows.items.map((r) => rowOf(r, live.get(r.number))), more: rows.more, counts: { open: counts.get("open") ?? 0, closed: counts.get("closed") ?? 0, merged: counts.get("merged") ?? 0 }, };}