Skip to content

Commit 5730dbc

Browse files
authored
Merge pull request #67 from weierophinney/hotfix/64-release-notes-normalization
Normalize generated text to strip extra lines and redundant version information
2 parents 0e71d7a + 5423e88 commit 5730dbc

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

src/Github/CreateReleaseTextThroughChangelog.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use function explode;
1717
use function implode;
1818
use function preg_match;
19+
use function preg_quote;
20+
use function preg_replace;
1921
use function str_replace;
2022
use function strtr;
2123
use function trim;
@@ -48,10 +50,13 @@ public function __invoke(
4850
$replacements = [
4951
'%release%' => $this->markdownLink($milestone->title(), $milestone->url()),
5052
'%description%' => (string) $milestone->description(),
51-
'%changelogText%' => $this->normalizeChangelogHeadings($this->generateChangelog->__invoke(
52-
$repositoryName,
53-
$semVerVersion
54-
)),
53+
'%changelogText%' => $this->normalizeChangelog(
54+
$this->generateChangelog->__invoke(
55+
$repositoryName,
56+
$semVerVersion
57+
),
58+
$semVerVersion->fullReleaseName()
59+
),
5560
];
5661

5762
$text = str_replace(
@@ -80,6 +85,15 @@ private function markdownLink(string $text, UriInterface $uri): string
8085
return '[' . $text . '](' . $uri->__toString() . ')';
8186
}
8287

88+
private function normalizeChangelog(string $changelog, string $version): string
89+
{
90+
$changelog = $this->normalizeChangelogHeadings($changelog);
91+
$changelog = $this->removeRedundantVersionHeadings($changelog, $version);
92+
$changelog = $this->collapseMultiLineBreaks($changelog);
93+
94+
return $changelog;
95+
}
96+
8397
/**
8498
* Normalize changelog headings
8599
*
@@ -135,4 +149,14 @@ private function normalizeChangelogHeadings(string $changelog): string
135149

136150
return implode("\n", $lines);
137151
}
152+
153+
private function collapseMultiLineBreaks(string $text): string
154+
{
155+
return preg_replace("/\n\n\n+/s", "\n\n", $text);
156+
}
157+
158+
private function removeRedundantVersionHeadings(string $changelog, string $version): string
159+
{
160+
return preg_replace("/\n\#{3,} " . preg_quote($version, '/') . "\n/s", '', $changelog);
161+
}
138162
}

test/unit/Github/CreateChangelogTextTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,30 @@ public function testGeneratedReleaseText(): void
1919
$generatedReleaseNotes = <<< 'NOTES'
2020
-----
2121
22+
2223
2.12.3
2324
======
2425
2526
- Total issues resolved: 0
2627
- Total pull requests resolved: 1
2728
- Total contributors: 1
2829
30+
2931
-----
3032
33+
3134
Bug
3235
---
3336
37+
3438
- [999: Some bug that got fixed](https://www.example.com/issues/999) thanks to @somebody
3539

3640
NOTES;
3741

3842
$generateChangelog = $this->createMock(GenerateChangelog::class);
3943

4044
$repositoryName = RepositoryName::fromFullName('laminas/repository-name');
41-
$semVerVersion = SemVerVersion::fromMilestoneName('1.0.0');
45+
$semVerVersion = SemVerVersion::fromMilestoneName('2.12.3');
4246

4347
$generateChangelog->expects(self::once())
4448
->method('__invoke')
@@ -53,8 +57,6 @@ public function testGeneratedReleaseText(): void
5357
5458
-----
5559
56-
### 2.12.3
57-
5860
- Total issues resolved: 0
5961
- Total pull requests resolved: 1
6062
- Total contributors: 1
@@ -118,7 +120,7 @@ public function testGeneratedReleaseText(): void
118120
]),
119121
$repositoryName,
120122
$semVerVersion,
121-
BranchName::fromName('1.0.x'),
123+
BranchName::fromName('2.12.x'),
122124
__DIR__
123125
)
124126
->contents()

0 commit comments

Comments
 (0)