Skip to content

Commit 277dbc9

Browse files
committed
Refactor: create tag via console if not exists
Signed-off-by: Nathanael Esayeas <[email protected]>
1 parent 7199f49 commit 277dbc9

File tree

3 files changed

+70
-19
lines changed

3 files changed

+70
-19
lines changed

bin/console.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Laminas\AutomaticReleases\Git\CreateTagViaConsole;
2525
use Laminas\AutomaticReleases\Git\FetchAndSetCurrentUserByReplacingCurrentOriginRemote;
2626
use Laminas\AutomaticReleases\Git\GetMergeTargetCandidateBranchesFromRemoteBranches;
27+
use Laminas\AutomaticReleases\Git\HasTagViaConsole;
2728
use Laminas\AutomaticReleases\Git\PushViaConsole;
2829
use Laminas\AutomaticReleases\Github\Api\GraphQL\Query\GetMilestoneFirst100IssuesAndPullRequests;
2930
use Laminas\AutomaticReleases\Github\Api\GraphQL\RunGraphQLQuery;
@@ -127,7 +128,7 @@ static function (int $errorCode, string $message = '', string $file = '', int $l
127128
$getMilestone,
128129
$commitChangelog,
129130
$createReleaseText,
130-
new CreateTagViaConsole(),
131+
new CreateTagViaConsole(new HasTagViaConsole()),
131132
$push,
132133
$createRelease,
133134
),

src/Git/CreateTagViaConsole.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,39 @@
66

77
use Laminas\AutomaticReleases\Git\Value\BranchName;
88
use Laminas\AutomaticReleases\Gpg\SecretKeyId;
9-
use Psl\Env;
10-
use Psl\File;
11-
use Psl\Filesystem;
12-
use Psl\Shell;
9+
10+
use function Psl\Env\temp_dir;
11+
use function Psl\File\write;
12+
use function Psl\Filesystem\create_temporary_file;
13+
use function Psl\Shell\execute;
1314

1415
final class CreateTagViaConsole implements CreateTag
1516
{
17+
public function __construct(private HasTag $hasTag)
18+
{
19+
}
20+
1621
public function __invoke(
1722
string $repositoryDirectory,
1823
BranchName $sourceBranch,
1924
string $tagName,
2025
string $changelog,
2126
SecretKeyId $keyId,
2227
): void {
23-
$tagFileName = Filesystem\create_temporary_file(Env\temp_dir(), 'created_tag');
28+
if (($this->hasTag)($repositoryDirectory, $tagName) === true) {
29+
return;
30+
}
31+
32+
$tagFileName = create_temporary_file(temp_dir(), 'created_tag');
33+
34+
write($tagFileName, $changelog);
2435

25-
File\write($tagFileName, $changelog);
36+
execute('git', ['checkout', $sourceBranch->name()], $repositoryDirectory);
2637

27-
Shell\execute('git', ['checkout', $sourceBranch->name()], $repositoryDirectory);
28-
Shell\execute('git', ['tag', $tagName, '-F', $tagFileName, '--cleanup=whitespace', '--local-user=' . $keyId->id()], $repositoryDirectory);
38+
execute(
39+
'git',
40+
['tag', $tagName, '-F', $tagFileName, '--cleanup=whitespace', '--local-user=' . $keyId->id()],
41+
$repositoryDirectory,
42+
);
2943
}
3044
}

test/unit/Git/CreateTagViaConsoleTest.php

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Laminas\AutomaticReleases\Test\Unit\Git;
66

77
use Laminas\AutomaticReleases\Git\CreateTagViaConsole;
8+
use Laminas\AutomaticReleases\Git\HasTag;
9+
use Laminas\AutomaticReleases\Git\HasTagViaConsole;
810
use Laminas\AutomaticReleases\Git\Value\BranchName;
911
use Laminas\AutomaticReleases\Gpg\ImportGpgKeyFromStringViaTemporaryFile;
1012
use Laminas\AutomaticReleases\Gpg\SecretKeyId;
@@ -47,18 +49,20 @@ protected function setUp(): void
4749
public function testCreatesSignedTag(): void
4850
{
4951
$sourceUri = $this->createMock(UriInterface::class);
52+
$sourceUri->method('__toString')->willReturn($this->repository);
5053

51-
$sourceUri->method('__toString')
52-
->willReturn($this->repository);
54+
$hasTag = $this->createMock(HasTag::class);
55+
$hasTag->method('__invoke')
56+
->with($this->repository, 'name-of-the-tag')
57+
->willReturn(false);
5358

54-
(new CreateTagViaConsole())
55-
->__invoke(
56-
$this->repository,
57-
BranchName::fromName('tag-branch'),
58-
'name-of-the-tag',
59-
'changelog text for the tag',
60-
$this->key,
61-
);
59+
(new CreateTagViaConsole($hasTag))(
60+
$this->repository,
61+
BranchName::fromName('tag-branch'),
62+
'name-of-the-tag',
63+
'changelog text for the tag',
64+
$this->key,
65+
);
6266

6367
Shell\execute('git', ['tag', '-v', 'name-of-the-tag'], $this->repository);
6468

@@ -69,4 +73,36 @@ public function testCreatesSignedTag(): void
6973
self::assertStringContainsString('a commit', $fetchedTag);
7074
self::assertStringContainsString('-----BEGIN PGP SIGNATURE-----', $fetchedTag);
7175
}
76+
77+
public function testSkipsIfTagAlreadyExists(): void
78+
{
79+
$sourceUri = $this->createMock(UriInterface::class);
80+
$sourceUri->method('__toString')->willReturn($this->repository);
81+
82+
$hasTag = new HasTagViaConsole();
83+
84+
(new CreateTagViaConsole($hasTag))(
85+
$this->repository,
86+
BranchName::fromName('tag-branch'),
87+
'name-of-the-tag',
88+
'changelog text for the tag',
89+
$this->key,
90+
);
91+
92+
Shell\execute('git', ['tag', '-v', 'name-of-the-tag'], $this->repository);
93+
$fetchedTag = Shell\execute('git', ['show', 'name-of-the-tag'], $this->repository);
94+
95+
self::assertStringContainsString('tag name-of-the-tag', $fetchedTag);
96+
self::assertStringContainsString('changelog text for the tag', $fetchedTag);
97+
self::assertStringContainsString('a commit', $fetchedTag);
98+
self::assertStringContainsString('-----BEGIN PGP SIGNATURE-----', $fetchedTag);
99+
100+
(new CreateTagViaConsole($hasTag))(
101+
$this->repository,
102+
BranchName::fromName('tag-branch'),
103+
'name-of-the-tag',
104+
'changelog text for the tag',
105+
$this->key,
106+
);
107+
}
72108
}

0 commit comments

Comments
 (0)