From e7aa70317dafb2e352cbcc04d513dd6d34f4a86e Mon Sep 17 00:00:00 2001 From: tmsmith Date: Wed, 10 Feb 2021 09:13:18 -0500 Subject: [PATCH] added ability to use private github repositories To do this, define a gh_api_token in the sccop config, if the repository is private it will use the apis to get and use the asset id --- lib/core.ps1 | 14 ++++++++++++++ lib/install.ps1 | 28 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/core.ps1 b/lib/core.ps1 index 9b3b2871df..e12ecc25c7 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -930,6 +930,20 @@ function handle_special_urls($url) # Reshapes the URL to avoid redirections $url = "https://downloads.sourceforge.net/project/$($matches['project'])/$($matches['file'])" } + + # Github.com + if ($url -match "github.com\/(?[^\/]+)\/(?[^\/]+)\/releases\/download\/(?[^\/]+)\/(?.*)" -and (get_config 'gh_api_token')) { + $headers = @{ + "Authorization" = "token $(get_config 'gh_api_token')" + } + $privateUrl = "https://api.github.com/repos/$($matches['owner'])/$($matches['repo'])" + $assetUrl="https://api.github.com/repos/$($matches['owner'])/$($matches['repo'])/releases/tags/$($matches['tag'])" + + $isPrivate = (Invoke-RestMethod -Uri $privateUrl -Headers $headers).private + if ($isPrivate) { + $url = ((Invoke-RestMethod -Uri $assetUrl -Headers $headers).assets | Where-Object { $_.name -eq $matches['file'] }).url + } + } return $url } diff --git a/lib/install.ps1 b/lib/install.ps1 index 5c78268b31..66277d173e 100644 --- a/lib/install.ps1 +++ b/lib/install.ps1 @@ -360,6 +360,23 @@ function dl_with_cache_aria2($app, $version, $manifest, $architecture, $dir, $co } } +function get_headers($url) { + $hosts = get_config "hosts" + if ($hosts) { + $hosts | Where-Object { $url -match $_.match } | ForEach-Object { + return $_.headers + } + } + return {} +} + +function get-members($obj) { + $obj | Get-Member -MemberType NoteProperty | ForEach-Object { + $key = $_.Name + [PSCustomObject]@{Key = $key; Value = $obj."$key"} + } +} + # download with filesize and progress indicator function dl($url, $to, $cookies, $progress) { $reqUrl = ($url -split "#")[0] @@ -369,9 +386,20 @@ function dl($url, $to, $cookies, $progress) { if (-not ($url -imatch "sourceforge\.net" -or $url -imatch "portableapps\.com")) { $wreq.referer = strip_filename $url } + if ($url -imatch "api\.github\.com\/repos") { + $wreq.accept = "application/octet-stream" + $wreq.headers["Authorization"] = "token $(get_config 'gh_api_token')" + } if($cookies) { $wreq.headers.add('Cookie', (cookie_header $cookies)) } + + $customHeaders = get_headers $url + if ($customHeaders) { + get-members $customHeaders | ForEach-Object { + $wreq.headers[$_.Key] = $_.Value + } + } } try {