Forward-port #1006 changes

This commit is contained in:
crazywhalecc
2026-02-05 19:21:13 +08:00
parent 8fc2da9acf
commit 97634b009f
7 changed files with 49 additions and 22 deletions

View File

@@ -21,10 +21,11 @@ class GitHubRelease implements DownloadTypeInterface, ValidatorInterface
private ?string $version = null;
public function getGitHubReleases(string $name, string $repo, bool $prefer_stable = true): array
public function getGitHubReleases(string $name, string $repo, bool $prefer_stable = true, ?string $query = null): array
{
logger()->debug("Fetching {$name} GitHub releases from {$repo}");
$url = str_replace('{repo}', $repo, self::API_URL);
$url .= ($query ?? '');
$headers = $this->getGitHubTokenHeaders();
$data2 = default_shell()->executeCurl($url, headers: $headers);
$data = json_decode($data2 ?: '', true);
@@ -45,9 +46,10 @@ class GitHubRelease implements DownloadTypeInterface, ValidatorInterface
* Get the latest GitHub release assets for a given repository.
* match_asset is provided, only return the asset that matches the regex.
*/
public function getLatestGitHubRelease(string $name, string $repo, bool $prefer_stable, string $match_asset): array
public function getLatestGitHubRelease(string $name, string $repo, bool $prefer_stable, string $match_asset, ?string $query = null): array
{
$url = str_replace('{repo}', $repo, self::API_URL);
$url .= ($query ?? '');
$headers = $this->getGitHubTokenHeaders();
$data2 = default_shell()->executeCurl($url, headers: $headers);
$data = json_decode($data2 ?: '', true);
@@ -81,7 +83,7 @@ class GitHubRelease implements DownloadTypeInterface, ValidatorInterface
if (!isset($config['match'])) {
throw new DownloaderException("GitHubRelease downloader requires 'match' config for {$name}");
}
$rel = $this->getLatestGitHubRelease($name, $config['repo'], $config['prefer-stable'] ?? true, $config['match']);
$rel = $this->getLatestGitHubRelease($name, $config['repo'], $config['prefer-stable'] ?? true, $config['match'], $config['query'] ?? null);
// download file using curl
$asset_url = str_replace(['{repo}', '{id}'], [$config['repo'], $rel['id']], self::ASSET_URL);

View File

@@ -23,9 +23,10 @@ class GitHubTarball implements DownloadTypeInterface
* If match_url is provided, only return the tarball that matches the regex.
* Otherwise, return the first tarball found.
*/
public function getGitHubTarballInfo(string $name, string $repo, string $rel_type, bool $prefer_stable = true, ?string $match_url = null, ?string $basename = null): array
public function getGitHubTarballInfo(string $name, string $repo, string $rel_type, bool $prefer_stable = true, ?string $match_url = null, ?string $basename = null, ?string $query = null): array
{
$url = str_replace(['{repo}', '{rel_type}'], [$repo, $rel_type], self::API_URL);
$url .= ($query ?? '');
$data = default_shell()->executeCurl($url, headers: $this->getGitHubTokenHeaders());
$data = json_decode($data ?: '', true);
if (!is_array($data)) {
@@ -33,7 +34,10 @@ class GitHubTarball implements DownloadTypeInterface
}
$url = null;
foreach ($data as $rel) {
if (($rel['prerelease'] ?? false) === true && $prefer_stable) {
$prerelease = $rel['prerelease'] ?? false;
$draft = $rel['draft'] ?? false;
$tarball_url = $rel['tarball_url'] ?? null;
if ($prerelease && $prefer_stable || $draft && $prefer_stable || !$tarball_url) {
continue;
}
if ($match_url === null) {
@@ -70,7 +74,7 @@ class GitHubTarball implements DownloadTypeInterface
'ghtagtar' => 'tags',
default => throw new DownloaderException("Invalid GitHubTarball type for {$name}"),
};
[$url, $filename] = $this->getGitHubTarballInfo($name, $config['repo'], $rel_type, $config['prefer-stable'] ?? true, $config['match'] ?? null, $name);
[$url, $filename] = $this->getGitHubTarballInfo($name, $config['repo'], $rel_type, $config['prefer-stable'] ?? true, $config['match'] ?? null, $name, $config['query'] ?? null);
$path = DOWNLOAD_PATH . "/{$filename}";
default_shell()->executeCurlDownload($url, $path, headers: $this->getGitHubTokenHeaders());
return DownloadResult::archive($filename, $config, $config['extract'] ?? null, version: $this->version);