2

My requirement is to read/extract source code of each file present in an Azure DevOps pull request using APIs or C#. I am able to download the code for a particular file using sample URL below -

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/items?path=/{CodePath}&version={branch name}&api-version=5.1

Now I need, list of files with location where it is stored in a branch of Azure DevOps

I have tried different GET calls from REST APIs available. For example:

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/iterations/{iterationId}?api-version=5.1

or

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/attachments/{fileName}?api-version=5.1-preview.1

These calls are returning information:

  1. About file commits
  2. About documents attached in description

Information I have to fulfill the requirement: Organization name, Repository name, Branch Name, Pull request ID

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63

3 Answers3

4

Is there a way to list pull request files(from source branch) with it's location in Azure Devops using APIs? Is there any other way using C#?

I am afraid there is no such out of way to list pull request files(from source branch) with it's location in Azure Devops using APIs.

First, when we use the pull request API, we could get the Organization name and Pull request ID directly.

Then we could use the REST API Pull Requests - Get Pull Requests to get the repository name and sourceRefName (Branch Name):

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests?api-version=5.1

enter image description here

Second, we could use the REST API Pull Request Commits - Get Pull Request Commits to get the commits in the PR:

enter image description here

Then, we could use the commit id with REST API Commits - Get Changes to file path:

enter image description here

Now, we get all the info what we want.

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • Drawback of this method is, for multiple commits to a same file, it lists different version of that file. – Ratheesh Nov 16 '22 at 04:31
1

According to @Leo Liu answer, this is my code in powershell (working great!):

      $AzureDevOpsPAT = "$(System.AccessToken)";
      $headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$AzureDevOpsPAT")) }
      $organization = "YOUR ORGANIZATION"
      $project = "YOUR PROJECT"
      $repoId = "YOUR REPOSITORY ID"
      $baseUrl = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repoId"
      $pullRequest = $(System.PullRequest.PullRequestId);

      $commits = Invoke-RestMethod -Method Get -Headers $headers "$baseUrl/pullRequests/$pullRequest/commits" -verbose
      if ($commits.count -gt 0) {                
        foreach ($commit in $commits.value) {                      
              $commitId = $commit.commitId
              $changes = Invoke-RestMethod -Method Get -Headers $headers "$baseUrl/commits/$commitId/changes" -verbose
              foreach ($change in $changes.changes) { 
                  write-host $change.item.path                          
              }
        }    
      }
Shoshana Tzi
  • 99
  • 4
  • 10
0

I believe the API you're looking for is really close one of them you currently have:

https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/iterations/{iterationId}/changes

The addition of the changes at the end gives you a list of files: For example:

{
  "changeEntries": [
    {
      "changeTrackingId": 1,
      "changeId": 1,
      "item": {
        "objectId": "e21e56d119ae81fb4ffebc4fefc6351f5b5ef888",
        "path": "/new_feature.cpp"
      },
      "changeType": "add"
    },
    {
      "changeTrackingId": 2,
      "changeId": 2,
      "item": {
        "objectId": "5ec0f71ffb8b47bd4c0117f624647963e021f3d2",
        "path": "/new_feature.h"
      },
      "changeType": "add"
    }
  ]
}

From there, you should be able to use the path to get the source code as I think you've already noted you know how.

See Pull Request Iteration Changes - Get

AaronR
  • 81
  • 1
  • 3