Azure Dev Ops REST API - Is there a way to get the final cumulative changes in a pull request

910 views Asked by At

I want to get all the file contents that are included in a pull request. I tried the rest API endpoint https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull%20request%20commits/get%20pull%20request%20commits?view=azure-devops-rest-6.0 and this does get me all the commits that are in the pull request. I then was able to retrieve the file contents for these commits. However when there are multiple commits and the same file was changed, I end up getting the old file contents that is no longer valid in the latest pull request iteration.

Example:

  1. File A changed in commit 1. New File B was also added in commit 1.
  2. File A changed again in commit 2.
  3. Pull request created.

In the example above, I want to get the file content from commit 2 for File A plus file content of File B from commit 1. I might be able to calculate this by calling https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull%20request%20iteration%20changes/get?view=azure-devops-rest-6.0 but hoping there is a good way to get the final view of the pull request, and it's file contents.

My goal is to run custom validations against the files in the pull request and block completing the pull request. To run the validations, I need to get the file contents.

1

There are 1 answers

1
Leo Liu On BEST ANSWER

My goal is to run custom validations against the files in the pull request and block completing the pull request. To run the validations, I need to get the file contents.

I fully understand your needs, but I am afraid there is no such of way to achieve this.

As in your example, File A changed in the commit 1 and commit 2, we will get two content from the same files. But we should just need the content from the latest commit.

To resolve this issue, we have to loop through the commits and write the contents of the modified file in each commit to a file, like .txt, and name the file as the name of the modified file.

We could use the REST API Pull Request Commits - Get Pull Request Commits to get all the commits Id, we get the array from newest to old, and we need to reverse this array. This way we can start the cycle from the old commit:

$connectionToken="You PAT"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))

$url = "https://dev.azure.com/YourOrganization/YourProject/_apis/git/repositories/<repositorieId>/pullRequests/<pullRequestsId>/commits?api-version=6.0"
$Commits = (Invoke-RestMethod -Uri $url -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}) 
$CommitId= $Commits.value.commitId

[array]::Reverse($CommitId)

Write-Host The commit Id is: $CommitId

Then we could use foreach to loop through the commitId with REST API Commits - Get Changes to find the modified files and write them to the file and name them with the name of the modified file.

You could check following thread for some more details:

How to retrieve a single file from a specific revision in Git?

In this way, the content from the new commit will re-write the old commit.