Add type declarations to function in GH Action update scripts

This commit also fixes some bugs with function redeclaration and missing parameters.
[MAILPOET-6097]
This commit is contained in:
Jan Lysý
2024-06-21 12:14:36 +02:00
committed by Rostislav Wolný
parent 3989594a56
commit 0d794b3b2f
4 changed files with 32 additions and 40 deletions

View File

@@ -7,4 +7,4 @@ $downloadCommand = 'download:woo-commerce-memberships-zip';
$configParameterName = 'woo_memberships_version'; $configParameterName = 'woo_memberships_version';
$versionsFilename = 'woocommerce_memberships_versions.txt'; $versionsFilename = 'woocommerce_memberships_versions.txt';
replacePrivatePluginVersion($repository, $downloadCommand, $configParameterName); replacePrivatePluginVersion($repository, $downloadCommand, $configParameterName, $versionsFilename);

View File

@@ -2,6 +2,10 @@
require_once __DIR__ . '/helpers.php'; require_once __DIR__ . '/helpers.php';
$downloadCommand = 'download:woo-commerce-zip';
$configParameterName = 'woo_core_version';
$versionsFilename = 'woocommerce_versions.txt';
/** /**
* We get the official WooCommerce versions from the WordPress API. * We get the official WooCommerce versions from the WordPress API.
*/ */
@@ -17,22 +21,6 @@ function getWooCommerceVersions(): array {
return array_keys($data['versions']); return array_keys($data['versions']);
} }
function replaceLatestVersion($latestVersion): void {
replaceVersionInFile(
__DIR__ . './../../../.circleci/config.yml',
'/(.\/do download:woo-commerce-zip )\d+\.\d+\.\d+/',
'${1}' . $latestVersion
);
}
function replacePreviousVersion($previousVersion): void {
replaceVersionInFile(
__DIR__ . './../../../.circleci/config.yml',
'/(woo_core_version: )\d+\.\d+\.?\d*/',
'${1}' . $previousVersion
);
}
$allVersions = getWooCommerceVersions(); $allVersions = getWooCommerceVersions();
$stableVersions = filterStableVersions($allVersions); $stableVersions = filterStableVersions($allVersions);
[$latestVersion, $previousVersion] = getLatestAndPreviousMinorMajorVersions($stableVersions); [$latestVersion, $previousVersion] = getLatestAndPreviousMinorMajorVersions($stableVersions);
@@ -42,16 +30,16 @@ echo "Previous WooCommerce version: $previousVersion\n";
if ($latestVersion) { if ($latestVersion) {
echo "Replacing the latest version in the config file...\n"; echo "Replacing the latest version in the config file...\n";
replaceLatestVersion($latestVersion); replaceLatestVersion($latestVersion, $downloadCommand);
} else { } else {
echo "No latest version found.\n"; echo "No latest version found.\n";
} }
if ($previousVersion) { if ($previousVersion) {
echo "Replacing the previous version in the config file...\n"; echo "Replacing the previous version in the config file...\n";
replacePreviousVersion($previousVersion); replacePreviousVersion($previousVersion, $configParameterName);
} else { } else {
echo "No previous version found.\n"; echo "No previous version found.\n";
} }
saveVersionsToFile($latestVersion, $previousVersion, 'woocommerce_versions.txt'); saveVersionsToFile($latestVersion, $previousVersion, $versionsFilename);

View File

@@ -5,7 +5,7 @@ require_once __DIR__ . '/helpers.php';
/** /**
* We try to get the current available official Docker images for WordPress. * We try to get the current available official Docker images for WordPress.
*/ */
function getWordpressVersions($page = 1, $pageSize = 100): array { function getWordpressVersions(int $page = 1, int $pageSize = 100): array {
$url = "https://registry.hub.docker.com/v2/repositories/library/wordpress/tags?page_size={$pageSize}&page={$page}"; $url = "https://registry.hub.docker.com/v2/repositories/library/wordpress/tags?page_size={$pageSize}&page={$page}";
$response = file_get_contents($url); $response = file_get_contents($url);
$data = json_decode($response, true); $data = json_decode($response, true);
@@ -16,7 +16,7 @@ function getWordpressVersions($page = 1, $pageSize = 100): array {
* We prefer the latest patch versions of WordPress with specified PHP versions. * We prefer the latest patch versions of WordPress with specified PHP versions.
* For example: 6.5.4-php8.3 * For example: 6.5.4-php8.3
*/ */
function filterVersions($versions): array { function filterVersions(array $versions): array {
return array_filter($versions, fn($version) => preg_match('/^\d+\.\d+\.\d+-php\d+\.\d+$/', $version)); return array_filter($versions, fn($version) => preg_match('/^\d+\.\d+\.\d+-php\d+\.\d+$/', $version));
} }
@@ -42,7 +42,7 @@ function sortVersions(&$versions) {
* This function group docker tags by the WordPress version and returns the latest with the higher PHP version * This function group docker tags by the WordPress version and returns the latest with the higher PHP version
* abd the previous with the lower PHP version. * abd the previous with the lower PHP version.
*/ */
function getLatestAndPreviousVersions($sortedVersions): array { function getLatestAndPreviousVersions(array $sortedVersions): array {
$uniqueVersions = []; $uniqueVersions = [];
foreach ($sortedVersions as $version) { foreach ($sortedVersions as $version) {
[$wpVersion] = explode('-php', $version); [$wpVersion] = explode('-php', $version);
@@ -54,17 +54,16 @@ function getLatestAndPreviousVersions($sortedVersions): array {
$latestVersionGroup = reset($uniqueVersions); $latestVersionGroup = reset($uniqueVersions);
$previousVersionGroup = next($uniqueVersions); $previousVersionGroup = next($uniqueVersions);
if ($previousVersionGroup === false) { $latestVersion = $latestVersionGroup === false ? null : reset($latestVersionGroup);
return [$latestVersionGroup[0], null]; $previousVersion = $previousVersionGroup === false ? null : end($previousVersionGroup);
}
return [reset($latestVersionGroup), end($previousVersionGroup)]; return [$latestVersion, $previousVersion];
} }
/** /**
* We specify the latest WordPress version only in the docker-compose file for the tests. * We specify the latest WordPress version only in the docker-compose file for the tests.
*/ */
function replaceLatestVersion($latestVersion): void { function replaceLatestWordPressVersion(string $latestVersion): void {
replaceVersionInFile( replaceVersionInFile(
__DIR__ . './../../../mailpoet/tests/docker/docker-compose.yml', __DIR__ . './../../../mailpoet/tests/docker/docker-compose.yml',
'/(wordpress:\${WORDPRESS_IMAGE_VERSION:-\s*)\d+\.\d+\.?\d*-php\d+\.\d+(})/', '/(wordpress:\${WORDPRESS_IMAGE_VERSION:-\s*)\d+\.\d+\.?\d*-php\d+\.\d+(})/',
@@ -75,7 +74,7 @@ function replaceLatestVersion($latestVersion): void {
/** /**
* We use the previous WordPress version only in the CircleCI config file. * We use the previous WordPress version only in the CircleCI config file.
*/ */
function replacePreviousVersion($previousVersion): void { function replacePreviousWordPressVersion(string $previousVersion): void {
replaceVersionInFile( replaceVersionInFile(
__DIR__ . './../../../.circleci/config.yml', __DIR__ . './../../../.circleci/config.yml',
'/(wordpress_image_version: )\d+\.\d+\.?\d*-php\d+\.\d+/', '/(wordpress_image_version: )\d+\.\d+\.?\d*-php\d+\.\d+/',
@@ -106,14 +105,14 @@ echo "Previous version: $previousVersion\n";
if ($latestVersion) { if ($latestVersion) {
echo "Replacing the latest version in the docker file...\n"; echo "Replacing the latest version in the docker file...\n";
replaceLatestVersion($latestVersion); replaceLatestWordPressVersion($latestVersion);
} else { } else {
echo "No latest version found.\n"; echo "No latest version found.\n";
} }
if ($previousVersion) { if ($previousVersion) {
echo "Replacing the previous version in the config file...\n"; echo "Replacing the previous version in the config file...\n";
replacePreviousVersion($previousVersion); replacePreviousWordPressVersion($previousVersion);
} else { } else {
echo "No previous version found.\n"; echo "No previous version found.\n";
} }

View File

@@ -3,7 +3,7 @@
/** /**
* Function replacing versions in a file by the regex pattern. * Function replacing versions in a file by the regex pattern.
*/ */
function replaceVersionInFile($filePath, $pattern, $replacement): void { function replaceVersionInFile(string $filePath, string $pattern, string $replacement): void {
$content = file_get_contents($filePath); $content = file_get_contents($filePath);
if ($content === false) { if ($content === false) {
@@ -25,7 +25,7 @@ function replaceVersionInFile($filePath, $pattern, $replacement): void {
/** /**
* Function to filter stable versions from a list of versions. * Function to filter stable versions from a list of versions.
*/ */
function filterStableVersions($versions): array { function filterStableVersions(array $versions): array {
return array_filter($versions, function($version) { return array_filter($versions, function($version) {
// Only include stable versions (exclude versions with -rc, -beta, -alpha, etc.) // Only include stable versions (exclude versions with -rc, -beta, -alpha, etc.)
return preg_match('/^\d+\.\d+\.\d+$/', $version); return preg_match('/^\d+\.\d+\.\d+$/', $version);
@@ -35,7 +35,7 @@ function filterStableVersions($versions): array {
/** /**
* Function to get the latest and previous minor/major versions from a list of versions. * Function to get the latest and previous minor/major versions from a list of versions.
*/ */
function getLatestAndPreviousMinorMajorVersions($versions): array { function getLatestAndPreviousMinorMajorVersions(array $versions): array {
usort($versions, 'version_compare'); usort($versions, 'version_compare');
$currentVersion = end($versions); $currentVersion = end($versions);
@@ -50,7 +50,7 @@ function getLatestAndPreviousMinorMajorVersions($versions): array {
return [$currentVersion, $previousVersion]; return [$currentVersion, $previousVersion];
} }
function getMinorMajorVersion($version): string { function getMinorMajorVersion(string $version): string {
$parts = explode('.', $version); $parts = explode('.', $version);
return $parts[0] . '.' . $parts[1]; return $parts[0] . '.' . $parts[1];
} }
@@ -58,7 +58,7 @@ function getMinorMajorVersion($version): string {
/** /**
* Function to fetch tags from a GitHub repository. * Function to fetch tags from a GitHub repository.
*/ */
function fetchGitHubTags($repo, $token): array { function fetchGitHubTags(string $repo, string $token): array {
$url = "https://api.github.com/repos/$repo/tags"; $url = "https://api.github.com/repos/$repo/tags";
$ch = curl_init($url); $ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@@ -85,7 +85,7 @@ function fetchGitHubTags($repo, $token): array {
/** /**
* Function saving versions to a temporary file. * Function saving versions to a temporary file.
*/ */
function saveVersionsToFile($latestVersion, $previousVersion, $fileName): void { function saveVersionsToFile(string $latestVersion, string $previousVersion, string $fileName): void {
$value = ""; $value = "";
if ($latestVersion) { if ($latestVersion) {
$value .= "- latest version: {$latestVersion}\n"; $value .= "- latest version: {$latestVersion}\n";
@@ -96,7 +96,7 @@ function saveVersionsToFile($latestVersion, $previousVersion, $fileName): void {
file_put_contents("/tmp/{$fileName}", $value); file_put_contents("/tmp/{$fileName}", $value);
} }
function replaceLatestVersion($latestVersion, $downloadCommand): void { function replaceLatestVersion(string $latestVersion, string $downloadCommand): void {
replaceVersionInFile( replaceVersionInFile(
__DIR__ . '/../../../.circleci/config.yml', __DIR__ . '/../../../.circleci/config.yml',
'/(.\/do ' . $downloadCommand . ' )\d+\.\d+\.\d+/', '/(.\/do ' . $downloadCommand . ' )\d+\.\d+\.\d+/',
@@ -104,7 +104,7 @@ function replaceLatestVersion($latestVersion, $downloadCommand): void {
); );
} }
function replacePreviousVersion($previousVersion, $configParameterName): void { function replacePreviousVersion(string $previousVersion, string $configParameterName): void {
replaceVersionInFile( replaceVersionInFile(
__DIR__ . '/../../../.circleci/config.yml', __DIR__ . '/../../../.circleci/config.yml',
'/(' . $configParameterName . ': )\d+\.\d+\.\d+/', '/(' . $configParameterName . ': )\d+\.\d+\.\d+/',
@@ -117,7 +117,12 @@ function replacePreviousVersion($previousVersion, $configParameterName): void {
* The function fetches the tags from the GitHub repository, filters stable versions, * The function fetches the tags from the GitHub repository, filters stable versions,
* gets the latest and previous minor/major versions, and replaces the versions in the CircleCI config file. * gets the latest and previous minor/major versions, and replaces the versions in the CircleCI config file.
*/ */
function replacePrivatePluginVersion($repository, $downloadCommand, $configParameterName, $versionsFilename): void { function replacePrivatePluginVersion(
string $repository,
string $downloadCommand,
string $configParameterName,
string $versionsFilename
): void {
// Read the GitHub token from environment variable // Read the GitHub token from environment variable
$token = getenv('GH_TOKEN'); $token = getenv('GH_TOKEN');
if (!$token) { if (!$token) {