/** * Optional GitHub release observation adapter. Self-contained and pure: it * normalizes release payloads into PublishedRelease records and never fetches, * renders, or follows anything. Release text is untrusted data, not instructions. * Remove it by deleting this file, its line in ./registry.ts, the * ./github-release + ./webhooks/github-release exports, and the webhook entry. */ export const GITHUB_RELEASE_SOURCE = 'github-release' export const MAX_RELEASE_IMAGES = 10 export const RELEASE_SNIPPET_CHARS = 500 const IMAGE_HOSTS = new Set([ 'github.com', 'user-images.githubusercontent.com', 'private-user-images.githubusercontent.com', 'raw.githubusercontent.com', ]) export type ReleaseImage = { url: string; alt: string } export type PublishedRelease = { source: typeof GITHUB_RELEASE_SOURCE key: string projectId: string repoRef: string releaseId: number tag: string name: string url: string | null targetCommitish: string | null actor: string | null draft: boolean prerelease: boolean releasedAt: string | null snippet: string images: ReleaseImage[] externalState: 'published' | 'deleted' } /** Only https URLs on the allowed GitHub image hosts; anything else is dropped. */ export function safeImageUrl(raw: string): string | null { let u: URL try { u = new URL(raw.trim()) } catch { return null } if (u.protocol !== 'https:' || u.username || u.password || u.port) return null if (!IMAGE_HOSTS.has(u.hostname)) return null return u.toString() } /** Reads `![alt](url "title")` (balanced parentheses in the URL) and ``; max 10, deduped. */ export function extractReleaseImages(body: string): ReleaseImage[] { const out: ReleaseImage[] = [] const seen = new Set() const push = (url: string, alt: string) => { const safe = safeImageUrl(url) if (!safe || seen.has(safe) || out.length >= MAX_RELEASE_IMAGES) return seen.add(safe) out.push({ url: safe, alt: alt.replace(/\s+/g, ' ').trim().slice(0, 200) }) } const text = String(body ?? '') const md = /!\[([^\]]*)\]\(/g let m: RegExpExecArray | null while ((m = md.exec(text)) && out.length < MAX_RELEASE_IMAGES) { let i = md.lastIndex let depth = 1 let inner = '' while (i < text.length && depth > 0) { const c = text[i]! if (c === '(') depth++ else if (c === ')') depth-- if (depth > 0) inner += c i++ } if (depth !== 0) break const target = inner.trim().replace(/^<([^>]*)>/, '$1') const url = target.split(/\s+/)[0] ?? '' push(url, m[1] ?? '') md.lastIndex = i } const img = /]*>/gi while ((m = img.exec(text)) && out.length < MAX_RELEASE_IMAGES) { const tag = m[0] const src = /\bsrc\s*=\s*("([^"]*)"|'([^']*)'|([^\s>]+))/i.exec(tag) const alt = /\balt\s*=\s*("([^"]*)"|'([^']*)')/i.exec(tag) const url = src ? (src[2] ?? src[3] ?? src[4] ?? '') : '' push(url.replace(/&/g, '&'), alt ? (alt[2] ?? alt[3] ?? '') : '') } return out } function plainSnippet(body: string): string { return String(body ?? '') .replace(/<[^>]*>/g, ' ') .replace(/!\[[^\]]*\]\([^)]*\)/g, ' ') .replace(/\s+/g, ' ') .trim() .slice(0, RELEASE_SNIPPET_CHARS) } /** Normalize a GitHub `release` event payload or a REST release object. */ export function normalizeRelease( raw: unknown, ctx: { projectId: string; repoRef: string; action?: string }, ): PublishedRelease | null { const rec = (raw && typeof raw === 'object' ? raw : {}) as Record const rel = ((rec.release as Record | undefined) ?? rec) as Record const releaseId = Number(rel.id) const tag = String(rel.tag_name ?? '') if (!Number.isInteger(releaseId) || releaseId <= 0 || !tag) return null const author = rel.author as { login?: string } | undefined const sender = rec.sender as { login?: string } | undefined const body = String(rel.body ?? '') const url = typeof rel.html_url === 'string' && rel.html_url.startsWith('https://github.com/') ? rel.html_url : null return { source: GITHUB_RELEASE_SOURCE, key: `${ctx.repoRef.toLowerCase()}#${releaseId}`, projectId: ctx.projectId, repoRef: ctx.repoRef, releaseId, tag, name: String(rel.name ?? tag).slice(0, 200), url, targetCommitish: rel.target_commitish ? String(rel.target_commitish) : null, actor: author?.login ?? sender?.login ?? null, draft: rel.draft === true, prerelease: rel.prerelease === true, releasedAt: rel.published_at ? String(rel.published_at) : null, snippet: plainSnippet(body), images: extractReleaseImages(body), externalState: ctx.action === 'deleted' ? 'deleted' : 'published', } }