Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(install): Allow downloading from private repositories #4254

Merged
merged 17 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
- **relpath:** Use `$PSScriptRoot` instead of `relpath` ([#4793](https://github.com/ScoopInstaller/Scoop/issues/4793))
- **reset_aliases:** Move core function of `reset_aliases` to `scoop` ([#4794](https://github.com/ScoopInstaller/Scoop/issues/4794))

### Features

- **install:** Update to allow for dowloading from private repositories ([$4254](https://github.com/ScoopInstaller/Scoop/issues/4243))

## [v0.1.0](https://github.com/ScoopInstaller/Scoop/compare/2021-12-26...v0.1.0) - 2022-03-01

### Features
Expand Down
15 changes: 15 additions & 0 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,21 @@ 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>[^\/#]+)(?<filename>.*)" -and (get_config 'checkver_token')) {
niheaven marked this conversation as resolved.
Show resolved Hide resolved
$headers = @{
"Authorization" = "token $(get_config 'checkver_token')"
niheaven marked this conversation as resolved.
Show resolved Hide resolved
}
$privateUrl = "https://api.github.com/repos/$($matches['owner'])/$($matches['repo'])"
$assetUrl="https://api.github.com/repos/$($matches['owner'])/$($matches['repo'])/releases/tags/$($matches['tag'])"
niheaven marked this conversation as resolved.
Show resolved Hide resolved

$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 + $($matches['filename'])
niheaven marked this conversation as resolved.
Show resolved Hide resolved
}
}

return $url
}

Expand Down
13 changes: 13 additions & 0 deletions lib/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,22 @@ 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") {
niheaven marked this conversation as resolved.
Show resolved Hide resolved
$wreq.accept = "application/octet-stream"
$wreq.headers["Authorization"] = "token $(get_config 'checkver_token')"
}
if($cookies) {
$wreq.headers.add('Cookie', (cookie_header $cookies))
}

$hosts = get_config "hosts"
if ($hosts) {
$hosts | Where-Object { $url -match $_.match } | ForEach-Object {
(ConvertFrom-StringData -StringData $_.headers).GetEnumerator() | ForEach-Object {
$wreq.headers[$_.Key] = $_.Value
}
}
}
}

try {
Expand Down