返回 CodeWhale
github.ts
根目录 / web / lib / github.ts
1 import type { FeedItem, RepoStats } from "./types";
2
3 const REPO = process.env.GITHUB_REPO ?? "Hmbown/CodeWhale";
4 const GH = "https://api.github.com";
5 const MIN_KNOWN_CONTRIBUTORS = 141;
6
7 function isProductionBuild(): boolean {
8 return process.env.NEXT_PHASE === "phase-production-build";
9 }
10
11 function headers(token?: string): HeadersInit {
12 const h: Record<string, string> = {
13 Accept: "application/vnd.github+json",
14 "X-GitHub-Api-Version": "2022-11-28",
15 "User-Agent": "codewhale-web",
16 };
17 if (token) h.Authorization = `Bearer ${token}`;
18 return h;
19 }
20
21 export async function fetchRepoStats(token?: string): Promise<RepoStats> {
22 // Live repository chrome is optional. Static generation must stay
23 // deterministic and offline; deployed requests and ISR refreshes populate
24 // the current values after the build.
25 if (isProductionBuild()) {
26 return {
27 stars: 0,
28 forks: 0,
29 openIssues: 0,
30 openPulls: 0,
31 contributors: MIN_KNOWN_CONTRIBUTORS,
32 fetchedAt: new Date().toISOString(),
33 };
34 }
35
36 const [repoRes, contribRes, releaseRes] = await Promise.all([
37 fetch(`${GH}/repos/${REPO}`, { headers: headers(token), next: { revalidate: 1800 } }),
38 fetch(`${GH}/repos/${REPO}/contributors?per_page=1&anon=true`, {
39 headers: headers(token),
40 next: { revalidate: 3600 },
41 }),
42 fetch(`${GH}/repos/${REPO}/releases/latest`, { headers: headers(token), next: { revalidate: 3600 } }),
43 ]);
44
45 const repo = repoRes.ok ? await repoRes.json().catch(() => null) : null;
46 const stars = numberField(repo, "stargazers_count");
47 const forks = numberField(repo, "forks_count");
48 const repoOpenCount = numberField(repo, "open_issues_count");
49
50 const contributors = await contributorCount(contribRes);
51
52 // Open PRs: cheapest path is the search API.
53 const prRes = await fetch(
54 `${GH}/search/issues?q=${encodeURIComponent(`repo:${REPO} is:pr is:open`)}&per_page=1`,
55 { headers: headers(token), next: { revalidate: 1800 } }
56 );
57 const prJson = prRes.ok ? ((await prRes.json().catch(() => null)) as { total_count?: number } | null) : null;
58 const openPulls = typeof prJson?.total_count === "number" ? prJson.total_count : 0;
59 const openIssues = Math.max(0, repoOpenCount - openPulls);
60
61 let latestRelease: RepoStats["latestRelease"];
62 if (releaseRes.ok) {
63 const r = (await releaseRes.json()) as { tag_name: string; published_at: string; html_url: string };
64 latestRelease = { tag: r.tag_name, publishedAt: r.published_at, url: r.html_url };
65 }
66
67 return {
68 stars,
69 forks,
70 openIssues,
71 openPulls,
72 contributors,
73 latestRelease,
74 fetchedAt: new Date().toISOString(),
75 };
76 }
77
78 function numberField(body: unknown, key: string): number {
79 if (!body || typeof body !== "object") return 0;
80 const value = (body as Record<string, unknown>)[key];
81 return typeof value === "number" && Number.isFinite(value) ? value : 0;
82 }
83
84 async function contributorCount(res: Response): Promise<number> {
85 if (!res.ok) return MIN_KNOWN_CONTRIBUTORS;
86
87 const fromLink = lastPageFromLink(res.headers.get("link"));
88 if (fromLink) return Math.max(fromLink, MIN_KNOWN_CONTRIBUTORS);
89
90 const body = await res.json().catch(() => null);
91 if (Array.isArray(body)) return Math.max(body.length, MIN_KNOWN_CONTRIBUTORS);
92
93 return MIN_KNOWN_CONTRIBUTORS;
94 }
95
96 export function lastPageFromLink(link: string | null): number | undefined {
97 if (!link) return undefined;
98
99 for (const part of link.split(",")) {
100 const [rawUrl, rawRel] = part.split(";").map((segment) => segment.trim());
101 if (rawRel !== 'rel="last"') continue;
102
103 const match = rawUrl.match(/^<(.+)>$/);
104 if (!match) continue;
105
106 const page = new URL(match[1]).searchParams.get("page");
107 const parsed = page ? Number.parseInt(page, 10) : NaN;
108 if (Number.isFinite(parsed) && parsed > 0) return parsed;
109 }
110
111 return undefined;
112 }
113
114 interface RawIssue {
115 number: number;
116 title: string;
117 html_url: string;
118 state: "open" | "closed";
119 user: { login: string; avatar_url: string };
120 created_at: string;
121 updated_at: string;
122 closed_at?: string | null;
123 comments: number;
124 labels: { name: string; color: string }[];
125 pull_request?: unknown;
126 draft?: boolean;
127 body?: string | null;
128 /**
129 * GitHub's relationship verdict for the author, present on both list
130 * endpoints. "FIRST_TIME_CONTRIBUTOR" is the only value we read.
131 */
132 author_association?: string;
133 }
134
135 interface RawRelease {
136 tag_name: string;
137 name?: string | null;
138 html_url: string;
139 created_at: string;
140 published_at?: string | null;
141 draft?: boolean;
142 prerelease?: boolean;
143 author?: { login: string; avatar_url: string } | null;
144 }
145
146 /** How many releases to pull. The tail is noise; the ticker sorts by date. */
147 const RELEASE_WINDOW = 5;
148
149 /** How recent a release must be to keep a reserved slot in a busy feed. */
150 const RELEASE_PIN_WINDOW_MS = 60 * 24 * 60 * 60 * 1000;
151
152 function firstTimer(association?: string): boolean {
153 return association === "FIRST_TIME_CONTRIBUTOR";
154 }
155
156 /**
157 * GitHub marks app accounts with a `[bot]` suffix on the login — its own
158 * verdict, not our inference. The wire exists to put the people behind the
159 * repository on the front page; dependency bumps and automated closes spend
160 * slots that belong to them, so bot-authored issues and pulls stay off. A
161 * published release is news no matter who pushed the button, so it keeps its
162 * slot — with a bot publisher's byline dropped instead
163 * (`author === ""` renders no by-line in components/ticker.tsx).
164 */
165 function isBot(login: string): boolean {
166 return login.endsWith("[bot]");
167 }
168
169 /**
170 * The repository's recent life: issues, pull requests, and releases.
171 *
172 * Three cached GitHub calls, no per-item follow-ups. Merge state, the
173 * author's handle, and GitHub's first-time-contributor verdict all arrive in
174 * the list payloads we already fetch, so naming a newcomer on the homepage
175 * costs nothing extra. Releases change rarely and cache for an hour;
176 * unauthenticated that is ~13 requests/hour against GitHub's 60/hour/IP.
177 */
178 export async function fetchFeed(token?: string, limit = 30): Promise<FeedItem[]> {
179 return (await loadFeed(token, limit)).items;
180 }
181
182 /**
183 * Why the feed is what it is. `fetchFeed` flattens this to a list, which is
184 * right for optional chrome (the homepage ticker) but wrong for a page whose
185 * whole body is the feed: there, "GitHub had nothing" and "GitHub was not
186 * asked" or "GitHub refused" must render differently, or a rate limit and a
187 * build-time prerender both masquerade as an honest empty record.
188 *
189 * - `ok` — that list endpoint answered; an empty list is real.
190 * - `skipped` — static generation; nothing was fetched.
191 * - `unavailable` — that call came back non-ok (rate limit, outage);
192 * `items` holds whatever did arrive.
193 *
194 * Availability is tracked per list: when exactly one endpoint refuses, the
195 * other column must not be told that "the source did not answer".
196 */
197 export type FeedLoadStatus = "ok" | "skipped" | "unavailable";
198
199 export interface FeedLoad {
200 items: FeedItem[];
201 issuesStatus: FeedLoadStatus;
202 pullsStatus: FeedLoadStatus;
203 }
204
205 export async function loadFeed(token?: string, limit = 30): Promise<FeedLoad> {
206 if (isProductionBuild())
207 return { items: [], issuesStatus: "skipped", pullsStatus: "skipped" };
208
209 const [issuesRes, pullsRes, releasesRes] = await Promise.all([
210 fetch(
211 `${GH}/repos/${REPO}/issues?state=all&per_page=${limit}&sort=updated&direction=desc`,
212 { headers: headers(token), next: { revalidate: 600 } }
213 ),
214 fetch(
215 `${GH}/repos/${REPO}/pulls?state=all&per_page=${limit}&sort=updated&direction=desc`,
216 { headers: headers(token), next: { revalidate: 600 } }
217 ),
218 fetch(`${GH}/repos/${REPO}/releases?per_page=${RELEASE_WINDOW}`, {
219 headers: headers(token),
220 next: { revalidate: 3600 },
221 }),
222 ]);
223
224 // Releases are a garnish on the feed; the two list calls are the record,
225 // and each answers for itself.
226 const issuesStatus: FeedLoadStatus = issuesRes.ok ? "ok" : "unavailable";
227 const pullsStatus: FeedLoadStatus = pullsRes.ok ? "ok" : "unavailable";
228 const issues = await responseArray<RawIssue>(issuesRes);
229 const pulls = await responseArray<RawIssue & { merged_at?: string | null }>(pullsRes);
230 const releases = await responseArray<RawRelease>(releasesRes);
231
232 const items: FeedItem[] = [];
233
234 for (const it of issues) {
235 if (it.pull_request) continue; // GH issues endpoint returns PRs too
236 if (isBot(it.user.login)) continue; // automated maintenance, not contributor life
237 items.push({
238 kind: "issue",
239 number: it.number,
240 title: it.title,
241 url: it.html_url,
242 state: it.state,
243 author: it.user.login,
244 authorAvatar: it.user.avatar_url,
245 createdAt: it.created_at,
246 updatedAt: it.updated_at,
247 eventAt: (it.state === "closed" ? it.closed_at : it.created_at) ?? it.created_at,
248 comments: it.comments,
249 labels: it.labels?.map((l) => ({ name: l.name, color: l.color })) ?? [],
250 body: it.body ?? undefined,
251 firstTimeContributor: firstTimer(it.author_association),
252 });
253 }
254
255 for (const pr of pulls) {
256 if (isBot(pr.user.login)) continue; // automated maintenance, not contributor life
257 let state: FeedItem["state"] = pr.state;
258 let eventAt = pr.created_at;
259 if (pr.merged_at) {
260 state = "merged";
261 eventAt = pr.merged_at;
262 } else if (pr.draft) {
263 state = "draft";
264 } else if (pr.state === "closed") {
265 eventAt = pr.closed_at ?? pr.updated_at;
266 }
267 items.push({
268 kind: "pull",
269 number: pr.number,
270 title: pr.title,
271 url: pr.html_url,
272 state,
273 author: pr.user.login,
274 authorAvatar: pr.user.avatar_url,
275 createdAt: pr.created_at,
276 updatedAt: pr.updated_at,
277 eventAt,
278 comments: pr.comments,
279 labels: pr.labels?.map((l) => ({ name: l.name, color: l.color })) ?? [],
280 body: pr.body ?? undefined,
281 firstTimeContributor: firstTimer(pr.author_association),
282 });
283 }
284
285 for (const rel of releases) {
286 if (rel.draft) continue; // an unpublished draft is not news
287 const publishedAt = rel.published_at ?? rel.created_at;
288 // A bot-published release keeps its slot but not its byline.
289 const publisher =
290 rel.author && !isBot(rel.author.login) ? rel.author.login : "";
291 items.push({
292 kind: "release",
293 number: 0,
294 tag: rel.tag_name,
295 title: rel.name?.trim() || rel.tag_name,
296 url: rel.html_url,
297 state: "published",
298 author: publisher,
299 authorAvatar: publisher ? rel.author?.avatar_url ?? "" : "",
300 createdAt: rel.created_at,
301 updatedAt: publishedAt,
302 eventAt: publishedAt,
303 comments: 0,
304 labels: [],
305 });
306 }
307
308 const ordered = items.sort((a, b) => +new Date(b.updatedAt) - +new Date(a.updatedAt));
309 const kept = ordered.slice(0, limit);
310
311 // A release is the one event a busy week can bury: twenty issue comments
312 // will push last week's tag out of a pure recency window. Keep the newest
313 // published release in view — but only a recent one, and always carrying its
314 // real date, so a quiet quarter reads as a quiet quarter instead of pinning
315 // a two-year-old tag beside today's merges.
316 const newestRelease = ordered.find((i) => i.kind === "release");
317 const pinnable =
318 newestRelease &&
319 Date.now() - +new Date(newestRelease.eventAt ?? newestRelease.updatedAt) <
320 RELEASE_PIN_WINDOW_MS;
321 if (pinnable && kept.length === limit && !kept.some((i) => i.kind === "release")) {
322 kept[kept.length - 1] = newestRelease;
323 }
324
325 return { items: kept, issuesStatus, pullsStatus };
326 }
327
328 async function responseArray<T>(res: Response): Promise<T[]> {
329 if (!res.ok) return [];
330 const body = await res.json().catch(() => null);
331 return Array.isArray(body) ? (body as T[]) : [];
332 }
333
334 /** Compact star-count label, e.g. 39312 → "39.3k". */
335 export function formatStars(n: number): string {
336 if (n >= 1000) {
337 return `${(n / 1000).toFixed(1).replace(/\.0$/, "")}k`;
338 }
339 return String(n);
340 }
341
342 /**
343 * An age expressed the way `Intl.RelativeTimeFormat` wants it: a negative
344 * count and a unit. Past ages are negative; anything under a minute (and any
345 * unparseable or future date) is `0 seconds`, which `numeric: "auto"` renders
346 * as the locale's own "now".
347 *
348 * This exists so a surface can print an age in the reader's language without
349 * a hand-translated abbreviation table per locale — CLDR already has one, and
350 * the masthead already formats its date the same way off `chrome.dateLocale`.
351 */
352 export interface RelativeAge {
353 value: number;
354 unit: "second" | "minute" | "hour" | "day" | "month" | "year";
355 }
356
357 export function relativeAge(iso: string): RelativeAge {
358 const then = +new Date(iso);
359 if (!Number.isFinite(then)) return { value: 0, unit: "second" };
360
361 const mins = Math.round((Date.now() - then) / 60000);
362 if (mins < 1) return { value: 0, unit: "second" };
363 if (mins < 60) return { value: -mins, unit: "minute" };
364 const hrs = Math.round(mins / 60);
365 if (hrs < 24) return { value: -hrs, unit: "hour" };
366 const days = Math.round(hrs / 24);
367 if (days < 30) return { value: -days, unit: "day" };
368 const months = Math.round(days / 30);
369 if (months < 12) return { value: -months, unit: "month" };
370 return { value: -Math.round(months / 12), unit: "year" };
371 }
372
373 const AGE_SUFFIX: Record<RelativeAge["unit"], string> = {
374 second: "",
375 minute: "m",
376 hour: "h",
377 day: "d",
378 month: "mo",
379 year: "y",
380 };
381
382 /** Compact English age, e.g. "5m", "3h", "2y". Same thresholds as `relativeAge`. */
383 export function relativeTime(iso: string): string {
384 const age = relativeAge(iso);
385 if (age.unit === "second") return "just now";
386 return `${Math.abs(age.value)}${AGE_SUFFIX[age.unit]}`;
387 }
388
388 lines TYPESCRIPT