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

2.6k views Asked by At

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.

2

There are 2 answers

0
Leo Liu On BEST ANSWER

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

0
Hugh Lin On

For the first question, I am afraid that there is no out-of-the-box parameter to return the latest version of a package directly through the rest api, because the isLatest parameter is not provided in rest api url to return the latest version. In addition, the $top parameter is not provided in the url parameteres to specify the returned count.

enter image description here

Will latest version be always the first item in the response?

For the second question, the answer is Yes, the latest version will be always the first item in the response.

So as workaround, we can simply filter response through powershell script to return the latest package version.

$url = 'https://feeds.dev.azure.com/{org}/_apis/packaging/Feeds/{feedId}/Packages/{packageId}/versions?api-version=6.0-preview.1';

$token = "{PAT}"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get 

$result = $response.value | Where {$_.isLatest -eq "true"} #|

Write-Host "results = $($result | ConvertTo-Json -Depth 100)"

enter image description here