From c627424f719c85e90a6ba1a8f949d45528a0efaf Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Fri, 24 May 2024 09:34:33 +1200 Subject: [PATCH] FIX Ensure mergeups iterates over inputs in a consistent order (#34) --- src/BranchLogic.php | 7 +++++++ tests/BranchLogicTest.php | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/BranchLogic.php b/src/BranchLogic.php index 66367e7..2b5b31d 100644 --- a/src/BranchLogic.php +++ b/src/BranchLogic.php @@ -60,6 +60,13 @@ public static function getBranchesForMergeUp( return []; } + // Make sure the input is in a consistent order to ensure the following logic always gets the same results + // The order should be highest to lowest, so getMajorDiff correctly checks the highest branch first. + usort($repoBranches, 'version_compare'); + $repoBranches = array_reverse($repoBranches); + usort($repoTags, 'version_compare'); + $repoTags = array_reverse($repoTags); + $onlyMajorBranches = array_filter($repoBranches, fn ($branch) => ctype_digit((string) $branch)); $majorDiff = static::getMajorDiff($repoMetaData, $onlyMajorBranches, $defaultBranch, $composerJson); diff --git a/tests/BranchLogicTest.php b/tests/BranchLogicTest.php index 04e200f..5675c8e 100644 --- a/tests/BranchLogicTest.php +++ b/tests/BranchLogicTest.php @@ -727,7 +727,7 @@ public function provideGetBranchesForMergeUp(): array '5.0.4', '5.1.21', '4.7.4', - '4.8.6' + '4.8.6', ], 'repoBranches' => [ '8', @@ -741,7 +741,36 @@ public function provideGetBranchesForMergeUp(): array '5.1', '4', '4.7', - '4.8' + '4.8', + ], + 'composerJson' => null, + 'expected' => ['6.0', '6', '7.1', '7', '8'], + ], + 'Fluent with branches in the reverse order, which used to fail' => [ + 'githubRepository' => 'tractorcow-farm/silverstripe-fluent', + 'defaultBranch' => '7', + 'repoTags' => [ + '4.8.6', + '4.7.4', + '5.1.21', + '5.0.4', + '6.0.5', + '7.1.0', + '7.0.1', + ], + 'repoBranches' => [ + '4.8', + '4.7', + '4', + '5.1', + '5.0', + '5', + '6.0', + '6', + '7.1', + '7.0', + '7', + '8', ], 'composerJson' => null, 'expected' => ['6.0', '6', '7.1', '7', '8'],