Skip to content

Commit 763195a

Browse files
authored
Merge pull request #68 from weierophinney/hotfix/63-remove-empty-changelog-sections
Strip empty sections from keep-a-changelog release notes
2 parents 5730dbc + 7f82195 commit 763195a

File tree

2 files changed

+108
-42
lines changed

2 files changed

+108
-42
lines changed

src/Github/CreateReleaseTextViaKeepAChangelog.php

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,25 @@
1818
use Symfony\Component\Process\Process;
1919
use Webmozart\Assert\Assert;
2020

21+
use function array_reduce;
2122
use function count;
2223
use function explode;
2324
use function implode;
2425
use function preg_match;
2526
use function preg_quote;
2627
use function preg_replace;
2728
use function sprintf;
28-
use function str_replace;
2929

3030
class CreateReleaseTextViaKeepAChangelog implements CreateReleaseText
3131
{
32-
private const DEFAULT_CONTENTS = <<< 'CONTENTS'
33-
### Added
34-
35-
- Nothing.
36-
37-
### Changed
38-
39-
- Nothing.
40-
41-
### Deprecated
42-
43-
- Nothing.
44-
45-
### Removed
46-
47-
- Nothing.
48-
49-
### Fixed
50-
51-
- Nothing.
52-
53-
CONTENTS;
54-
32+
/** @psalm-var list<string> */
33+
private const DEFAULT_SECTIONS = [
34+
'Added',
35+
'Changed',
36+
'Deprecated',
37+
'Removed',
38+
'Fixed',
39+
];
5540

5641
private ChangelogExists $changelogExists;
5742
private Clock $clock;
@@ -152,7 +137,15 @@ private function updateReleaseDate(string $changelog, string $version): string
152137
*/
153138
private function removeDefaultContents(string $changelog): string
154139
{
155-
$contents = str_replace(self::DEFAULT_CONTENTS, '', $changelog);
140+
$contents = array_reduce(
141+
self::DEFAULT_SECTIONS,
142+
static fn (string $changelog, string $section): string => preg_replace(
143+
"/\n\#{3} " . $section . "\n\n- Nothing.\n/s",
144+
'',
145+
$changelog
146+
),
147+
$changelog
148+
);
156149
Assert::notEmpty($contents);
157150

158151
return $contents;

test/unit/Github/CreateReleaseTextViaKeepAChangelogTest.php

Lines changed: 90 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,22 +109,43 @@ public function testExtractsReleaseTextViaChangelogFile(): void
109109
### Added
110110
111111
- Everything.
112+
END, $date);
113+
114+
self::assertStringContainsString(
115+
$expected,
116+
(new CreateReleaseTextViaKeepAChangelog(new ChangelogExistsViaConsole(), $this->clock))
117+
->__invoke(
118+
$this->createMockMilestone(),
119+
RepositoryName::fromFullName('example/repo'),
120+
SemVerVersion::fromMilestoneName('1.0.0'),
121+
BranchName::fromName('1.0.x'),
122+
$workingPath
123+
)
124+
->contents()
125+
);
126+
}
127+
128+
public function testExtractsNonEmptySectionsForVersionViaChangelogFile(): void
129+
{
130+
$date = $this->clock->now()->format('Y-m-d');
131+
$changelogContents = sprintf(self::CHANGELOG_MULTI_SECTION, $date);
132+
$repositoryPath = $this->createMockRepositoryWithChangelog(
133+
$changelogContents,
134+
'CHANGELOG.md',
135+
'2.3.x'
136+
);
137+
$workingPath = $this->checkoutMockRepositoryWithChangelog($repositoryPath);
138+
139+
$expected = sprintf(<<< 'END'
140+
## 2.3.12 - %s
112141
113-
### Changed
114-
115-
- Nothing.
116-
117-
### Deprecated
118-
119-
- Nothing.
120-
121-
### Removed
122-
123-
- Nothing.
142+
### Added
124143
144+
- Something.
145+
125146
### Fixed
126-
127-
- Nothing.
147+
148+
- Several things
128149
END, $date);
129150

130151
self::assertStringContainsString(
@@ -133,8 +154,8 @@ public function testExtractsReleaseTextViaChangelogFile(): void
133154
->__invoke(
134155
$this->createMockMilestone(),
135156
RepositoryName::fromFullName('example/repo'),
136-
SemVerVersion::fromMilestoneName('1.0.0'),
137-
BranchName::fromName('1.0.x'),
157+
SemVerVersion::fromMilestoneName('2.3.12'),
158+
BranchName::fromName('2.3.x'),
138159
$workingPath
139160
)
140161
->contents()
@@ -163,7 +184,8 @@ private function createMockMilestone(): Milestone
163184
*/
164185
private function createMockRepositoryWithChangelog(
165186
string $template,
166-
string $filename = 'CHANGELOG.md'
187+
string $filename = 'CHANGELOG.md',
188+
string $initialBranch = '1.0.x'
167189
): string {
168190
$repo = tempnam(sys_get_temp_dir(), 'CreateReleaseTextViaKeepAChangelog');
169191
Assert::notEmpty($repo);
@@ -181,7 +203,7 @@ private function createMockRepositoryWithChangelog(
181203
(new Process(['git', 'config', 'user.email', '[email protected]'], $repo))->mustRun();
182204
(new Process(['git', 'config', 'user.name', 'Just Me'], $repo))->mustRun();
183205
(new Process(['git', 'commit', '-m', 'Initial import'], $repo))->mustRun();
184-
(new Process(['git', 'switch', '-c', '1.0.x'], $repo))->mustRun();
206+
(new Process(['git', 'switch', '-c', $initialBranch], $repo))->mustRun();
185207

186208
return $repo;
187209
}
@@ -261,5 +283,56 @@ private function checkoutMockRepositoryWithChangelog(string $origin): string
261283
262284
- Nothing.
263285

286+
END;
287+
288+
private const CHANGELOG_MULTI_SECTION = <<< 'END'
289+
# Changelog
290+
291+
All notable changes to this project will be documented in this file, in reverse chronological order by release.
292+
293+
## 2.3.12 - %s
294+
295+
### Added
296+
297+
- Something.
298+
299+
### Changed
300+
301+
- Nothing.
302+
303+
### Deprecated
304+
305+
- Nothing.
306+
307+
### Removed
308+
309+
- Nothing.
310+
311+
### Fixed
312+
313+
- Several things
314+
315+
## 0.1.0 - 2019-01-01
316+
317+
### Added
318+
319+
- Everything.
320+
321+
### Changed
322+
323+
- Nothing.
324+
325+
### Deprecated
326+
327+
- Nothing.
328+
329+
### Removed
330+
331+
- Nothing.
332+
333+
### Fixed
334+
335+
- Nothing.
336+
264337
END;
265338
}

0 commit comments

Comments
 (0)