/** * On-demand, read-only GitHub release reconcile for projects that enable * `sources.githubRelease`. dryRun (default) never writes. Reports the last * release per repo and, only when a project sets `expectWithinHours`, whether * it is overdue. Never creates releases, work items, or posts. */ import { loadProjects } from '../projects/config.ts' import { normalizeRelease, type PublishedRelease } from '../adapters/github-release.ts' import { releaseReposFor } from '../adapters/registry.ts' import { applyRelease, loadReleaseState, saveReleaseState } from '../lib/release-store.ts' import { GithubSetupError, githubCredentialFetch, githubReadonlyRequest, type GithubFetch } from '../lib/github-readonly.ts' export const RELEASE_MAX_COUNT = 20 /** * Reconcile recent GitHub releases. * * @param params - `{ projectId?, dryRun = true, maxCount = 10 (max 20) }` * @returns Per-repo report with the latest releases (references only) * * @example * import releases from 'kody:@kody/kody-factory/github-release' * await releases({ projectId: 'kody' }) // dry run */ export default async function githubRelease( params: { projectId?: string; dryRun?: boolean; maxCount?: number } = {}, deps: { fetch?: GithubFetch; now?: Date } = {}, ) { const dryRun = params?.dryRun !== false const maxCount = Math.max(1, Math.min(RELEASE_MAX_COUNT, Math.floor(Number(params?.maxCount ?? 10)) || 10)) const now = deps.now ?? new Date() const projects = await loadProjects() const targets: Array<{ projectId: string; repoRef: string; expectWithinHours?: number }> = [] for (const p of Object.values(projects.projects)) { if (params?.projectId && p.id !== params.projectId) continue for (const repoRef of releaseReposFor(p)) targets.push({ projectId: p.id, repoRef, expectWithinHours: p.sources?.githubRelease?.expectWithinHours }) } const base = { dryRun, maxCount, generatedAt: now.toISOString() } if (!targets.length) return { ...base, ok: true, enabled: false, message: 'GitHub release observation is off for every project (sources.githubRelease.enabled).', repos: [] } let fetchFn: GithubFetch try { fetchFn = deps.fetch ?? (await githubCredentialFetch()) } catch (err) { return { ...base, ok: false, enabled: true, setup: 'github-credential-missing', message: (err as Error).message, repos: [] } } const state = await loadReleaseState() let wrote = false const repos = [] for (const t of targets.slice(0, 10)) { const [owner, name] = t.repoRef.split('/') try { const raw = await githubReadonlyRequest(fetchFn, `/repos/${encodeURIComponent(owner!)}/${encodeURIComponent(name!)}/releases?per_page=${maxCount}`) if (!Array.isArray(raw)) throw new Error('GitHub returned a non-list response') const releases = raw.slice(0, maxCount).map((r) => normalizeRelease(r, t)).filter((r): r is PublishedRelease => r !== null && !r.draft) const changes = releases.map((r) => applyRelease(state, r, 'reconcile', now.toISOString())) if (changes.some((c) => c !== 'unchanged')) wrote = true const latest = releases.filter((r) => r.releasedAt).sort((a, b) => b.releasedAt!.localeCompare(a.releasedAt!))[0] ?? null const ageHours = latest?.releasedAt ? (now.getTime() - Date.parse(latest.releasedAt)) / 3.6e6 : null repos.push({ projectId: t.projectId, repoRef: t.repoRef, fetched: releases.length, new: changes.filter((c) => c === 'created').length, latest: latest ? { tag: latest.tag, releasedAt: latest.releasedAt, url: latest.url, images: latest.images.length } : null, ...(t.expectWithinHours ? { expectWithinHours: t.expectWithinHours, overdue: ageHours == null || ageHours > t.expectWithinHours } : {}), }) } catch (err) { repos.push({ projectId: t.projectId, repoRef: t.repoRef, error: err instanceof Error ? err.message : String(err), ...(err instanceof GithubSetupError ? { setup: 'github-credential-missing' } : {}) }) } } if (!dryRun && wrote) await saveReleaseState(state) return { ...base, ok: true, enabled: true, repos } }