Elffuss BraidHub

kikocis/utopia public

An empty connection and no stars look the same, so say which it was (#186)
Changesetfe16bbb4f71aed6f3832d1008c1f86334b5ea1dd50cbea5ef4ba1d0a19100b03
AuthorWaylandYang <145302500+WaylandYang@users.noreply.github.com> · 2026-09-02 19:38 +08:00
CommitterGitHub <noreply@github.com> · 2026-09-02 19:38 +08:00
Parents5458acc09ac1

Browse the files at this changeset

1 file changed

modified scripts/star-history.mjs +24 −2
@@ -29,9 +29,15 @@
2929 * GraphQL 的 `stargazers` 连接认这个 token——CI 上验过。
3030 * 改这里,好过为了一个接口去放宽凭据。 */
3131 async function stargazerDates() {
32+ /* `viewerPermission` 与 `totalCount` 不是装饰。**受限数据 GitHub 回的是
33+ 空集合,不是报错**——只看 `edges` 的话,「没权限」和「真的零颗星」
34+ 长得一模一样。把这两个一起取回来,失败时说得出是哪一种 */
3235 const query = `query($owner:String!,$name:String!,$cursor:String){
3336 repository(owner:$owner,name:$name){
37+ stargazerCount
38+ viewerPermission
3439 stargazers(first:100,after:$cursor,orderBy:{field:STARRED_AT,direction:ASC}){
40+ totalCount
3541 pageInfo{hasNextPage endCursor}
3642 edges{starredAt}
3743 }
@@ -58,8 +64,15 @@
5864 if (body.errors) {
5965 throw new Error(`graphql page ${page}: ${JSON.stringify(body.errors)}`);
6066 }
61- const conn = body.data?.repository?.stargazers;
67+ const node = body.data?.repository;
68+ const conn = node?.stargazers;
6269 if (!conn) throw new Error(`graphql page ${page}: no stargazers in response`);
70+ if (page === 1) {
71+ console.log(
72+ `repo sees ${node.stargazerCount} stars; connection reports ` +
73+ `${conn.totalCount}; token permission = ${node.viewerPermission}`,
74+ );
75+ }
6376 for (const e of conn.edges) if (e.starredAt) dates.push(new Date(e.starredAt));
6477 if (!conn.pageInfo.hasNextPage) return dates;
6578 cursor = conn.pageInfo.endCursor;
@@ -68,7 +81,16 @@
6881 }
6982
7083 const dates = (await stargazerDates()).sort((a, b) => a - b);
71-if (dates.length === 0) throw new Error("no stargazer timestamps came back");
84+if (dates.length === 0) {
85+ // 上面那行日志已经说了 repo 报多少颗星、连接报多少、token 是什么权限。
86+ // **不要在这里静默出一张空图**——一张画着零的曲线比没有图更糟
87+ throw new Error(
88+ "no stargazer timestamps came back — see the line above for what the API " +
89+ "reported. An empty connection with a non-zero star count means the token " +
90+ "cannot read the stargazer timeline (restricted to admins and collaborators " +
91+ "since 2026-06-30).",
92+ );
93+}
7294
7395 /* 按天聚合成累计值。**每一天都要有点**,哪怕当天没有新增——
7496 缺口跳过去的话,横轴的间距就不再代表时间,曲线的斜率也就骗人了 */