Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- **core:** Check `$deprecated_dir` exists before accessing it ([#6574](https://github.com/ScoopInstaller/Scoop/issues/6574))
- **checkver:** Remove redundant always-true condition in GitHub checkver logic ([#6571](https://github.com/ScoopInstaller/Scoop/issues/6571))
- **core:** Give `dark` higher priority when use `Extract-DarkArchive` ([#6637](https://github.com/ScoopInstaller/Scoop/issues/6637))
- **checkver:** Harden github checkver ([#6641](https://github.com/ScoopInstaller/Scoop/issues/6641))

### Code Refactoring

Expand Down
64 changes: 41 additions & 23 deletions bin/checkver.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ Get-Event | Remove-Event
Get-EventSubscriber | Unregister-Event

# start all downloads
$in_progress = 0
$Queue | ForEach-Object {
$name, $json, $file = $_

Expand All @@ -119,7 +120,6 @@ $Queue | ForEach-Object {
} else {
$wc.Headers.Add('User-Agent', (Get-UserAgent))
}
Register-ObjectEvent $wc downloadDataCompleted -ErrorAction Stop | Out-Null

# Not Specified
if ($json.checkver.url) {
Expand All @@ -141,25 +141,45 @@ $Queue | ForEach-Object {
$replace = ''
$useGithubAPI = $false

# GitHub
if ($regex) {
$githubRegex = $regex
} else {
$githubRegex = '/releases/tag/(?:v|V)?([\d.]+)'
}
if ($json.checkver -eq 'github') {
if (!$json.homepage.StartsWith('https://github.com/')) {
error "$name checkver expects the homepage to be a github repository"
## GitHub
#
# ```json
# "homepage": "<valid-repository-url>",
# "checkver": "github"
# ```
#
# or
#
# ```json
# "checkver": {
# "github": "<valid-repository-url-or-repository-api-url>"
# }
# ```
if (($json.checkver -eq 'github') -or $json.checkver.github) {
$githubUrlPattern = '^https://((www\.)?github\.com/[\w.-]+/[\w.-]+/?|api\.github\.com/repos/[\w.-]+/[\w.-]+(/.*)?)$'
$regex = if ($regex) { $regex } else { '/releases/tag/(?:v|V)?([\d.]+)' }

$inputGithubUrl = $json.homepage
$fieldUsed = 'homepage'
if ($json.checkver.github) {
$inputGithubUrl = $json.checkver.github
$fieldUsed = 'checkver.github'
}

if ($inputGithubUrl -notmatch $githubUrlPattern) {
error "$name checkver expects $fieldUsed to be a valid GitHub repository URL"
return
}

$url = $inputGithubUrl.TrimEnd('/')
if ($url -notlike 'https://api.github.com*') {
$url = $url + '/releases/latest'
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Comment thread
chawyehsu marked this conversation as resolved.
Outdated
$url = $json.homepage.TrimEnd('/') + '/releases/latest'
$regex = $githubRegex
$useGithubAPI = $true
}

if ($json.checkver.github) {
$url = $json.checkver.github.TrimEnd('/') + '/releases/latest'
$regex = $githubRegex
$useGithubAPI = $true
if ($GitHubToken) {
$url = $url -replace '//(www\.)?github.com/', '//api.github.com/repos/'
$useGithubAPI = $true
}
Comment thread
chawyehsu marked this conversation as resolved.
}

# SourceForge
Expand Down Expand Up @@ -216,10 +236,7 @@ $Queue | ForEach-Object {

$reverse = $json.checkver.reverse -and $json.checkver.reverse -eq 'true'

if ($url -like '*api.github.com/*') { $useGithubAPI = $true }

if ($useGithubAPI -and ($null -ne $GitHubToken)) {
$url = $url -replace '//(www\.)?github.com/', '//api.github.com/repos/'
if ($useGithubAPI) {
$wc.Headers.Add('Authorization', "token $GitHubToken")
}

Expand All @@ -244,6 +261,8 @@ $Queue | ForEach-Object {
}

$wc.Headers.Add('Referer', (strip_filename $url))
Register-ObjectEvent $wc downloadDataCompleted -ErrorAction Stop | Out-Null
$in_progress++
$wc.DownloadDataAsync($url, $state)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

Expand All @@ -253,7 +272,6 @@ function next($er) {
}

# wait for all to complete
$in_progress = $Queue.length
while ($in_progress -gt 0) {
$ev = Wait-Event
Remove-Event $ev.SourceIdentifier
Expand Down
Loading