Skip to content

fix(leaderboard): 脚本调 GitHub API 带 Authorization(修 name 全是占位符)#280

Merged
longsizhuo merged 2 commits intomainfrom
fix/leaderboard-github-api-auth
Apr 14, 2026
Merged

fix(leaderboard): 脚本调 GitHub API 带 Authorization(修 name 全是占位符)#280
longsizhuo merged 2 commits intomainfrom
fix/leaderboard-github-api-auth

Conversation

@longsizhuo
Copy link
Copy Markdown
Member

现象

`/rank` 页 Contributors tab 所有用户都显示 "GitHub User ",而不是真实的 GitHub login。

根因

`scripts/generate-leaderboard.mjs` 调 `https://api.github.com/user/:id\`没带 Authorization header

  • 匿名 GitHub API 限流 60/hour,批量查 100 人大部分 403
  • `catch (err) {}` 把所有错误吞掉,失败后 fallback 到 `name: "GitHub User "` 占位符

workflow 已经在 env 里注入了 `GITHUB_TOKEN: ${{ secrets.GH_PAT }}`,脚本没读用。

修复

  • 脚本读取 `process.env.GITHUB_TOKEN` / `GH_PAT`,带 `Authorization: Bearer` header
  • 失败时首次打印响应片段 + 累计 success/failure 计数,后续排查可追溯
  • 本地用 21 位贡献者跑通:21/0 全成功,name 回到真实 login(longsizhuo / Mira190 / ...)
  • 附带:把最新的 `generated/site-leaderboard.json` 重新生成提交(之前是脏数据)

后续

如果线上运行时 `secrets.GH_PAT` 过期/撤销了,这次失败日志会直接显示 401 响应,不再静默。

Test plan

  • 本地 `tsx scripts/generate-leaderboard.mjs` 跑通
  • `/rank` Contributors tab 刷新看真实 name
  • Reviewer: 确认 `secrets.GH_PAT` 当前未过期

现象:排行榜上所有用户 name 字段都是 "GitHub User <id>" 占位符。
根因:脚本匿名调用 GitHub /user/:id API,限流 60/hour,前排 21 人基本全部 403,
静默 fallback 到占位符;catch 块之前把所有错误吞掉,看不到问题。

修复:
- 读取 GITHUB_TOKEN / GH_PAT(workflow 已在 env 注入),带 Bearer 头走认证
- 失败时打印首个响应片段,累计 success / failure 计数便于排查
- 本地用 21 位贡献者验证:21/0 通过,name 字段回到真实 login (longsizhuo / Mira190 / ...)
- 顺手重新生成 generated/site-leaderboard.json
Copilot AI review requested due to automatic review settings April 14, 2026 18:53
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 14, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
involutionhell-github-io Ready Ready Preview, Comment Apr 14, 2026 7:10pm
website-preview Ready Ready Preview, Comment Apr 14, 2026 7:10pm

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes the /rank Contributors tab showing placeholder names by authenticating the leaderboard generation script’s GitHub API calls, and refreshes the generated leaderboard snapshot accordingly.

Changes:

  • Read process.env.GITHUB_TOKEN / process.env.GH_PAT and send Authorization: Bearer ... when calling https://api.github.com/user/:id.
  • Add basic success/failure counters and print a sample failure response once for easier debugging.
  • Regenerate generated/site-leaderboard.json so names/URLs reflect the latest successful enrichment output.

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated 2 comments.

File Description
scripts/generate-leaderboard.mjs Adds token-based GitHub API auth plus minimal logging/metrics for enrichment.
generated/site-leaderboard.json Updates snapshot data (real logins instead of placeholders; refreshed URLs/ordering/data).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

const ghToken = process.env.GITHUB_TOKEN || process.env.GH_PAT || "";
if (!ghToken) {
console.warn(
"[generate-leaderboard] 未检测到 GITHUB_TOKEN,名字获取会因限流失败,榜单只会展示 id 占位符",
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ghToken 同时支持从 GITHUB_TOKENGH_PAT 读取,但这里的告警文案只提到了 GITHUB_TOKEN,可能会误导排查(例如本地只设置了 GH_PAT 时)。建议把提示改成“未检测到 GITHUB_TOKEN/GH_PAT(或 GitHub token)…”。

Suggested change
"[generate-leaderboard] 未检测到 GITHUB_TOKEN,名字获取会因限流失败,榜单只会展示 id 占位符",
"[generate-leaderboard] 未检测到 GITHUB_TOKEN/GH_PAT(GitHub token),名字获取会因限流失败,榜单只会展示 id 占位符",

Copilot uses AI. Check for mistakes.
}
} catch (err) {
// Ignore fetch errors
failureCount++;
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catch (err) 分支现在只累计失败次数但不输出任何信息;如果首次失败来自网络/DNS/SSL 等异常(而非非 2xx 响应),日志里会缺少可定位线索。建议至少在首次捕获异常时输出 err.message(或 err)并说明后续将静默计数,保持和非 2xx 分支一致的可追溯性。

Suggested change
failureCount++;
failureCount++;
if (failureCount === 1) {
console.warn(
"[generate-leaderboard] GitHub API 请求异常,后续失败将静默计数。示例错误:",
err instanceof Error ? err.message : err,
);
}

Copilot uses AI. Check for mistakes.
Copilot CR #280:
- 告警文案原来只提 GITHUB_TOKEN,可能误导本地只设 GH_PAT 的排查
- catch 分支之前只累加 failureCount,网络/DNS/SSL 异常无任何日志线索;补一次首次异常输出,与非 2xx 分支保持一致
@longsizhuo longsizhuo merged commit f7a47fa into main Apr 14, 2026
7 checks passed
@longsizhuo longsizhuo deleted the fix/leaderboard-github-api-auth branch April 14, 2026 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants