import { fetchSnapshot, replyToReviewComment, resolveReviewThread } from './github-port.ts' import { reviewerMention } from './classify.ts' import { isHumanComment } from './human.ts' import { makeMentionSafe } from './mention-safe.ts' import { asRecord, locatorKeys, optionalBoolean, parseLocator, rejectUnknownKeys, requiredString, } from './input.ts' const NAME = 'ship-pr/reply-review' const allowed = [...locatorKeys, 'commentId', 'body', 'resolve', 'mentionReviewer', 'dryRun'] export type ReplyReviewInput = { prUrl?: string owner?: string repo?: string prNumber?: number /** Review comment id in the thread (tick lists the root `commentId`). */ commentId: number /** * Reply text. Cite the fixing commit ("Fixed in abc1234: …") or the wontfix reasoning. * Posted with headers-only secret resolution, so quoting placeholder syntax is sent as written. */ body: string /** * Also resolve the review thread (kody-bot). Bot threads only: a thread * opened by a human is never resolved by ship-pr (the reviewer resolves it), * so the reply posts and `resolveSkipped` says why. */ resolve?: boolean /** * Allow an @-mention of the reviewer who opened this thread (the thread's * first comment author), and nobody else. Use only when the reply * deliberately asks that reviewer to look again. Default false: the reply * refuses every @-mention. */ mentionReviewer?: boolean /** Preview without posting. */ dryRun?: boolean } /** * Reply to a pull-request review thread as kody-bot, optionally resolving it. * Use instead of `gh api` (Cloud Agent `gh` cannot post review replies). * A reply citing a fixing commit ("Fixed in abc1234") or "wontfix" moves a * sorted Bugbot/Devin/Seer finding to `alreadyAddressed` on the next tick. * * The body goes through the mention-safety pass. Every @-mention is refused * unless `mentionReviewer` allows the thread opener. Threads opened by a human * are never resolved here (`resolve` is skipped with `resolveSkipped`); a * human reply after yours re-opens the item on the next tick. * * @param input - PR locator, `commentId`, `body`, optional `resolve`, optional `mentionReviewer`, optional `dryRun`. * @returns `{ replied, url, resolved, threadId, dryRun }` * * @example * import replyReview from 'kody:@kody/kody-factory/ship-pr/reply-review' * await replyReview({ * prUrl: 'https://github.com/kody-hq/kody/pull/123', * commentId: 4185778067, * body: 'Fixed in 688e5da51: builtins are excluded from the gate.', * resolve: true, * }) * // => { replied: true, url: 'https://github.com/…#discussion_r…', resolved: true } */ export default async function replyReview(rawInput: ReplyReviewInput) { const input = asRecord(rawInput, NAME) rejectUnknownKeys(input, allowed, NAME) const locator = parseLocator(input, NAME) const commentId = Number(input.commentId) if (!Number.isInteger(commentId) || commentId <= 0) { throw new Error(`${NAME}: commentId must be the numeric review comment id.`) } const body = requiredString(input, 'body', NAME) const resolve = optionalBoolean(input, 'resolve', NAME) ?? false const mentionReviewer = optionalBoolean(input, 'mentionReviewer', NAME) ?? false const dryRun = optionalBoolean(input, 'dryRun', NAME) ?? false let threadId: string | null = null let humanThread = false let allowedMentions: Array = [] if (resolve || dryRun || mentionReviewer) { const snapshot = await fetchSnapshot(locator) const thread = snapshot.threads.find((t) => t.comments.some((c) => c.id === commentId)) threadId = thread?.id ?? null if (!thread) { throw new Error(`${NAME}: no review thread on ${locator.prUrl} contains comment ${commentId}.`) } humanThread = Boolean(thread.comments[0] && isHumanComment(thread.comments[0])) if (mentionReviewer) allowedMentions = reviewerMention(thread.comments[0]?.author) } const resolveSkipped = resolve && humanThread ? 'human thread: ship-pr never resolves it; the reviewer does' : undefined const willResolve = resolve && !humanThread if (dryRun) { return { replied: false, dryRun: true, prUrl: locator.prUrl, commentId, threadId, body: makeMentionSafe(body), resolve: willResolve, ...(resolveSkipped ? { resolveSkipped } : {}), mentionReviewer, } } const reply = await replyToReviewComment(locator, commentId, body, { allowedMentions }) const resolved = willResolve && threadId ? await resolveReviewThread(threadId) : false return { replied: true, dryRun: false, url: reply.url, commentId, threadId, resolved, ...(resolveSkipped ? { resolveSkipped } : {}) } }