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. 开源贡献者:想要展示自己在各大开源项目中的贡献
  2. 技术博主:在个人简介中展示技术实力
  3. 求职者:在简历或个人主页中突出开源经历
  4. 团队负责人:统计团队成员的开源贡献情况
  5. 开源项目维护者:展示项目贡献者排行榜

使用方法

1. 基础配置

在你的 GitHub 仓库中创建一个工作流文件 .github/workflows/contribution-stats.yml

yaml 复制代码
name: Update Contribution Stats

on:
  schedule:
    - cron: '0 16 * * *' # 每日北京时间晚上12点更新 (UTC+8,所以是UTC 16:00)
  workflow_dispatch: # 支持手动触发

jobs:
  update-stats:
    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 Contribution Statistics
        id: stats
        uses: lxKylin/repo-contribution-count-action@v1.1.0
        with:
          pr-links: |
            https://github.com/vitejs/docs-cn/commits?author=your-username
            https://github.com/vitest-dev/docs-cn/commits?author=your-username
            https://github.com/your-org/your-project/pulls?q=is%3Apr+author%3Ayour-username
          github-token: ${{ secrets.GITHUB_TOKEN }}
          badge-style: 'flat'
          output-format: 'markdown'
          sort-by-count: 'true'
      - 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 "refactor: 更新 PR 统计数据 $(date '+%Y-%m-%d %H:%M:%S')"
            git push
            echo "提交完成!"
          fi

2. 在 README 中展示

在你的 README.md 文件中添加标记:

markdown 复制代码
<!-- 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 执行后会生成三个输出:

  1. badges: 格式化的徽章代码
  2. summary: 统计摘要,如"总计在 3 个仓库中创建了 25 个 PRs"
  3. repo-counts: JSON 格式的详细数据

徽章展示效果

Markdown形式

perl 复制代码
![Total PRs](https://img.shields.io/badge/Total%20PRs-25%20in%203%20repos-brightgreen?style=flat)

[![microsoft/vscode PRs](https://img.shields.io/badge/microsoft%2Fvscode-15%20PRs-brightgreen?style=flat)](https://github.com/microsoft/vscode)

[![facebook/react PRs](https://img.shields.io/badge/facebook%2Freact-8%20PRs-green?style=flat)](https://github.com/facebook/react)

[![vercel/next.js PRs](https://img.shields.io/badge/vercel%2Fnext.js-2%20PRs-green?style=flat)](https://github.com/vercel/next.js)

[![vitejs/docs-cn PRs](https://img.shields.io/static/v1?label=vitejs%2Fdocs-cn&message=31+PRs&color=orange&style=flat)](https://github.com/vitejs/docs-cn)

[![vitest-dev/docs-cn PRs](https://img.shields.io/static/v1?label=vitest-dev%2Fdocs-cn&message=54+PRs&color=red&style=flat)](https://github.com/vitest-dev/docs-cn)

markdown预览图:(被掘金添加了水印)

HTML形式

HTML 复制代码
<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>

html预览图:(被掘金添加了水印)

高级用法

自定义样式

支持多种徽章样式,适用于不同场景:

yaml 复制代码
# 扁平方形样式
badge-style: 'flat-square'

# 大型徽章样式(适合重要展示)
badge-style: 'for-the-badge'

# 社交媒体样式
badge-style: 'social'

多格式输出

根据不同使用场景选择合适的输出格式:

yaml 复制代码
# Markdown 格式(适合 README 文件)
output-format: 'markdown'

# HTML 格式(适合网页展示)
output-format: 'html'

# JSON 格式(适合程序处理)
output-format: 'json'

最佳实践建议

🔒 安全性

  • 使用 GitHub 提供的 GITHUB_TOKEN 而非个人访问令牌
  • 对于私有仓库,确保 token 具有足够权限

⚡ 性能优化

  • 合理设置更新频率,避免过于频繁的 API 调用
  • 对于大量仓库的统计,考虑分批处理

🎨 展示效果

  • 根据展示环境选择合适的徽章样式
  • 利用排序功能突出重要贡献

技术实现亮点

智能链接解析

Action 采用了强大的正则表达式引擎,能够解析多种 GitHub 链接格式:

javascript 复制代码
// 支持的链接类型
// 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

立即开始使用 :查看 详细文档使用示例

相关推荐
FromCyun7 小时前
优化GitHub访问问题
github·host
24K纯帅ll8 小时前
神经网络与人脑的联结JS如何模拟意识流动
开源
法欧特斯卡雷特8 小时前
从 Kotlin 编译器 API 的变化开始: 2.2.2X -> 2.3.0-Beta1
后端·架构·开源
居7然8 小时前
京东开源王炸!JoyAgent-JDGenie如何重新定义智能体开发?
人工智能·开源·大模型·mcp
听风吹雨yu9 小时前
RK3588从数据集到训练到部署YoloV8
linux·yolo·开源·rk3588·rknn
uhakadotcom9 小时前
NVIDIA CUDA Python 常用 API 及详细教程
算法·面试·github
ajassi200010 小时前
开源 C# 快速开发(十七)进程--消息队列MSMQ
windows·开源·c#
ApacheSeaTunnel11 小时前
从小时级到分钟级:多点DMALL如何用Apache SeaTunnel把数据集成成本砍到1/3?
大数据·开源·数据集成·seatunnel·技术分享
逛逛GitHub12 小时前
这个 GitHub 项目牛逼!在手机上用 Claude Code 或 Codex。
github