Sync eng/common directory with azure-sdk-tools for PR 17077 (#3858)

---------

Co-authored-by: Anton Kolesnyk <antkmsft@users.noreply.github.com>
Co-authored-by: Anton Kolesnyk <41349689+antkmsft@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-10-06 01:34:17 -07:00
1 parent aab35e1bdb
commit 46ab6a5dd7
1 file changed
+60 -21
@@ -135,35 +135,73 @@ function Install-Standalone-Tool (
}
$tag = "${Package}_${Version}"
$downloadUrlPrefix = "https://github.com/$Repository/releases/download"
if (!$Version -or $Version -eq "*") {
Write-Host "Attempting to find latest version for package '$Package'"
$found = $false
# First attempt: use git ls-remote on repository tags to avoid GitHub REST API rate limits
try {
$remoteUrl = "https://github.com/$Repository.git"
$rawTags = git ls-remote --tags --refs --sort=-version:refname $remoteUrl "${Package}_*"
if ($rawTags) {
$matchingTags = @()
foreach ($line in $rawTags) {
if ($line -match "refs/tags/(${Package}_(?!.*dev)(.+))$") {
$matchingTags += [PSCustomObject]@{
Tag = $matches[1]
Version = $matches[2]
}
}
}
if ($matchingTags.Count -gt 0) {
$latest = $matchingTags[0]
$tag = $latest.Tag
$Version = $latest.Version
if ($Package -eq "azsdk") {
$StorageAccount = "azuresdkartifacts"
$Container = "public-azsdk-cli"
try {
$Version = ( `
[xml]( `
[string]( `
Invoke-RestMethod `
-Uri "https://${StorageAccount}.blob.core.windows.net/${Container}?restype=container&comp=list&maxresults=100000" `
-Method Get`
) -replace "`u{FEFF}", "" `
) `
).EnumerationResults.Blobs `
| Select-Object -ExpandProperty Blob `
| Select-Object -ExpandProperty Name `
| Select-String -Pattern "^${Package}_(?<Version>(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*))/$([regex]::Escape($systemDetails.file_name))$" `
| %{ [semver]$_.Matches.Captures.Groups["Version"].Value } `
| Sort-Object -Descending `
| Get-Unique `
| %{ $_.ToString() } `
| Select-Object -First 1
if ($Version) {
$tag = "${Package}_${Version}"
$downloadUrlPrefix = "https://${StorageAccount}.blob.core.windows.net/${Container}"
$found = $true
Write-Host "Found the latest version of ${Package} ${Version} from ${StorageAccount}/${Container}"
}
}
catch {
Write-Host "Failed to get latest version from ${StorageAccount}/${Container}: $_"
}
}
catch {
Write-Host "git ls-remote failed: $_. Falling back to GitHub REST API."
if (!$found) {
# First attempt: use git ls-remote on repository tags to avoid GitHub REST API rate limits
try {
$remoteUrl = "https://github.com/$Repository.git"
$rawTags = git ls-remote --tags --refs --sort=-version:refname $remoteUrl "${Package}_*"
if ($rawTags) {
$matchingTags = @()
foreach ($line in $rawTags) {
if ($line -match "refs/tags/(${Package}_(?!.*dev)(.+))$") {
$matchingTags += [PSCustomObject]@{
Tag = $matches[1]
Version = $matches[2]
}
}
}
if ($matchingTags.Count -gt 0) {
$latest = $matchingTags[0]
$tag = $latest.Tag
$Version = $latest.Version
$found = $true
Write-Host "Found the latest version of $Package $Version via git ls-remote"
}
}
}
catch {
Write-Host "git ls-remote failed: $_. Falling back to GitHub REST API."
}
}
# Fallback: GitHub REST API
@@ -175,6 +213,7 @@ function Install-Standalone-Tool (
$tag = $release.tag_name
$Version = $release.tag_name -replace "${Package}_", ""
$found = $true
Write-Host "Found the latest version of $Package $Version via GitHub REST API"
break
}
}
@@ -186,7 +225,7 @@ function Install-Standalone-Tool (
}
$downloadFolder = Resolve-Path $Directory
$downloadUrl = "https://github.com/$Repository/releases/download/$tag/$($systemDetails.file_name)"
$downloadUrl = "$downloadUrlPrefix/$tag/$($systemDetails.file_name)"
$downloadFile = $downloadUrl.Split('/')[-1]
$downloadLocation = Join-Path $downloadFolder $downloadFile
$savedVersionTxt = Join-Path $downloadFolder "downloaded_version.txt"