You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何高效统计GitHub团队在200+仓库中的Pull Request数量

高效统计GitHub团队PR数据的方案

核心方案:用GitHub GraphQL API批量查询

GraphQL支持嵌套查询和批量拉取数据,能彻底解决逐个仓库遍历的低效问题,还能更优雅处理分页。

第一步:先拿到团队所有成员

先一次性拉取团队成员的登录名,避免后续重复校验:

query GetTeamMembers($org: String!, $teamSlug: String!, $cursor: String) {
  organization(login: $org) {
    team(slug: $teamSlug) {
      members(first: 100, after: $cursor) {
        pageInfo {
          endCursor
          hasNextPage
        }
        nodes {
          login
        }
      }
    }
  }
}

循环请求直到hasNextPage为false,把所有成员login存下来。

第二步:批量拉取团队仓库的PR数据

直接查询团队名下的仓库,同时拉取这些仓库里属于团队成员的PR,一次请求能覆盖多个仓库:

query GetTeamPRs($org: String!, $teamSlug: String!, $memberLogins: [String!]!, $cursor: String) {
  organization(login: $org) {
    team(slug: $teamSlug) {
      repositories(first: 50, after: $cursor) {
        pageInfo {
          endCursor
          hasNextPage
        }
        nodes {
          name
          pullRequests(
            first: 100
            states: [OPEN, CLOSED, MERGED]
            createdBy: {login: $memberLogins}
            orderBy: {field: CREATED_AT, direction: DESC}
          ) {
            pageInfo {
              endCursor
              hasNextPage
            }
            nodes {
              createdAt
              author {
                login
              }
            }
          }
        }
      }
    }
  }
}

通过cursor分页处理仓库和PR的多页数据,一次请求最多能拉50个仓库、每个仓库100条PR,比REST API少N倍请求。

第三步:本地聚合统计

把拉取到的所有PR数据存在本地数组或数据库里,然后:

  • 总数统计:直接数数组长度就行
  • 每日提交量:按createdAt的日期部分(比如2024-05-20)分组,用计数器统计每天的数量(比如Python用collections.Counter,JS用reduce)

实用优化点

  • 缓存数据:团队成员列表和已拉取的PR数据存本地缓存,避免重复请求浪费配额
  • 处理请求限制:GitHub GraphQL的配额是5000点/小时,遇到403错误就等几分钟再重试,别硬怼
  • 并行请求:可以并行处理仓库的分页请求,但别太猛,控制并发数不超配额

快速实现方案:用GitHub CLI+Shell脚本

不想写代码的话,用gh CLI结合shell脚本也能搞定,虽然效率不如GraphQL,但胜在快速:

  1. 导出团队成员:gh api orgs/{你的组织名}/teams/{团队slug}/members --jq '.[].login' > members.txt
  2. 导出团队仓库:gh api orgs/{你的组织名}/teams/{团队slug}/repos --jq '.[].full_name' > repos.txt
  3. 遍历统计:
total_prs=0
declare -A daily_counts

while read repo; do
  gh api repos/$repo/pulls?state=all --paginate --jq '.[] | {created_at, author: .login}' | while read pr; do
    login=$(echo $pr | jq -r '.author')
    if grep -q "^$login$" members.txt; then
      ((total_prs++))
      date=$(echo $pr | jq -r '.created_at' | cut -c1-10)
      ((daily_counts[$date]++))
    fi
  done
done < repos.txt

echo "Total PRs: $total_prs"
echo "Daily counts:"
for date in "${!daily_counts[@]}"; do
  echo "$date: ${daily_counts[$date]}"
done

内容的提问来源于stack exchange,提问作者Legend ile

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 00:49:54