Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
37 / 37 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| ChangelogCheckCommand | |
100.00% |
37 / 37 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| configure | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * Fast Forward Development Tools for PHP projects. |
| 7 | * |
| 8 | * This file is part of fast-forward/dev-tools project. |
| 9 | * |
| 10 | * @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 11 | * @license https://opensource.org/licenses/MIT MIT License |
| 12 | * |
| 13 | * @see https://github.com/php-fast-forward/ |
| 14 | * @see https://github.com/php-fast-forward/dev-tools |
| 15 | * @see https://github.com/php-fast-forward/dev-tools/issues |
| 16 | * @see https://php-fast-forward.github.io/dev-tools/ |
| 17 | * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 18 | */ |
| 19 | |
| 20 | namespace FastForward\DevTools\Console\Command; |
| 21 | |
| 22 | use FastForward\DevTools\Console\Command\Traits\LogsCommandResults; |
| 23 | use FastForward\DevTools\Changelog\Checker\UnreleasedEntryCheckerInterface; |
| 24 | use FastForward\DevTools\Console\Input\HasJsonOption; |
| 25 | use FastForward\DevTools\Filesystem\FilesystemInterface; |
| 26 | use Symfony\Component\Console\Attribute\AsCommand; |
| 27 | use Symfony\Component\Console\Command\Command; |
| 28 | use Symfony\Component\Console\Input\InputInterface; |
| 29 | use Symfony\Component\Console\Input\InputOption; |
| 30 | use Symfony\Component\Console\Output\OutputInterface; |
| 31 | |
| 32 | /** |
| 33 | * Verifies that the changelog contains pending unreleased notes. |
| 34 | */ |
| 35 | #[AsCommand( |
| 36 | name: 'changelog:check', |
| 37 | description: 'Checks whether a changelog file contains meaningful unreleased entries.' |
| 38 | )] |
| 39 | class ChangelogCheckCommand extends Command |
| 40 | { |
| 41 | use HasJsonOption; |
| 42 | use LogsCommandResults; |
| 43 | |
| 44 | /** |
| 45 | * @param FilesystemInterface $filesystem |
| 46 | * @param UnreleasedEntryCheckerInterface $unreleasedEntryChecker |
| 47 | */ |
| 48 | public function __construct( |
| 49 | private FilesystemInterface $filesystem, |
| 50 | private UnreleasedEntryCheckerInterface $unreleasedEntryChecker, |
| 51 | ) { |
| 52 | parent::__construct(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Configures changelog verification options. |
| 57 | */ |
| 58 | protected function configure(): void |
| 59 | { |
| 60 | $this->setHelp( |
| 61 | 'This command validates the current Unreleased section and may compare it against a base git' |
| 62 | . ' reference to enforce pull request changelog updates.' |
| 63 | ); |
| 64 | |
| 65 | $this->addJsonOption() |
| 66 | ->addOption( |
| 67 | name: 'against', |
| 68 | mode: InputOption::VALUE_REQUIRED, |
| 69 | description: 'Optional git reference used as the baseline changelog file.', |
| 70 | ) |
| 71 | ->addOption( |
| 72 | name: 'file', |
| 73 | mode: InputOption::VALUE_REQUIRED, |
| 74 | description: 'Path to the changelog file.', |
| 75 | default: 'CHANGELOG.md', |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Executes the changelog verification. |
| 81 | * |
| 82 | * @param InputInterface $input |
| 83 | * @param OutputInterface $output |
| 84 | */ |
| 85 | protected function execute(InputInterface $input, OutputInterface $output): int |
| 86 | { |
| 87 | $path = $this->filesystem->getAbsolutePath($input->getOption('file')); |
| 88 | $against = $input->getOption('against'); |
| 89 | |
| 90 | $hasPendingChanges = $this->unreleasedEntryChecker |
| 91 | ->hasPendingChanges($path, $against); |
| 92 | |
| 93 | if ($hasPendingChanges) { |
| 94 | return $this->success( |
| 95 | 'The changelog contains unreleased changes ready for review.', |
| 96 | $input, |
| 97 | [ |
| 98 | 'has_pending_changes' => true, |
| 99 | ], |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | return $this->failure( |
| 104 | 'The changelog must add a meaningful entry to the Unreleased section.', |
| 105 | $input, |
| 106 | [ |
| 107 | 'has_pending_changes' => false, |
| 108 | ], |
| 109 | (string) $input->getOption('file'), |
| 110 | ); |
| 111 | } |
| 112 | } |