Fix fetching versions

We have an issue when previous version of the plugin was not on the first page. This commit adds while loop to avoid this issue.
[MAILPOET-6097]
This commit is contained in:
Jan Lysý
2024-06-26 14:34:51 +02:00
committed by Jan Lysý
parent 808f4cf1f8
commit 4c163d2cd4

View File

@@ -58,8 +58,8 @@ function getMinorMajorVersion(string $version): string {
/** /**
* Function to fetch tags from a GitHub repository. * Function to fetch tags from a GitHub repository.
*/ */
function fetchGitHubTags(string $repo, string $token): array { function fetchGitHubTags(string $repo, string $token, int $page = 1, int $limit = 50): array {
$url = "https://api.github.com/repos/$repo/tags"; $url = "https://api.github.com/repos/$repo/tags?per_page=$limit&page=$page";
$ch = curl_init($url); $ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); // GitHub API requires a user agent curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); // GitHub API requires a user agent
@@ -86,7 +86,7 @@ function fetchGitHubTags(string $repo, string $token): array {
* Function saving versions to a temporary files. * Function saving versions to a temporary files.
* File containing latest version is prefixed with 'latest_' and previous version is prefixed with 'previous_'. * File containing latest version is prefixed with 'latest_' and previous version is prefixed with 'previous_'.
*/ */
function saveVersionsToFiles(string $latestVersion, string $previousVersion, string $fileNameSuffix): void { function saveVersionsToFiles(?string $latestVersion, ?string $previousVersion, string $fileNameSuffix): void {
file_put_contents("/tmp/latest_{$fileNameSuffix}", $latestVersion); file_put_contents("/tmp/latest_{$fileNameSuffix}", $latestVersion);
file_put_contents("/tmp/previous_{$fileNameSuffix}", $previousVersion); file_put_contents("/tmp/previous_{$fileNameSuffix}", $previousVersion);
} }
@@ -124,9 +124,16 @@ function replacePrivatePluginVersion(
die("GitHub token not found. Make sure it's set in the environment variable 'GH_TOKEN'."); die("GitHub token not found. Make sure it's set in the environment variable 'GH_TOKEN'.");
} }
$allVersions = fetchGitHubTags($repository, $token); $page = 1;
$latestVersion = null;
$previousVersion = null;
$allVersions = [];
while (($latestVersion === null || $previousVersion === null) && $page < 10) {
$allVersions = array_merge($allVersions, fetchGitHubTags($repository, $token, $page));
$stableVersions = filterStableVersions($allVersions); $stableVersions = filterStableVersions($allVersions);
[$latestVersion, $previousVersion] = getLatestAndPreviousMinorMajorVersions($stableVersions); [$latestVersion, $previousVersion] = getLatestAndPreviousMinorMajorVersions($stableVersions);
$page++;
}
echo "Latest version: $latestVersion\n"; echo "Latest version: $latestVersion\n";
echo "Previous version: $previousVersion\n"; echo "Previous version: $previousVersion\n";