From 37d2f0afbe028247ae15af6f1e9ad36bcc8e40aa Mon Sep 17 00:00:00 2001 From: Zbigniew Malcherczyk Date: Fri, 21 Oct 2022 16:33:38 +0200 Subject: [PATCH 1/3] [WIP] - Inform when there is no minor version --- src/Git/PickLastMinorVersionFromCollection.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Git/PickLastMinorVersionFromCollection.php b/src/Git/PickLastMinorVersionFromCollection.php index 592f0f74..7e0d06fa 100644 --- a/src/Git/PickLastMinorVersionFromCollection.php +++ b/src/Git/PickLastMinorVersionFromCollection.php @@ -24,6 +24,10 @@ public function assert(Version $version): bool return ! $version->isPreRelease(); } }); + + if ($stableVersions->isEmpty()) { + throw new \Exception('Your library does not have any minor version'); + } $versionsSortedDescending = $stableVersions->sortedDescending(); From 59999b8256ddffcb9bd0ff7447c967d71dfa3417 Mon Sep 17 00:00:00 2001 From: Ferror Date: Sun, 23 Oct 2022 20:32:47 +0200 Subject: [PATCH 2/3] Instead of exception let's just inform --- src/Command/AssertBackwardsCompatible.php | 52 ++++++++----------- .../PickLastMinorVersionFromCollection.php | 13 +---- .../StableVersionConstraint.php | 16 ++++++ 3 files changed, 40 insertions(+), 41 deletions(-) create mode 100644 src/VersionConstraint/StableVersionConstraint.php diff --git a/src/Command/AssertBackwardsCompatible.php b/src/Command/AssertBackwardsCompatible.php index 3f2f83ad..febd33d7 100644 --- a/src/Command/AssertBackwardsCompatible.php +++ b/src/Command/AssertBackwardsCompatible.php @@ -21,8 +21,8 @@ use Roave\BackwardCompatibility\Git\ParseRevision; use Roave\BackwardCompatibility\Git\PerformCheckoutOfRevision; use Roave\BackwardCompatibility\Git\PickVersionFromVersionCollection; -use Roave\BackwardCompatibility\Git\Revision; use Roave\BackwardCompatibility\LocateDependencies\LocateDependencies; +use Roave\BackwardCompatibility\VersionConstraint\StableVersionConstraint; use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Exception\InvalidArgumentException; @@ -115,9 +115,27 @@ public function execute(InputInterface $input, OutputInterface $output): int // @todo fix flaky assumption about the path of the source repo... $sourceRepo = CheckedOutRepository::fromPath(Env\current_dir()); - $fromRevision = $input->getOption('from') !== null - ? $this->parseRevisionFromInput($input, $sourceRepo) - : $this->determineFromRevisionFromRepository($sourceRepo, $stdErr); + if ($input->getOption('from') !== null) { + $maybeRevision = Type\string()->coerce($input->getOption('from')); + } else { + $versions = $this->getVersions->fromRepository($sourceRepo); + + Psl\invariant(Iter\count($versions) >= 1, 'Could not detect any released versions for the given repository'); + + $stableVersions = $versions->matching(new StableVersionConstraint()); + + if ($stableVersions->isEmpty()) { + $stdErr->writeln(Str\format('Your library does not have any stable versions. Therefore cannot have BC breaks.')); + + return Command::SUCCESS; + } + + $maybeRevision = $this->pickFromVersion->forVersions($versions)->toString(); + + $stdErr->writeln(Str\format('Detected last minor version: %s', $maybeRevision)); + } + + $fromRevision = $this->parseRevision->fromStringForRepository($maybeRevision, $sourceRepo); $to = Type\string()->coerce($input->getOption('to')); @@ -187,30 +205,4 @@ private function printOutcomeAndExit(Changes $changes, OutputInterface $stdErr): return $hasBcBreaks ? 3 : 0; } - - /** @throws InvalidArgumentException */ - private function parseRevisionFromInput(InputInterface $input, CheckedOutRepository $repository): Revision - { - $from = Type\string()->coerce($input->getOption('from')); - - return $this->parseRevision->fromStringForRepository($from, $repository); - } - - private function determineFromRevisionFromRepository( - CheckedOutRepository $repository, - OutputInterface $output, - ): Revision { - $versions = $this->getVersions->fromRepository($repository); - - Psl\invariant(Iter\count($versions) >= 1, 'Could not detect any released versions for the given repository'); - - $versionString = $this->pickFromVersion->forVersions($versions)->toString(); - - $output->writeln(Str\format('Detected last minor version: %s', $versionString)); - - return $this->parseRevision->fromStringForRepository( - $versionString, - $repository, - ); - } } diff --git a/src/Git/PickLastMinorVersionFromCollection.php b/src/Git/PickLastMinorVersionFromCollection.php index 7e0d06fa..0eb2912a 100644 --- a/src/Git/PickLastMinorVersionFromCollection.php +++ b/src/Git/PickLastMinorVersionFromCollection.php @@ -6,8 +6,8 @@ use Psl; use Psl\Type; +use Roave\BackwardCompatibility\VersionConstraint\StableVersionConstraint; use Version\Comparison\Constraint\CompositeConstraint; -use Version\Comparison\Constraint\Constraint; use Version\Comparison\Constraint\OperationConstraint; use Version\Version; use Version\VersionCollection; @@ -18,16 +18,7 @@ public function forVersions(VersionCollection $versionsCollection): Version { Psl\invariant(! $versionsCollection->isEmpty(), 'Cannot determine latest minor version from an empty collection'); - $stableVersions = $versionsCollection->matching(new class implements Constraint { - public function assert(Version $version): bool - { - return ! $version->isPreRelease(); - } - }); - - if ($stableVersions->isEmpty()) { - throw new \Exception('Your library does not have any minor version'); - } + $stableVersions = $versionsCollection->matching(new StableVersionConstraint()); $versionsSortedDescending = $stableVersions->sortedDescending(); diff --git a/src/VersionConstraint/StableVersionConstraint.php b/src/VersionConstraint/StableVersionConstraint.php new file mode 100644 index 00000000..056454bd --- /dev/null +++ b/src/VersionConstraint/StableVersionConstraint.php @@ -0,0 +1,16 @@ +isPreRelease(); + } +} From 20d05f9b027c0f25dd2a878eb1c4656b555683a9 Mon Sep 17 00:00:00 2001 From: Ferror Date: Sun, 23 Oct 2022 20:40:39 +0200 Subject: [PATCH 3/3] test and minor fix --- README.md | 26 +++++++++---------- .../StableVersionConstraintTest.php | 24 +++++++++++++++++ 2 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 test/unit/VersionConstraint/StableVersionConstraintTest.php diff --git a/README.md b/README.md index a41b3b93..05c1abcd 100644 --- a/README.md +++ b/README.md @@ -58,19 +58,19 @@ as part of your CI pipeline. For example in a GitHub action, note the use of jobs: roave-backwards-compatibility-check: name: Roave Backwards Compatibility Check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - with: - fetch-depth: 0 - - name: "Install PHP" - uses: shivammathur/setup-php@v2 - with: - php-version: "8.0" - - name: "Install dependencies" - run: "composer install" - - name: "Check for BC breaks" - run: "vendor/bin/roave-backward-compatibility-check" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: "Install PHP" + uses: shivammathur/setup-php@v2 + with: + php-version: "8.0" + - name: "Install dependencies" + run: "composer install" + - name: "Check for BC breaks" + run: "vendor/bin/roave-backward-compatibility-check" ``` #### Nyholm Github Action diff --git a/test/unit/VersionConstraint/StableVersionConstraintTest.php b/test/unit/VersionConstraint/StableVersionConstraintTest.php new file mode 100644 index 00000000..e16c131e --- /dev/null +++ b/test/unit/VersionConstraint/StableVersionConstraintTest.php @@ -0,0 +1,24 @@ +assert(Version::fromString('1.0.0'))); + self::assertTrue($constraint->assert(Version::fromString('0.1.0'))); + self::assertTrue($constraint->assert(Version::fromString('0.0.1'))); + self::assertFalse($constraint->assert(Version::fromString('1.0.0-alpha.1'))); + self::assertFalse($constraint->assert(Version::fromString('1.0.0-beta.1'))); + self::assertFalse($constraint->assert(Version::fromString('1.0.0-rc.1'))); + } +}