-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnreleasedEntryChecker.php
More file actions
104 lines (88 loc) · 3.26 KB
/
UnreleasedEntryChecker.php
File metadata and controls
104 lines (88 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
declare(strict_types=1);
/**
* Fast Forward Development Tools for PHP projects.
*
* This file is part of fast-forward/dev-tools project.
*
* @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
* @license https://opensource.org/licenses/MIT MIT License
*
* @see https://github.com/php-fast-forward/
* @see https://github.com/php-fast-forward/dev-tools
* @see https://github.com/php-fast-forward/dev-tools/issues
* @see https://php-fast-forward.github.io/dev-tools/
* @see https://datatracker.ietf.org/doc/html/rfc2119
*/
namespace FastForward\DevTools\Changelog\Checker;
use FastForward\DevTools\Changelog\Document\ChangelogRelease;
use FastForward\DevTools\Changelog\Entry\ChangelogEntryType;
use FastForward\DevTools\Filesystem\FilesystemInterface;
use FastForward\DevTools\Git\GitClientInterface;
use FastForward\DevTools\Changelog\Parser\ChangelogParserInterface;
use Throwable;
use function array_diff;
use function array_values;
/**
* Compares unreleased changelog entries against the current branch or a base ref.
*/
final readonly class UnreleasedEntryChecker implements UnreleasedEntryCheckerInterface
{
/**
* Constructs a new UnreleasedEntryChecker.
*
* @param GitClientInterface $gitClient the Git client used for baseline inspection
* @param FilesystemInterface $filesystem
* @param ChangelogParserInterface $parser
*/
public function __construct(
private FilesystemInterface $filesystem,
private GitClientInterface $gitClient,
private ChangelogParserInterface $parser,
) {}
/**
* Checks if there are pending unreleased entries in the changelog compared to a given reference.
*
* @param string $file the changelog file path to inspect
* @param string|null $againstReference The reference to compare against (e.g., a branch or commit hash).
*
* @return bool true if there are pending unreleased entries, false otherwise
*/
public function hasPendingChanges(string $file, ?string $againstReference = null): bool
{
try {
$content = $this->filesystem->readFile($file);
} catch (Throwable) {
return false;
}
$currentEntries = $this->flattenEntries($this->parser->parse($content)->getUnreleased());
if ([] === $currentEntries) {
return false;
}
if (null === $againstReference) {
return true;
}
try {
$baseline = $this->gitClient->show($againstReference, $file, $this->filesystem->getDirectory($file));
} catch (Throwable) {
return true;
}
$baselineEntries = $this->flattenEntries($this->parser->parse($baseline)->getUnreleased());
return [] !== array_values(array_diff($currentEntries, $baselineEntries));
}
/**
* Flattens release entries for set comparison.
*
* @param ChangelogRelease $release
*
* @return list<string>
*/
private function flattenEntries(ChangelogRelease $release): array
{
$entries = [];
foreach (ChangelogEntryType::ordered() as $type) {
$entries = [...$entries, ...$release->getEntriesFor($type)];
}
return array_values(array_unique($entries));
}
}