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 0c0f120 commit 1ad99e1
Showing 1 changed file with 64 additions and 29 deletions.
93 changes: 64 additions & 29 deletions .github/update_packages.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
<?php

/**
* Script to update package versions in a YAML configuration file.
*/

require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Symfony\Component\Yaml\Yaml;

function getLatestVersion($packageName)
{
/**
* Fetches the latest version of a package from Packagist or Drupal.org.
*
* @param string $packageName
* The name of the package.
*
* @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";

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

$versions = array_keys($data['packages'][$packageName]);
usort($versions, 'version_compare');
Expand All @@ -23,43 +35,60 @@ function getLatestVersion($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 $versionParts[0] . '.x';
}

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

function getLatestVersionFromDrupalOrg($packageName)
{
/**
* Fetches the latest version of a package from Drupal.org.
*
* @param string $packageName
* The name of the package.
*
* @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}";

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

if (!empty($data['list']) && isset($data['list'][0]['field_release_version'])) {
$latestVersion = $data['list'][0]['field_release_version'];
return $latestVersion;
} else {
echo "No releases found for {$packageName} on Drupal.org.\n";
return null;
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;
return NULL;
}
}

function isMajorUpdate($currentVersion, $latestVersion)
{
/**
* Determines if the latest version is a major update compared to the current version.
*
* @param string|null $currentVersion
* The current version.
* @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;
return FALSE;
}

$currentMajor = explode('.', $currentVersion)[0];
Expand All @@ -68,8 +97,13 @@ function isMajorUpdate($currentVersion, $latestVersion)
return $currentMajor !== $latestMajor;
}

function updatePackagesYaml($filePath)
{
/**
* Updates package versions in a YAML file.
*
* @param string $filePath
* The path to the YAML file.
*/
function update_packages_yaml($filePath) {
$fileLines = file($filePath);
$comments = [];

Expand All @@ -86,10 +120,10 @@ function updatePackagesYaml($filePath)
if (isset($details['core_matrix'])) {
// Update only '*' entry
if (isset($details['core_matrix']['*'])) {
$currentVersion = $details['core_matrix']['*']['version'] ?? null;
$latestVersion = getLatestVersion($package);
$currentVersion = $details['core_matrix']['*']['version'] ?? NULL;
$latestVersion = get_latest_version($package);

if ($latestVersion && isMajorUpdate($currentVersion, $latestVersion)) {
if ($latestVersion && is_major_update($currentVersion, $latestVersion)) {
$details['core_matrix']['*']['version'] = $latestVersion;
echo "Updated $package for '*' to version $latestVersion.\n";
}
Expand All @@ -98,10 +132,10 @@ function updatePackagesYaml($filePath)
}
} else {
// Update non-core_matrix packages
$currentVersion = $details['version'] ?? null;
$latestVersion = getLatestVersion($package);
$currentVersion = $details['version'] ?? NULL;
$latestVersion = get_latest_version($package);

if ($latestVersion && isMajorUpdate($currentVersion, $latestVersion)) {
if ($latestVersion && is_major_update($currentVersion, $latestVersion)) {
$details['version'] = $latestVersion;
echo "Updated $package to version $latestVersion.\n";
}
Expand All @@ -112,6 +146,7 @@ function updatePackagesYaml($filePath)
file_put_contents($filePath, implode('', $comments) . "\n" . Yaml::dump($packages, 2));
}

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

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

0 comments on commit 1ad99e1

Please sign in to comment.