一个用于统计用户在指定仓库中 PR 或 Commits 数量并生成图标的 GitHub Action。
- 📊 统计用户在多个仓库中的 PR 或 Commits 数量
- 🎨 生成美观的图标展示统计结果
- 🔄 支持按贡献数量排序(高到低)
- 🔗 支持批量输入 PR 链接或 Commits 链接
- 📝 支持多种输出格式(Markdown、HTML、JSON)
- 🎯 自动解析仓库信息和用户名
- 🌈 根据贡献数量自动选择图标颜色
| 参数名 | 必需 | 默认值 | 说明 |
|---|---|---|---|
pr-links |
✅ | - | PR 链接列表,用换行符分隔 |
github-token |
✅ | - | GitHub token,用于访问 GitHub API |
badge-style |
❌ | flat |
图标样式(flat, flat-square, plastic, for-the-badge, social) |
output-format |
❌ | markdown |
输出格式(markdown, html, json) |
sort-by-count |
❌ | true |
是否按贡献数量排序(true/false) |
include-merge-commits |
❌ | true |
是否包含Merge commits(true/false) |
| 输出名 | 说明 |
|---|---|
badges |
生成的图标标记 |
summary |
PR 统计摘要 |
repo-counts |
各仓库 PR 数量的 JSON 格式 |
name: Update Profile PR Stats
on:
schedule:
- cron: '0 16 * * *' # 每日北京时间晚上12点更新 (UTC+8,所以是UTC 16:00)
workflow_dispatch: # 允许手动触发
jobs:
update-profile:
runs-on: ubuntu-latest
steps:
- name: Checkout Profile Repo
uses: actions/checkout@v4
with:
repository: your-username/your-username
token: ${{ secrets.GITHUB_TOKEN }}
- name: Generate PR Statistics
id: stats
uses: lxKylin/repo-contribution-count-action@main
with:
pr-links: |
https://github.com/vitejs/docs-cn/commits?author=lxKylin
https://github.com/vitest-dev/docs-cn/commits?author=lxKylin
https://github.com/vitejs/docs-cn/pulls?q=is%3Apr+author%3AlxKylin
https://github.com/element-plus/element-plus/commits?author=lxKylin
github-token: ${{ secrets.GITHUB_TOKEN }}
badge-style: 'flat'
output-format: 'html'
sort-by-count: 'true' # 按贡献数量排序
include-merge-commits: 'false' # 排除Merge commits,更接近GitHub页面显示
- name: Update Profile README
run: |
echo "开始更新 README.md..."
# 检查当前目录内容
echo "当前目录内容:"
ls -la
# 检查 README.md 是否存在标记
echo "检查 README.md 中的标记..."
grep -n "PR_STATS" README.md || echo "未找到标记"
# 创建临时文件包含新的内容
cat > temp_stats.md << 'EOF'
<!-- PR_STATS_START -->
### 我的开源贡献(由 [repo-contribution-count-action](https://github.com/lxKylin/repo-contribution-count-action) 生成)
${{ steps.stats.outputs.badges }}
> ${{ steps.stats.outputs.summary }}
<!-- PR_STATS_END -->
EOF
echo "临时文件内容:"
cat temp_stats.md
# 使用 awk 替换 README.md 中指定部分的内容
awk '
BEGIN { in_section = 0; found_start = 0 }
/<!-- PR_STATS_START -->/ {
if (!found_start) {
found_start = 1
in_section = 1
while ((getline line < "temp_stats.md") > 0) {
print line
}
close("temp_stats.md")
next
}
}
/<!-- PR_STATS_END -->/ {
if (in_section) {
in_section = 0
next
}
}
!in_section { print }
' README.md > README_new.md
# 检查文件是否生成成功
if [ -f README_new.md ]; then
echo "README_new.md 生成成功"
echo "文件大小对比:"
wc -l README.md README_new.md
mv README_new.md README.md
echo "README.md 更新完成"
else
echo "ERROR: README_new.md 生成失败"
exit 1
fi
# 清理临时文件
rm -f temp_stats.md
# 显示更新后的文件内容(前20行)
echo "更新后的 README.md 前20行:"
head -20 README.md
- name: Commit and Push
run: |
echo "检查文件更改..."
git status
# 检查是否有更改
if git diff --quiet README.md; then
echo "README.md 没有变化,跳过提交"
else
echo "README.md 有更改,准备提交..."
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add README.md
git commit -m "docs: 更新 PR 统计数据 $(date '+%Y-%m-%d %H:%M:%S')"
git push
echo "提交完成!"
fi- 需要在你的
profile-repo的README.md文件中添加以下标记:<!-- PR_STATS_START -->和<!-- PR_STATS_END -->,添加后,才能将 PR 统计数据插入到标记之间。 - 如果不理解,可以参考我的 示例
Action 支持多种 PR 链接格式:
-
具体的 PR 链接(推荐)
https://github.com/vitejs/docs-cn/commits?author=lxKylin -
搜索链接(这个不一定准确,会包含一些
closed的PR,如果说你希望也统计closed的PR,那么使用这种链接是对的,如果要求准确,建议使用第一种链接)https://github.com/vitejs/docs-cn/pulls?q=is%3Apr+author%3AlxKylin
默认情况下,生成的图标会按贡献数量从高到低排序,让最有价值的贡献置顶显示。
假设有以下贡献数据:
- microsoft/vscode: 15 PRs
- facebook/react: 8 PRs
- nodejs/node: 5 PRs
- kubernetes/kubernetes: 12 PRs
开启排序(sort-by-count: true):
- microsoft/vscode (15 PRs)
- kubernetes/kubernetes (12 PRs)
- facebook/react (8 PRs)
- nodejs/node (5 PRs)
关闭排序(sort-by-count: false):
- microsoft/vscode (15 PRs)
- facebook/react (8 PRs)
- nodejs/node (5 PRs)
- kubernetes/kubernetes (12 PRs)
- 开启排序:适合个人资料页、项目展示,突出重要贡献
- 关闭排序:适合需要保持特定顺序的场景
Action 支持灵活处理 Merge commits,这对于匹配 GitHub 页面显示非常有用。
在实际使用中,你可能会发现 Action 统计的数量与 GitHub 页面显示不一致:
- Action 统计:61 commits
- GitHub 页面:40 commits
这种差异主要是因为:
- API 覆盖范围:GitHub API 返回所有分支的所有 commits
- 页面过滤:GitHub 页面可能过滤了 Merge commits 或有显示限制
- Merge commits:自动包含了大量的 Merge commits
使用 include-merge-commits 参数来控制是否包含 Merge commits:
# 包含 Merge commits(默认)
include-merge-commits: 'true' # 统计所有 commits
# 排除 Merge commits(更接近 GitHub 页面)
include-merge-commits: 'false' # 只统计普通 commits以 vitest-dev/docs-cn 仓库为例:
| 配置 | 结果 | 说明 |
|---|---|---|
include-merge-commits: true |
61 commits | 包含 22 个 Merge commits + 39 个普通 commits |
include-merge-commits: false |
39 commits | 只包含普通 commits,更接近 GitHub 页面显示 |
建议:如果希望统计结果与 GitHub 页面一致,请设置 include-merge-commits: false。
支持 shields.io 的所有样式:
flat- 扁平样式(默认)flat-square- 扁平方形plastic- 塑料质感for-the-badge- 大型徽章social- 社交样式

[](https://github.com/microsoft/vscode)
[](https://github.com/facebook/react)
[](https://github.com/vercel/next.js)
[](https://github.com/vitejs/docs-cn)
[](https://github.com/vitest-dev/docs-cn)<img
src="https://img.shields.io/badge/Total%20PRs-25%20in%203%20repos-brightgreen?style=flat"
alt="Total PRs"
/>
<a href="https://github.com/microsoft/vscode">
<img
src="https://img.shields.io/badge/microsoft%2Fvscode-15%20PRs-brightgreen?style=flat"
alt="microsoft/vscode PRs"
/>
</a>
<a href="https://github.com/facebook/react">
<img
src="https://img.shields.io/badge/facebook%2Freact-8%20PRs-green?style=flat"
alt="facebook/react PRs"
/>
</a>
<a href="https://github.com/vercel/next.js">
<img
src="https://img.shields.io/badge/vercel%2Fnext.js-2%20PRs-green?style=flat"
alt="vercel/next.js PRs"
/>
</a>
<a href="https://github.com/vitejs/docs-cn">
<img
src="https://img.shields.io/static/v1?label=vitejs%2Fdocs-cn&message=31+PRs&color=orange&style=flat"
alt="vitejs/docs-cn PRs"
/>
</a>
<a href="https://github.com/vitest-dev/docs-cn">
<img
src="https://img.shields.io/static/v1?label=vitest-dev%2Fdocs-cn&message=54+PRs&color=red&style=flat"
alt="vitest-dev/docs-cn PRs"
/>
</a>[
{
"repository": "SUMMARY",
"count": 25,
"badgeUrl": "https://img.shields.io/badge/Total%20PRs-25%20in%203%20repos-brightgreen?style=flat",
"repoUrl": null
},
{
"repository": "microsoft/vscode",
"count": 15,
"badgeUrl": "https://img.shields.io/badge/microsoft%2Fvscode-15%20PRs-brightgreen?style=flat",
"repoUrl": "https://github.com/microsoft/vscode"
}
]我们提供了完整的本地测试工具,让您可以在本地验证 Action 的功能:
pnpm test:local# 立即查看 Action 效果(使用模拟数据)
npm run test:demo这会生成各种格式的演示文件到 test-output/ 目录。
-
获取 GitHub Token
- 访问 GitHub Settings > Personal access tokens
- 创建 token 并选择
public_repo权限
-
运行测试
# 基础测试 GITHUB_TOKEN=your_token npm run test:local basic # 自定义 PR 链接测试 GITHUB_TOKEN=your_token \ TEST_PR_LINKS="https://github.com/vitejs/docs-cn/commits?author=lxKylin" \ npm run test:local custom
-
查看结果 测试结果会保存在
test-output/目录中,包括:- 生成的图标文件
- 详细的 JSON 数据
- 完整的测试报告
| 测试名称 | 描述 |
|---|---|
basic |
基础功能测试 |
multiFormat |
多格式输出测试 |
styleTest |
图标样式测试 |
custom |
自定义配置测试 |
详细的本地测试指南请查看 test-local/README.md。
-
克隆仓库
git clone https://github.com/lxKylin/repo-contribution-count-action.git cd repo-contribution-count-action -
安装依赖
pnpm install
-
运行测试
pnpm test test:local -
构建项目
pnpm run build
.
├── action.yml # Action 配置文件
├── package.json # Node.js 项目配置
├── src/ # 源代码目录
│ ├── index.js # 主入口文件
│ ├── pr-counter.js # PR 统计逻辑
│ └── badge-generator.js # 图标生成逻辑
├── dist/ # 构建输出目录
└── README.md # 项目文档
- 该 Action 使用 GitHub API 获取 PR 信息
- 默认的
GITHUB_TOKEN有一定的 API 调用限制 - 对于大量仓库的统计,建议添加适当的延迟
- 建议在 Action 中缓存结果,避免频繁调用
A: 请检查:
- GitHub token 是否有足够的权限
- PR 链接格式是否正确
- 仓库是否为私有仓库(需要相应权限)
A: 支持,但需要使用有相应权限的 GitHub token。
A: 目前图标颜色根据 PR 数量自动确定,后续版本会支持自定义颜色。
本项目使用 MIT 许可证。详见 LICENSE 文件。
- 感谢 shields.io 提供图标服务
- 感谢 GitHub Actions 平台