/** * Single source of truth for reading, updating, deleting, and creating "progress comments" — * the Gitea comments shockbot uses to surface a run's status. */ import type { Gitea } from "./gitea.ts"; export type ProgressCommentType = "issue"; export type ProgressComment = { id: number; type: ProgressCommentType; }; export function parseProgressComment( raw: { id: string; type: ProgressCommentType } | null | undefined ): ProgressComment | undefined { if (!raw?.id) return undefined; const id = parseInt(raw.id, 10); if (Number.isNaN(id) || id <= 0) return undefined; return { id, type: raw.type }; } interface ApiCtx { gitea: Gitea; owner: string; repo: string; } export async function getProgressComment( ctx: ApiCtx, comment: ProgressComment ): Promise<{ id: number; body: string | undefined; html_url: string }> { const result = await ctx.gitea.rest.issue.issueGetComment({ owner: ctx.owner, repo: ctx.repo, id: comment.id, }); return { id: result.data.id!, body: result.data.body ?? undefined, html_url: result.data.html_url! }; } export async function updateProgressComment( ctx: ApiCtx, comment: ProgressComment, body: string ): Promise<{ id: number; body: string | undefined; html_url: string; node_id: string | undefined }> { const result = await ctx.gitea.rest.issue.issueEditComment({ owner: ctx.owner, repo: ctx.repo, id: comment.id, body: { body }, }); return { id: result.data.id!, body: result.data.body ?? undefined, html_url: result.data.html_url!, node_id: undefined, }; } export async function deleteProgressCommentApi( ctx: ApiCtx, comment: ProgressComment ): Promise { await ctx.gitea.rest.issue.issueDeleteComment({ owner: ctx.owner, repo: ctx.repo, id: comment.id, }); } export type CreateProgressCommentTarget = | { kind: "issue"; issueNumber: number } | { kind: "reviewReply"; pullNumber: number; replyToCommentId: number }; export interface CreatedProgressComment { comment: ProgressComment; body: string | undefined; html_url: string; } export async function createLeapingProgressComment( ctx: ApiCtx, target: CreateProgressCommentTarget, body: string ): Promise { const issueNumber = target.kind === "issue" ? target.issueNumber : target.pullNumber; const result = await ctx.gitea.rest.issue.issueCreateComment({ owner: ctx.owner, repo: ctx.repo, index: issueNumber, body: { body }, }); return { comment: { id: result.data.id!, type: "issue" }, body: result.data.body ?? undefined, html_url: result.data.html_url!, }; }