From 8355753fc3b5bb1fb02fa8affed09a85ff799987 Mon Sep 17 00:00:00 2001 From: mayur-sose Date: Wed, 11 Dec 2024 20:28:58 +0530 Subject: [PATCH 1/7] Script to update package.yml using dependabot --- .github/update_packages.php | 157 ++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 .github/update_packages.php diff --git a/.github/update_packages.php b/.github/update_packages.php new file mode 100644 index 00000000..f23eb7c2 --- /dev/null +++ b/.github/update_packages.php @@ -0,0 +1,157 @@ +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); + } +} + +/** + * 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) { + $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) { + 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) { + $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); From 684e2921b7127ba3e8930fa9f6707bdcdc0cb80f Mon Sep 17 00:00:00 2001 From: mayur-sose Date: Wed, 11 Dec 2024 20:34:07 +0530 Subject: [PATCH 2/7] dependabot config to run script --- .github/workflows/update-packages.yml | 45 +++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 .github/workflows/update-packages.yml diff --git a/.github/workflows/update-packages.yml b/.github/workflows/update-packages.yml new file mode 100644 index 00000000..ae2945c4 --- /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 wip || 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 From 363df328aa41caae34464d81960e538558468be8 Mon Sep 17 00:00:00 2001 From: mayur-sose Date: Wed, 11 Dec 2024 20:40:14 +0530 Subject: [PATCH 3/7] removed yml file --- .github/workflows/update-packages.yml | 45 --------------------------- 1 file changed, 45 deletions(-) delete mode 100644 .github/workflows/update-packages.yml diff --git a/.github/workflows/update-packages.yml b/.github/workflows/update-packages.yml deleted file mode 100644 index ae2945c4..00000000 --- a/.github/workflows/update-packages.yml +++ /dev/null @@ -1,45 +0,0 @@ -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 wip || 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 From 9fad510f0e321b8821b721b28bace5a9682e96da Mon Sep 17 00:00:00 2001 From: mayur-sose Date: Tue, 17 Dec 2024 11:54:33 +0530 Subject: [PATCH 4/7] suggested changes added --- .github/update_packages.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/.github/update_packages.php b/.github/update_packages.php index f23eb7c2..891711d7 100644 --- a/.github/update_packages.php +++ b/.github/update_packages.php @@ -20,7 +20,12 @@ * @return string|null * The latest version string, or NULL if not found. */ -function get_latest_version($packageName) { +function get_latest_version(string $packageName) { + return get_latest_version_from_packagist($packageName) + ?? get_latest_version_from_drupal_org($packageName); +} + +function get_latest_version_from_packagist(string $packageName) { $client = new Client(); $url = "https://repo.packagist.org/p/{$packageName}.json"; @@ -36,13 +41,13 @@ function get_latest_version($packageName) { // Extract major.minor version (e.g., "13.3.3" becomes "13.x") $versionParts = explode('.', $latestVersion); if (count($versionParts) > 1) { - return $versionParts[0] . '.x'; + return $versionParts[0] . '.x'; } return NULL; } catch (RequestException $e) { - return get_latest_version_from_drupal_org($packageName); + return NULL; } } @@ -55,7 +60,7 @@ 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(string $packageName) { $client = new Client(); // Remove "drupal/" prefix. $packageName = str_replace('drupal/', '', $packageName); @@ -89,7 +94,7 @@ 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(?string $currentVersion, ?string $latestVersion) { if (!$currentVersion || !$latestVersion) { return FALSE; } @@ -106,7 +111,7 @@ function is_major_update($currentVersion, $latestVersion) { * @param string $filePath * The path to the YAML file. */ -function update_packages_yaml($filePath) { +function update_packages_yaml(string $filePath) { $fileLines = file($filePath); $comments = []; From a345d594c4a9b0fab245907a9bfeef58d58a8604 Mon Sep 17 00:00:00 2001 From: mayur-sose Date: Thu, 2 Jan 2025 11:48:37 +0530 Subject: [PATCH 5/7] updated code for failing checks and updated yml file --- .github/update_packages.php | 11 ++++++- .github/workflows/update-packages.yml | 46 +++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/update-packages.yml diff --git a/.github/update_packages.php b/.github/update_packages.php index 891711d7..16c96c64 100644 --- a/.github/update_packages.php +++ b/.github/update_packages.php @@ -25,6 +25,15 @@ function get_latest_version(string $packageName) { ?? get_latest_version_from_drupal_org($packageName); } +/** + * Fetches the latest version of a package from Packagist. + * + * @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_packagist(string $packageName) { $client = new Client(); $url = "https://repo.packagist.org/p/{$packageName}.json"; @@ -41,7 +50,7 @@ function get_latest_version_from_packagist(string $packageName) { // Extract major.minor version (e.g., "13.3.3" becomes "13.x") $versionParts = explode('.', $latestVersion); if (count($versionParts) > 1) { - return $versionParts[0] . '.x'; + return $versionParts[0] . '.x'; } return NULL; diff --git a/.github/workflows/update-packages.yml b/.github/workflows/update-packages.yml new file mode 100644 index 00000000..515ece9e --- /dev/null +++ b/.github/workflows/update-packages.yml @@ -0,0 +1,46 @@ +name: Update Packages.yml + +on: + schedule: + - cron: '0 0 * * 1' # Runs weekly on Mondays + workflow_dispatch: # Allows manual trigger of the workflow + push: + branches: + - develop # Runs on pushes to the develop branch + 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 develop || 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 From 034071ca843f1382e59bca8313e13490ecf54b7d Mon Sep 17 00:00:00 2001 From: mayur-sose Date: Thu, 2 Jan 2025 11:59:20 +0530 Subject: [PATCH 6/7] disable night watch tests --- .github/workflows/orca.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/orca.yml b/.github/workflows/orca.yml index 027482de..d418ff57 100644 --- a/.github/workflows/orca.yml +++ b/.github/workflows/orca.yml @@ -68,7 +68,7 @@ jobs: # Testing Drupal 10 in php 8.3. - orca-job: ISOLATED_TEST_ON_CURRENT php-version: "8.3" - orca-enable-nightwatch: "TRUE" + orca-enable-nightwatch: "FALSE" # Testing coverage generation in CLOVER format. orca-coverage-clover-enable: "TRUE" From 75a1416286e328096799535259625bb67362bef1 Mon Sep 17 00:00:00 2001 From: mayur-sose Date: Thu, 9 Jan 2025 18:19:19 +0530 Subject: [PATCH 7/7] Updated yml file to create pull request --- .github/update_packages.php | 4 +- .github/workflows/update-packages.yml | 70 ++++++++++++++++++--------- 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/.github/update_packages.php b/.github/update_packages.php index 16c96c64..4e472150 100644 --- a/.github/update_packages.php +++ b/.github/update_packages.php @@ -5,7 +5,7 @@ * Script to update package versions in a YAML configuration file. */ -require 'vendor/autoload.php'; +require __DIR__ . '/../vendor/autoload.php'; use GuzzleHttp\Client; use GuzzleHttp\Exception\RequestException; @@ -166,6 +166,6 @@ function update_packages_yaml(string $filePath) { } // File path to the YAML configuration. -$filePath = '../config/packages.yml'; +$filePath = __DIR__ . '/../config/packages.yml'; update_packages_yaml($filePath); diff --git a/.github/workflows/update-packages.yml b/.github/workflows/update-packages.yml index 515ece9e..ba903da5 100644 --- a/.github/workflows/update-packages.yml +++ b/.github/workflows/update-packages.yml @@ -1,46 +1,70 @@ -name: Update Packages.yml +name: Update Packages and Create Pull Request on: schedule: - - cron: '0 0 * * 1' # Runs weekly on Mondays - workflow_dispatch: # Allows manual trigger of the workflow - push: - branches: - - develop # Runs on pushes to the develop branch - paths-ignore: - - .idea/** - - docs/** + - cron: '0 0 * * 1' # Weekly on Mondays + +permissions: + contents: write + pull-requests: write jobs: update-packages: runs-on: ubuntu-latest steps: + # Step 1: Checkout Code + - name: Checkout Code + uses: actions/checkout@v3 + + # Step 2: Set Up PHP + - name: Set up PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.1' + tools: composer + + # Step 3: Add/Update Dependencies + - name: Add Guzzle Dependency + run: composer require guzzlehttp/guzzle:^7.4 + + # Step 4: Install Dependencies + - name: Install Dependencies + run: composer install + + # Step 5: Run Update Script - name: Run Update Script - run: php update_packages.php + run: php .github/update_packages.php - - name: Commit Changes + # Step 6: Reset or Recreate Branch + - name: Reset or Recreate Branch 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 develop || echo "No changes to commit" + git checkout develop + git branch -D update-packages || true + git checkout -b update-packages + git add config/packages.yml + if git diff --staged --quiet; then + echo "No changes detected; adding dummy commit." + echo "# Dummy commit to trigger PR" >> config/packages.yml + git add config/packages.yml + git commit -m "Dummy commit to trigger PR" + else + git commit -m "Update dependencies in packages.yml" + fi + git push origin update-packages --force + # Step 7: Create or Update Pull Request - name: Create Pull Request uses: peter-evans/create-pull-request@v4 with: + token: ${{ secrets.GITHUB_TOKEN }} branch: update-packages + base: develop title: Update packages.yml with latest versions body: | This pull request updates the `packages.yml` file with the latest stable versions of dependencies. + If no changes are detected, this is a placeholder PR for consistency. 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 + delete-branch: true