|
| 1 | +import { fetch as undiciFetch, RequestInit, Response } from 'undici'; |
| 2 | +import semver, { SemVer } from 'semver'; |
| 3 | + |
| 4 | +const CPYTHON_TAGS_URL = 'https://api.github.com/repos/python/cpython/tags'; |
| 5 | + |
| 6 | +type FetchLike = (input: string, init?: RequestInit) => Promise<Response>; |
| 7 | + |
| 8 | +export interface StableTag { |
| 9 | + tagName: string; |
| 10 | + version: string; |
| 11 | + major: number; |
| 12 | + minor: number; |
| 13 | + patch: number; |
| 14 | + commitSha: string; |
| 15 | +} |
| 16 | + |
| 17 | +export interface FetchStableTagsOptions { |
| 18 | + token?: string; |
| 19 | + perPage?: number; |
| 20 | + fetchImpl?: FetchLike; |
| 21 | + signal?: AbortSignal; |
| 22 | + userAgent?: string; |
| 23 | +} |
| 24 | + |
| 25 | +const defaultFetch = (globalThis.fetch ?? undiciFetch) as FetchLike; |
| 26 | + |
| 27 | +interface GitHubTag { |
| 28 | + name?: unknown; |
| 29 | + commit?: { |
| 30 | + sha?: unknown; |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +function buildHeaders(token?: string, userAgent?: string): Record<string, string> { |
| 35 | + const headers: Record<string, string> = { |
| 36 | + Accept: 'application/vnd.github+json', |
| 37 | + 'User-Agent': userAgent ?? 'python-version-patch-pr/0.1.0', |
| 38 | + }; |
| 39 | + |
| 40 | + if (token) { |
| 41 | + headers.Authorization = `Bearer ${token}`; |
| 42 | + } |
| 43 | + |
| 44 | + return headers; |
| 45 | +} |
| 46 | + |
| 47 | +function parseStableVersion(tagName: string): SemVer | null { |
| 48 | + let normalized = tagName.trim(); |
| 49 | + if (normalized.startsWith('refs/tags/')) { |
| 50 | + normalized = normalized.substring('refs/tags/'.length); |
| 51 | + } |
| 52 | + if (normalized.startsWith('v')) { |
| 53 | + normalized = normalized.substring(1); |
| 54 | + } |
| 55 | + |
| 56 | + const parsed = semver.parse(normalized); |
| 57 | + if (!parsed || parsed.prerelease.length > 0) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + return parsed; |
| 62 | +} |
| 63 | + |
| 64 | +export async function fetchStableCpythonTags( |
| 65 | + options: FetchStableTagsOptions = {}, |
| 66 | +): Promise<StableTag[]> { |
| 67 | + const { token, perPage = 100, fetchImpl = defaultFetch, signal, userAgent } = options; |
| 68 | + |
| 69 | + if (perPage <= 0) { |
| 70 | + throw new Error('perPage must be greater than zero.'); |
| 71 | + } |
| 72 | + |
| 73 | + const results: StableTag[] = []; |
| 74 | + let page = 1; |
| 75 | + |
| 76 | + while (true) { |
| 77 | + const url = new URL(CPYTHON_TAGS_URL); |
| 78 | + url.searchParams.set('per_page', String(perPage)); |
| 79 | + url.searchParams.set('page', String(page)); |
| 80 | + |
| 81 | + const response = await fetchImpl(url.toString(), { |
| 82 | + method: 'GET', |
| 83 | + headers: buildHeaders(token, userAgent), |
| 84 | + signal, |
| 85 | + } satisfies RequestInit); |
| 86 | + |
| 87 | + if (!response.ok) { |
| 88 | + throw new Error(`Failed to fetch CPython tags from GitHub (status ${response.status}).`); |
| 89 | + } |
| 90 | + |
| 91 | + const payload = (await response.json()) as unknown; |
| 92 | + |
| 93 | + if (!Array.isArray(payload)) { |
| 94 | + throw new Error('Unexpected payload when fetching CPython tags from GitHub.'); |
| 95 | + } |
| 96 | + |
| 97 | + let addedForPage = 0; |
| 98 | + |
| 99 | + for (const entry of payload) { |
| 100 | + const tag = entry as GitHubTag; |
| 101 | + if (typeof tag.name !== 'string' || typeof tag.commit?.sha !== 'string') { |
| 102 | + continue; |
| 103 | + } |
| 104 | + |
| 105 | + const parsed = parseStableVersion(tag.name); |
| 106 | + if (!parsed) { |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + results.push({ |
| 111 | + tagName: tag.name, |
| 112 | + version: parsed.version, |
| 113 | + major: parsed.major, |
| 114 | + minor: parsed.minor, |
| 115 | + patch: parsed.patch, |
| 116 | + commitSha: tag.commit.sha, |
| 117 | + }); |
| 118 | + addedForPage += 1; |
| 119 | + } |
| 120 | + |
| 121 | + if (payload.length < perPage) { |
| 122 | + break; |
| 123 | + } |
| 124 | + |
| 125 | + // Guard against infinite loops if GitHub returns empty arrays equal to perPage. |
| 126 | + if (payload.length === 0 || addedForPage === 0) { |
| 127 | + break; |
| 128 | + } |
| 129 | + |
| 130 | + page += 1; |
| 131 | + } |
| 132 | + |
| 133 | + results.sort((a, b) => semver.rcompare(a.version, b.version)); |
| 134 | + return results; |
| 135 | +} |
0 commit comments