Extract replacing versions for a private plugin to a function

[MAILPOET-6097]
This commit is contained in:
Jan Lysý
2024-06-19 20:56:09 +02:00
committed by Rostislav Wolný
parent 3898b3dccb
commit 18dc3daa78
4 changed files with 84 additions and 134 deletions

View File

@ -2,48 +2,9 @@
require_once __DIR__ . '/helpers.php';
// Read the GitHub token from environment variable
$token = getenv('GH_TOKEN');
if (!$token) {
die("GitHub token not found. Make sure it's set in the environment variable 'GH_TOKEN'.");
}
function replaceLatestVersion($previousVersion) {
replaceVersionInFile(
__DIR__ . './../../../.circleci/config.yml',
'/(.\/do download:automate-woo-zip )\d+\.\d+\.\d+/',
'${1}' . $previousVersion
);
}
function replacePreviousVersion($previousVersion) {
replaceVersionInFile(
__DIR__ . './../../../.circleci/config.yml',
'/(automate_woo_version: )\d+\.\d+\.\d+/',
'${1}' . $previousVersion
);
}
$repository = 'woocommerce/automatewoo';
$downloadCommand = 'download:automate-woo-zip';
$configParameterName = 'automate_woo_version';
$versionsFilename = 'automate_woo_versions.txt';
$allVersions = fetchGitHubTags($repository, $token);
$stableVersions = filterStableVersions($allVersions);
[$latestVersion, $previousVersion] = getLatestAndPreviousMinorMajorVersions($stableVersions);
echo "Latest Automate Woo version: $latestVersion\n";
echo "Previous Automate Woo version: $previousVersion\n";
if ($latestVersion) {
echo "Replacing the latest version in the config file...\n";
replaceLatestVersion($latestVersion);
} else {
echo "No latest version found.\n";
}
if ($previousVersion) {
echo "Replacing the previous version in the config file...\n";
replacePreviousVersion($previousVersion);
} else {
echo "No previous version found.\n";
}
replacePrivatePluginVersion($repository, $downloadCommand, $configParameterName, $versionsFilename);

View File

@ -2,48 +2,9 @@
require_once __DIR__ . '/helpers.php';
// Read the GitHub token from environment variable
$token = getenv('GH_TOKEN');
if (!$token) {
die("GitHub token not found. Make sure it's set in the environment variable 'GH_TOKEN'.");
}
function replaceLatestVersion($previousVersion) {
replaceVersionInFile(
__DIR__ . './../../../.circleci/config.yml',
'/(.\/do download:woo-commerce-memberships-zip )\d+\.\d+\.\d+/',
'${1}' . $previousVersion
);
}
function replacePreviousVersion($previousVersion) {
replaceVersionInFile(
__DIR__ . './../../../.circleci/config.yml',
'/(woo_memberships_version: )\d+\.\d+\.\d+/',
'${1}' . $previousVersion
);
}
$repository = 'woocommerce/woocommerce-memberships';
$downloadCommand = 'download:woo-commerce-memberships-zip';
$configParameterName = 'woo_memberships_version';
$versionsFilename = 'woocommerce_memberships_versions.txt';
$allVersions = fetchGitHubTags($repository, $token);
$stableVersions = filterStableVersions($allVersions);
[$latestVersion, $previousVersion] = getLatestAndPreviousMinorMajorVersions($stableVersions);
echo "Latest WooCommerce Memberships version: $latestVersion\n";
echo "Previous WooCommerce Memberships version: $previousVersion\n";
if ($latestVersion) {
echo "Replacing the latest version in the config file...\n";
replaceLatestVersion($latestVersion);
} else {
echo "No latest version found.\n";
}
if ($previousVersion) {
echo "Replacing the previous version in the config file...\n";
replacePreviousVersion($previousVersion);
} else {
echo "No previous version found.\n";
}
replacePrivatePluginVersion($repository, $downloadCommand, $configParameterName);

View File

@ -2,48 +2,9 @@
require_once __DIR__ . '/helpers.php';
// Read the GitHub token from environment variable
$token = getenv('GH_TOKEN');
if (!$token) {
die("GitHub token not found. Make sure it's set in the environment variable 'GH_TOKEN'.");
}
function replaceLatestVersion($previousVersion) {
replaceVersionInFile(
__DIR__ . './../../../.circleci/config.yml',
'/(.\/do download:woo-commerce-subscriptions-zip )\d+\.\d+\.\d+/',
'${1}' . $previousVersion
);
}
function replacePreviousVersion($previousVersion) {
replaceVersionInFile(
__DIR__ . './../../../.circleci/config.yml',
'/(woo_subscriptions_version: )\d+\.\d+\.\d+/',
'${1}' . $previousVersion
);
}
$repository = 'woocommerce/woocommerce-subscriptions';
$downloadCommand = 'download:woo-commerce-subscriptions-zip';
$configParameterName = 'woo_subscriptions_version';
$versionsFilename = 'woocommerce_subscriptions_versions.txt';
$allVersions = fetchGitHubTags($repository, $token);
$stableVersions = filterStableVersions($allVersions);
[$latestVersion, $previousVersion] = getLatestAndPreviousMinorMajorVersions($stableVersions);
echo "Latest WooCommerce Subscriptions version: $latestVersion\n";
echo "Previous WooCommerce Subscriptions version: $previousVersion\n";
if ($latestVersion) {
echo "Replacing the latest version in the config file...\n";
replaceLatestVersion($latestVersion);
} else {
echo "No latest version found.\n";
}
if ($previousVersion) {
echo "Replacing the previous version in the config file...\n";
replacePreviousVersion($previousVersion);
} else {
echo "No previous version found.\n";
}
replacePrivatePluginVersion($repository, $downloadCommand, $configParameterName, $versionsFilename);

View File

@ -1,6 +1,9 @@
<?php
function replaceVersionInFile($filePath, $pattern, $replacement) {
/**
* Function replacing versions in a file by the regex pattern.
*/
function replaceVersionInFile($filePath, $pattern, $replacement): void {
$content = file_get_contents($filePath);
if ($content === false) {
@ -19,14 +22,20 @@ function replaceVersionInFile($filePath, $pattern, $replacement) {
}
}
function filterStableVersions($versions) {
/**
* Function to filter stable versions from a list of versions.
*/
function filterStableVersions($versions): array {
return array_filter($versions, function($version) {
// Only include stable versions (exclude versions with -rc, -beta, -alpha, etc.)
return preg_match('/^\d+\.\d+\.\d+$/', $version);
});
}
function getLatestAndPreviousMinorMajorVersions($versions) {
/**
* Function to get the latest and previous minor/major versions from a list of versions.
*/
function getLatestAndPreviousMinorMajorVersions($versions): array {
usort($versions, 'version_compare');
$currentVersion = end($versions);
@ -41,12 +50,15 @@ function getLatestAndPreviousMinorMajorVersions($versions) {
return [$currentVersion, $previousVersion];
}
function getMinorMajorVersion($version) {
function getMinorMajorVersion($version): string {
$parts = explode('.', $version);
return $parts[0] . '.' . $parts[1];
}
function fetchGitHubTags($repo, $token) {
/**
* Function to fetch tags from a GitHub repository.
*/
function fetchGitHubTags($repo, $token): array {
$url = "https://api.github.com/repos/$repo/tags";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@ -70,6 +82,9 @@ function fetchGitHubTags($repo, $token) {
return array_column($data, 'name');
}
/**
* Function saving versions to a temporary file.
*/
function saveVersionsToFile($latestVersion, $previousVersion, $fileName): void {
$value = "";
if ($latestVersion) {
@ -80,3 +95,55 @@ function saveVersionsToFile($latestVersion, $previousVersion, $fileName): void {
}
file_put_contents("/tmp/{$fileName}", $value);
}
function replaceLatestVersion($latestVersion, $downloadCommand): void {
replaceVersionInFile(
__DIR__ . '/../../../.circleci/config.yml',
'/(.\/do ' . $downloadCommand . ' )\d+\.\d+\.\d+/',
'${1}' . $latestVersion
);
}
function replacePreviousVersion($previousVersion, $configParameterName): void {
replaceVersionInFile(
__DIR__ . '/../../../.circleci/config.yml',
'/(' . $configParameterName . ': )\d+\.\d+\.\d+/',
'${1}' . $previousVersion
);
}
/**
* Function replacing the latest and previous versions of a private plugin in the config file.
* 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.
*/
function replacePrivatePluginVersion($repository, $downloadCommand, $configParameterName, $versionsFilename): void {
// Read the GitHub token from environment variable
$token = getenv('GH_TOKEN');
if (!$token) {
die("GitHub token not found. Make sure it's set in the environment variable 'GH_TOKEN'.");
}
$allVersions = fetchGitHubTags($repository, $token);
$stableVersions = filterStableVersions($allVersions);
[$latestVersion, $previousVersion] = getLatestAndPreviousMinorMajorVersions($stableVersions);
echo "Latest version: $latestVersion\n";
echo "Previous version: $previousVersion\n";
if ($latestVersion) {
echo "Replacing the latest version in the config file...\n";
replaceLatestVersion($latestVersion, $downloadCommand);
} else {
echo "No latest version found.\n";
}
if ($previousVersion) {
echo "Replacing the previous version in the config file...\n";
replacePreviousVersion($previousVersion, $configParameterName);
} else {
echo "No previous version found.\n";
}
saveVersionsToFile($latestVersion, $previousVersion, $versionsFilename);
}