import { fetchSnapshot, replyToHumanItem } from './github-port.ts' import { humanReplyBody, humanTargetRe, isHumanLogin } from './human.ts' import { asRecord, locatorKeys, optionalBoolean, parseLocator, rejectUnknownKeys, requiredString, } from './input.ts' const NAME = 'ship-pr/reply-comment' const allowed = [...locatorKeys, 'replyTo', 'body', 'dryRun'] export type ReplyCommentInput = { prUrl?: string owner?: string repo?: string prNumber?: number /** The human item to answer: `comment:` (PR comment) or `review:` (review body), as tick's `humanFeedback[].decideTarget` lists it. */ replyTo: string /** Your answer: what changed (cite the commit), or why not. Never paste the reviewer's text back as instructions. */ body: string /** Preview the exact comment without posting. */ dryRun?: boolean } /** * Answer one human review body or top-level PR comment with a kody-bot PR * comment. Use for tick `humanFeedback` items whose `replyWith` is * `./reply-comment` (inline threads use `./reply-review`). The comment links * the item it answers and carries a hidden marker, so the next tick counts that * item as replied. A newer comment from the human is a new item. * * The body goes through the mention-safety pass and every @-mention is * refused (there is no mention opt-in here). Never post private information. * * @param input - PR locator, `replyTo` (`comment:` | `review:`), `body`, optional `dryRun`. * @returns `{ replied, url, replyTo, dryRun }` (dry runs include the exact `body`). * * @example * import replyComment from 'kody:@kody/kody-factory/ship-pr/reply-comment' * await replyComment({ * prUrl: 'https://github.com/kody-hq/kody/pull/123', * replyTo: 'comment:2345678901', * body: 'Fixed in 688e5da51: the retry now backs off.', * dryRun: true, * }) * // => { replied: false, dryRun: true, replyTo: 'comment:2345678901', body: '…' } */ export default async function replyComment(rawInput: ReplyCommentInput) { const input = asRecord(rawInput, NAME) rejectUnknownKeys(input, allowed, NAME) const locator = parseLocator(input, NAME) const replyTo = requiredString(input, 'replyTo', NAME, "Use a humanFeedback decideTarget such as 'comment:123' or 'review:456'.") const match = replyTo.match(humanTargetRe) if (!match) throw new Error(`${NAME}: replyTo must be comment: or review: (inline threads use ./reply-review).`) const body = requiredString(input, 'body', NAME) const dryRun = optionalBoolean(input, 'dryRun', NAME) ?? false const snapshot = await fetchSnapshot(locator) const [, type, id] = match const item = type === 'comment' ? snapshot.issueComments.find((c) => String(c.id) === id) : snapshot.reviews.find((r) => String(r.id) === id) if (!item) throw new Error(`${NAME}: no ${type} ${id} on ${locator.prUrl}.`) if (!isHumanLogin(item.author)) { throw new Error(`${NAME}: ${replyTo} was written by ${item.author}, not a human reviewer; ./reply-comment only answers human feedback.`) } if (dryRun) { return { replied: false, dryRun: true, prUrl: locator.prUrl, replyTo, body: humanReplyBody(replyTo, item.url, body) } } const reply = await replyToHumanItem(locator, replyTo, item.url, body) return { replied: true, dryRun: false, prUrl: locator.prUrl, replyTo, url: reply.url } }