Skip to content

Commit

Permalink
RCAT-376 : to test changes on wip branch
Browse files Browse the repository at this point in the history
  • Loading branch information
mayur-sose committed Dec 9, 2024
1 parent 916a792 commit 97fd39c
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 0 deletions.
110 changes: 110 additions & 0 deletions .github/update_packages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

require 'vendor/autoload.php';

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

function getLatestVersion($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) {
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);
45 changes: 45 additions & 0 deletions .github/workflows/update-packages.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 97fd39c

Please sign in to comment.