mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-07-08 01:15:37 +08:00
Improve GitHub source retrieval by using latest release endpoint and enhancing version handling
This commit is contained in:
@@ -21,49 +21,59 @@ class GitHubTarball implements DownloadTypeInterface, CheckUpdateInterface
|
|||||||
/**
|
/**
|
||||||
* Get the GitHub tarball URL for a given repository and release type.
|
* Get the GitHub tarball URL for a given repository and release type.
|
||||||
* If match_url is provided, only return the tarball that matches the regex.
|
* 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, ?string $query = 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);
|
if ($rel_type === 'releases' && $match_url === null && $query === null && $prefer_stable) {
|
||||||
$url .= ($query ?? '');
|
$api_url = str_replace(['{repo}', '{rel_type}'], [$repo, 'releases/latest'], self::API_URL);
|
||||||
$data = default_shell()->executeCurl($url, headers: $this->getGitHubTokenHeaders());
|
$data = default_shell()->executeCurl($api_url, headers: $this->getGitHubTokenHeaders());
|
||||||
$data = json_decode($data ?: '', true);
|
$data = json_decode($data ?: '', true);
|
||||||
if (!is_array($data)) {
|
if (!is_array($data) || empty($data['tarball_url'])) {
|
||||||
throw new DownloaderException("Failed to get GitHub tarball URL for {$repo} from {$url}");
|
throw new DownloaderException("Failed to get GitHub latest release for {$repo} from {$api_url}");
|
||||||
}
|
|
||||||
$url = null;
|
|
||||||
foreach ($data as $rel) {
|
|
||||||
$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) {
|
$rel_url = $data['tarball_url'];
|
||||||
$url = $rel['tarball_url'] ?? null;
|
$this->version = $data['tag_name'] ?? $data['name'] ?? null;
|
||||||
$version = $rel['tag_name'] ?? $rel['name'] ?? null;
|
} else {
|
||||||
break;
|
$api_url = str_replace(['{repo}', '{rel_type}'], [$repo, $rel_type], self::API_URL);
|
||||||
|
$api_url .= ($query ?? '');
|
||||||
|
$data = default_shell()->executeCurl($api_url, headers: $this->getGitHubTokenHeaders());
|
||||||
|
$data = json_decode($data ?: '', true);
|
||||||
|
if (!is_array($data)) {
|
||||||
|
throw new DownloaderException("Failed to get GitHub tarball URL for {$repo} from {$api_url}");
|
||||||
}
|
}
|
||||||
if (preg_match("|{$match_url}|", $rel['tarball_url'] ?? '')) {
|
$rel_url = null;
|
||||||
$url = $rel['tarball_url'];
|
foreach ($data as $rel) {
|
||||||
$version = $rel['tag_name'] ?? $rel['name'] ?? null;
|
$prerelease = $rel['prerelease'] ?? false;
|
||||||
break;
|
$draft = $rel['draft'] ?? false;
|
||||||
|
$tarball_url = $rel['tarball_url'] ?? null;
|
||||||
|
if ($prerelease && $prefer_stable || $draft && $prefer_stable || !$tarball_url) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ($match_url === null) {
|
||||||
|
$rel_url = $rel['tarball_url'] ?? null;
|
||||||
|
$version = $rel['tag_name'] ?? $rel['name'] ?? null;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (preg_match("|{$match_url}|", $rel['tarball_url'] ?? '')) {
|
||||||
|
$rel_url = $rel['tarball_url'];
|
||||||
|
$version = $rel['tag_name'] ?? $rel['name'] ?? null;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if (!$rel_url) {
|
||||||
|
throw new DownloaderException("No suitable GitHub tarball found for {$repo}");
|
||||||
|
}
|
||||||
|
$this->version = $version ?? null;
|
||||||
}
|
}
|
||||||
if (!$url) {
|
$head = default_shell()->executeCurl($rel_url, 'HEAD', headers: $this->getGitHubTokenHeaders()) ?: '';
|
||||||
throw new DownloaderException("No suitable GitHub tarball found for {$repo}");
|
|
||||||
}
|
|
||||||
$this->version = $version ?? null;
|
|
||||||
$head = default_shell()->executeCurl($url, 'HEAD', headers: $this->getGitHubTokenHeaders()) ?: '';
|
|
||||||
preg_match('/^content-disposition:\s+attachment;\s*filename=("?)(?<filename>.+\.tar\.gz)\1/im', $head, $matches);
|
preg_match('/^content-disposition:\s+attachment;\s*filename=("?)(?<filename>.+\.tar\.gz)\1/im', $head, $matches);
|
||||||
if ($matches) {
|
if ($matches) {
|
||||||
$filename = $matches['filename'];
|
$filename = $matches['filename'];
|
||||||
} else {
|
} else {
|
||||||
$basename = $basename ?? basename($repo);
|
$basename = $basename ?? basename($repo);
|
||||||
$filename = "{$basename}-" . ($rel_type === 'releases' ? ($data['tag_name'] ?? $data['name']) : $data['name']) . '.tar.gz';
|
$filename = "{$basename}-" . ($this->version ?? 'latest') . '.tar.gz';
|
||||||
}
|
}
|
||||||
return [$url, $filename];
|
return [$rel_url, $filename];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function download(string $name, array $config, ArtifactDownloader $downloader): DownloadResult
|
public function download(string $name, array $config, ArtifactDownloader $downloader): DownloadResult
|
||||||
|
|||||||
Reference in New Issue
Block a user