|
| 1 | +/** |
| 2 | + * Best-effort changelog resolver. Given an image's source label + current |
| 3 | + * version, fetch GitHub release notes newer than what's running, or fall back |
| 4 | + * to a "where to look" link (the source URL, Docker Hub tags, GHCR repo). |
| 5 | + * |
| 6 | + * The parsing/selection helpers are pure (unit-tested); only fetchGitHubReleases |
| 7 | + * touches the network. |
| 8 | + */ |
| 9 | + |
| 10 | +import { parseRef } from './reconcile.js'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Extract {owner, repo} from a GitHub URL, or null. |
| 14 | + * @param {string|null} sourceUrl |
| 15 | + * @returns {{owner: string, repo: string}|null} |
| 16 | + */ |
| 17 | +export function parseGitHubRepo(sourceUrl) { |
| 18 | + if (typeof sourceUrl !== 'string') return null; |
| 19 | + const m = sourceUrl.match(/github\.com[/:]([^/]+)\/([^/#?]+)/i); |
| 20 | + if (!m) return null; |
| 21 | + const owner = m[1]; |
| 22 | + const repo = m[2].replace(/\.git$/i, ''); |
| 23 | + if (!owner || !repo) return null; |
| 24 | + return { owner, repo }; |
| 25 | +} |
| 26 | + |
| 27 | +function normalizeVer(v) { |
| 28 | + return String(v || '').trim().replace(/^v/i, ''); |
| 29 | +} |
| 30 | + |
| 31 | +function truncate(s, n) { |
| 32 | + if (typeof s !== 'string') return ''; |
| 33 | + return s.length > n ? `${s.slice(0, n)}…` : s; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * From a newest-first list of releases, pick those newer than currentVersion. |
| 38 | + * Heuristic: walk from newest until we hit the release matching the running |
| 39 | + * version; if we never match, show the most recent few. Pure + testable. |
| 40 | + * |
| 41 | + * @param {Array<{tag_name?: string, name?: string}>} releases |
| 42 | + * @param {string|null} currentVersion |
| 43 | + * @returns {Array<object>} |
| 44 | + */ |
| 45 | +export function selectNewerReleases(releases, currentVersion) { |
| 46 | + if (!Array.isArray(releases)) return []; |
| 47 | + if (!currentVersion) return releases.slice(0, 5); |
| 48 | + const cur = normalizeVer(currentVersion); |
| 49 | + const out = []; |
| 50 | + for (const r of releases) { |
| 51 | + const tag = normalizeVer(r.tag_name || r.name || ''); |
| 52 | + if (tag && tag === cur) break; // reached the running version |
| 53 | + out.push(r); |
| 54 | + if (out.length >= 10) break; |
| 55 | + } |
| 56 | + return out; |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Best-effort "where to look" link for an image with no GitHub source label. |
| 61 | + * @param {string} image |
| 62 | + * @returns {{url: string, label: string}|null} |
| 63 | + */ |
| 64 | +export function buildRegistryLink(image) { |
| 65 | + let parsed; |
| 66 | + try { |
| 67 | + parsed = parseRef(image); |
| 68 | + } catch { |
| 69 | + return null; |
| 70 | + } |
| 71 | + const { registry, repository } = parsed; |
| 72 | + if (registry === 'docker.io') { |
| 73 | + if (repository.startsWith('library/')) { |
| 74 | + return { url: `https://hub.docker.com/_/${repository.slice('library/'.length)}`, label: 'Docker Hub' }; |
| 75 | + } |
| 76 | + return { url: `https://hub.docker.com/r/${repository}/tags`, label: 'Docker Hub' }; |
| 77 | + } |
| 78 | + if (registry === 'ghcr.io') { |
| 79 | + return { url: `https://github.com/${repository}`, label: 'GitHub' }; |
| 80 | + } |
| 81 | + return null; |
| 82 | +} |
| 83 | + |
| 84 | +async function fetchGitHubReleases(owner, repo, timeoutMs = 10000) { |
| 85 | + const url = `https://api.github.com/repos/${owner}/${repo}/releases?per_page=30`; |
| 86 | + const res = await fetch(url, { |
| 87 | + headers: { |
| 88 | + Accept: 'application/vnd.github+json', |
| 89 | + 'User-Agent': 'diun-updater', |
| 90 | + }, |
| 91 | + signal: AbortSignal.timeout(timeoutMs), |
| 92 | + }); |
| 93 | + if (!res.ok) throw new Error(`GitHub API ${res.status}`); |
| 94 | + return res.json(); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Resolve a changelog payload for a container's image. |
| 99 | + * |
| 100 | + * @param {{ image: string, sourceUrl: string|null, currentVersion: string|null }} meta |
| 101 | + * @returns {Promise<object>} |
| 102 | + */ |
| 103 | +export async function getChangelog({ image, sourceUrl, currentVersion }) { |
| 104 | + const gh = parseGitHubRepo(sourceUrl); |
| 105 | + if (gh) { |
| 106 | + const releasesUrl = `https://github.com/${gh.owner}/${gh.repo}/releases`; |
| 107 | + try { |
| 108 | + const releases = await fetchGitHubReleases(gh.owner, gh.repo); |
| 109 | + const selected = selectNewerReleases(releases, currentVersion); |
| 110 | + return { |
| 111 | + type: 'github', |
| 112 | + repoUrl: `https://github.com/${gh.owner}/${gh.repo}`, |
| 113 | + releasesUrl, |
| 114 | + currentVersion: currentVersion || null, |
| 115 | + releases: selected.map((r) => ({ |
| 116 | + tag: r.tag_name || r.name || '', |
| 117 | + name: r.name || r.tag_name || '', |
| 118 | + url: r.html_url, |
| 119 | + publishedAt: r.published_at, |
| 120 | + body: truncate(r.body || '', 1500), |
| 121 | + })), |
| 122 | + }; |
| 123 | + } catch (err) { |
| 124 | + return { |
| 125 | + type: 'link', |
| 126 | + url: releasesUrl, |
| 127 | + label: 'Releases', |
| 128 | + note: `Couldn't fetch release notes (${err.message}).`, |
| 129 | + }; |
| 130 | + } |
| 131 | + } |
| 132 | + if (sourceUrl) return { type: 'link', url: sourceUrl, label: 'Source' }; |
| 133 | + const reg = buildRegistryLink(image); |
| 134 | + if (reg) return { type: 'link', url: reg.url, label: reg.label }; |
| 135 | + return { type: 'none' }; |
| 136 | +} |
| 137 | + |
| 138 | +export default { parseGitHubRepo, selectNewerReleases, buildRegistryLink, getChangelog }; |
0 commit comments