Skip to content

Check for changelog and/or pull contents from changelog using remote origin #55

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

Merged
merged 4 commits into from
Aug 14, 2020
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#55](https://github.com/laminas/automatic-releases/pull/55) fixes issues with identifying and retrieving `CHANGELOG.md` contents from non-default branches.

## 1.2.3 - 2020-08-13

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __invoke(
): void {
if (! ($this->changelogExists)($sourceBranch, $repositoryDirectory)) {
// No changelog
$this->logger->info('BumpAndCommitChangelog: No CHANGELOG.md file detected');
$this->logger->info('BumpAndCommitChangelogVersion: No CHANGELOG.md file detected');

return;
}
Expand Down Expand Up @@ -81,7 +81,7 @@ public function __invoke(
($this->push)($repositoryDirectory, $sourceBranch->name());

$this->logger->info(sprintf(
'BumpAndCommitChangelog: bumped %s to version %s in branch %s',
'BumpAndCommitChangelogVersion: bumped %s to version %s in branch %s',
self::CHANGELOG_FILE,
$newVersion,
$sourceBranch->name()
Expand Down
2 changes: 1 addition & 1 deletion src/Changelog/ChangelogExistsViaConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __invoke(
BranchName $sourceBranch,
string $repositoryDirectory
): bool {
$process = new Process(['git', 'show', $sourceBranch->name() . ':CHANGELOG.md'], $repositoryDirectory);
$process = new Process(['git', 'show', 'origin/' . $sourceBranch->name() . ':CHANGELOG.md'], $repositoryDirectory);
$process->run();

return $process->isSuccessful();
Expand Down
8 changes: 4 additions & 4 deletions src/Changelog/CommitReleaseChangelogViaKeepAChangelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function __invoke(
): void {
if (! ($this->changelogExists)($sourceBranch, $repositoryDirectory)) {
// No changelog
$this->logger->info('No CHANGELOG.md file detected');
$this->logger->info('CommitReleaseChangelog: No CHANGELOG.md file detected');

return;
}
Expand Down Expand Up @@ -99,7 +99,7 @@ private function updateChangelog(string $changelogFile, string $versionString):

if ($event->failed()) {
$this->logger->info(sprintf(
'Failed to find release version "%s" in "%s"',
'CommitReleaseChangelog: Failed to find release version "%s" in "%s"',
$versionString,
$changelogFile
));
Expand All @@ -111,7 +111,7 @@ private function updateChangelog(string $changelogFile, string $versionString):

if ($event->failed()) {
$this->logger->info(sprintf(
'Failed setting release date for version "%s" in "%s"',
'CommitReleaseChangelog: Failed setting release date for version "%s" in "%s"',
$versionString,
$changelogFile
));
Expand All @@ -120,7 +120,7 @@ private function updateChangelog(string $changelogFile, string $versionString):
}

$this->logger->info(sprintf(
'Set release date for version "%s" in "%s" to "%s"',
'CommitReleaseChangelog: Set release date for version "%s" in "%s" to "%s"',
$versionString,
$changelogFile,
$this->clock->now()->format('Y-m-d')
Expand Down
5 changes: 3 additions & 2 deletions src/Git/CommitFileViaConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ private function assertWeAreOnBranch(
$output = trim($process->getOutput());

Assert::same($output, $expectedBranch->name(), sprintf(
'Cannot commit file %s to branch %s, as a different branch is currently checked out.',
'CommitFile: Cannot commit file %s to branch %s, as a different branch is currently checked out (%s).',
$filename,
$expectedBranch->name()
$expectedBranch->name(),
$output
));
}
}
2 changes: 1 addition & 1 deletion src/Github/CreateReleaseTextViaKeepAChangelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private function fetchChangelogContentsFromBranch(
BranchName $sourceBranch,
string $repositoryDirectory
): string {
$process = new Process(['git', 'show', $sourceBranch->name() . ':CHANGELOG.md'], $repositoryDirectory);
$process = new Process(['git', 'show', 'origin/' . $sourceBranch->name() . ':CHANGELOG.md'], $repositoryDirectory);
$process->mustRun();

$contents = $process->getOutput();
Expand Down
23 changes: 21 additions & 2 deletions test/unit/Changelog/ChangelogExistsViaConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,24 @@ class ChangelogExistsViaConsoleTest extends TestCase
{
public function testReturnsFalseWhenChangelogIsNotPresentInBranch(): void
{
$repository = $this->createMockRepositoryWithChangelog();
$workingDir = $this->checkoutMockRepositoryWithChangelog($repository);
self::assertFalse(
(new ChangelogExistsViaConsole())(
BranchName::fromName('0.99.x'),
$this->createMockRepositoryWithChangelog()
$workingDir
)
);
}

public function testReturnsTrueWhenChangelogIsPresentInBranch(): void
{
$repository = $this->createMockRepositoryWithChangelog();
$workingDir = $this->checkoutMockRepositoryWithChangelog($repository);
self::assertTrue(
(new ChangelogExistsViaConsole())(
BranchName::fromName('1.0.x'),
$this->createMockRepositoryWithChangelog()
$workingDir
)
);
}
Expand Down Expand Up @@ -90,4 +94,19 @@ private function createMockRepositoryWithChangelog(): string

return $repo;
}

/**
* @psalm-param non-empty-string $origin
* @psalm-return non-empty-string
*/
private function checkoutMockRepositoryWithChangelog(string $origin): string
{
$repo = tempnam(sys_get_temp_dir(), 'CreateReleaseTextViaKeepAChangelog');
Assert::notEmpty($repo);
unlink($repo);

(new Process(['git', 'clone', $origin, $repo]))->mustRun();

return $repo;
}
}
44 changes: 31 additions & 13 deletions test/unit/Changelog/ReleaseChangelogViaKeepAChangelogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@ public function testNoOpWhenChangelogFileDoesNotExist(): void

public function testNoOpWhenUnableToFindMatchingChangelogEntry(): void
{
$repo = $this->createMockRepositoryWithChangelog(self::INVALID_CHANGELOG);
$branch = BranchName::fromName('1.0.x');
$repo = $this->createMockRepositoryWithChangelog(self::INVALID_CHANGELOG);
$checkout = $this->checkoutMockRepositoryWithChangelog($repo);
$branch = BranchName::fromName('1.0.x');

$this->checkoutBranch
->expects($this->once())
->method('__invoke')
->with($repo, $branch);
->with($checkout, $branch);

$this
->logger
Expand All @@ -104,7 +105,7 @@ public function testNoOpWhenUnableToFindMatchingChangelogEntry(): void

self::assertNull(
$this->releaseChangelog->__invoke(
$repo,
$checkout,
SemVerVersion::fromMilestoneName('1.0.0'),
$branch
)
Expand All @@ -113,13 +114,14 @@ public function testNoOpWhenUnableToFindMatchingChangelogEntry(): void

public function testNoOpWhenFailedToSetReleaseDateInChangelogEntry(): void
{
$repo = $this->createMockRepositoryWithChangelog(self::RELEASED_CHANGELOG);
$branch = BranchName::fromName('1.0.x');
$repo = $this->createMockRepositoryWithChangelog(self::RELEASED_CHANGELOG);
$checkout = $this->checkoutMockRepositoryWithChangelog($repo);
$branch = BranchName::fromName('1.0.x');

$this->checkoutBranch
->expects($this->once())
->method('__invoke')
->with($repo, $branch);
->with($checkout, $branch);

$this
->logger
Expand All @@ -131,7 +133,7 @@ public function testNoOpWhenFailedToSetReleaseDateInChangelogEntry(): void

self::assertNull(
$this->releaseChangelog->__invoke(
$repo,
$checkout,
SemVerVersion::fromMilestoneName('1.0.0'),
$branch
)
Expand All @@ -143,12 +145,13 @@ public function testWritesCommitsAndPushesChangelogWhenFoundAndReadyToRelease():
$existingChangelog = sprintf(self::READY_CHANGELOG, 'TBD');
$expectedChangelog = sprintf(self::READY_CHANGELOG, $this->clock->now()->format('Y-m-d'));
$repositoryPath = $this->createMockRepositoryWithChangelog($existingChangelog);
$checkout = $this->checkoutMockRepositoryWithChangelog($repositoryPath);
$sourceBranch = BranchName::fromName('1.0.x');

$this->checkoutBranch
->expects($this->once())
->method('__invoke')
->with($repositoryPath, $sourceBranch);
->with($checkout, $sourceBranch);

$this
->logger
Expand All @@ -160,7 +163,7 @@ public function testWritesCommitsAndPushesChangelogWhenFoundAndReadyToRelease():
->expects($this->once())
->method('__invoke')
->with(
$this->equalTo($repositoryPath),
$this->equalTo($checkout),
$this->equalTo($sourceBranch),
$this->equalTo('CHANGELOG.md'),
$this->stringContains('1.0.0 readiness')
Expand All @@ -170,19 +173,19 @@ public function testWritesCommitsAndPushesChangelogWhenFoundAndReadyToRelease():
->expects($this->once())
->method('__invoke')
->with(
$this->equalTo($repositoryPath),
$this->equalTo($checkout),
$this->equalTo('1.0.x'),
);

self::assertNull(
$this->releaseChangelog->__invoke(
$repositoryPath,
$checkout,
SemVerVersion::fromMilestoneName('1.0.0'),
BranchName::fromName('1.0.x')
)
);

$this->assertStringEqualsFile($repositoryPath . '/CHANGELOG.md', $expectedChangelog);
$this->assertStringEqualsFile($checkout . '/CHANGELOG.md', $expectedChangelog);
}

/**
Expand Down Expand Up @@ -213,6 +216,21 @@ private function createMockRepositoryWithChangelog(
return $repo;
}

/**
* @psalm-param non-empty-string $origin
* @psalm-return non-empty-string
*/
private function checkoutMockRepositoryWithChangelog(string $origin): string
{
$repo = tempnam(sys_get_temp_dir(), 'CreateReleaseTextViaKeepAChangelog');
Assert::notEmpty($repo);
unlink($repo);

(new Process(['git', 'clone', $origin, $repo]))->mustRun();

return $repo;
}

private const INVALID_CHANGELOG = <<< 'END'
# NOT A CHANGELOG
Expand Down
27 changes: 23 additions & 4 deletions test/unit/Github/CreateReleaseTextViaKeepAChangelogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function testReportsCannotCreateReleaseTextIfChangelogFileIsMissing(): vo
self::INVALID_CHANGELOG,
'NOT-A-CHANGELOG.md'
);
$workingPath = $this->checkoutMockRepositoryWithChangelog($repositoryPath);

self::assertFalse(
(new CreateReleaseTextViaKeepAChangelog(new ChangelogExistsViaConsole()))
Expand All @@ -37,7 +38,7 @@ public function testReportsCannotCreateReleaseTextIfChangelogFileIsMissing(): vo
RepositoryName::fromFullName('example/repo'),
SemVerVersion::fromMilestoneName('1.0.0'),
BranchName::fromName('1.0.x'),
$repositoryPath
$workingPath
)
);
}
Expand All @@ -48,6 +49,7 @@ public function testReportsCannotCreateReleaseTextIfChangelogFileDoesNotContainV
self::INVALID_CHANGELOG,
'CHANGELOG.md'
);
$workingPath = $this->checkoutMockRepositoryWithChangelog($repositoryPath);

self::assertFalse(
(new CreateReleaseTextViaKeepAChangelog(new ChangelogExistsViaConsole()))
Expand All @@ -56,7 +58,7 @@ public function testReportsCannotCreateReleaseTextIfChangelogFileDoesNotContainV
RepositoryName::fromFullName('example/repo'),
SemVerVersion::fromMilestoneName('1.0.0'),
BranchName::fromName('1.0.x'),
$repositoryPath
$workingPath
)
);
}
Expand All @@ -68,6 +70,7 @@ public function testReportsCanCreateReleaseWhenChangelogWithVersionExists(): voi
$changelogContents,
'CHANGELOG.md'
);
$workingPath = $this->checkoutMockRepositoryWithChangelog($repositoryPath);

self::assertTrue(
(new CreateReleaseTextViaKeepAChangelog(new ChangelogExistsViaConsole()))
Expand All @@ -76,7 +79,7 @@ public function testReportsCanCreateReleaseWhenChangelogWithVersionExists(): voi
RepositoryName::fromFullName('example/repo'),
SemVerVersion::fromMilestoneName('1.0.0'),
BranchName::fromName('1.0.x'),
$repositoryPath
$workingPath
)
);
}
Expand All @@ -89,6 +92,7 @@ public function testExtractsReleaseTextViaChangelogFile(): void
$changelogContents,
'CHANGELOG.md'
);
$workingPath = $this->checkoutMockRepositoryWithChangelog($repositoryPath);

$expected = sprintf(<<< 'END'
### Added
Expand Down Expand Up @@ -120,7 +124,7 @@ public function testExtractsReleaseTextViaChangelogFile(): void
RepositoryName::fromFullName('example/repo'),
SemVerVersion::fromMilestoneName('1.0.0'),
BranchName::fromName('1.0.x'),
$repositoryPath
$workingPath
)
);
}
Expand Down Expand Up @@ -170,6 +174,21 @@ private function createMockRepositoryWithChangelog(
return $repo;
}

/**
* @psalm-param non-empty-string $origin
* @psalm-return non-empty-string
*/
private function checkoutMockRepositoryWithChangelog(string $origin): string
{
$repo = tempnam(sys_get_temp_dir(), 'CreateReleaseTextViaKeepAChangelog');
Assert::notEmpty($repo);
unlink($repo);

(new Process(['git', 'clone', $origin, $repo]))->mustRun();

return $repo;
}

private const INVALID_CHANGELOG = <<< 'END'
# NOT A CHANGELOG
Expand Down