0

Is there a way to calculate lines of code in a Pull Request API in Azure Devops for each file. I have gone through below two links and but was not of much help.

Is there a way to get the amount of lines changed in a Pull Request via the Dev Ops Service REST API?

Lines of Code modified in each Commit in TFS rest api. How do i get?

Thank you.

Ajay Kumar
  • 504
  • 2
  • 10
  • 23

1 Answers1

4

Steps:

a. Get the commit IDs for the specified pull request

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

b. Get commit path via the commit ID

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits/{commitId}/changes?api-version=5.0

c. Get parents commit ID via commit ID

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

d. Get the result via below API and request body.

POST https://dev.azure.com/{Org name}/_apis/Contribution/HierarchyQuery/project/{Project name}?api-version=5.1-preview

Request Body:

{
  "contributionIds": [
    "ms.vss-code-web.file-diff-data-provider"
  ],
  "dataProviderContext": {
    "properties": {
      "repositoryId": "{Repo ID}",
      "diffParameters": {
        "includeCharDiffs": true,
        "modifiedPath": "{Commit path}",
        "modifiedVersion": "GC{Commit ID}",
        "originalPath": "{Commit path}",
        "originalVersion": "GC{parents commit ID}",
        "partialDiff": true
      }
    }
  }
}

Result:

enter image description here

Vito Liu
  • 7,525
  • 1
  • 8
  • 17
  • Thank you and I'll try to get difference from master repository rather than between commits. any help will be appreciated. Also, In pull request, suppose there are 20 commits I only need to get difference between final commit version and master version. – Ajay Kumar Dec 29 '20 at 09:39
  • Hi @AjayVerma, Do you mean list all difference? If you could get it in the UI, could you share the screenshot result here? And we could get it via F12. – Vito Liu Dec 29 '20 at 10:03
  • Hi, Yes. I mean all difference for [final iteration files i.e. iteration count is 2](GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/iterations/2/changes?api-version=6.0) which gives us change entries. In your screenshot, like it shows **-2 and +3** between master and current branch details. – Ajay Kumar Dec 29 '20 at 12:14