buildpull-requestazure-devops-rest-apibuild-pipelinetfs-code-review

How do I get the code reviewer(s) of a build in Azure DevOps pipeline?


Given a build id, how do I get the code reviewer(s) name in azure DevOps pipeline? Assume, the build was off of a master branch - which devs merge their feature branch after code has been reviewed in a pull request. This is the policy and no one directly commit their change into master. So that means, every build has a code reviewer behind it. How do i get that?

Thanks!


Solution

  • You can use below Rest api to get the PR reviewers.

    1, First call below build rest api with the buildId. And in the response you will get the commit id from the build's sourceVersion and the repository id.

    GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}?api-version=5.1
    

    2, After you get the commit id and repository id. You can call commit rest api to get the associated PR id from the comments in the response.

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

    3, Then Call pull request reviewer rest api to get the Reviewers.

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

    Below is the example scripts in powershell. See this link to get a Personal access token

    $buildId= " "
    
    $burl =" https://dev.azure.com/OrgName/ProjName/_apis/build/builds/$($buildId)?api-version=5.1"
    
    $PAT="personel access token"
    
    $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
    
    $buildInfo = Invoke-RestMethod -Uri $curl -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo1)} -Method get -ContentType "application/json"
    #get CommitId and repoId
    $commitId = $buildInfo.sourceVersion
    $repoId=$buildInfo.repository.id
    
    #commit rest api
    $curl = "https://dev.azure.com/OrgName/ProjName/_apis/git/repositories/$($repoId)/commits/$($commitId)?api-version=5.1"
    
    $commitInfo = Invoke-RestMethod -Uri $curl -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo1)} -Method get -ContentType "application/json"
    #get PR id
    $prId = $commitInfo.comment.split(" ")[2].TrimEnd(":")
    
    $prurl = "https://dev.azure.com/OrgName/ProjName/_apis/git/repositories/$($repoId)/pullRequests/$($prId)/reviewers?api-version=5.1"
    
    Invoke-RestMethod -Uri $prurl -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo1)} -Method get -ContentType "application/json"
    

    If you can find the build from the pipeline runs history in the UI page with a give buildId. It will be much easier. You can get the PR id from the title directly. See below pic.

    enter image description here

    You can also click on the commit id shown on above screenshot, to see the details of the commit, where you will get the associated PR.

    enter image description here