Skip to content

Commit 8355753

Browse files
committed
Script to update package.yml using dependabot
1 parent e0be1fc commit 8355753

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

.github/update_packages.php

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Script to update package versions in a YAML configuration file.
6+
*/
7+
8+
require 'vendor/autoload.php';
9+
10+
use GuzzleHttp\Client;
11+
use GuzzleHttp\Exception\RequestException;
12+
use Symfony\Component\Yaml\Yaml;
13+
14+
/**
15+
* Fetches the latest version of a package from Packagist or Drupal.org.
16+
*
17+
* @param string $packageName
18+
* The name of the package.
19+
*
20+
* @return string|null
21+
* The latest version string, or NULL if not found.
22+
*/
23+
function get_latest_version($packageName) {
24+
$client = new Client();
25+
$url = "https://repo.packagist.org/p/{$packageName}.json";
26+
27+
try {
28+
$response = $client->get($url);
29+
$data = json_decode($response->getBody(), TRUE);
30+
31+
$versions = array_keys($data['packages'][$packageName]);
32+
usort($versions, 'version_compare');
33+
34+
$latestVersion = end($versions);
35+
36+
// Extract major.minor version (e.g., "13.3.3" becomes "13.x")
37+
$versionParts = explode('.', $latestVersion);
38+
if (count($versionParts) > 1) {
39+
return $versionParts[0] . '.x';
40+
}
41+
42+
return NULL;
43+
}
44+
catch (RequestException $e) {
45+
return get_latest_version_from_drupal_org($packageName);
46+
}
47+
}
48+
49+
/**
50+
* Fetches the latest version of a package from Drupal.org.
51+
*
52+
* @param string $packageName
53+
* The name of the package.
54+
*
55+
* @return string|null
56+
* The latest version string, or NULL if not found.
57+
*/
58+
function get_latest_version_from_drupal_org($packageName) {
59+
$client = new Client();
60+
// Remove "drupal/" prefix.
61+
$packageName = str_replace('drupal/', '', $packageName);
62+
$drupalApiUrl = "https://www.drupal.org/api-d7/node.json?field_project_machine_name={$packageName}";
63+
64+
try {
65+
$response = $client->get($drupalApiUrl);
66+
$data = json_decode($response->getBody(), TRUE);
67+
68+
if (!empty($data['list']) && isset($data['list'][0]['field_release_version'])) {
69+
return $data['list'][0]['field_release_version'];
70+
}
71+
72+
echo "No new releases found for {$packageName} on Drupal.org.\n";
73+
return NULL;
74+
}
75+
catch (RequestException $e) {
76+
echo "Error fetching data for {$packageName} on Drupal.org: " . $e->getMessage() . PHP_EOL;
77+
return NULL;
78+
}
79+
}
80+
81+
/**
82+
* Determines if latest version is a major update compared to current version.
83+
*
84+
* @param string|null $currentVersion
85+
* The current version.
86+
* @param string|null $latestVersion
87+
* The latest version.
88+
*
89+
* @return bool
90+
* TRUE if it is a major update, FALSE otherwise.
91+
*/
92+
function is_major_update($currentVersion, $latestVersion) {
93+
if (!$currentVersion || !$latestVersion) {
94+
return FALSE;
95+
}
96+
97+
$currentMajor = explode('.', $currentVersion)[0];
98+
$latestMajor = explode('.', $latestVersion)[0];
99+
100+
return $currentMajor !== $latestMajor;
101+
}
102+
103+
/**
104+
* Updates package versions in a YAML file.
105+
*
106+
* @param string $filePath
107+
* The path to the YAML file.
108+
*/
109+
function update_packages_yaml($filePath) {
110+
$fileLines = file($filePath);
111+
$comments = [];
112+
113+
// Extract comments.
114+
foreach ($fileLines as $line) {
115+
if (preg_match('/^\s*#/', $line)) {
116+
$comments[] = $line;
117+
}
118+
}
119+
120+
$packages = Yaml::parseFile($filePath);
121+
122+
foreach ($packages as $package => &$details) {
123+
if (isset($details['core_matrix'])) {
124+
// Update only '*' entry.
125+
if (isset($details['core_matrix']['*'])) {
126+
$currentVersion = $details['core_matrix']['*']['version'] ?? NULL;
127+
$latestVersion = get_latest_version($package);
128+
129+
if ($latestVersion && is_major_update($currentVersion, $latestVersion)) {
130+
$details['core_matrix']['*']['version'] = $latestVersion;
131+
echo "Updated $package for '*' to version $latestVersion.\n";
132+
}
133+
}
134+
else {
135+
echo "Skipping $package as '*' is not defined in core_matrix.\n";
136+
}
137+
}
138+
else {
139+
// Update non-core_matrix packages.
140+
$currentVersion = $details['version'] ?? NULL;
141+
$latestVersion = get_latest_version($package);
142+
143+
if ($latestVersion && is_major_update($currentVersion, $latestVersion)) {
144+
$details['version'] = $latestVersion;
145+
echo "Updated $package to version $latestVersion.\n";
146+
}
147+
}
148+
}
149+
150+
// Write back the YAML, appending the comments.
151+
file_put_contents($filePath, implode('', $comments) . "\n" . Yaml::dump($packages, 2));
152+
}
153+
154+
// File path to the YAML configuration.
155+
$filePath = '../config/packages.yml';
156+
157+
update_packages_yaml($filePath);

0 commit comments

Comments
 (0)