Skip to content

Commit

Permalink
fix: normalize generated text to strip extra lines and redundant vers…
Browse files Browse the repository at this point in the history
…ion information

This patch fixes two issues in the jwage/changelog-generator -generated
notes:

- It removes extra empty lines.
- It removes extra headings that only note the version number (this is
  always on the release page, always in the changelog, and at the top of
  the release notes already)

Fixes laminas#64

Signed-off-by: Matthew Weier O'Phinney <matthew@weierophinney.net>
  • Loading branch information
weierophinney committed Sep 3, 2020
1 parent 0e71d7a commit 5423e88
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
32 changes: 28 additions & 4 deletions src/Github/CreateReleaseTextThroughChangelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use function explode;
use function implode;
use function preg_match;
use function preg_quote;
use function preg_replace;
use function str_replace;
use function strtr;
use function trim;
Expand Down Expand Up @@ -48,10 +50,13 @@ public function __invoke(
$replacements = [
'%release%' => $this->markdownLink($milestone->title(), $milestone->url()),
'%description%' => (string) $milestone->description(),
'%changelogText%' => $this->normalizeChangelogHeadings($this->generateChangelog->__invoke(
$repositoryName,
$semVerVersion
)),
'%changelogText%' => $this->normalizeChangelog(
$this->generateChangelog->__invoke(
$repositoryName,
$semVerVersion
),
$semVerVersion->fullReleaseName()
),
];

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

private function normalizeChangelog(string $changelog, string $version): string
{
$changelog = $this->normalizeChangelogHeadings($changelog);
$changelog = $this->removeRedundantVersionHeadings($changelog, $version);
$changelog = $this->collapseMultiLineBreaks($changelog);

return $changelog;
}

/**
* Normalize changelog headings
*
Expand Down Expand Up @@ -135,4 +149,14 @@ private function normalizeChangelogHeadings(string $changelog): string

return implode("\n", $lines);
}

private function collapseMultiLineBreaks(string $text): string
{
return preg_replace("/\n\n\n+/s", "\n\n", $text);
}

private function removeRedundantVersionHeadings(string $changelog, string $version): string
{
return preg_replace("/\n\#{3,} " . preg_quote($version, '/') . "\n/s", '', $changelog);
}
}
10 changes: 6 additions & 4 deletions test/unit/Github/CreateChangelogTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,30 @@ public function testGeneratedReleaseText(): void
$generatedReleaseNotes = <<< 'NOTES'
-----
2.12.3
======
- Total issues resolved: 0
- Total pull requests resolved: 1
- Total contributors: 1
-----
Bug
---
- [999: Some bug that got fixed](https://www.example.com/issues/999) thanks to @somebody
NOTES;

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

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

$generateChangelog->expects(self::once())
->method('__invoke')
Expand All @@ -53,8 +57,6 @@ public function testGeneratedReleaseText(): void
-----
### 2.12.3
- Total issues resolved: 0
- Total pull requests resolved: 1
- Total contributors: 1
Expand Down Expand Up @@ -118,7 +120,7 @@ public function testGeneratedReleaseText(): void
]),
$repositoryName,
$semVerVersion,
BranchName::fromName('1.0.x'),
BranchName::fromName('2.12.x'),
__DIR__
)
->contents()
Expand Down

0 comments on commit 5423e88

Please sign in to comment.