-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Script to update package.yml using dependabot #641
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,157 @@ | ||||||
<?php | ||||||
|
||||||
/** | ||||||
* @file | ||||||
* 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; | ||||||
|
||||||
/** | ||||||
* 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); | ||||||
|
||||||
$versions = array_keys($data['packages'][$packageName]); | ||||||
usort($versions, 'version_compare'); | ||||||
|
||||||
$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) { | ||||||
return get_latest_version_from_drupal_org($packageName); | ||||||
} | ||||||
} | ||||||
Comment on lines
+23
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be much more readable if you would extract the Packagist part this into a separate function just like you did with function get_latest_version($packageName) {
return get_latest_version_from_packagist($packageName)
?? get_latest_version_from_drupal_org($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) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
$client = new Client(); | ||||||
// Remove "drupal/" prefix. | ||||||
$packageName = str_replace('drupal/', '', $packageName); | ||||||
$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']; | ||||||
} | ||||||
|
||||||
echo "No new 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 latest version is a major update compared to 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) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if (!$currentVersion || !$latestVersion) { | ||||||
return FALSE; | ||||||
} | ||||||
|
||||||
$currentMajor = explode('.', $currentVersion)[0]; | ||||||
$latestMajor = explode('.', $latestVersion)[0]; | ||||||
|
||||||
return $currentMajor !== $latestMajor; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Updates package versions in a YAML file. | ||||||
* | ||||||
* @param string $filePath | ||||||
* The path to the YAML file. | ||||||
*/ | ||||||
function update_packages_yaml($filePath) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
$fileLines = file($filePath); | ||||||
$comments = []; | ||||||
|
||||||
// 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); | ||||||
|
||||||
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)); | ||||||
} | ||||||
|
||||||
// File path to the YAML configuration. | ||||||
$filePath = '../config/packages.yml'; | ||||||
|
||||||
update_packages_yaml($filePath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.