-
-
Notifications
You must be signed in to change notification settings - Fork 22
Idempotent operations for reading, checkout branch prior to writing #47
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
Ocramius
merged 6 commits into
laminas:1.2.x
from
weierophinney:hotfix/fix-changelog-operations
Aug 12, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fb3c2a3
fix: test for changelog in branch idempotently; switch to release bra…
weierophinney aa5da1f
fix: do not checkout source branch for read-only operations
weierophinney 4dcc849
refactor: extract ChangelogExists interface and implementation
weierophinney 600b3ae
refactor: use `ChangelogExists` anywhere we need to check for a chang…
weierophinney f67d56a
qa: ensure we set git author before committing in tests
weierophinney 00a391d
Updates CHANGELOG with entry for #47
weierophinney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\AutomaticReleases\Changelog; | ||
|
||
use Laminas\AutomaticReleases\Git\Value\BranchName; | ||
|
||
interface ChangelogExists | ||
{ | ||
/** | ||
* @param non-empty-string $repositoryDirectory | ||
*/ | ||
public function __invoke( | ||
BranchName $sourceBranch, | ||
string $repositoryDirectory | ||
): bool; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\AutomaticReleases\Changelog; | ||
|
||
use Laminas\AutomaticReleases\Git\Value\BranchName; | ||
use Symfony\Component\Process\Process; | ||
|
||
class ChangelogExistsViaConsole implements ChangelogExists | ||
{ | ||
/** | ||
* @param non-empty-string $repositoryDirectory | ||
*/ | ||
public function __invoke( | ||
BranchName $sourceBranch, | ||
string $repositoryDirectory | ||
): bool { | ||
$process = new Process(['git', 'show', $sourceBranch->name() . ':CHANGELOG.md'], $repositoryDirectory); | ||
$process->run(); | ||
|
||
return $process->isSuccessful(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,19 +5,25 @@ | |
namespace Laminas\AutomaticReleases\Github; | ||
|
||
use InvalidArgumentException; | ||
use Laminas\AutomaticReleases\Changelog\ChangelogExists; | ||
use Laminas\AutomaticReleases\Git\Value\BranchName; | ||
use Laminas\AutomaticReleases\Git\Value\SemVerVersion; | ||
use Laminas\AutomaticReleases\Github\Api\GraphQL\Query\GetMilestoneChangelog\Response\Milestone; | ||
use Laminas\AutomaticReleases\Github\Value\RepositoryName; | ||
use Phly\KeepAChangelog\Common\ChangelogParser; | ||
use Phly\KeepAChangelog\Exception\ExceptionInterface; | ||
use Symfony\Component\Process\Process; | ||
use Webmozart\Assert\Assert; | ||
|
||
use function file_exists; | ||
use function file_get_contents; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like that these filesystem access operations went away from here 👍 |
||
|
||
class CreateReleaseTextViaKeepAChangelog implements CreateReleaseText | ||
{ | ||
private ChangelogExists $changelogExists; | ||
|
||
public function __construct(ChangelogExists $changelogExists) | ||
{ | ||
$this->changelogExists = $changelogExists; | ||
} | ||
|
||
public function __invoke( | ||
Milestone $milestone, | ||
RepositoryName $repositoryName, | ||
|
@@ -27,7 +33,7 @@ public function __invoke( | |
): string { | ||
$changelog = (new ChangelogParser()) | ||
->findChangelogForVersion( | ||
file_get_contents($repositoryDirectory . '/CHANGELOG.md'), | ||
$this->fetchChangelogContentsFromBranch($sourceBranch, $repositoryDirectory), | ||
$semVerVersion->fullReleaseName() | ||
); | ||
|
||
|
@@ -43,15 +49,14 @@ public function canCreateReleaseText( | |
BranchName $sourceBranch, | ||
string $repositoryDirectory | ||
): bool { | ||
$changelogFile = $repositoryDirectory . '/CHANGELOG.md'; | ||
if (! file_exists($changelogFile)) { | ||
if (! ($this->changelogExists)($sourceBranch, $repositoryDirectory)) { | ||
return false; | ||
} | ||
|
||
try { | ||
$changelog = (new ChangelogParser()) | ||
->findChangelogForVersion( | ||
file_get_contents($changelogFile), | ||
$this->fetchChangelogContentsFromBranch($sourceBranch, $repositoryDirectory), | ||
$semVerVersion->fullReleaseName() | ||
); | ||
|
||
|
@@ -62,4 +67,21 @@ public function canCreateReleaseText( | |
return false; | ||
} | ||
} | ||
|
||
/** | ||
* @psalm-param non-empty-string $repositoryDirectory | ||
* @psalm-return non-empty-string | ||
*/ | ||
private function fetchChangelogContentsFromBranch( | ||
BranchName $sourceBranch, | ||
string $repositoryDirectory | ||
): string { | ||
$process = new Process(['git', 'show', $sourceBranch->name() . ':CHANGELOG.md'], $repositoryDirectory); | ||
$process->mustRun(); | ||
|
||
$contents = $process->getOutput(); | ||
Assert::notEmpty($contents); | ||
|
||
return $contents; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
|
||
use Laminas\AutomaticReleases\Changelog\BumpAndCommitChangelogVersion; | ||
use Laminas\AutomaticReleases\Changelog\BumpAndCommitChangelogVersionViaKeepAChangelog; | ||
use Laminas\AutomaticReleases\Changelog\ChangelogExists; | ||
use Laminas\AutomaticReleases\Changelog\ChangelogExistsViaConsole; | ||
use Laminas\AutomaticReleases\Git\CheckoutBranch; | ||
use Laminas\AutomaticReleases\Git\CommitFile; | ||
use Laminas\AutomaticReleases\Git\Push; | ||
|
@@ -44,6 +46,7 @@ protected function setUp(): void | |
$this->push = $this->createMock(Push::class); | ||
$this->logger = $this->createMock(LoggerInterface::class); | ||
$this->bumpAndCommitChangelog = new BumpAndCommitChangelogVersionViaKeepAChangelog( | ||
new ChangelogExistsViaConsole(), | ||
$this->checkoutBranch, | ||
$this->commitFile, | ||
$this->push, | ||
|
@@ -58,12 +61,8 @@ public function testReturnsEarlyWhenNoChangelogFilePresent(): void | |
$version = SemVerVersion::fromMilestoneName('1.0.1'); | ||
|
||
$this->checkoutBranch | ||
->expects($this->once()) | ||
->method('__invoke') | ||
->with( | ||
$this->equalTo($repoDir), | ||
$sourceBranch | ||
); | ||
->expects($this->never()) | ||
->method('__invoke'); | ||
|
||
$this->logger | ||
->expects($this->once()) | ||
|
@@ -118,6 +117,13 @@ public function testAddsNewReleaseVersionUsingBumpTypeToChangelogFileAndCommitsA | |
|
||
Assert::stringNotEmpty($repoDir); | ||
|
||
$changelogExists = $this->createMock(ChangelogExists::class); | ||
$changelogExists | ||
->expects($this->once()) | ||
->method('__invoke') | ||
->with($sourceBranch, $repoDir) | ||
->willReturn(true); | ||
|
||
$this->logger | ||
->expects($this->once()) | ||
->method('info') | ||
|
@@ -154,8 +160,16 @@ public function testAddsNewReleaseVersionUsingBumpTypeToChangelogFileAndCommitsA | |
->with( | ||
); | ||
|
||
$bumpAndCommitChangelog = new BumpAndCommitChangelogVersionViaKeepAChangelog( | ||
$changelogExists, | ||
$this->checkoutBranch, | ||
$this->commitFile, | ||
$this->push, | ||
$this->logger | ||
); | ||
|
||
$this->assertNull( | ||
($this->bumpAndCommitChangelog)( | ||
$bumpAndCommitChangelog( | ||
$bumpType, | ||
$repoDir, | ||
$version, | ||
|
@@ -191,6 +205,13 @@ private function createMockChangelog(): string | |
|
||
file_put_contents($changelogFile, self::CHANGELOG_STUB); | ||
|
||
(new Process(['git', 'init', '.'], $repo))->mustRun(); | ||
(new Process(['git', 'config', 'user.email', '[email protected]'], $repo))->mustRun(); | ||
(new Process(['git', 'config', 'user.name', 'Just Me'], $repo))->mustRun(); | ||
(new Process(['git', 'add', '.'], $repo))->mustRun(); | ||
(new Process(['git', 'commit', '-m', 'Initial import'], $repo))->mustRun(); | ||
(new Process(['git', 'switch', '-c', '1.0.x'], $repo))->mustRun(); | ||
|
||
return $changelogFile; | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very specifically not "mustRun()" here, as failure is an expected status.