← Public packages
@kody/kody-factory
Forkable multi-project software factory: signals, priority queue, project UI, sweep job. Agent runners and Sentry reach it by events.
Files
/**
* GitHub issue write-back: when a work item changes column, post or edit
* a single status comment on the source GitHub issue, and optionally
* set a label. Gated behind issueWritebackEnabled (default false),
* respects pilot scope, never writes to repos outside the pilot.
*/
import { loadSettings, type PilotScope } from '../lib/settings.ts'
import { inPilotScope } from '../core/scope.ts'
import type { BoardColumn } from '../core/board.ts'
import { BOARD_COLUMN_LABELS } from '../core/board.ts'
import type { WorkItem } from '../core/work-item.ts'
export type WritebackResult =
| { ok: true; action: 'created' | 'updated' | 'labeled'; commentId?: string }
| { ok: false; reason: string }
export type WritebackGateResult =
| { allowed: true }
| { allowed: false; reason: string }
export function checkWritebackGate(opts: {
enabled: boolean
pilotScope: PilotScope
projectId: string
repoRef: string | null
}): WritebackGateResult {
if (!opts.enabled) {
return { allowed: false, reason: 'issue write-back is disabled' }
}
if (!opts.repoRef) {
return { allowed: false, reason: 'no repository reference on the work item' }
}
if (!inPilotScope(opts.pilotScope, opts.projectId, opts.repoRef)) {
return { allowed: false, reason: 'repository is outside the pilot scope' }
}
return { allowed: true }
}
export function formatStatusComment(item: WorkItem, column: BoardColumn): string {
const label = BOARD_COLUMN_LABELS[column] ?? column
const lines = [
`**Factory status: ${label}**`,
'',
`Work item \`${item.id}\` is now in the **${label}** column.`,
]
if (item.pr?.url) {
lines.push(`PR: ${item.pr.url}`)
}
if (column === 'shipped') {
lines.push('', 'This item has shipped.')
}
if (column === 'needs-kent') {
lines.push('', 'This item needs a decision before it can move forward.')
}
lines.push('', '---', '*Updated by Kody Factory. This comment is edited in place.*')
return lines.join('\n')
}
export function writebackLabel(column: BoardColumn): string | null {
switch (column) {
case 'running': return 'factory:building'
case 'in-review': return 'factory:in-review'
case 'shipped': return 'factory:shipped'
case 'needs-kent': return 'factory:needs-decision'
case 'dropped': return 'factory:dropped'
default: return null
}
}
export type GithubWriteClient = {
fetch: typeof fetch
}
const COMMENT_MARKER = '<!-- kody-factory-status -->'
export async function writeStatusToIssue(opts: {
github: GithubWriteClient
repoRef: string
issueNumber: number
body: string
label: string | null
existingCommentId?: string | null
}): Promise<WritebackResult> {
const [owner, repo] = opts.repoRef.split('/')
if (!owner || !repo) {
return { ok: false, reason: 'invalid repoRef' }
}
const markedBody = `${COMMENT_MARKER}\n${opts.body}`
try {
let commentId = opts.existingCommentId
if (commentId) {
const res = await opts.github.fetch(
`https://api.github.com/repos/${owner}/${repo}/issues/comments/${commentId}`,
{
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
Accept: 'application/vnd.github+json',
'x-kody-secret-resolution': 'headers-only',
},
body: JSON.stringify({ body: markedBody }),
},
)
if (res.ok) {
return { ok: true, action: 'updated', commentId }
}
}
const createRes = await opts.github.fetch(
`https://api.github.com/repos/${owner}/${repo}/issues/${opts.issueNumber}/comments`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/vnd.github+json',
'x-kody-secret-resolution': 'headers-only',
},
body: JSON.stringify({ body: markedBody }),
},
)
if (!createRes.ok) {
return { ok: false, reason: `GitHub API ${createRes.status}` }
}
const data = await createRes.json() as { id?: number }
commentId = data.id ? String(data.id) : undefined
if (opts.label) {
await opts.github.fetch(
`https://api.github.com/repos/${owner}/${repo}/issues/${opts.issueNumber}/labels`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/vnd.github+json',
'x-kody-secret-resolution': 'headers-only',
},
body: JSON.stringify({ labels: [opts.label] }),
},
).catch(() => {})
}
return { ok: true, action: 'created', commentId }
} catch (err) {
return { ok: false, reason: err instanceof Error ? err.message : String(err) }
}
}