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/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 592f0f74..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,12 +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(); - } - }); + $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(); + } +} 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'))); + } +}