Changesets · Branches · Tags · Compare
| Changeset | 5458acc09ac1e9feeebfa98d2f2600050a933a7cc01355dcc5f7ec62261c04f3 |
|---|---|
| Author | WaylandYang <145302500+WaylandYang@users.noreply.github.com> · 2026-09-02 19:27 +08:00 |
| Committer | GitHub <noreply@github.com> · 2026-09-02 19:27 +08:00 |
| Parents | 8cafdd364966 |
1 file changed
modified scripts/star-history.mjs +41 −24
| @@ -8,10 +8,9 @@ | ||
| 8 | 8 | * 自己画还顺手去掉了一件事:那是个跑在 `contents: write` 之下的第三方 |
| 9 | 9 | * action。现在这个权限底下跑的是这份脚本。 |
| 10 | 10 | * |
| 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 | + * 那类站点从此只回一张占位图。 | |
| 15 | 14 | */ |
| 16 | 15 | import { writeFileSync, mkdirSync } from "node:fs"; |
| 17 | 16 | import { dirname } from "node:path"; |
| @@ -22,32 +21,50 @@ | ||
| 22 | 21 | if (!owner || !repo) throw new Error("REPO must be set as owner/name"); |
| 23 | 22 | if (!token) throw new Error("GITHUB_TOKEN must be set"); |
| 24 | 23 | |
| 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 | + * 改这里,好过为了一个接口去放宽凭据。 */ | |
| 26 | 31 | 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 | + }`; | |
| 27 | 40 | 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`, | |
| 38 | 49 | }, |
| 39 | - ); | |
| 50 | + body: JSON.stringify({ query, variables: { owner, name: repo, cursor } }), | |
| 51 | + }); | |
| 40 | 52 | 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)}`); | |
| 44 | 60 | } |
| 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; | |
| 50 | 66 | } |
| 67 | + return dates; | |
| 51 | 68 | } |
| 52 | 69 | |
| 53 | 70 | const dates = (await stargazerDates()).sort((a, b) => a - b); |