diff --git a/.github/update_packages.php b/.github/update_packages.php new file mode 100644 index 00000000..3695dc3c --- /dev/null +++ b/.github/update_packages.php @@ -0,0 +1,110 @@ +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) { + echo "Package {$packageName} not found on Packagist. Trying Drupal.org...\n"; + return getLatestVersionFromDrupalOrg($packageName); + } +} + +function getLatestVersionFromDrupalOrg($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); + + // Check if the response contains releases + 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; + } + } catch (RequestException $e) { + echo "Error fetching data for {$packageName} on Drupal.org: " . $e->getMessage() . PHP_EOL; + return null; + } +} + +function isMajorUpdate($currentVersion, $latestVersion) { + if (!$currentVersion || !$latestVersion) { + return false; + } + + $currentMajor = explode('.', $currentVersion)[0]; + $latestMajor = explode('.', $latestVersion)[0]; + + return $currentMajor !== $latestMajor; +} + +function updatePackagesYaml($filePath) { + + $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) { // Use reference to modify $packages directly + if (isset($details['core_matrix'])) { + foreach ($details['core_matrix'] as $coreVersion => &$coreDetails) { // Use reference here as well + $currentVersion = $coreDetails['version'] ?? null; + $latestVersion = getLatestVersion($package); + + if ($latestVersion && isMajorUpdate($currentVersion, $latestVersion)) { + $coreDetails['version'] = $latestVersion; + echo "Updated $package for Drupal $coreVersion to version $latestVersion.\n"; + } + } + } else { + $currentVersion = $details['version'] ?? null; + $latestVersion = getLatestVersion($package); + + if ($latestVersion && isMajorUpdate($currentVersion, $latestVersion)) { + $details['version'] = $latestVersion; // This directly updates $packages + echo "Updated $package to version $latestVersion.\n"; + } + } + } + + file_put_contents($filePath, implode('', $comments) . "\n" . Yaml::dump($packages, 2)); +} + +$filePath = '../config/packages.yml'; + +updatePackagesYaml($filePath); diff --git a/.github/workflows/update-packages.yml b/.github/workflows/update-packages.yml new file mode 100644 index 00000000..f1256499 --- /dev/null +++ b/.github/workflows/update-packages.yml @@ -0,0 +1,45 @@ +name: Update Packages.yml + +on: + schedule: + - cron: '0 0 * * 1' # Runs weekly on Mondays + workflow_dispatch: # Allows manual trigger of the workflow + push: + branches: [ wip ] + paths-ignore: + - .idea/** + - docs/** + +jobs: + update-packages: + runs-on: ubuntu-latest + + steps: + - name: Run Update Script + run: php update_packages.php + + - name: Commit Changes + run: | + git config --local user.name "github-actions[bot]" + git config --local user.email "github-actions[bot]@users.noreply.github.com" + git add packages.yml + git commit -m "Update packages.yml with latest versions" + git push origin HEAD || echo "No changes to commit" + + - name: Create Pull Request + uses: peter-evans/create-pull-request@v4 + with: + branch: update-packages + title: Update packages.yml with latest versions + body: | + This pull request updates the `packages.yml` file with the latest stable versions of dependencies. + labels: dependencies + + run-cron: + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v3 + + - name: Run script or cron job + run: php update_packages.php \ No newline at end of file