Skip to content

Commit 16a2f04

Browse files
authored
Sync eng/common directory with azure-sdk-tools for PR 12267 (#37854)
* Use authenticated GitHub request when env variable is present
1 parent 2805926 commit 16a2f04

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

eng/common/scripts/Helpers/AzSdkTool-Helpers.ps1

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,39 @@ function isNewVersion(
105105
return $true
106106
}
107107

108+
function Get-GitHubApiHeaders {
109+
# Use GitHub cli to get an auth token if available
110+
$token = ""
111+
if (Get-Command gh -ErrorAction SilentlyContinue) {
112+
try
113+
{
114+
$token = gh auth token 2>$null
115+
}
116+
catch
117+
{
118+
Write-Host "Failed to get GitHub CLI auth token."
119+
}
120+
}
121+
122+
# Get token from env if not available from gh cli
123+
if (!$token)
124+
{
125+
Write-Host "Checking for GITHUB_TOKEN environment variable."
126+
$token = $env:GITHUB_TOKEN
127+
}
128+
129+
if ($token)
130+
{
131+
Write-Host "Using authenticated GitHub API requests."
132+
$headers = @{
133+
Authorization = "Bearer $token"
134+
}
135+
return $headers
136+
}
137+
Write-Host "Using unauthenticated GitHub API requests."
138+
return @{}
139+
}
140+
108141
<#
109142
.SYNOPSIS
110143
Installs a standalone version of an engsys tool.
@@ -135,11 +168,12 @@ function Install-Standalone-Tool (
135168
}
136169

137170
$tag = "${Package}_${Version}"
171+
$headers = Get-GitHubApiHeaders
138172

139173
if (!$Version -or $Version -eq "*") {
140174
Write-Host "Attempting to find latest version for package '$Package'"
141175
$releasesUrl = "https://api.github.com/repos/$Repository/releases"
142-
$releases = Invoke-RestMethod -Uri $releasesUrl
176+
$releases = Invoke-RestMethod -Uri $releasesUrl -Headers $headers
143177
$found = $false
144178
foreach ($release in $releases) {
145179
if ($release.tag_name -like "$Package*") {
@@ -163,7 +197,7 @@ function Install-Standalone-Tool (
163197

164198
if (isNewVersion $version $downloadFolder) {
165199
Write-Host "Installing '$Package' '$Version' to '$downloadFolder' from $downloadUrl"
166-
Invoke-WebRequest -Uri $downloadUrl -OutFile $downloadLocation
200+
Invoke-WebRequest -Uri $downloadUrl -OutFile $downloadLocation -Headers $headers
167201

168202
if ($downloadFile -like "*.zip") {
169203
Expand-Archive -Path $downloadLocation -DestinationPath $downloadFolder -Force

0 commit comments

Comments
 (0)