From 5b3bf66882088dad53c3238a548148676caf5f0b Mon Sep 17 00:00:00 2001 From: Azure SDK Bot <53356347+azure-sdk@users.noreply.github.com> Date: Thu, 2 Oct 2025 12:50:51 -0700 Subject: [PATCH] Sync eng/common directory with azure-sdk-tools for PR 12267 (#6758) * Use authenticated GitHub request when env variable is present --- .../scripts/Helpers/AzSdkTool-Helpers.ps1 | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/eng/common/scripts/Helpers/AzSdkTool-Helpers.ps1 b/eng/common/scripts/Helpers/AzSdkTool-Helpers.ps1 index 8d74ece07..57f40a0e0 100644 --- a/eng/common/scripts/Helpers/AzSdkTool-Helpers.ps1 +++ b/eng/common/scripts/Helpers/AzSdkTool-Helpers.ps1 @@ -105,6 +105,39 @@ function isNewVersion( return $true } +function Get-GitHubApiHeaders { + # Use GitHub cli to get an auth token if available + $token = "" + if (Get-Command gh -ErrorAction SilentlyContinue) { + try + { + $token = gh auth token 2>$null + } + catch + { + Write-Host "Failed to get GitHub CLI auth token." + } + } + + # Get token from env if not available from gh cli + if (!$token) + { + Write-Host "Checking for GITHUB_TOKEN environment variable." + $token = $env:GITHUB_TOKEN + } + + if ($token) + { + Write-Host "Using authenticated GitHub API requests." + $headers = @{ + Authorization = "Bearer $token" + } + return $headers + } + Write-Host "Using unauthenticated GitHub API requests." + return @{} +} + <# .SYNOPSIS Installs a standalone version of an engsys tool. @@ -135,11 +168,12 @@ function Install-Standalone-Tool ( } $tag = "${Package}_${Version}" + $headers = Get-GitHubApiHeaders if (!$Version -or $Version -eq "*") { Write-Host "Attempting to find latest version for package '$Package'" $releasesUrl = "https://api.github.com/repos/$Repository/releases" - $releases = Invoke-RestMethod -Uri $releasesUrl + $releases = Invoke-RestMethod -Uri $releasesUrl -Headers $headers $found = $false foreach ($release in $releases) { if ($release.tag_name -like "$Package*") { @@ -163,7 +197,7 @@ function Install-Standalone-Tool ( if (isNewVersion $version $downloadFolder) { Write-Host "Installing '$Package' '$Version' to '$downloadFolder' from $downloadUrl" - Invoke-WebRequest -Uri $downloadUrl -OutFile $downloadLocation + Invoke-WebRequest -Uri $downloadUrl -OutFile $downloadLocation -Headers $headers if ($downloadFile -like "*.zip") { Expand-Archive -Path $downloadLocation -DestinationPath $downloadFolder -Force