Skip to content

Commit 210b44d

Browse files
committed
Rework HTTP request to get release list
- Not using UseDefaultCredentials anymore. - Adjust accept and version headers according to github docs. - Wrap web request with try catch to perform new attempt with github token on failure.
1 parent 95dbeae commit 210b44d

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

install-nf-build-components.ps1

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#
55

66
[System.Net.WebClient]$webClient = New-Object System.Net.WebClient
7-
$webClient.UseDefaultCredentials = $true
87

98
function DownloadVsixFile($fileUrl, $downloadFileName)
109
{
@@ -19,7 +18,8 @@ $tempDir = $env:RUNNER_TEMP
1918
# Get latest releases of nanoFramework VS extension
2019
[System.Net.WebClient]$webClient = New-Object System.Net.WebClient
2120
$webClient.Headers.Add("User-Agent", "request")
22-
$webClient.Headers.Add("Accept", "application/vnd.github.v3+json")
21+
$webClient.Headers.Add("Accept", "application/vnd.github+json")
22+
$webClient.Headers.Add("X-GitHub-Api-Version", "2022-11-28")
2323
$webClient.Headers.Add("ContentType", "application/json")
2424

2525
if($env:GITHUB_AUTH_TOKEN)
@@ -32,7 +32,38 @@ if($env:GITHUB_AUTH_TOKEN)
3232
$webClient.Headers.Add("Authorization", $auth)
3333
}
3434

35-
$releaseList = $webClient.DownloadString('https://api.github.com/repos/nanoframework/nf-Visual-Studio-extension/releases?per_page=100')
35+
try
36+
{
37+
$releaseList = $webClient.DownloadString('https://api.github.com/repos/nanoframework/nf-Visual-Studio-extension/releases?per_page=100')
38+
}
39+
catch
40+
{
41+
42+
# assume exception is with try with GITHUB_TOKEN
43+
if($env:GITHUB_TOKEN)
44+
{
45+
Write-Output "INFO: 2nd try using GITHUB TOKEN"
46+
47+
# authorization header with github token
48+
$auth = "Bearer $env:GITHUB_TOKEN"
49+
50+
$webClient.Headers.Remove("Authorization")
51+
$webClient.Headers.Add("Authorization", $auth)
52+
53+
$releaseList = $webClient.DownloadString('https://api.github.com/repos/nanoframework/nf-Visual-Studio-extension/releases?per_page=100')
54+
}
55+
else
56+
{
57+
$result = $_.Exception.Response.GetResponseStream()
58+
$reader = New-Object System.IO.StreamReader($result)
59+
$reader.BaseStream.Position = 0
60+
$reader.DiscardBufferedData()
61+
$responseBody = $reader.ReadToEnd()
62+
63+
Write-Output "🛑 performing request: $responseBody"
64+
exit 1
65+
}
66+
}
3667

3768
if($releaseList -match '\"(?<VS2022_version>v2022\.\d+\.\d+\.\d+)\"')
3869
{

0 commit comments

Comments
 (0)