0
votes

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!

2
Builds aren't code reviewed, PRs are. You can look up the branch that was built and see if it was from a PR branch, then work backwards from that to the PR ID.Daniel Mann
@DanielMann I know build aren't code reviewed. But the build is always from master branch. And any code change in master is always from a PR that was reviewed by at least one dev. The problem is how to know the PR given the build id. Or basically how to know the PR associated with the build? Remember, you can have build1, build2, ...buildn, each at different datetime with different PRs. So i can't rely simply on the latest PR. The only refernce i have is a build and how do i get PR from it?Koref Koref

2 Answers

0
votes

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

0
votes

Here is what I have finally working. Took Levi's code snippet above and just fixed a line to get pull request id working in various scenarios. Kudos to Levi's for helping! Hope it helps someone.


$PAT="personel access token"
$base64EncodedPAT = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
$basicAuth = @{Authorization = "Basic $base64EncodedPAT" }
$buildId= "..."

function GetCodeReviewers() {
    #Get build info
    $buildUrl = "https://dev.azure.com/OrgName/ProjName/_apis/build/builds/$($buildId)?api-version=5.1"
    $buildInfo = Invoke-RestMethod -Method Get -Uri $buildUrl -Headers $basicAuth

    # Get Commit Info
    $commitUrl = "https://dev.azure.com/OrgName/ProjName/_apis/git/repositories/$($buildInfo.repository.id)/commits/$($buildInfo.sourceVersion)?api-version=5.1"
    $commitInfo = Invoke-RestMethod -Uri $commitUrl  -Method Get -Headers $basicAuth

    #Get Code Reviewers
    $comment = $commitInfo.comment
    #$pullRequestId = $comment.split(" ")[2].TrimEnd(":") # it turns out, the 3rd item may not always be the PullRequestID so the next line may not work for all scenarios
    #note that, a comment could come as follows:
    # case 1: Merge PR 1234: some other text here including story or bug numbers
    # case 2: Merge pull request 1234 some additional text goes here including story or bug numbers
    # The following will pick the first number - which I assume will always be the PullRequestID
    $pullRequestId = $null
    $pullRequestId = $comment.Replace(':', '').Split(" ").Trim() | Where-Object {[int]::TryParse($_, $pullRequestId)} | Select-Object -First 1
    $pullRequestUrl = "https://dev.azure.com/OrgName/ProjName/_apis/git/repositories/$($buildInfo.repository.id)/pullRequests/$($pullRequestId)/reviewers?api-version=5.1"
    $reviewers = Invoke-RestMethod -Uri $pullRequestUrl -Method Get -Headers $basicAuth

    return $reviewers.value
}