Skip to content

Commit 06b2902

Browse files
authored
Merge pull request #55 from weierophinney/hotfix/changelog-commits
Check for changelog and/or pull contents from changelog using remote origin
2 parents 2e2a7f7 + 9e8082e commit 06b2902

9 files changed

+87
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ All notable changes to this project will be documented in this file, in reverse
2222

2323
### Fixed
2424

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

2727
## 1.2.3 - 2020-08-13
2828

src/Changelog/BumpAndCommitChangelogVersionViaKeepAChangelog.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function __invoke(
5353
): void {
5454
if (! ($this->changelogExists)($sourceBranch, $repositoryDirectory)) {
5555
// No changelog
56-
$this->logger->info('BumpAndCommitChangelog: No CHANGELOG.md file detected');
56+
$this->logger->info('BumpAndCommitChangelogVersion: No CHANGELOG.md file detected');
5757

5858
return;
5959
}
@@ -81,7 +81,7 @@ public function __invoke(
8181
($this->push)($repositoryDirectory, $sourceBranch->name());
8282

8383
$this->logger->info(sprintf(
84-
'BumpAndCommitChangelog: bumped %s to version %s in branch %s',
84+
'BumpAndCommitChangelogVersion: bumped %s to version %s in branch %s',
8585
self::CHANGELOG_FILE,
8686
$newVersion,
8787
$sourceBranch->name()

src/Changelog/ChangelogExistsViaConsole.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function __invoke(
1616
BranchName $sourceBranch,
1717
string $repositoryDirectory
1818
): bool {
19-
$process = new Process(['git', 'show', $sourceBranch->name() . ':CHANGELOG.md'], $repositoryDirectory);
19+
$process = new Process(['git', 'show', 'origin/' . $sourceBranch->name() . ':CHANGELOG.md'], $repositoryDirectory);
2020
$process->run();
2121

2222
return $process->isSuccessful();

src/Changelog/CommitReleaseChangelogViaKeepAChangelog.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function __invoke(
6363
): void {
6464
if (! ($this->changelogExists)($sourceBranch, $repositoryDirectory)) {
6565
// No changelog
66-
$this->logger->info('No CHANGELOG.md file detected');
66+
$this->logger->info('CommitReleaseChangelog: No CHANGELOG.md file detected');
6767

6868
return;
6969
}
@@ -99,7 +99,7 @@ private function updateChangelog(string $changelogFile, string $versionString):
9999

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

112112
if ($event->failed()) {
113113
$this->logger->info(sprintf(
114-
'Failed setting release date for version "%s" in "%s"',
114+
'CommitReleaseChangelog: Failed setting release date for version "%s" in "%s"',
115115
$versionString,
116116
$changelogFile
117117
));
@@ -120,7 +120,7 @@ private function updateChangelog(string $changelogFile, string $versionString):
120120
}
121121

122122
$this->logger->info(sprintf(
123-
'Set release date for version "%s" in "%s" to "%s"',
123+
'CommitReleaseChangelog: Set release date for version "%s" in "%s" to "%s"',
124124
$versionString,
125125
$changelogFile,
126126
$this->clock->now()->format('Y-m-d')

src/Git/CommitFileViaConsole.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ private function assertWeAreOnBranch(
4545
$output = trim($process->getOutput());
4646

4747
Assert::same($output, $expectedBranch->name(), sprintf(
48-
'Cannot commit file %s to branch %s, as a different branch is currently checked out.',
48+
'CommitFile: Cannot commit file %s to branch %s, as a different branch is currently checked out (%s).',
4949
$filename,
50-
$expectedBranch->name()
50+
$expectedBranch->name(),
51+
$output
5152
));
5253
}
5354
}

src/Github/CreateReleaseTextViaKeepAChangelog.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private function fetchChangelogContentsFromBranch(
7676
BranchName $sourceBranch,
7777
string $repositoryDirectory
7878
): string {
79-
$process = new Process(['git', 'show', $sourceBranch->name() . ':CHANGELOG.md'], $repositoryDirectory);
79+
$process = new Process(['git', 'show', 'origin/' . $sourceBranch->name() . ':CHANGELOG.md'], $repositoryDirectory);
8080
$process->mustRun();
8181

8282
$contents = $process->getOutput();

test/unit/Changelog/ChangelogExistsViaConsoleTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,24 @@ class ChangelogExistsViaConsoleTest extends TestCase
2020
{
2121
public function testReturnsFalseWhenChangelogIsNotPresentInBranch(): void
2222
{
23+
$repository = $this->createMockRepositoryWithChangelog();
24+
$workingDir = $this->checkoutMockRepositoryWithChangelog($repository);
2325
self::assertFalse(
2426
(new ChangelogExistsViaConsole())(
2527
BranchName::fromName('0.99.x'),
26-
$this->createMockRepositoryWithChangelog()
28+
$workingDir
2729
)
2830
);
2931
}
3032

3133
public function testReturnsTrueWhenChangelogIsPresentInBranch(): void
3234
{
35+
$repository = $this->createMockRepositoryWithChangelog();
36+
$workingDir = $this->checkoutMockRepositoryWithChangelog($repository);
3337
self::assertTrue(
3438
(new ChangelogExistsViaConsole())(
3539
BranchName::fromName('1.0.x'),
36-
$this->createMockRepositoryWithChangelog()
40+
$workingDir
3741
)
3842
);
3943
}
@@ -90,4 +94,19 @@ private function createMockRepositoryWithChangelog(): string
9094

9195
return $repo;
9296
}
97+
98+
/**
99+
* @psalm-param non-empty-string $origin
100+
* @psalm-return non-empty-string
101+
*/
102+
private function checkoutMockRepositoryWithChangelog(string $origin): string
103+
{
104+
$repo = tempnam(sys_get_temp_dir(), 'CreateReleaseTextViaKeepAChangelog');
105+
Assert::notEmpty($repo);
106+
unlink($repo);
107+
108+
(new Process(['git', 'clone', $origin, $repo]))->mustRun();
109+
110+
return $repo;
111+
}
93112
}

test/unit/Changelog/ReleaseChangelogViaKeepAChangelogTest.php

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,14 @@ public function testNoOpWhenChangelogFileDoesNotExist(): void
8686

8787
public function testNoOpWhenUnableToFindMatchingChangelogEntry(): void
8888
{
89-
$repo = $this->createMockRepositoryWithChangelog(self::INVALID_CHANGELOG);
90-
$branch = BranchName::fromName('1.0.x');
89+
$repo = $this->createMockRepositoryWithChangelog(self::INVALID_CHANGELOG);
90+
$checkout = $this->checkoutMockRepositoryWithChangelog($repo);
91+
$branch = BranchName::fromName('1.0.x');
9192

9293
$this->checkoutBranch
9394
->expects($this->once())
9495
->method('__invoke')
95-
->with($repo, $branch);
96+
->with($checkout, $branch);
9697

9798
$this
9899
->logger
@@ -104,7 +105,7 @@ public function testNoOpWhenUnableToFindMatchingChangelogEntry(): void
104105

105106
self::assertNull(
106107
$this->releaseChangelog->__invoke(
107-
$repo,
108+
$checkout,
108109
SemVerVersion::fromMilestoneName('1.0.0'),
109110
$branch
110111
)
@@ -113,13 +114,14 @@ public function testNoOpWhenUnableToFindMatchingChangelogEntry(): void
113114

114115
public function testNoOpWhenFailedToSetReleaseDateInChangelogEntry(): void
115116
{
116-
$repo = $this->createMockRepositoryWithChangelog(self::RELEASED_CHANGELOG);
117-
$branch = BranchName::fromName('1.0.x');
117+
$repo = $this->createMockRepositoryWithChangelog(self::RELEASED_CHANGELOG);
118+
$checkout = $this->checkoutMockRepositoryWithChangelog($repo);
119+
$branch = BranchName::fromName('1.0.x');
118120

119121
$this->checkoutBranch
120122
->expects($this->once())
121123
->method('__invoke')
122-
->with($repo, $branch);
124+
->with($checkout, $branch);
123125

124126
$this
125127
->logger
@@ -131,7 +133,7 @@ public function testNoOpWhenFailedToSetReleaseDateInChangelogEntry(): void
131133

132134
self::assertNull(
133135
$this->releaseChangelog->__invoke(
134-
$repo,
136+
$checkout,
135137
SemVerVersion::fromMilestoneName('1.0.0'),
136138
$branch
137139
)
@@ -143,12 +145,13 @@ public function testWritesCommitsAndPushesChangelogWhenFoundAndReadyToRelease():
143145
$existingChangelog = sprintf(self::READY_CHANGELOG, 'TBD');
144146
$expectedChangelog = sprintf(self::READY_CHANGELOG, $this->clock->now()->format('Y-m-d'));
145147
$repositoryPath = $this->createMockRepositoryWithChangelog($existingChangelog);
148+
$checkout = $this->checkoutMockRepositoryWithChangelog($repositoryPath);
146149
$sourceBranch = BranchName::fromName('1.0.x');
147150

148151
$this->checkoutBranch
149152
->expects($this->once())
150153
->method('__invoke')
151-
->with($repositoryPath, $sourceBranch);
154+
->with($checkout, $sourceBranch);
152155

153156
$this
154157
->logger
@@ -160,7 +163,7 @@ public function testWritesCommitsAndPushesChangelogWhenFoundAndReadyToRelease():
160163
->expects($this->once())
161164
->method('__invoke')
162165
->with(
163-
$this->equalTo($repositoryPath),
166+
$this->equalTo($checkout),
164167
$this->equalTo($sourceBranch),
165168
$this->equalTo('CHANGELOG.md'),
166169
$this->stringContains('1.0.0 readiness')
@@ -170,19 +173,19 @@ public function testWritesCommitsAndPushesChangelogWhenFoundAndReadyToRelease():
170173
->expects($this->once())
171174
->method('__invoke')
172175
->with(
173-
$this->equalTo($repositoryPath),
176+
$this->equalTo($checkout),
174177
$this->equalTo('1.0.x'),
175178
);
176179

177180
self::assertNull(
178181
$this->releaseChangelog->__invoke(
179-
$repositoryPath,
182+
$checkout,
180183
SemVerVersion::fromMilestoneName('1.0.0'),
181184
BranchName::fromName('1.0.x')
182185
)
183186
);
184187

185-
$this->assertStringEqualsFile($repositoryPath . '/CHANGELOG.md', $expectedChangelog);
188+
$this->assertStringEqualsFile($checkout . '/CHANGELOG.md', $expectedChangelog);
186189
}
187190

188191
/**
@@ -213,6 +216,21 @@ private function createMockRepositoryWithChangelog(
213216
return $repo;
214217
}
215218

219+
/**
220+
* @psalm-param non-empty-string $origin
221+
* @psalm-return non-empty-string
222+
*/
223+
private function checkoutMockRepositoryWithChangelog(string $origin): string
224+
{
225+
$repo = tempnam(sys_get_temp_dir(), 'CreateReleaseTextViaKeepAChangelog');
226+
Assert::notEmpty($repo);
227+
unlink($repo);
228+
229+
(new Process(['git', 'clone', $origin, $repo]))->mustRun();
230+
231+
return $repo;
232+
}
233+
216234
private const INVALID_CHANGELOG = <<< 'END'
217235
# NOT A CHANGELOG
218236

test/unit/Github/CreateReleaseTextViaKeepAChangelogTest.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function testReportsCannotCreateReleaseTextIfChangelogFileIsMissing(): vo
2929
self::INVALID_CHANGELOG,
3030
'NOT-A-CHANGELOG.md'
3131
);
32+
$workingPath = $this->checkoutMockRepositoryWithChangelog($repositoryPath);
3233

3334
self::assertFalse(
3435
(new CreateReleaseTextViaKeepAChangelog(new ChangelogExistsViaConsole()))
@@ -37,7 +38,7 @@ public function testReportsCannotCreateReleaseTextIfChangelogFileIsMissing(): vo
3738
RepositoryName::fromFullName('example/repo'),
3839
SemVerVersion::fromMilestoneName('1.0.0'),
3940
BranchName::fromName('1.0.x'),
40-
$repositoryPath
41+
$workingPath
4142
)
4243
);
4344
}
@@ -48,6 +49,7 @@ public function testReportsCannotCreateReleaseTextIfChangelogFileDoesNotContainV
4849
self::INVALID_CHANGELOG,
4950
'CHANGELOG.md'
5051
);
52+
$workingPath = $this->checkoutMockRepositoryWithChangelog($repositoryPath);
5153

5254
self::assertFalse(
5355
(new CreateReleaseTextViaKeepAChangelog(new ChangelogExistsViaConsole()))
@@ -56,7 +58,7 @@ public function testReportsCannotCreateReleaseTextIfChangelogFileDoesNotContainV
5658
RepositoryName::fromFullName('example/repo'),
5759
SemVerVersion::fromMilestoneName('1.0.0'),
5860
BranchName::fromName('1.0.x'),
59-
$repositoryPath
61+
$workingPath
6062
)
6163
);
6264
}
@@ -68,6 +70,7 @@ public function testReportsCanCreateReleaseWhenChangelogWithVersionExists(): voi
6870
$changelogContents,
6971
'CHANGELOG.md'
7072
);
73+
$workingPath = $this->checkoutMockRepositoryWithChangelog($repositoryPath);
7174

7275
self::assertTrue(
7376
(new CreateReleaseTextViaKeepAChangelog(new ChangelogExistsViaConsole()))
@@ -76,7 +79,7 @@ public function testReportsCanCreateReleaseWhenChangelogWithVersionExists(): voi
7679
RepositoryName::fromFullName('example/repo'),
7780
SemVerVersion::fromMilestoneName('1.0.0'),
7881
BranchName::fromName('1.0.x'),
79-
$repositoryPath
82+
$workingPath
8083
)
8184
);
8285
}
@@ -89,6 +92,7 @@ public function testExtractsReleaseTextViaChangelogFile(): void
8992
$changelogContents,
9093
'CHANGELOG.md'
9194
);
95+
$workingPath = $this->checkoutMockRepositoryWithChangelog($repositoryPath);
9296

9397
$expected = sprintf(<<< 'END'
9498
### Added
@@ -120,7 +124,7 @@ public function testExtractsReleaseTextViaChangelogFile(): void
120124
RepositoryName::fromFullName('example/repo'),
121125
SemVerVersion::fromMilestoneName('1.0.0'),
122126
BranchName::fromName('1.0.x'),
123-
$repositoryPath
127+
$workingPath
124128
)
125129
);
126130
}
@@ -170,6 +174,21 @@ private function createMockRepositoryWithChangelog(
170174
return $repo;
171175
}
172176

177+
/**
178+
* @psalm-param non-empty-string $origin
179+
* @psalm-return non-empty-string
180+
*/
181+
private function checkoutMockRepositoryWithChangelog(string $origin): string
182+
{
183+
$repo = tempnam(sys_get_temp_dir(), 'CreateReleaseTextViaKeepAChangelog');
184+
Assert::notEmpty($repo);
185+
unlink($repo);
186+
187+
(new Process(['git', 'clone', $origin, $repo]))->mustRun();
188+
189+
return $repo;
190+
}
191+
173192
private const INVALID_CHANGELOG = <<< 'END'
174193
# NOT A CHANGELOG
175194

0 commit comments

Comments
 (0)