Skip to content

Commit b23528b

Browse files
author
tmsmith
committed
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
1 parent 227de6c commit b23528b

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

lib/core.ps1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,20 @@ function handle_special_urls($url)
930930
# Reshapes the URL to avoid redirections
931931
$url = "https://downloads.sourceforge.net/project/$($matches['project'])/$($matches['file'])"
932932
}
933+
934+
# Github.com
935+
if ($url -match "github.com\/(?<owner>[^\/]+)\/(?<repo>[^\/]+)\/releases\/download\/(?<tag>[^\/]+)\/(?<file>.*)" -and (get_config 'gh_api_token')) {
936+
$headers = @{
937+
"Authorization" = "token $(get_config 'gh_api_token')"
938+
}
939+
$privateUrl = "https://api.github.com/repos/$($matches['owner'])/$($matches['repo'])"
940+
$assetUrl="https://api.github.com/repos/$($matches['owner'])/$($matches['repo'])/releases/tags/$($matches['tag'])"
941+
942+
$isPrivate = (Invoke-RestMethod -Uri $privateUrl -Headers $headers).private
943+
if ($isPrivate) {
944+
$url = ((Invoke-RestMethod -Uri $assetUrl -Headers $headers).assets | Where-Object { $_.name -eq $matches['file'] }).url
945+
}
946+
}
933947
return $url
934948
}
935949

lib/install.ps1

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,18 +360,46 @@ function dl_with_cache_aria2($app, $version, $manifest, $architecture, $dir, $co
360360
}
361361
}
362362

363+
function get_headers($url) {
364+
$hosts = get_config "hosts"
365+
if ($hosts) {
366+
$hosts | Where-Object { $url -match $_.match } | ForEach-Object {
367+
return $_.headers
368+
}
369+
}
370+
return {}
371+
}
372+
373+
function get-members($obj) {
374+
$obj | Get-Member -MemberType NoteProperty | ForEach-Object {
375+
$key = $_.Name
376+
[PSCustomObject]@{Key = $key; Value = $obj."$key"}
377+
}
378+
}
379+
363380
# download with filesize and progress indicator
364381
function dl($url, $to, $cookies, $progress) {
365-
$reqUrl = ($url -split "#")[0]
382+
$reqUrl = ($url -split "#")[0]
366383
$wreq = [net.webrequest]::create($reqUrl)
367384
if($wreq -is [net.httpwebrequest]) {
368385
$wreq.useragent = Get-UserAgent
369386
if (-not ($url -imatch "sourceforge\.net" -or $url -imatch "portableapps\.com")) {
370387
$wreq.referer = strip_filename $url
371388
}
389+
if ($url -imatch "api\.github\.com\/repos") {
390+
$wreq.accept = "application/octet-stream"
391+
$wreq.headers["Authorization"] = "token $(get_config 'gh_api_token')"
392+
}
372393
if($cookies) {
373394
$wreq.headers.add('Cookie', (cookie_header $cookies))
374395
}
396+
397+
$customHeaders = get_headers $url
398+
if ($customHeaders) {
399+
get-members $customHeaders | ForEach-Object {
400+
$wreq.headers[$_.Key] = $_.Value
401+
}
402+
}
375403
}
376404

377405
try {

0 commit comments

Comments
 (0)