Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ChangelogCheckCommand
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 configure
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(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
20namespace FastForward\DevTools\Console\Command;
21
22use FastForward\DevTools\Console\Command\Traits\LogsCommandResults;
23use Composer\Command\BaseCommand;
24use FastForward\DevTools\Changelog\Checker\UnreleasedEntryCheckerInterface;
25use FastForward\DevTools\Console\Input\HasJsonOption;
26use FastForward\DevTools\Filesystem\FilesystemInterface;
27use Psr\Log\LoggerInterface;
28use Symfony\Component\Console\Attribute\AsCommand;
29use Symfony\Component\Console\Input\InputInterface;
30use Symfony\Component\Console\Input\InputOption;
31use Symfony\Component\Console\Output\OutputInterface;
32
33/**
34 * Verifies that the changelog contains pending unreleased notes.
35 */
36#[AsCommand(
37    name: 'changelog:check',
38    description: 'Checks whether a changelog file contains meaningful unreleased entries.',
39    help: 'This command validates the current Unreleased section and may compare it against a base git reference to enforce pull request changelog updates.'
40)]
41 class ChangelogCheckCommand extends BaseCommand implements LoggerAwareCommandInterface
42{
43    use HasJsonOption;
44    use LogsCommandResults;
45
46    /**
47     * @param FilesystemInterface $filesystem
48     * @param UnreleasedEntryCheckerInterface $unreleasedEntryChecker
49     * @param LoggerInterface $logger
50     */
51    public function __construct(
52        private  FilesystemInterface $filesystem,
53        private  UnreleasedEntryCheckerInterface $unreleasedEntryChecker,
54        private  LoggerInterface $logger,
55    ) {
56        parent::__construct();
57    }
58
59    /**
60     * Configures changelog verification options.
61     */
62    protected function configure(): void
63    {
64        $this->addJsonOption()
65            ->addOption(
66                name: 'against',
67                mode: InputOption::VALUE_REQUIRED,
68                description: 'Optional git reference used as the baseline changelog file.',
69            )
70            ->addOption(
71                name: 'file',
72                mode: InputOption::VALUE_REQUIRED,
73                description: 'Path to the changelog file.',
74                default: 'CHANGELOG.md',
75            );
76    }
77
78    /**
79     * Executes the changelog verification.
80     *
81     * @param InputInterface $input
82     * @param OutputInterface $output
83     */
84    protected function execute(InputInterface $input, OutputInterface $output): int
85    {
86        $path = $this->filesystem->getAbsolutePath($input->getOption('file'));
87        $against = $input->getOption('against');
88
89        $hasPendingChanges = $this->unreleasedEntryChecker
90            ->hasPendingChanges($path, $against);
91
92        if ($hasPendingChanges) {
93            return $this->success(
94                'The changelog contains unreleased changes ready for review.',
95                $input,
96                [
97                    'has_pending_changes' => true,
98                ],
99            );
100        }
101
102        return $this->failure(
103            'The changelog must add a meaningful entry to the Unreleased section.',
104            $input,
105            [
106                'has_pending_changes' => false,
107            ],
108            (string) $input->getOption('file'),
109        );
110    }
111}