Skip to content

Commit

Permalink
RCAT-376 : push branch changed wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mayur-sose committed Dec 11, 2024
1 parent a171bb6 commit 33dc828
Showing 1 changed file with 79 additions and 79 deletions.
158 changes: 79 additions & 79 deletions .github/update_packages.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,33 @@
* @param string $packageName
* The name of the package.
*
* @return string|NULL
* @return string|null
* The latest version string, or NULL if not found.
*/
function get_latest_version($packageName) {
$client = new Client();
$url = "https://repo.packagist.org/p/{$packageName}.json";
$client = new Client();
$url = "https://repo.packagist.org/p/{$packageName}.json";

try {
$response = $client->get($url);
$data = json_decode($response->getBody(), TRUE);
try {
$response = $client->get($url);
$data = json_decode($response->getBody(), TRUE);

$versions = array_keys($data['packages'][$packageName]);
usort($versions, 'version_compare');
$versions = array_keys($data['packages'][$packageName]);
usort($versions, 'version_compare');

$latestVersion = end($versions);
$latestVersion = end($versions);

// Extract major.minor version (e.g., "13.3.3" becomes "13.x")
$versionParts = explode('.', $latestVersion);
if (count($versionParts) > 1) {
return $versionParts[0] . '.x';
}

return NULL;
} catch (RequestException $e) {
// echo "Package {$packageName} not found on Packagist. Trying Drupal.org...\n";
return get_latest_version_from_drupal_org($packageName);
// Extract major.minor version (e.g., "13.3.3" becomes "13.x")
$versionParts = explode('.', $latestVersion);
if (count($versionParts) > 1) {
return $versionParts[0] . '.x';
}

return NULL;
} catch (RequestException $e) {
echo "Package {$packageName} not found on Packagist. Trying Drupal.org...\n";
return get_latest_version_from_drupal_org($packageName);
}
}

/**
Expand All @@ -52,50 +52,50 @@ function get_latest_version($packageName) {
* @param string $packageName
* The name of the package.
*
* @return string|NULL
* @return string|null
* The latest version string, or NULL if not found.
*/
function get_latest_version_from_drupal_org($packageName) {
$client = new Client();
$packageName = str_replace('drupal/', '', $packageName); // Remove "drupal/" prefix
$drupalApiUrl = "https://www.drupal.org/api-d7/node.json?field_project_machine_name={$packageName}";
$client = new Client();
$packageName = str_replace('drupal/', '', $packageName); // Remove "drupal/" prefix
$drupalApiUrl = "https://www.drupal.org/api-d7/node.json?field_project_machine_name={$packageName}";

try {
$response = $client->get($drupalApiUrl);
$data = json_decode($response->getBody(), TRUE);

if (!empty($data['list']) && isset($data['list'][0]['field_release_version'])) {
return $data['list'][0]['field_release_version'];
}
try {
$response = $client->get($drupalApiUrl);
$data = json_decode($response->getBody(), TRUE);

echo "No releases found for {$packageName} on Drupal.org.\n";
return NULL;
} catch (RequestException $e) {
echo "Error fetching data for {$packageName} on Drupal.org: " . $e->getMessage() . PHP_EOL;
return NULL;
if (!empty($data['list']) && isset($data['list'][0]['field_release_version'])) {
return $data['list'][0]['field_release_version'];
}

echo "No releases found for {$packageName} on Drupal.org.\n";
return NULL;
} catch (RequestException $e) {
echo "Error fetching data for {$packageName} on Drupal.org: " . $e->getMessage() . PHP_EOL;
return NULL;
}
}

/**
* Determines if the latest version is a major update compared to the current version.
*
* @param string|NULL $currentVersion
* @param string|null $currentVersion
* The current version.
* @param string|NULL $latestVersion
* @param string|null $latestVersion
* The latest version.
*
* @return bool
* TRUE if it is a major update, FALSE otherwise.
*/
function is_major_update($currentVersion, $latestVersion) {
if (!$currentVersion || !$latestVersion) {
return FALSE;
}
if (!$currentVersion || !$latestVersion) {
return FALSE;
}

$currentMajor = explode('.', $currentVersion)[0];
$latestMajor = explode('.', $latestVersion)[0];
$currentMajor = explode('.', $currentVersion)[0];
$latestMajor = explode('.', $latestVersion)[0];

return $currentMajor !== $latestMajor;
return $currentMajor !== $latestMajor;
}

/**
Expand All @@ -105,49 +105,49 @@ function is_major_update($currentVersion, $latestVersion) {
* The path to the YAML file.
*/
function update_packages_yaml($filePath) {
$fileLines = file($filePath);
$comments = [];
$fileLines = file($filePath);
$comments = [];

// Extract comments
foreach ($fileLines as $line) {
if (preg_match('/^\s*#/', $line)) {
$comments[] = $line;
}
// Extract comments
foreach ($fileLines as $line) {
if (preg_match('/^\s*#/', $line)) {
$comments[] = $line;
}
}

$packages = Yaml::parseFile($filePath);

foreach ($packages as $package => &$details) {
if (isset($details['core_matrix'])) {
// Update only '*' entry
if (isset($details['core_matrix']['*'])) {
$currentVersion = $details['core_matrix']['*']['version'] ?? NULL;
$latestVersion = get_latest_version($package);

$packages = Yaml::parseFile($filePath);

foreach ($packages as $package => &$details) {
if (isset($details['core_matrix'])) {
// Update only '*' entry
if (isset($details['core_matrix']['*'])) {
$currentVersion = $details['core_matrix']['*']['version'] ?? NULL;
$latestVersion = get_latest_version($package);

if ($latestVersion && is_major_update($currentVersion, $latestVersion)) {
$details['core_matrix']['*']['version'] = $latestVersion;
echo "Updated $package for '*' to version $latestVersion.\n";
}
} else {
echo "Skipping $package as '*' is not defined in core_matrix.\n";
}
} else {
// Update non-core_matrix packages
$currentVersion = $details['version'] ?? NULL;
$latestVersion = get_latest_version($package);

if ($latestVersion && is_major_update($currentVersion, $latestVersion)) {
$details['version'] = $latestVersion;
echo "Updated $package to version $latestVersion.\n";
}
if ($latestVersion && is_major_update($currentVersion, $latestVersion)) {
$details['core_matrix']['*']['version'] = $latestVersion;
echo "Updated $package for '*' to version $latestVersion.\n";
}
} else {
echo "Skipping $package as '*' is not defined in core_matrix.\n";
}
} else {
// Update non-core_matrix packages
$currentVersion = $details['version'] ?? NULL;
$latestVersion = get_latest_version($package);

if ($latestVersion && is_major_update($currentVersion, $latestVersion)) {
$details['version'] = $latestVersion;
echo "Updated $package to version $latestVersion.\n";
}
}
}

// Write back the YAML, appending the comments
file_put_contents($filePath, implode('', $comments) . "\n" . Yaml::dump($packages, 2));
// Write back the YAML, appending the comments
file_put_contents($filePath, implode('', $comments) . "\n" . Yaml::dump($packages, 2));
}

// File path to the YAML configuration
$filePath = 'packages.yml';
$filePath = '../config/packages.yml';

update_packages_yaml($filePath);
update_packages_yaml($filePath);

0 comments on commit 33dc828

Please sign in to comment.