Search code examples
azure-devops-rest-apiazure-artifacts

How to get only the latest version of a package via AzureDevOps REST API


Below API returns all the versions of a specific package. In order to get the latest version of that package, I can use the isLatest:true data from the returned response.

https://learn.microsoft.com/en-us/rest/api/azure/devops/artifacts/artifact%20%20details/get%20package%20versions?view=azure-devops-rest-6.0

But I was wondering if there is way to only get the latest version in the response rather than all the version? Is it possible?

If there is no possibility of that, then a second question - Will latest version be always the first item in the response? (I assume there is a limit to the return item count (1000?) so I was wondering if one API call would always be sufficient if I need to get the latest version.


Solution

  • If there is no possibility of that, then a second question - Will latest version be always the first item in the response?

    The answer is yes. The latest version be always the first item in the response.

    As test, I published a test package to my feed with versions 1.0.0, 2.0.0, then published the version 1.0.1-preview1.2, 1.0.1. But Azure devops will be sorted in order of package version:

    enter image description here

    So, we could use REST API with powershell parameter Select-Object -first 1 to get the latest package version:

    $connectionToken="Your PAT"
    $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
    
    $url = 'GET https://feeds.dev.azure.com/{organization}/{project}/_apis/packaging/Feeds/{feedId}/Packages/{packageId}/versions?api-version=6.0-preview.1
    '
    $PackageInfo = (Invoke-RestMethod -Uri $url -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}) 
    
    $LatestVersion= $PackageInfo.value.version | Select-Object -first 1
    
    
    Write-Host "Latest package Version = $LatestVersion"
    

    The test result:

    enter image description here