-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetch-github-data.ts
More file actions
399 lines (362 loc) · 14.3 KB
/
fetch-github-data.ts
File metadata and controls
399 lines (362 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/**
* fetch-github-data.ts — Fetch repo metadata + README HTML from GitHub API
*
* Called during build to produce repos.json for the project map and
* per-repo landing pages. Auth token resolution order:
* 1. GITHUB_TOKEN env var (CI / explicit)
* 2. `gh auth token` CLI fallback (local dev)
* 3. Anonymous (60 req/hr rate limit — falls back to cache on failure)
*/
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { execSync } from "node:child_process";
import { join } from "node:path";
import { REPO_OVERRIDES, EXCLUDE_REPOS, MIN_STARS, RELATIONSHIPS, REPO_SYMBOLS, DEFAULT_SYMBOL } from "./repo-config";
import type { ExternalLink } from "./repo-config";
const OWNER = "tikoci";
const CACHE_MAX_AGE_MS = 60 * 60 * 1000; // 1 hour
/** Resolve a GitHub token: GITHUB_TOKEN env → gh CLI fallback → undefined */
function resolveGitHubToken(): string | undefined {
if (process.env.GITHUB_TOKEN) return process.env.GITHUB_TOKEN;
try {
const token = execSync("gh auth token", { encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] }).trim();
if (token) {
console.log(" Using token from gh CLI");
return token;
}
} catch {
// gh not installed or not authenticated — continue without token
}
return undefined;
}
const GITHUB_TOKEN = resolveGitHubToken();
export interface RepoData {
name: string;
description: string;
stars: number;
language: string | null;
topics: string[];
homepage: string | null;
html_url: string;
created_at: string;
updated_at: string;
default_branch: string;
readmeHtml: string;
bonusDocs: { name: string; path: string; url: string }[];
category?: string;
/** VS Code Marketplace extension ID (passthrough from config) */
vscodeExtensionId?: string;
/** Docker Hub image names (passthrough from config) */
dockerImages?: string[];
/** External links (passthrough from config) */
externalLinks?: ExternalLink[];
/** Whether the repo has a Dockerfile in the root */
hasDockerfile: boolean;
/** Dockerfile raw content (if present and fetchable) */
dockerfileContent?: string;
/** Viewable file contents fetched at build time (markdown rendered to HTML) */
viewableFileContents: { name: string; html: string }[];
/** SEO meta description override from repo-config (120–160 chars) */
metaDescription?: string;
}
export interface GraphData {
nodes: {
id: string;
name: string;
description: string;
stars: number;
language: string | null;
topics: string[];
url: string;
pageUrl: string;
category?: string;
symbol: string;
}[];
links: {
source: string;
target: string;
type: string;
}[];
}
function githubHeaders(): Record<string, string> {
const headers: Record<string, string> = {
Accept: "application/vnd.github+json",
"User-Agent": "tikoci-website-build",
};
if (GITHUB_TOKEN) {
headers.Authorization = `Bearer ${GITHUB_TOKEN}`;
}
return headers;
}
function readmeHeaders(): Record<string, string> {
const headers: Record<string, string> = {
Accept: "application/vnd.github.html+json",
"User-Agent": "tikoci-website-build",
};
if (GITHUB_TOKEN) {
headers.Authorization = `Bearer ${GITHUB_TOKEN}`;
}
return headers;
}
async function fetchRepoList(): Promise<any[]> {
const url = `https://api.github.com/users/${OWNER}/repos?per_page=100&sort=updated`;
const resp = await fetch(url, { headers: githubHeaders() });
if (!resp.ok) throw new Error(`GitHub API ${resp.status}: ${resp.statusText}`);
return resp.json();
}
async function fetchReadmeHtml(repo: string): Promise<string> {
const url = `https://api.github.com/repos/${OWNER}/${repo}/readme`;
const resp = await fetch(url, { headers: readmeHeaders() });
if (!resp.ok) return "";
return resp.text();
}
async function checkBonusDoc(repo: string, path: string, defaultBranch: string): Promise<{ name: string; path: string; url: string } | null> {
const url = `https://api.github.com/repos/${OWNER}/${repo}/contents/${path}`;
const resp = await fetch(url, { headers: githubHeaders() });
if (!resp.ok) return null;
return {
name: path,
path,
url: `https://github.com/${OWNER}/${repo}/blob/${defaultBranch}/${path}`,
};
}
/** Check if a file exists in a repo's root */
async function fileExists(repo: string, path: string): Promise<boolean> {
const url = `https://api.github.com/repos/${OWNER}/${repo}/contents/${path}`;
const resp = await fetch(url, { headers: githubHeaders(), method: "HEAD" });
return resp.ok;
}
/** Fetch raw text content of a file from a repo */
async function fetchRawFile(repo: string, path: string, defaultBranch: string): Promise<string | null> {
const url = `https://raw.githubusercontent.com/${OWNER}/${repo}/${defaultBranch}/${path}`;
const resp = await fetch(url);
if (!resp.ok) return null;
return resp.text();
}
/** Fetch a markdown file and get GitHub-rendered HTML */
async function fetchFileAsHtml(repo: string, path: string): Promise<string | null> {
const url = `https://api.github.com/repos/${OWNER}/${repo}/contents/${path}`;
const resp = await fetch(url, { headers: readmeHeaders() });
if (!resp.ok) return null;
return resp.text();
}
/**
* Sanitize GitHub-rendered README HTML:
* - Strip <script> tags (defense-in-depth)
* - Rewrite relative image URLs to absolute raw.githubusercontent.com URLs
* - Downshift headings (h1→h2 … h5→h6) so README h1s don't compete with the page h1
* - Add alt="" to any <img> missing an alt attribute
*/
function sanitizeReadmeHtml(html: string, repo: string, defaultBranch: string): string {
// Strip script tags
let clean = html.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "");
// Downshift headings so README h1 doesn't clash with page h1 (SEO/a11y)
clean = clean.replace(/<(\/?)h([1-5])(\s|>)/gi, (_, slash, level, rest) =>
`<${slash}h${Number(level) + 1}${rest}`
);
// Rewrite relative image src to absolute
const rawBase = `https://raw.githubusercontent.com/${OWNER}/${repo}/${defaultBranch}`;
clean = clean.replace(
/(<img\s[^>]*src=")(?!https?:\/\/|data:)([^"]+)(")/gi,
(_, pre, src, post) => {
const cleanSrc = src.replace(/^\.\//, "");
return `${pre}${rawBase}/${cleanSrc}${post}`;
}
);
// Add alt="" to images that are missing an alt attribute
clean = clean.replace(/<img\b(?![^>]*\balt=)([^>]*?)(\s*\/?>)/gi, '<img alt=""$1$2');
// Also rewrite relative href for links to images/docs
clean = clean.replace(
/(<a\s[^>]*href=")(?!https?:\/\/|#|mailto:)([^"]+)(")/gi,
(_, pre, href, post) => {
const cleanHref = href.replace(/^\.\//, "");
return `${pre}https://github.com/${OWNER}/${repo}/blob/${defaultBranch}/${cleanHref}${post}`;
}
);
return clean;
}
interface CacheData {
timestamp: number;
repos: RepoData[];
}
function readCache(cachePath: string): RepoData[] | null {
if (!existsSync(cachePath)) return null;
try {
const data: CacheData = JSON.parse(readFileSync(cachePath, "utf-8"));
if (Date.now() - data.timestamp < CACHE_MAX_AGE_MS) {
return data.repos;
}
} catch { /* cache corrupt, refetch */ }
return null;
}
function writeCache(cachePath: string, repos: RepoData[]) {
const data: CacheData = { timestamp: Date.now(), repos };
writeFileSync(cachePath, JSON.stringify(data), "utf-8");
}
/**
* Re-apply config-driven overrides (externalLinks, dockerImages, etc.) over
* cached repo data so that changes to repo-config.ts take effect without
* requiring a full GitHub API re-fetch.
*/
function applyConfigOverrides(repos: RepoData[]): RepoData[] {
return repos.map(repo => {
const override = REPO_OVERRIDES[repo.name];
if (!override) return repo;
return {
...repo,
category: override.category ?? repo.category,
vscodeExtensionId: override.vscodeExtensionId ?? repo.vscodeExtensionId,
dockerImages: override.dockerImages ?? repo.dockerImages,
externalLinks: override.externalLinks ?? repo.externalLinks,
metaDescription: override.metaDescription ?? repo.metaDescription,
};
});
}
/**
* Main entry: fetch all repo data and produce repos.json + graph data.
* @param distDir - The dist/ output directory
* @returns The full array of RepoData for page generation
*/
export async function fetchGitHubData(distDir: string): Promise<RepoData[]> {
const dataDir = join(distDir, "data");
mkdirSync(dataDir, { recursive: true });
// Cache lives outside dist/ so it survives the dist/ clean step between builds.
// This prevents repeated builds from exhausting the anonymous GitHub API rate limit.
const cachePath = join(import.meta.dirname, ".github-cache.json");
// Try cache first (dev mode optimization)
const forceRefresh = !!process.env.CI || process.argv.includes("--fresh");
if (!forceRefresh) {
const cached = readCache(cachePath);
if (cached) {
console.log(" Using cached GitHub data (< 1 hour old)");
const repos = applyConfigOverrides(cached);
writeReposJson(dataDir, repos);
return repos;
}
}
console.log(" Fetching repos from GitHub API...");
let rawRepos: any[];
try {
rawRepos = await fetchRepoList();
} catch (err) {
console.warn(` GitHub API fetch failed: ${err}`);
const cached = readCache(cachePath);
if (cached) {
console.log(" Falling back to stale cache");
const repos = applyConfigOverrides(cached);
writeReposJson(dataDir, repos);
return repos;
}
console.warn(" No cache available — building with empty repo data");
writeReposJson(dataDir, []);
return [];
}
// Filter repos (include forks — some like netinstall are active projects)
const filtered = rawRepos.filter(r =>
r.stargazers_count >= MIN_STARS &&
!EXCLUDE_REPOS.includes(r.name)
);
console.log(` ${filtered.length} repos qualify (${MIN_STARS}+ stars)`);
// Fetch README + bonus docs + enrichments for each repo
const repos: RepoData[] = [];
for (const r of filtered) {
const override = REPO_OVERRIDES[r.name] || {};
console.log(` Fetching README for ${r.name}...`);
const readmeHtml = await fetchReadmeHtml(r.name);
const cleanHtml = sanitizeReadmeHtml(readmeHtml, r.name, r.default_branch);
// Check bonus docs
const bonusDocs: { name: string; path: string; url: string }[] = [];
if (override.bonusDocs) {
for (const docPath of override.bonusDocs) {
const doc = await checkBonusDoc(r.name, docPath, r.default_branch);
if (doc) bonusDocs.push(doc);
}
}
// Detect Dockerfile in root
let hasDockerfile = false;
let dockerfileContent: string | undefined;
if (await fileExists(r.name, "Dockerfile")) {
hasDockerfile = true;
const raw = await fetchRawFile(r.name, "Dockerfile", r.default_branch);
if (raw) dockerfileContent = raw;
console.log(` Dockerfile detected in ${r.name}`);
}
// Fetch viewable file contents (rendered as HTML for markdown)
const viewableFileContents: { name: string; html: string }[] = [];
if (override.viewableFiles) {
for (const filePath of override.viewableFiles) {
const html = await fetchFileAsHtml(r.name, filePath);
if (html) {
const cleanViewable = sanitizeReadmeHtml(html, r.name, r.default_branch);
viewableFileContents.push({ name: filePath, html: cleanViewable });
console.log(` Fetched viewable: ${filePath}`);
}
}
}
repos.push({
name: r.name,
description: r.description || "",
stars: r.stargazers_count,
language: r.language,
topics: r.topics || [],
homepage: r.homepage || null,
html_url: r.html_url,
created_at: r.created_at,
updated_at: r.updated_at,
default_branch: r.default_branch,
readmeHtml: cleanHtml,
bonusDocs,
category: override.category,
vscodeExtensionId: override.vscodeExtensionId,
dockerImages: override.dockerImages,
externalLinks: override.externalLinks,
hasDockerfile,
dockerfileContent,
viewableFileContents,
metaDescription: override.metaDescription,
});
}
// Cache for dev
writeCache(cachePath, repos);
writeReposJson(dataDir, repos);
return repos;
}
function writeReposJson(dataDir: string, repos: RepoData[]) {
// Write a slimmed version for the map (no readmeHtml to keep size small)
const slim = repos.map(r => ({
name: r.name,
description: r.description,
stars: r.stars,
language: r.language,
topics: r.topics,
html_url: r.html_url,
category: r.category,
}));
writeFileSync(join(dataDir, "repos.json"), JSON.stringify(slim, null, 2), "utf-8");
}
/**
* Build graph data for the D3 force-directed map.
* Filters relationships to only include repos that actually exist in the dataset.
*/
export function buildGraphData(repos: RepoData[]): GraphData {
const repoNames = new Set(repos.map(r => r.name));
const nodes = repos.map(r => ({
id: r.name,
name: r.name,
description: r.description,
stars: r.stars,
language: r.language,
topics: r.topics,
url: r.html_url,
pageUrl: `p/${r.name}.html`,
category: r.category,
symbol: REPO_SYMBOLS[r.name] || DEFAULT_SYMBOL,
}));
const links = RELATIONSHIPS
.filter(rel => repoNames.has(rel.source) && repoNames.has(rel.target))
.map(rel => ({
source: rel.source,
target: rel.target,
type: rel.type,
}));
return { nodes, links };
}