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

如何统计GitHub用户针对指定仓库PR的代码增删总行数(可选时间范围)

统计GitHub用户指定仓库PR代码增删行数的方法

一、无需编写代码的简便方案

  • 仓库页面手动统计
    进入目标仓库的Pull Requests页面,在搜索框输入author:目标用户名筛选该用户的PR,还可叠加时间范围筛选(比如created:2023-01-01..2023-12-31)。点开每个PR的「Files changed」标签,顶部会显示该PR的新增(+)和删除(-)行数,手动累加即可得到总和,适合PR数量较少的场景。
  • GitHub GraphQL Explorer(复制模板即可)
    用GitHub官方的GraphQL Explorer,无需部署额外工具,只需复制修改以下查询模板:
    {
      user(login: "目标用户名") {
        pullRequests(
          states: MERGED
          created: {since: "2023-01-01T00:00:00Z", until: "2023-12-31T23:59:59Z"}
          first: 100
          repository: {name: "仓库名", owner: "仓库所有者用户名"}
        ) {
          nodes {
            additions
            deletions
            title
          }
          totalCount
        }
      }
    }
    
    执行后会返回符合条件的所有PR的增删行数,手动求和就能得到总数。

二、自动化代码方案(适合批量统计)

如果需要处理大量PR,用Python结合GitHub API更高效:

  1. 安装依赖:pip install pygithub
  2. 示例代码(替换参数后直接运行):
from github import Github
from datetime import datetime

# 替换为你的GitHub Token、目标用户、仓库信息和时间范围
g = Github("your_github_token")
repo = g.get_repo("owner_username/repo_name")
target_user = "target_username"
start_date = datetime(2023, 1, 1)
end_date = datetime(2023, 12, 31)

total_add = 0
total_del = 0

# 遍历该用户的合并状态PR,按时间筛选
for pr in repo.get_pulls(state="merged", sort="created", direction="desc"):
    if pr.user.login == target_user and start_date <= pr.created_at <= end_date:
        total_add += pr.additions
        total_del += pr.deletions

print(f"总新增行数: {total_add}, 总删除行数: {total_del}")

三、时间范围筛选支持

所有上述方法都支持按时间范围筛选:

  • 网页端:搜索框使用created:起始日期..结束日期格式,日期支持YYYY-MM-DD或更精确的格式
  • API/GraphQL:通过since和until参数传入ISO 8601格式的时间戳

四、关于ossinsight的自定义SQL限制

目前ossinsight确实不支持在网站上运行自定义SQL查询,若需要更灵活的统计需求,建议优先使用GitHub官方的工具或上述代码方案。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 13:50:22