Repository Contribution Count Action:开源贡献者的专属统计神器
作为一名开源贡献者,你是否曾为如何展示自己在各大项目中的贡献而烦恼?手动统计 PR 和 Commits 数量不仅费时费力,而且难以保持实时更新。今天给大家介绍一款 GitHub Action 工具 —— Repository Contribution Count Action,它可以自动统计你在指定仓库中的贡献数量,并生成美观的图标徽章,让你的开源成就一目了然。
什么是 Repository Contribution Count Action?
Repository Contribution Count Action 是一个专门用于统计用户在 GitHub 仓库中 PR 或 Commits 数量并生成图标展示的 GitHub Action。它能够帮助开发者自动化展示自己在开源社区的贡献,适用于个人 Profile 页面、项目文档、技术博客等多种场景。
核心功能特性
📊 多维度贡献统计
- 支持 PR 和 Commits 两种贡献类型的统计
- 自动解析多种 GitHub 链接格式
- 支持批量输入多个仓库链接
🎨 美观的可视化展示
- 基于 shields.io 生成专业级徽章
- 智能颜色分级(根据贡献数量自动选择颜色)
- 支持 5 种徽章样式(flat、flat-square、plastic、for-the-badge、social)
🔄 灵活的排序与输出
- 支持按贡献数量排序(从高到低)
- 提供 Markdown、HTML、JSON 三种输出格式
- 自动生成统计摘要信息
适用人群
这款工具特别适合以下人群:
- 开源贡献者:想要展示自己在各大开源项目中的贡献
- 技术博主:在个人简介中展示技术实力
- 求职者:在简历或个人主页中突出开源经历
- 团队负责人:统计团队成员的开源贡献情况
- 开源项目维护者:展示项目贡献者排行榜
使用方法
1. 基础配置
在你的 GitHub 仓库中创建一个工作流文件 .github/workflows/contribution-stats.yml
:
name: Update Contribution Statson:schedule:- cron: '0 16 * * *' # 每日北京时间晚上12点更新 (UTC+8,所以是UTC 16:00)workflow_dispatch: # 支持手动触发jobs:update-stats:runs-on: ubuntu-lateststeps:- name: Checkout Profile Repouses: actions/checkout@v4with:repository: your-username/your-usernametoken: ${{ secrets.GITHUB_TOKEN }}- name: Generate Contribution Statisticsid: statsuses: lxKylin/repo-contribution-count-action@v1.1.0with:pr-links: |https://github.com/vitejs/docs-cn/commits?author=your-usernamehttps://github.com/vitest-dev/docs-cn/commits?author=your-usernamehttps://github.com/your-org/your-project/pulls?q=is%3Apr+author%3Ayour-usernamegithub-token: ${{ secrets.GITHUB_TOKEN }}badge-style: 'flat'output-format: 'markdown'sort-by-count: 'true'- name: Update Profile READMErun: |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 -->EOFecho "临时文件内容:"cat temp_stats.md# 使用 awk 替换 README.md 中指定部分的内容awk 'BEGIN { in_section = 0; found_start = 0 }/<!-- PR_STATS_START -->/ {if (!found_start) {found_start = 1in_section = 1while ((getline line < "temp_stats.md") > 0) {print line}close("temp_stats.md")next}}/<!-- PR_STATS_END -->/ {if (in_section) {in_section = 0next}}!in_section { print }' README.md > README_new.md# 检查文件是否生成成功if [ -f README_new.md ]; thenecho "README_new.md 生成成功"echo "文件大小对比:"wc -l README.md README_new.mdmv README_new.md README.mdecho "README.md 更新完成"elseecho "ERROR: README_new.md 生成失败"exit 1fi# 清理临时文件rm -f temp_stats.md# 显示更新后的文件内容(前20行)echo "更新后的 README.md 前20行:"head -20 README.md- name: Commit and Pushrun: |echo "检查文件更改..."git status# 检查是否有更改if git diff --quiet README.md; thenecho "README.md 没有变化,跳过提交"elseecho "README.md 有更改,准备提交..."git config --local user.email "action@github.com"git config --local user.name "GitHub Action"git add README.mdgit commit -m "refactor: 更新 PR 统计数据 $(date '+%Y-%m-%d %H:%M:%S')"git pushecho "提交完成!"fi
2. 在 README 中展示
在你的 README.md 文件中添加标记:
<!-- PR_STATS_START --><!-- PR_STATS_END -->
可参照示例来使用
配置参数详解
参数名 | 是否必需 | 默认值 | 说明 |
---|---|---|---|
pr-links | ✅ | - | PR 或 Commits 链接列表,用换行符分隔 |
github-token | ✅ | - | GitHub token,用于访问 GitHub API |
badge-style | ❌ | flat | 图标样式 |
output-format | ❌ | markdown | 输出格式 |
sort-by-count | ❌ | true | 是否按贡献数量排序 |
include-merge-commits | ❌ | true | 是否包含 Merge commits |
输出结果示例
Action 执行后会生成三个输出:
badges
: 格式化的徽章代码summary
: 统计摘要,如"总计在 3 个仓库中创建了 25 个 PRs"repo-counts
: JSON 格式的详细数据
徽章展示效果
Markdown形式
)[)](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)
HTML形式
<imgsrc="https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=https%3A%2F%2Fimg.shields.io%2Fbadge%2FTotal%2520PRs-25%2520in%25203%2520repos-brightgreen%3Fstyle%3Dflat&pos_id=img-SkNRt1mV-1760146670769)"alt="Total PRs"
/>
<a href="https://github.com/microsoft/vscode"><imgsrc="https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=https%3A%2F%2Fimg.shields.io%2Fbadge%2Fmicrosoft%252Fvscode-15%2520PRs-brightgreen%3Fstyle%3Dflat&pos_id=img-WVMDkoY5-1760146671139)"alt="microsoft/vscode PRs"/>
</a>
<a href="https://github.com/facebook/react"><imgsrc="https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=https%3A%2F%2Fimg.shields.io%2Fbadge%2Ffacebook%252Freact-8%2520PRs-green%3Fstyle%3Dflat&pos_id=img-bF3tHo5y-1760146671549)"alt="facebook/react PRs"/>
</a>
<a href="https://github.com/vercel/next.js"><imgsrc="https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=https%3A%2F%2Fimg.shields.io%2Fbadge%2Fvercel%252Fnext.js-2%2520PRs-green%3Fstyle%3Dflat&pos_id=img-o3XmH6WJ-1760146672048)"alt="vercel/next.js PRs"/>
</a>
<a href="https://github.com/vitejs/docs-cn"><imgsrc="https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=https%3A%2F%2Fimg.shields.io%2Fstatic%2Fv1%3Flabel%3Dvitejs%252Fdocs-cn%26message%3D31%2BPRs%26color%3Dorange%26style%3Dflat&pos_id=img-0025lrjG-1760146672472)"alt="vitejs/docs-cn PRs"/>
</a>
<a href="https://github.com/vitest-dev/docs-cn"><imgsrc="https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=https%3A%2F%2Fimg.shields.io%2Fstatic%2Fv1%3Flabel%3Dvitest-dev%252Fdocs-cn%26message%3D54%2BPRs%26color%3Dred%26style%3Dflat&pos_id=img-qqzqMisK-1760146672880)"alt="vitest-dev/docs-cn PRs"/>
</a>
高级用法
自定义样式
支持多种徽章样式,适用于不同场景:
# 扁平方形样式
badge-style: 'flat-square'# 大型徽章样式(适合重要展示)
badge-style: 'for-the-badge'# 社交媒体样式
badge-style: 'social'
多格式输出
根据不同使用场景选择合适的输出格式:
# Markdown 格式(适合 README 文件)
output-format: 'markdown'# HTML 格式(适合网页展示)
output-format: 'html'# JSON 格式(适合程序处理)
output-format: 'json'
最佳实践建议
🔒 安全性
- 使用 GitHub 提供的
GITHUB_TOKEN
而非个人访问令牌 - 对于私有仓库,确保 token 具有足够权限
⚡ 性能优化
- 合理设置更新频率,避免过于频繁的 API 调用
- 对于大量仓库的统计,考虑分批处理
🎨 展示效果
- 根据展示环境选择合适的徽章样式
- 利用排序功能突出重要贡献
技术实现亮点
智能链接解析
Action 采用了强大的正则表达式引擎,能够解析多种 GitHub 链接格式:
// 支持的链接类型
// 1. Commits 链接
https://github.com/vitejs/docs-cn/commits?author=lxKylin// 2. PR 搜索链接
https://github.com/vitejs/docs-cn/pulls?q=is%3Apr+author%3AlxKylin
高效的 API 调用
为了避免 GitHub API 限制,Action 实现了分页查询和智能延迟机制。
灵活的徽章生成
基于 shields.io 服务的徽章生成器支持动态颜色映射和多样式支持。
结语
Repository Contribution Count Action 不仅是一个简单的统计工具,更是开源贡献者展示成就、团队管理者追踪进度、项目维护者了解社区的得力助手。通过自动化的方式,它让开源贡献的价值得到更好的体现和认可。如果对你有帮助,欢迎点个🌟!
这是一个开源项目,欢迎社区贡献:
- 功能建议和改进意见
- 代码优化和bug修复
- 文档完善和示例补充
- 新功能开发
项目地址:Repository Contribution Count Action
立即开始使用:查看 详细文档 和 使用示例