Skip to content

Commit

Permalink
added ability to use private github repositories
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tmsmith committed Feb 10, 2021
1 parent 227de6c commit e7aa703
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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\/(?<owner>[^\/]+)\/(?<repo>[^\/]+)\/releases\/download\/(?<tag>[^\/]+)\/(?<file>.*)" -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
}

Expand Down
28 changes: 28 additions & 0 deletions lib/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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 {
Expand Down

0 comments on commit e7aa703

Please sign in to comment.