Sync eng/common directory with azure-sdk-tools for PR 12267 (#6758)

* Use authenticated GitHub request when env variable is present
This commit is contained in:
Azure SDK Bot 2025-10-02 12:50:51 -07:00 committed by GitHub
parent 132c633509
commit 5b3bf66882
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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