Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip empty sections from keep-a-changelog release notes #68

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 18 additions & 25 deletions src/Github/CreateReleaseTextViaKeepAChangelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,25 @@
use Symfony\Component\Process\Process;
use Webmozart\Assert\Assert;

use function array_reduce;
use function count;
use function explode;
use function implode;
use function preg_match;
use function preg_quote;
use function preg_replace;
use function sprintf;
use function str_replace;

class CreateReleaseTextViaKeepAChangelog implements CreateReleaseText
{
private const DEFAULT_CONTENTS = <<< 'CONTENTS'
### Added

- Nothing.

### Changed

- Nothing.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- Nothing.

CONTENTS;

/** @psalm-var list<string> */
private const DEFAULT_SECTIONS = [
'Added',
'Changed',
'Deprecated',
'Removed',
'Fixed',
];

private ChangelogExists $changelogExists;
private Clock $clock;
Expand Down Expand Up @@ -152,7 +137,15 @@ private function updateReleaseDate(string $changelog, string $version): string
*/
private function removeDefaultContents(string $changelog): string
{
$contents = str_replace(self::DEFAULT_CONTENTS, '', $changelog);
$contents = array_reduce(
self::DEFAULT_SECTIONS,
static fn (string $changelog, string $section): string => preg_replace(
"/\n\#{3} " . $section . "\n\n- Nothing.\n/s",
'',
$changelog
),
$changelog
);
Assert::notEmpty($contents);

return $contents;
Expand Down
107 changes: 90 additions & 17 deletions test/unit/Github/CreateReleaseTextViaKeepAChangelogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,43 @@ public function testExtractsReleaseTextViaChangelogFile(): void
### Added

- Everything.
END, $date);

self::assertStringContainsString(
$expected,
(new CreateReleaseTextViaKeepAChangelog(new ChangelogExistsViaConsole(), $this->clock))
->__invoke(
$this->createMockMilestone(),
RepositoryName::fromFullName('example/repo'),
SemVerVersion::fromMilestoneName('1.0.0'),
BranchName::fromName('1.0.x'),
$workingPath
)
->contents()
);
}

public function testExtractsNonEmptySectionsForVersionViaChangelogFile(): void
{
$date = $this->clock->now()->format('Y-m-d');
$changelogContents = sprintf(self::CHANGELOG_MULTI_SECTION, $date);
$repositoryPath = $this->createMockRepositoryWithChangelog(
$changelogContents,
'CHANGELOG.md',
'2.3.x'
);
$workingPath = $this->checkoutMockRepositoryWithChangelog($repositoryPath);

$expected = sprintf(<<< 'END'
## 2.3.12 - %s

### Changed

- Nothing.

### Deprecated

- Nothing.

### Removed

- Nothing.
### Added

- Something.

### Fixed
- Nothing.

- Several things
END, $date);

self::assertStringContainsString(
Expand All @@ -133,8 +154,8 @@ public function testExtractsReleaseTextViaChangelogFile(): void
->__invoke(
$this->createMockMilestone(),
RepositoryName::fromFullName('example/repo'),
SemVerVersion::fromMilestoneName('1.0.0'),
BranchName::fromName('1.0.x'),
SemVerVersion::fromMilestoneName('2.3.12'),
BranchName::fromName('2.3.x'),
$workingPath
)
->contents()
Expand Down Expand Up @@ -163,7 +184,8 @@ private function createMockMilestone(): Milestone
*/
private function createMockRepositoryWithChangelog(
string $template,
string $filename = 'CHANGELOG.md'
string $filename = 'CHANGELOG.md',
string $initialBranch = '1.0.x'
): string {
$repo = tempnam(sys_get_temp_dir(), 'CreateReleaseTextViaKeepAChangelog');
Assert::notEmpty($repo);
Expand All @@ -181,7 +203,7 @@ private function createMockRepositoryWithChangelog(
(new Process(['git', 'config', 'user.email', 'me@example.com'], $repo))->mustRun();
(new Process(['git', 'config', 'user.name', 'Just Me'], $repo))->mustRun();
(new Process(['git', 'commit', '-m', 'Initial import'], $repo))->mustRun();
(new Process(['git', 'switch', '-c', '1.0.x'], $repo))->mustRun();
(new Process(['git', 'switch', '-c', $initialBranch], $repo))->mustRun();

return $repo;
}
Expand Down Expand Up @@ -261,5 +283,56 @@ private function checkoutMockRepositoryWithChangelog(string $origin): string

- Nothing.

END;

private const CHANGELOG_MULTI_SECTION = <<< 'END'
# Changelog

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 2.3.12 - %s

### Added

- Something.

### Changed

- Nothing.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- Several things

## 0.1.0 - 2019-01-01

### Added

- Everything.

### Changed

- Nothing.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- Nothing.

END;
}