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 1ad99e1 commit d2f1469
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions .github/update_packages.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

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

Expand All @@ -19,13 +20,14 @@
* @return string|null
* The latest version string, or NULL if not found.
*/
function get_latest_version($packageName) {
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 @@ -38,7 +40,7 @@ function get_latest_version($packageName) {
return $versionParts[0] . '.x';
}

return NULL;
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 @@ -54,24 +56,25 @@ function get_latest_version($packageName) {
* @return string|null
* The latest version string, or NULL if not found.
*/
function get_latest_version_from_drupal_org($packageName) {
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'])) {
return $data['list'][0]['field_release_version'];
}

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

Expand All @@ -86,9 +89,10 @@ function get_latest_version_from_drupal_org($packageName) {
* @return bool
* TRUE if it is a major update, FALSE otherwise.
*/
function is_major_update($currentVersion, $latestVersion) {
function is_major_update($currentVersion, $latestVersion)
{
if (!$currentVersion || !$latestVersion) {
return FALSE;
return false;
}

$currentMajor = explode('.', $currentVersion)[0];
Expand All @@ -103,7 +107,8 @@ function is_major_update($currentVersion, $latestVersion) {
* @param string $filePath
* The path to the YAML file.
*/
function update_packages_yaml($filePath) {
function update_packages_yaml($filePath)
{
$fileLines = file($filePath);
$comments = [];

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

if ($latestVersion && is_major_update($currentVersion, $latestVersion)) {
Expand All @@ -132,7 +137,7 @@ function update_packages_yaml($filePath) {
}
} else {
// Update non-core_matrix packages
$currentVersion = $details['version'] ?? NULL;
$currentVersion = $details['version'] ?? null;
$latestVersion = get_latest_version($package);

if ($latestVersion && is_major_update($currentVersion, $latestVersion)) {
Expand All @@ -147,6 +152,6 @@ function update_packages_yaml($filePath) {
}

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

update_packages_yaml($filePath);

0 comments on commit d2f1469

Please sign in to comment.