← Public packages
@kentcdodds/ship-pr
Progressive ship-pr loop for agents: PR status, Bugbot, review sort, human review feedback, 7-day decisions, merge, deploy, wrap-up.
Files
/**
* Hand a ship report to a factory so it lands on the linked card. Never throws:
* a failed hand-off must not break the Discord summary or the tick step.
*
* Package events stay inside one org, so there are two transports:
* - the URL credential `factoryReportWebhook` is mapped: POST the payload to the
* factory's `ship-report` inbound webhook (a factory in another org), and
* - otherwise emit `@kentcdodds/ship-pr.report.posted` (a factory in this org
* subscribes to it).
* Only one runs, so a report is never applied twice. The webhook body carries no
* timestamp, so a retry with the same `Idempotency-Key` is the same request.
*/
import * as kodyRuntime from 'kody:runtime'
export const REPORT_POSTED_TOPIC = '@kentcdodds/ship-pr.report.posted'
export const REPORT_WEBHOOK_SLOT = 'factoryReportWebhook'
type Dispatch = (input: { topic: string; idempotencyKey: string; payload: Record<string, unknown> }) => Promise<unknown>
type WebhookClient = { fetch(url: string, init?: RequestInit): Promise<{ ok: boolean; status: number }> }
type Runtime = { events?: { dispatch?: Dispatch } | null; credential?: (slot: string) => Promise<WebhookClient> }
const detail = (error: unknown) => (error instanceof Error ? error.message.replace(/\s+/g, ' ').slice(0, 200) : 'failed')
export async function emitReportPosted(idempotencyKey: string, payload: Record<string, unknown>, dryRun: boolean, runtime: Runtime = kodyRuntime as unknown as Runtime) {
if (dryRun) return { emitted: false, skipped: 'dryRun' as const }
const body = { schemaVersion: 1, ...payload }
const hook = await runtime.credential?.(REPORT_WEBHOOK_SLOT).catch(() => null)
try {
if (hook) {
const res = await hook.fetch('', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Idempotency-Key': idempotencyKey, 'x-kody-secret-resolution': 'headers-only' }, body: JSON.stringify(body) })
if (!res.ok) throw new Error(`factory webhook answered HTTP ${res.status}`)
return { emitted: true as const, via: 'webhook' as const }
}
if (typeof runtime.events?.dispatch !== 'function') return { emitted: false, skipped: 'no-dispatch-binding' as const }
await runtime.events.dispatch({ topic: REPORT_POSTED_TOPIC, idempotencyKey, payload: { ...body, occurredAt: new Date().toISOString() } })
return { emitted: true as const, via: 'event' as const }
} catch (error) {
console.error(`ship-pr report hand-off failed: ${detail(error)}`)
return { emitted: false, error: detail(error) }
}
}