Skip to content

Commit 297e0dd

Browse files
chore(deps): replace simple-github with native fetch to drop request (#42)
1 parent fa491f2 commit 297e0dd

3 files changed

Lines changed: 41 additions & 453 deletions

File tree

generator.js

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,41 @@
11
const { promises: fs } = require("fs");
22
const path = require("path");
3-
const gh = require("simple-github")({
4-
owner: "wicg",
5-
//debug: "true",
6-
token: `${process.env.WICG_TOKEN}`,
7-
});
3+
4+
const GITHUB_API = "https://api.github.com";
5+
const OWNER = "wicg";
6+
const token = process.env.WICG_TOKEN;
7+
8+
// Minimal GitHub REST client: templates :owner, adds auth, and follows
9+
// Link-header pagination so list endpoints return every page.
10+
async function gh(route) {
11+
let url = GITHUB_API + route.replace(/:owner/g, OWNER);
12+
const headers = {
13+
"User-Agent": "wicg.io",
14+
Accept: "application/vnd.github+json",
15+
};
16+
if (token) headers.Authorization = `token ${token.replace(/^token\s+/, "")}`;
17+
let output;
18+
while (url) {
19+
const response = await fetch(url, { headers });
20+
if (!response.ok) {
21+
throw new Error(`GitHub ${response.status} ${response.statusText} for ${url}`);
22+
}
23+
const body = await response.json();
24+
const link = response.headers.get("link");
25+
if (!link) return output ? output.concat(body) : body;
26+
output = (output || []).concat(body);
27+
url = nextLink(link);
28+
}
29+
return output;
30+
}
31+
32+
function nextLink(linkHeader) {
33+
for (const part of linkHeader.split(",")) {
34+
const match = part.match(/<([^>]+)>;\s*rel="next"/);
35+
if (match) return match[1];
36+
}
37+
return null;
38+
}
839

940
const ms_to_days_ratio = 1000 * 60 * 60 * 24;
1041

@@ -50,7 +81,7 @@ const ignore_set = new Set([
5081
(async () => {
5182
let active = [];
5283
let archived = [];
53-
const repos = await gh.request("GET /orgs/:owner/repos", {});
84+
const repos = await gh("/orgs/:owner/repos");
5485
for (repo of repos.filter((filter) => !ignore_set.has(filter.name))) {
5586
const repo_object = {
5687
name: repo?.name ?? null,
@@ -68,13 +99,11 @@ const ignore_set = new Set([
6899
archived.push(repo_object);
69100
continue;
70101
}
71-
const commit_activity = await gh.request(
72-
"GET /repos/:owner/" + repo.name + "/stats/commit_activity",
73-
{}
102+
const commit_activity = await gh(
103+
"/repos/:owner/" + repo.name + "/stats/commit_activity"
74104
);
75-
const issues = await gh.request(
76-
"GET /repos/:owner/" + repo.name + "/issues?state=all",
77-
{}
105+
const issues = await gh(
106+
"/repos/:owner/" + repo.name + "/issues?state=all"
78107
);
79108
repo_object["score"] = combined_score(
80109
commit_score(commit_activity),

0 commit comments

Comments
 (0)