Search code examples
azureazure-devopsazure-pipelinesdevopsazure-powershell

Azure Devops - use a script to access the number of build validation pipelines in a Pull Request


i want to access the number of build validation pipeline checks in a Pull Request(PR). I was hoping i could use powershell in integrate the azure api i.e. https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull-requests/get-pull-request?view=azure-devops-rest-7.0

This doesn't seem to have this information. Does anyone know if this can be achieved?

devpos pull request build validation checks


Solution

  • i want to access the number of build validation pipeline checks in a Pull Request(PR).

    To meet your requirement, you can use the Rest API: Evaluations - List to get the number of the builds in a Pull Request.

    Here is an example:

    Rest API URL:

    GET https://dev.azure.com/{organization}/{project}/_apis/policy/evaluations?artifactId={artifactId}&api-version=7.1-preview.1
    

    ArtifactID sample:

    vstfs:///CodeReview/CodeReviewId/{projectId}/{pullRequestId}
    

    PowerShell sample:

    $token = "PAT"
    
    $url=" https://dev.azure.com/ORGNAME/PROJECTNAME/_apis/policy/evaluations?artifactId=vstfs:///CodeReview/CodeReviewId/projectid/pullrequestid&api-version=7.1-preview.1"
    
    $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
    
    $response = Invoke-RestMethod -Uri $url -Method Get  -Headers @{Authorization = "Basic $token"}   -ContentType 'application/json'
    
    $number = 0
    
    Foreach( $configurationlist in $response.value )
    {
       $buildid =  $configurationlist.configuration.settings.buildDefinitionId
       $type = $configurationlist.configuration.type.displayName
    
       if($type -eq "Build")
    
        {
           echo $buildid
           $number= $number+ 1
        }   
     
       
    
    }
    
    
    echo "Pull Request Build Number: $number"
    

    Result:

    enter image description here