如何使用Python统计Azure DevOps Git仓库的提交次数
使用Python统计Azure DevOps Git仓库提交次数实现方案
前置准备
- 首先安装依赖工具包:
执行命令:pip install azure-devops - 提前准备以下参数:
- Azure DevOps组织地址(格式:
https://dev.azure.com/你的组织名称) - 个人访问令牌(PAT):生成PAT时需要勾选「Code」分类下的「Read」权限,否则无法拉取提交数据
- 目标项目名称、目标Git仓库名称
- Azure DevOps组织地址(格式:
基础实现代码
from azure.devops.connection import Connection from msrest.authentication import BasicAuthentication from azure.devops.v7_1.git.models import GitQueryCommitsCriteria # 替换为你的实际参数 ORG_URL = "https://dev.azure.com/你的组织名" PAT = "你的个人访问令牌" PROJECT_NAME = "你的项目名" REPO_NAME = "你的仓库名" # 初始化Azure DevOps连接 credentials = BasicAuthentication("", PAT) connection = Connection(base_url=ORG_URL, creds=credentials) git_client = connection.clients.get_git_client() # 配置查询规则(可按需增加筛选条件) query_criteria = GitQueryCommitsCriteria() # 处理分页拉取所有提交,避免单次接口返回数量上限问题 all_commits = [] page_size = 100 skip_count = 0 while True: page_commits = git_client.get_commits( repository_id=REPO_NAME, project=PROJECT_NAME, criteria=query_criteria, top=page_size, skip=skip_count ) if not page_commits: break all_commits.extend(page_commits) skip_count += page_size # 输出统计结果 print(f"目标仓库总提交次数:{len(all_commits)}")
常用筛选拓展
你可以通过修改GitQueryCommitsCriteria的参数实现定向统计:
- 按时间范围统计:
# 统计2024年上半年的提交 query_criteria.from_date = "2024-01-01T00:00:00Z" query_criteria.to_date = "2024-06-30T23:59:59Z" - 按指定作者统计:
query_criteria.author = "作者的邮箱地址" - 按指定分支统计:
# 统计main分支的提交 query_criteria.item_version = "refs/heads/main"
内容的提问来源于stack exchange,提问作者Ali Haider
相关产品推荐
相关产品推荐

