diff --git a/CHANGELOG.md b/CHANGELOG.md index 609495e0..02179b41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Changelog/BumpAndCommitChangelogVersionViaKeepAChangelog.php b/src/Changelog/BumpAndCommitChangelogVersionViaKeepAChangelog.php index ca71ffbf..5d018459 100644 --- a/src/Changelog/BumpAndCommitChangelogVersionViaKeepAChangelog.php +++ b/src/Changelog/BumpAndCommitChangelogVersionViaKeepAChangelog.php @@ -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; } @@ -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() diff --git a/src/Changelog/ChangelogExistsViaConsole.php b/src/Changelog/ChangelogExistsViaConsole.php index 4161e03b..ad0b3482 100644 --- a/src/Changelog/ChangelogExistsViaConsole.php +++ b/src/Changelog/ChangelogExistsViaConsole.php @@ -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(); diff --git a/src/Changelog/CommitReleaseChangelogViaKeepAChangelog.php b/src/Changelog/CommitReleaseChangelogViaKeepAChangelog.php index 5bcd8b20..15e32ec6 100644 --- a/src/Changelog/CommitReleaseChangelogViaKeepAChangelog.php +++ b/src/Changelog/CommitReleaseChangelogViaKeepAChangelog.php @@ -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; } @@ -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 )); @@ -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 )); @@ -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') diff --git a/src/Git/CommitFileViaConsole.php b/src/Git/CommitFileViaConsole.php index 340f9efc..7fc2c159 100644 --- a/src/Git/CommitFileViaConsole.php +++ b/src/Git/CommitFileViaConsole.php @@ -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 )); } } diff --git a/src/Github/CreateReleaseTextViaKeepAChangelog.php b/src/Github/CreateReleaseTextViaKeepAChangelog.php index 43672ad8..5c325b20 100644 --- a/src/Github/CreateReleaseTextViaKeepAChangelog.php +++ b/src/Github/CreateReleaseTextViaKeepAChangelog.php @@ -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(); diff --git a/test/unit/Changelog/ChangelogExistsViaConsoleTest.php b/test/unit/Changelog/ChangelogExistsViaConsoleTest.php index 756e6359..8e564616 100644 --- a/test/unit/Changelog/ChangelogExistsViaConsoleTest.php +++ b/test/unit/Changelog/ChangelogExistsViaConsoleTest.php @@ -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 ) ); } @@ -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; + } } diff --git a/test/unit/Changelog/ReleaseChangelogViaKeepAChangelogTest.php b/test/unit/Changelog/ReleaseChangelogViaKeepAChangelogTest.php index 9b967224..c8dbfab7 100644 --- a/test/unit/Changelog/ReleaseChangelogViaKeepAChangelogTest.php +++ b/test/unit/Changelog/ReleaseChangelogViaKeepAChangelogTest.php @@ -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 @@ -104,7 +105,7 @@ public function testNoOpWhenUnableToFindMatchingChangelogEntry(): void self::assertNull( $this->releaseChangelog->__invoke( - $repo, + $checkout, SemVerVersion::fromMilestoneName('1.0.0'), $branch ) @@ -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 @@ -131,7 +133,7 @@ public function testNoOpWhenFailedToSetReleaseDateInChangelogEntry(): void self::assertNull( $this->releaseChangelog->__invoke( - $repo, + $checkout, SemVerVersion::fromMilestoneName('1.0.0'), $branch ) @@ -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 @@ -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') @@ -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); } /** @@ -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 diff --git a/test/unit/Github/CreateReleaseTextViaKeepAChangelogTest.php b/test/unit/Github/CreateReleaseTextViaKeepAChangelogTest.php index f1f4abfa..15e5010a 100644 --- a/test/unit/Github/CreateReleaseTextViaKeepAChangelogTest.php +++ b/test/unit/Github/CreateReleaseTextViaKeepAChangelogTest.php @@ -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())) @@ -37,7 +38,7 @@ public function testReportsCannotCreateReleaseTextIfChangelogFileIsMissing(): vo RepositoryName::fromFullName('example/repo'), SemVerVersion::fromMilestoneName('1.0.0'), BranchName::fromName('1.0.x'), - $repositoryPath + $workingPath ) ); } @@ -48,6 +49,7 @@ public function testReportsCannotCreateReleaseTextIfChangelogFileDoesNotContainV self::INVALID_CHANGELOG, 'CHANGELOG.md' ); + $workingPath = $this->checkoutMockRepositoryWithChangelog($repositoryPath); self::assertFalse( (new CreateReleaseTextViaKeepAChangelog(new ChangelogExistsViaConsole())) @@ -56,7 +58,7 @@ public function testReportsCannotCreateReleaseTextIfChangelogFileDoesNotContainV RepositoryName::fromFullName('example/repo'), SemVerVersion::fromMilestoneName('1.0.0'), BranchName::fromName('1.0.x'), - $repositoryPath + $workingPath ) ); } @@ -68,6 +70,7 @@ public function testReportsCanCreateReleaseWhenChangelogWithVersionExists(): voi $changelogContents, 'CHANGELOG.md' ); + $workingPath = $this->checkoutMockRepositoryWithChangelog($repositoryPath); self::assertTrue( (new CreateReleaseTextViaKeepAChangelog(new ChangelogExistsViaConsole())) @@ -76,7 +79,7 @@ public function testReportsCanCreateReleaseWhenChangelogWithVersionExists(): voi RepositoryName::fromFullName('example/repo'), SemVerVersion::fromMilestoneName('1.0.0'), BranchName::fromName('1.0.x'), - $repositoryPath + $workingPath ) ); } @@ -89,6 +92,7 @@ public function testExtractsReleaseTextViaChangelogFile(): void $changelogContents, 'CHANGELOG.md' ); + $workingPath = $this->checkoutMockRepositoryWithChangelog($repositoryPath); $expected = sprintf(<<< 'END' ### Added @@ -120,7 +124,7 @@ public function testExtractsReleaseTextViaChangelogFile(): void RepositoryName::fromFullName('example/repo'), SemVerVersion::fromMilestoneName('1.0.0'), BranchName::fromName('1.0.x'), - $repositoryPath + $workingPath ) ); } @@ -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