Azure Repos合并请求变更文件获取及流水线触发技术问询
Great question! Let's walk through exactly how to solve this—since you already know about some of the Azure DevOps REST APIs, let's focus on filling in the gaps around getting the mergeOperationId (or even a simpler alternative) in your post-merge pipeline.
1. Get Pull Request Changed Files (No Merge Operation ID Required)
First off, you don't actually need the mergeOperationId to fetch the list of changed files in the merged PR. The Get Pull Request Changes API is designed for this exact use case, and it only requires the PR ID:
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests/{pullRequestId}/changes?api-version=7.1-preview.1
This returns a list of all files added, modified, or deleted in the PR, including their full paths and change types.
2. How to Get the PR ID (and Merge Operation ID) in Your Post-Merge Pipeline
Since your pipeline triggers after the PR is merged, you won't have the PR ID directly from pre-defined variables (those are only available in PR-triggered pipelines). Instead, you can link the merge commit to its parent PR using these steps:
Step 1: Fetch the merge commit ID
Use the pre-defined pipeline variable $(Build.SourceVersion)—this holds the commit ID of the merge that triggered your pipeline.
Step 2: Find the PR associated with the commit
Call the Get Pull Requests by Commit API to retrieve the PR linked to that merge commit:
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests?searchCriteria.commitId=$(Build.SourceVersion)&api-version=7.1-preview.1
The response will include the full PR object, which contains:
pullRequestId: The ID of the merged PR (use this for the Changes API above)mergeOperation.mergeOperationId: The exact ID you were asking about, if you still need to use the Get Merge Requests interface
Example PowerShell Script for Pipeline
Here's a ready-to-use snippet you can add to your pipeline to fetch the files and process extensions:
# Set up variables using pipeline pre-defined values $orgUri = "$(System.TeamFoundationCollectionUri)" $projectName = "$(System.TeamProject)" $repoId = "$(Build.Repository.ID)" $mergeCommitId = "$(Build.SourceVersion)" $accessToken = "$(System.AccessToken)" # Create authorization header $headers = @{ Authorization = "Bearer $accessToken" } # Get the PR linked to the merge commit $prSearchUrl = "$orgUri/$projectName/_apis/git/repositories/$repoId/pullrequests?searchCriteria.commitId=$mergeCommitId&api-version=7.1-preview.1" $prResult = Invoke-RestMethod -Uri $prSearchUrl -Headers $headers -Method Get # Extract PR details (handle case where multiple PRs are linked, though rare for merges) $targetPr = $prResult.value[0] $pullRequestId = $targetPr.pullRequestId $mergeOperationId = $targetPr.mergeOperation.mergeOperationId Write-Host "Found merged PR #$pullRequestId with Merge Operation ID: $mergeOperationId" # Fetch the changed files in the PR $changesUrl = "$orgUri/$projectName/_apis/git/repositories/$repoId/pullrequests/$pullRequestId/changes?api-version=7.1-preview.1" $changesResult = Invoke-RestMethod -Uri $changesUrl -Headers $headers -Method Get # Process files by extension foreach ($change in $changesResult.changes) { $filePath = $change.item.path $fileExtension = [System.IO.Path]::GetExtension($filePath) Write-Host "Processing file: $filePath (Extension: $fileExtension)" # Add your logic here to trigger specific pipelines based on $fileExtension }
3. Key Pipeline Setup Notes
- Permissions: Ensure your pipeline's service account has the Contributor role on the repository and project—this is required to access PR and commit data via the API.
- AccessToken: Using
$(System.AccessToken)is preferred over a personal PAT, as it's managed by Azure DevOps and scoped to your pipeline's context. - Merge Types: This works for all merge types (squash, rebase, standard merge) since the merge commit will always be linked to the original PR.
内容的提问来源于stack exchange,提问作者humblebee

