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

如何通过Azure DevOps Git REST API获取分支的所有变更文件?

获取Azure DevOps分支所有变更的解决方案目录

问题分析

当前使用的Git API请求要么只返回最新单个提交,要么仅获取指定单个提交的变更,无法覆盖分支上的连续提交(比如示例中的Commit1、Commit2)。需要获取指定范围内所有提交的变更文件,再提取对应的根目录子文件夹(解决方案目录)。

解决方案脚本

场景1:分支构建(对比上次成功构建到当前提交的所有变更)

# 获取当前构建基础信息
$collectionUri = "$(System.CollectionUri)"
$teamProject = "$(System.TeamProject)"
$repoId = "$(Build.Repository.ID)"
$currentCommit = "$(Build.SourceVersion)"
$branchName = "$(Build.SourceBranch)"

# 获取上次成功构建的Commit ID(存在则赋值)
$lastSuccessfulCommit = $null
$buildsUrl = "$collectionUri/$teamProject/_apis/build/builds?definitions=$(Build.DefinitionId)&statusFilter=succeeded&branchName=$branchName&top=1&api-version=7.1-preview.7"
$lastBuild = Invoke-RestMethod -Uri $buildsUrl -Headers @{Authorization = "Bearer $(System.AccessToken)"} -Method Get
if ($lastBuild.count -gt 0) {
    $lastSuccessfulCommit = $lastBuild.value[0].sourceVersion
}

# 构造提交范围的API请求地址
if ($lastSuccessfulCommit) {
    # 有历史成功构建,获取两次提交间的所有记录
    $commitsUrl = "$collectionUri/$teamProject/_apis/git/repositories/$repoId/commits?searchCriteria.fromCommitId=$lastSuccessfulCommit&searchCriteria.toCommitId=$currentCommit&api-version=7.1-preview.1"
} else {
    # 首次构建,获取当前分支全部提交
    $commitsUrl = "$collectionUri/$teamProject/_apis/git/repositories/$repoId/commits?searchCriteria.itemVersion.version=$branchName&api-version=7.1-preview.1"
}

# 拉取所有目标提交
$commits = Invoke-RestMethod -Uri $commitsUrl -Headers @{Authorization = "Bearer $(System.AccessToken)"} -Method Get

# 收集所有变更文件对应的解决方案目录
$changedSolutions = @()
foreach ($commit in $commits.value) {
    # 获取当前提交的变更文件列表
    $changesUrl = "$collectionUri/$teamProject/_apis/git/repositories/$repoId/commits/$($commit.commitId)/changes?api-version=7.1-preview.1"
    $changes = Invoke-RestMethod -Uri $changesUrl -Headers @{Authorization = "Bearer $(System.AccessToken)"} -Method Get
    
    foreach ($change in $changes.changes) {
        # 提取根目录下的子文件夹(解决方案目录)
        $pathParts = $change.item.path -split '\\|/'
        if ($pathParts.Count -ge 2) {
            $solutionDir = $pathParts[1]
            $changedSolutions += $solutionDir
        }
    }
}

# 去重后输出结果,存入流水线变量供后续步骤使用
$uniqueSolutions = $changedSolutions | Select-Object -Unique
Write-Host "##vso[task.setvariable variable=ChangedSolutions;]$($uniqueSolutions -join ',')"
Write-Host "变更的解决方案目录:$($uniqueSolutions -join ',')"

场景2:PR构建(获取PR中所有变更的解决方案目录)

# 获取PR基础信息
$collectionUri = "$(System.CollectionUri)"
$teamProject = "$(System.TeamProject)"
$repoId = "$(Build.Repository.ID)"
$pullRequestId = "$(System.PullRequest.PullRequestId)"

# 获取PR的所有变更文件
$prChangesUrl = "$collectionUri/$teamProject/_apis/git/repositories/$repoId/pullRequests/$pullRequestId/changes?api-version=7.1-preview.1"
$prChanges = Invoke-RestMethod -Uri $prChangesUrl -Headers @{Authorization = "Bearer $(System.AccessToken)"} -Method Get

# 收集变更对应的解决方案目录
$changedSolutions = @()
foreach ($change in $prChanges.changes) {
    $pathParts = $change.item.path -split '\\|/'
    if ($pathParts.Count -ge 2) {
        $solutionDir = $pathParts[1]
        $changedSolutions += $solutionDir
    }
}

# 去重后输出结果,存入流水线变量
$uniqueSolutions = $changedSolutions | Select-Object -Unique
Write-Host "##vso[task.setvariable variable=ChangedSolutions;]$($uniqueSolutions -join ',')"
Write-Host "变更的解决方案目录:$($uniqueSolutions -join ',')"

使用说明

  1. 将上述脚本添加到Azure构建流水线的PowerShell任务中,需在任务"高级"选项里勾选允许脚本访问OAuth令牌。
  2. 后续构建步骤可通过变量$(ChangedSolutions)获取需要构建的解决方案目录列表,实现增量构建。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 01:52:36