import type { GiteaLabel } from './api' import { isMailboxIssue, MAILBOX_BODY, MAILBOX_LABEL, MAILBOX_TITLE } from '../cc/sessionHandoff' import { createIssue, createRepoLabel, listIssuesByFilter, listRepoLabels } from './api' export async function findMailboxIssue(opts: { host: string token: string owner: string repo: string }): Promise { const labels = await listRepoLabels(opts) if (!labels.some(l => l.name === MAILBOX_LABEL)) return undefined const issues = await listIssuesByFilter({ ...opts, labels: MAILBOX_LABEL }) return issues.find(i => isMailboxIssue(i))?.number } export async function ensureMailboxIssue(opts: { host: string token: string owner: string repo: string }): Promise { const existing = await findMailboxIssue(opts) if (existing !== undefined) return existing const labels = await listRepoLabels(opts) let label: GiteaLabel | undefined = labels.find(l => l.name === MAILBOX_LABEL) if (!label) label = await createRepoLabel({ ...opts, name: MAILBOX_LABEL, color: '586069', description: 'Superwork session mailbox' }) const created = await createIssue({ ...opts, title: MAILBOX_TITLE, body: MAILBOX_BODY, labels: [label.id], }) return created.number }