← 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
import {
loadPlanStore,
savePlanStore,
newPlanId,
PLAN_STATUSES,
type PlanStatus,
type PlanPriority,
type Plan,
} from '../../core/plan.ts'
import { nowIso } from '../../core/work-item.ts'
type Card = {
id: string
type: string
title: string
status: string
priority: string | null
updatedAt: string
createdAt: string
url: string
linked: { url: string; number: number; repo?: string; state?: string } | null
isArchived: boolean
body: string
contentId: string
fields: Record<string, string>
[k: string]: unknown
}
type ImportPayload = {
project: { org: string; number: number; title: string; url: string }
cards: Card[]
dryRun?: boolean
}
type CardResult = { cardId: string; planId: string; action: 'created' | 'updated' | 'unchanged' } | { cardId: string; error: string }
const STATUS_MAP: Record<string, PlanStatus> = {
exploring: 'exploring',
proposed: 'proposed',
decided: 'decided',
'in progress': 'in-progress',
shipped: 'shipped',
}
function mapStatus(raw: string): PlanStatus {
const lower = raw.toLowerCase().trim()
return STATUS_MAP[lower] ?? 'exploring'
}
function mapPriority(raw: string | null): PlanPriority {
if (!raw) return null
const m = /^p([0-3])/i.exec(raw.trim())
return m ? (Number(m[1]) as 0 | 1 | 2 | 3) : null
}
/**
* Import plans from a GitHub Projects v2 payload. Idempotent keyed on the
* GitHub item id (re-import updates in place, never duplicates). Never calls
* GitHub. Handles batched calls.
*
* @example
* import importPlans from 'kody:@kody/kody-factory/plans/import'
* await importPlans({ project: { org: 'kody-hq', number: 1, title: 'Kody plans', url: '...' }, cards: [...], dryRun: true })
*/
export default async function importPlans(input: ImportPayload) {
if (!input?.project) throw new Error('plans/import: project is required')
if (!Array.isArray(input.cards)) throw new Error('plans/import: cards array is required')
const dryRun = input.dryRun !== false
const store = await loadPlanStore()
const byItemId = new Map<string, string>()
for (const [id, plan] of Object.entries(store.plans)) {
if (plan.source?.kind === 'github-project' && plan.source.itemId) {
byItemId.set(plan.source.itemId, id)
}
}
const results: CardResult[] = []
let created = 0
let updated = 0
let unchanged = 0
let skipped = 0
const byStatus: Record<string, number> = {}
const byPriority: Record<string, number> = {}
for (const card of input.cards) {
try {
if (!card.id || !card.title) {
results.push({ cardId: card.id ?? '?', error: 'missing id or title' })
skipped++
continue
}
const status = mapStatus(card.status)
const priority = mapPriority(card.priority)
byStatus[status] = (byStatus[status] ?? 0) + 1
byPriority[priority === null ? 'none' : `P${priority}`] = (byPriority[priority === null ? 'none' : `P${priority}`] ?? 0) + 1
const { id: _id, type: _type, title: _title, status: _status, priority: _priority, updatedAt: _upd, createdAt: _cre, url: _url, linked: _linked, isArchived: _arch, body: _body, contentId: _cid, fields: _fields, ...extraFields } = card
const source = {
kind: 'github-project' as const,
itemId: card.id,
url: card.url || input.project.url,
contentId: card.contentId || '',
type: card.type || 'DRAFT_ISSUE',
importedAt: nowIso(),
extra: Object.keys(extraFields).length ? extraFields as Record<string, unknown> : undefined,
}
const existingId = byItemId.get(card.id)
if (existingId && store.plans[existingId]) {
const existing = store.plans[existingId]!
let changed = false
const next: Plan = { ...existing }
if (next.title !== card.title) { next.title = card.title; changed = true }
if (next.body !== (card.body ?? '')) { next.body = card.body ?? ''; changed = true }
if (next.priority !== priority) { next.priority = priority; changed = true }
if (next.archived !== card.isArchived) { next.archived = card.isArchived; changed = true }
if (card.linked && (!next.linkedIssue || next.linkedIssue.url !== card.linked.url)) {
next.linkedIssue = { url: card.linked.url, number: card.linked.number, repo: card.linked.repo, state: card.linked.state }
changed = true
}
if (card.fields && JSON.stringify(next.source?.extra?.fields) !== JSON.stringify(card.fields)) {
next.source = { ...source, extra: { ...source.extra, fields: card.fields } }
changed = true
}
if (status !== next.status) {
const at = card.updatedAt || nowIso()
next.status = status
next.statusChangedAt = at
next.events = [...next.events, { at, from: existing.status, to: status, reason: 'import update', actor: 'import' }]
changed = true
}
if (changed) {
next.updatedAt = nowIso()
next.source = { ...source, extra: { ...source.extra, fields: card.fields } }
if (!dryRun) store.plans[existingId] = next
results.push({ cardId: card.id, planId: existingId, action: 'updated' })
updated++
} else {
results.push({ cardId: card.id, planId: existingId, action: 'unchanged' })
unchanged++
}
} else {
const plan: Plan = {
id: newPlanId(),
title: card.title,
body: card.body ?? '',
status,
priority,
projectId: 'kody',
linkedIssue: card.linked ? { url: card.linked.url, number: card.linked.number, repo: card.linked.repo, state: card.linked.state } : undefined,
links: [],
progressNotes: [],
createdAt: card.createdAt || nowIso(),
updatedAt: card.updatedAt || nowIso(),
statusChangedAt: card.updatedAt || nowIso(),
events: [{ at: card.createdAt || nowIso(), from: null, to: status, reason: 'imported from GitHub Projects', actor: 'import' }],
source: { ...source, extra: { ...source.extra, fields: card.fields } },
archived: card.isArchived,
workItemIds: [],
}
if (!dryRun) {
store.plans[plan.id] = plan
byItemId.set(card.id, plan.id)
}
results.push({ cardId: card.id, planId: plan.id, action: 'created' })
created++
}
} catch (err) {
results.push({ cardId: card.id ?? '?', error: err instanceof Error ? err.message : String(err) })
skipped++
}
}
if (!dryRun) await savePlanStore(store)
return {
received: input.cards.length,
created,
updated,
unchanged,
skipped,
byStatus,
byPriority,
totalPlansAfter: Object.keys(store.plans).length,
results,
dryRun,
}
}