Azure DevOps中如何获取PR的文件修改列表及行增删改统计
获取Azure DevOps指定Pull Request的文件列表及行数统计的实现方式
1. 直接通过Azure DevOps UI查看
- 打开目标Pull Request页面,切换到Files标签页,即可看到所有被修改、新增或删除的文件列表。
- 每个文件名右侧会显示对应的新增行数(绿色
+)和删除行数(红色-),点击单个文件可进入详情页查看具体行变更。
2. 使用Azure DevOps CLI命令
首先确保已安装并配置Azure DevOps CLI:
- 安装CLI后,执行
az login登录账号,再用az devops configure --defaults organization=你的组织名 project=你的项目名设置默认组织和项目。 - 获取PR文件及统计:
该命令返回的JSON结果中,每个文件项包含az repos pr show --id <PR-ID> --include-files trueadditions(新增行数)、deletions(删除行数)字段。 - 如需表格化输出,添加
--output table参数:az repos pr show --id 123 --include-files true --output table
3. 调用Azure DevOps REST API
- 请求端点:
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests/{pullRequestId}/files?api-version=7.1-preview.1 - 参数替换:将
organization、project、repositoryId、pullRequestId替换为实际值。 - 身份验证:使用拥有代码读取权限的个人访问令牌(PAT),示例curl调用:
curl -u "你的用户名:你的PAT令牌" "https://dev.azure.com/MyOrg/MyProj/_apis/git/repositories/RepoId/pullrequests/123/files?api-version=7.1-preview.1" - 返回结果:每个文件对象包含
path、additions、deletions、changeType等字段,便于后续处理。
4. PowerShell脚本自动化获取
适合批量或自动化场景,示例脚本:
$orgUrl = "https://dev.azure.com/YourOrganization" $projectName = "YourProject" $prId = 123 $repoId = "你的仓库ID" $pat = "你的PAT令牌" $headers = @{ "Authorization" = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat")) } $apiUrl = "$orgUrl/$projectName/_apis/git/repositories/$repoId/pullrequests/$prId/files?api-version=7.1-preview.1" $response = Invoke-RestMethod -Uri $apiUrl -Headers $headers -Method Get foreach ($file in $response.value) { Write-Host "文件路径: $($file.path)" Write-Host "新增行数: $($file.additions) | 删除行数: $($file.deletions)" Write-Host "-------------------------" }
替换变量为实际值后执行,即可输出每个文件的详细统计。
5. 使用Azure DevOps Marketplace扩展
搜索安装如Pull Request Stats类的扩展,安装后可在PR页面直接查看更丰富的统计信息,包括总新增/删除行数、各文件的细分统计,甚至代码复杂度变化等,无需编写代码。
内容的提问来源于stack exchange,提问作者karthikN
相关产品推荐
相关产品推荐

