From abbaf03fc4a9b803cdfc535eef75f96490806643 Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Mon, 17 Apr 2023 16:49:06 +0700 Subject: [PATCH] _resolve GitHub workflow build artifact urls --- lib/core.ps1 | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/core.ps1 b/lib/core.ps1 index ddd274c255..dd2ad8affc 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -1329,6 +1329,33 @@ function handle_special_urls($url) } } + # Github.com build artifacts + + Write-Host " ××× handle_special_urls PSVersion=$($PSVersionTable.PSVersion)" -ForegroundColor DarkMagenta + if ($url -match 'api.github.com/repos/(?[^/]+)/(?[^/]+)/actions/artifacts/(?[\d]+)/zip') { + if ($token = Get-GitHubToken) { + $headers = @{ + "Accept" = "application/vnd.github+json" + "Authorization" = "Bearer $($token)" + "X-GitHub-Api-Version" = "2022-11-28" + } + $assetUrl = "https://api.github.com/repos/$($Matches.owner)/$($Matches.repo)/actions/artifacts/$($Matches.artifact_id)/zip" + + $response = Invoke-WebRequest -Method 'Get' -Uri $assetUrl -Headers $headers -MaximumRedirection 0 2>$null #-SkipHttpErrorCheck # (since v7) + $status = $response.StatusCode + if ($status -eq 302) { + $url = $response.Headers.Location + } + # switch ↑ to ↓ when min PowerShell is 7 + # Invoke-RestMethod -Method 'Get' -Uri $assetUrl -Headers $headers -MaximumRedirection 0 -ResponseHeadersVariable rHeader -SkipHttpErrorCheck -StatusCodeVariable rStatus #2>$null + # if ($rStatus -eq 302) { + # $url = $rHeader.Location + # } + } else { + warn ("↓ url needs GitHub API token to be set via 'scoop config gh_token `"`"`n", $url) + } + } + return $url }