Elffuss BraidHub

kikocis/utopia public

Read the stargazers over GraphQL, which this token can actually reach (#185)
Changeset5458acc09ac1e9feeebfa98d2f2600050a933a7cc01355dcc5f7ec62261c04f3
AuthorWaylandYang <145302500+WaylandYang@users.noreply.github.com> · 2026-09-02 19:27 +08:00
CommitterGitHub <noreply@github.com> · 2026-09-02 19:27 +08:00
Parents8cafdd364966

Browse the files at this changeset

1 file changed

modified scripts/star-history.mjs +41 −24
@@ -8,10 +8,9 @@
88 * 自己画还顺手去掉了一件事:那是个跑在 `contents: write` 之下的第三方
99 * action。现在这个权限底下跑的是这份脚本。
1010 *
11- * 数据只有一个接口:`GET /repos/{owner}/{repo}/stargazers`,配上
12- * `Accept: application/vnd.github.star+json` 才会回 `starred_at`。
13- * **GitHub 在 2026-06-30 把这份数据限制成只有仓库的管理员/协作者可读**,
14- * 所以必须带一个够权限的 token——匿名调用现在拿不到。
11+ * **GitHub 在 2026-06-30 把星标时间线限制成只有仓库的管理员/协作者可读**,
12+ * 所以必须带一个够权限的 token——匿名调用现在拿不到,star-history.com
13+ * 那类站点从此只回一张占位图。
1514 */
1615 import { writeFileSync, mkdirSync } from "node:fs";
1716 import { dirname } from "node:path";
@@ -22,32 +21,50 @@
2221 if (!owner || !repo) throw new Error("REPO must be set as owner/name");
2322 if (!token) throw new Error("GITHUB_TOKEN must be set");
2423
25-/** 每页 100,一直翻到空页。1867 颗星 = 19 页,不值得为它上并发 */
24+/** 每页 100,游标翻到底。1868 颗星 = 19 页,不值得为它上并发。
25+ *
26+ * **走 GraphQL,不走 REST。** REST 的 `/repos/{o}/{r}/stargazers` 用同一个
27+ * token 会回 404:那个接口要求 token 带仓库级 scope,而这个只有 `read:org`;
28+ * GitHub 对无权访问的资源回 404 而不是 403,免得泄露它存不存在。
29+ * GraphQL 的 `stargazers` 连接认这个 token——CI 上验过。
30+ * 改这里,好过为了一个接口去放宽凭据。 */
2631 async function stargazerDates() {
32+ const query = `query($owner:String!,$name:String!,$cursor:String){
33+ repository(owner:$owner,name:$name){
34+ stargazers(first:100,after:$cursor,orderBy:{field:STARRED_AT,direction:ASC}){
35+ pageInfo{hasNextPage endCursor}
36+ edges{starredAt}
37+ }
38+ }
39+ }`;
2740 const dates = [];
28- for (let page = 1; ; page++) {
29- const res = await fetch(
30- `https://api.github.com/repos/${owner}/${repo}/stargazers?per_page=100&page=${page}`,
31- {
32- headers: {
33- // 没有这个 Accept,回来的是用户列表,没有时间戳
34- accept: "application/vnd.github.star+json",
35- authorization: `Bearer ${token}`,
36- "user-agent": `${owner}-star-history`,
37- },
41+ let cursor = null;
42+ for (let page = 1; page <= 400; page++) {
43+ const res = await fetch("https://api.github.com/graphql", {
44+ method: "POST",
45+ headers: {
46+ authorization: `Bearer ${token}`,
47+ "content-type": "application/json",
48+ "user-agent": `${owner}-star-history`,
3849 },
39- );
50+ body: JSON.stringify({ query, variables: { owner, name: repo, cursor } }),
51+ });
4052 if (!res.ok) {
41- throw new Error(
42- `stargazers page ${page}: ${res.status} ${await res.text()}`,
43- );
53+ throw new Error(`graphql page ${page}: ${res.status} ${await res.text()}`);
54+ }
55+ const body = await res.json();
56+ // **GraphQL 出错也回 200**,所以必须自己看 `errors`——否则要等到下面
57+ // 读出 undefined 才发现,而那时错误原文已经丢了
58+ if (body.errors) {
59+ throw new Error(`graphql page ${page}: ${JSON.stringify(body.errors)}`);
4460 }
45- const rows = await res.json();
46- if (rows.length === 0) return dates;
47- for (const r of rows) if (r.starred_at) dates.push(new Date(r.starred_at));
48- // GitHub 对这个接口封顶 400 页;到不了也别转圈
49- if (page >= 400) return dates;
61+ const conn = body.data?.repository?.stargazers;
62+ if (!conn) throw new Error(`graphql page ${page}: no stargazers in response`);
63+ for (const e of conn.edges) if (e.starredAt) dates.push(new Date(e.starredAt));
64+ if (!conn.pageInfo.hasNextPage) return dates;
65+ cursor = conn.pageInfo.endCursor;
5066 }
67+ return dates;
5168 }
5269
5370 const dates = (await stargazerDates()).sort((a, b) => a - b);