How to return only specific properties from Azure Devops REST APIs

Viewed 1163

I am trying to fetch all build details for a particular project in Azure devops using GET: https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=5.1

This API returns a JSON object with many properties. In Jenkins APIs we have a provision to filter only the required properties. Is there a similar way in Azure Devops APIs?

As per the docs https://docs.microsoft.com/en-us/rest/api/azure/devops/build/builds/get?view=azure-devops-rest-5.1, I have tried using the propertyFilter but didn't help me out.

Any help or pointers will be useful.

2 Answers

The JSON that is returned from DevOps looks pretty much like this:

{
  "count": 1000,
  "value": [
    {
      "id": ,
      "buildNumber": "",
      "status": "",
      "result": "",
      "queueTime": "",
      "startTime": "",
      "finishTime": "",
      "url": "",
      "definition": {
        "drafts": [

        ],
        "id": ,
        "name": "",
        "url": "",
        "uri": "",
        "path": "",
        "type": "",
        "queueStatus": "",
        "revision": ,
        "project": {
          "id": "",
          "name": "<NAMEOFYOURPROJECT>",
          "url": "",
          "state": "",
          "revision": ,
          "visibility": "",
          "lastUpdateTime": ""
        }
      },
      "buildNumberRevision": 2,
      "project": {
        "id": "",
        "name": "<NAMEOFYOURPROJECT>",
        "url": "",
        "state": "",
        "revision": ,
        "visibility": "",
        "lastUpdateTime": "2"
      },
      ...

The key your are looking for to filter is project/name.

Your ODATA query string to filter for project/name would look like this: https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=5.1&$filter=startswith(project/name,'<NAMEOFYOURPROJECT>')

The properties query in the URI Parameters may not be the properties of the returned response json as what we think it is. It might be the properties in the property properties of returned build results.

As workaround, you can write scripts to filter the required properties from the returned build results. Please check below example in powershell scripts:

$url ="https://dev.azure.com/{OGR}/{PROJ}/_apis/build/builds?`$top=5&api-version=5.1"
$PAT="PAT"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
$result = Invoke-RestMethod -Uri $murl -Headers @{authorization = "Basic $base64AuthInfo"} -Method get 

# filter the requested properties from the result
$requiredProperties = $result.value | select {$_.project.name}, {$_.definition.name}, id, buildNumber  

$requiredProperties

Above scripts get the top 5 builds' results and use script ($result.value | select {$_.project.name}, {$_.definition.name}, id, buildNumber) to select the required properties afterwards. You will see the selected properties as below screenshot:

enter image description here

Hope above helps!

Related