Azure Devops Rest API- Get builds currently queued

2019-04-12 13:21发布

问题:

Is there a way to only get the builds that are waiting in queue for an available agent in a specific pool from the Azure DevOps rest API?

I currently have this endpoint that provides me with all the job requests that occurred in the pool:

https://dev.azure.com/{organization}/_apis/distributedtask/pools/{poolid}/jobrequests

I looked through the API documentation and am unable to find anything regarding agent pools.

回答1:

There is no out of the box such API, but we can use the regular API and filter the results.

For example, I use the API you provided and I got all the builds in the pool, then, I filtered the results with PowerShell to get only the builds that waiting for an available agent.

How do I know who waiting? in the JSON result, to each build have some properties, if the build started to run on an agent he got a property assignTime, so I search builds without this property.

#... Do the API call and get the repsone
$json = $repsone | ConvertFrom-Json

$json.value.ForEach
({
    if(!$_.assignTime)
    {
        Write-Host "Build waiting for an agent:"
        Write-Host Build Definition Name: $_.definition.name
        Write-Host Build Id: $_.owner.id
        Write-Host Queue Time $_.queueTime
        # You can print more details about the build
    }
})


# Printed on screen:
Build waiting for an agent:
Build Definition Name: GitSample-CI
Build Id: 59
Queue Time 2019-01-16T07:36:52.8666667Z

If you don't want to iterate on all the builds (that make sense) you can retrieve the waiting builds in this way:

$waitingBuilds = $json.value | where {-not $_.assignTime} 
# Then print the details