Add script updating used WC Memberships

[MAILPOET-6097]
This commit is contained in:
Jan Lysý
2024-06-17 19:31:04 +02:00
committed by Jan Lysý
parent aed63c7cd3
commit 57ebc876ab
2 changed files with 64 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
<?php
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';
$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";
echo "Replacing the latest version in the config file...\n";
replaceLatestVersion($latestVersion);
echo "Replacing the previous version in the config file...\n";
replacePreviousVersion($previousVersion);

View File

@@ -45,3 +45,27 @@ function getMinorMajorVersion($version) {
$parts = explode('.', $version); $parts = explode('.', $version);
return $parts[0] . '.' . $parts[1]; return $parts[0] . '.' . $parts[1];
} }
function fetchGitHubTags($repo, $token) {
$url = "https://api.github.com/repos/$repo/tags";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); // GitHub API requires a user agent
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: token $token"
]);
$response = curl_exec($ch);
curl_close($ch);
if ($response === false) {
die("Failed to fetch tags from GitHub.");
}
$data = json_decode($response, true);
if (isset($data['message']) && $data['message'] == 'Not Found') {
die("Repository not found or access denied.");
}
return array_column($data, 'name');
}