import { atUriOf, nsidOf, recordKeyOf, type AtUri, type Cid, type Did, type LabelKey, type Rkey } from "#lib/api/syntax.ts"; import { now as tidNow } from "@atcute/tid"; import type { OAuthUserAgent } from "@atcute/oauth-browser-client"; import { deleteRecord, putRecord } from "#lib/api/write.ts"; import { awaitReadable, awaitRemoved } from "#lib/api/awaitRecord.ts"; import { writeTargetFor } from "#lib/api/knot.ts"; import { eraseCob, withRkey, type Written } from "#lib/api/cob.ts"; import { createTicket, editTicket as restateOnKnot, TICKET_COLLECTION, type TicketProse } from "#lib/api/ticket.ts"; import type { BobbinContext, XrpcRequestInit } from "#lib/api/client.ts"; import { jsonGet } from "#lib/api/_request.ts"; import type { TicketRecord, TicketState } from "#lib/api/records.ts"; import type * as ShTangledRepoIssue from "#lib/api/lexicons/types/sh/tangled/repo/issue.ts"; export type IssueRecord = ShTangledRepoIssue.Main; const ISSUE_COLLECTION = nsidOf("sh.tangled.repo.issue"); export interface TicketInput { title: string; body?: string; targetBranch?: string; tangledLabels?: LabelKey[]; } const legacyIssue = (repoDid: Did, { title, body }: TicketInput): IssueRecord => ({ $type: "sh.tangled.repo.issue" as IssueRecord["$type"], repo: repoDid as IssueRecord["repo"], title, body: body ?? "", createdAt: new Date().toISOString() }) as IssueRecord; const proseOf = ({ title, body, targetBranch, tangledLabels }: TicketInput): TicketProse => ({ title, body: body ?? "", targetBranch, tangledLabels }); export const openTicket = async ( agent: OAuthUserAgent, bobbinUrl: string | undefined, repoDid: Did, input: TicketInput ): Promise => { const target = await writeTargetFor(bobbinUrl, repoDid); if (target.kind === "knot") return withRkey(await createTicket(agent, target.knot, repoDid, proseOf(input))); const saved = await putRecord( agent, ISSUE_COLLECTION, recordKeyOf(tidNow()), legacyIssue(repoDid, input) ); await awaitReadable(bobbinUrl, saved); return withRkey(saved); }; export const editTicket = async ( agent: OAuthUserAgent, bobbinUrl: string | undefined, repoDid: Did, input: TicketInput & { rkey: Rkey; swapRecord: Cid } ): Promise => { const target = await writeTargetFor(bobbinUrl, repoDid); if (target.kind === "knot") { const uri = atUriOf(`at://${repoDid}/${TICKET_COLLECTION}/${input.rkey}`); return withRkey( await restateOnKnot(agent, target.knot, { uri, cid: input.swapRecord }, proseOf(input)) ); } const saved = await putRecord( agent, ISSUE_COLLECTION, input.rkey, legacyIssue(repoDid, input), input.swapRecord ); await awaitReadable(bobbinUrl, saved); return withRkey(saved); }; export const removeTicket = async ( agent: OAuthUserAgent, bobbinUrl: string | undefined, repoDid: Did, input: { rkey: Rkey; swapRecord: Cid } ): Promise<{ uri: AtUri }> => { const target = await writeTargetFor(bobbinUrl, repoDid); const uri = atUriOf(`at://${repoDid}/${TICKET_COLLECTION}/${input.rkey}`); if (target.kind === "knot") await eraseCob(agent, target.knot, { uri, cid: input.swapRecord }); else { await deleteRecord(agent, ISSUE_COLLECTION, input.rkey, input.swapRecord); await awaitRemoved(bobbinUrl, uri); } return { uri }; }; export type IssueState = "open" | "closed"; // a listIssues row: the embedded issue record plus bobbin-derived state/counts. export interface IssueListItem { cid?: string; commentCount: number; state: TicketState; stateUpdatedAt?: string; uri: string; value: TicketRecord; } export interface IssueListPage { cursor?: string | null; total?: number; items: IssueListItem[]; } export interface ListIssuesFilter { state?: TicketState; author?: string; limit?: number; cursor?: string; order?: "asc" | "desc"; } export const listIssues = ( ctx: BobbinContext, subject: Did, filter: ListIssuesFilter = {}, init?: XrpcRequestInit ) => jsonGet(ctx, "org.tangled.repo.listIssues", { subject, ...filter }, init);