Skip to content

Skip creating GIT tag if one already exists, enabling re-running of the action if a release creation fails #230

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 5 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion bin/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Laminas\AutomaticReleases\Git\CreateTagViaConsole;
use Laminas\AutomaticReleases\Git\FetchAndSetCurrentUserByReplacingCurrentOriginRemote;
use Laminas\AutomaticReleases\Git\GetMergeTargetCandidateBranchesFromRemoteBranches;
use Laminas\AutomaticReleases\Git\HasTagViaConsole;
use Laminas\AutomaticReleases\Git\PushViaConsole;
use Laminas\AutomaticReleases\Github\Api\GraphQL\Query\GetMilestoneFirst100IssuesAndPullRequests;
use Laminas\AutomaticReleases\Github\Api\GraphQL\RunGraphQLQuery;
Expand Down Expand Up @@ -127,7 +128,7 @@ static function (int $errorCode, string $message = '', string $file = '', int $l
$getMilestone,
$commitChangelog,
$createReleaseText,
new CreateTagViaConsole(),
new CreateTagViaConsole(new HasTagViaConsole()),
$push,
$createRelease,
),
Expand Down
30 changes: 22 additions & 8 deletions src/Git/CreateTagViaConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,39 @@

use Laminas\AutomaticReleases\Git\Value\BranchName;
use Laminas\AutomaticReleases\Gpg\SecretKeyId;
use Psl\Env;
use Psl\File;
use Psl\Filesystem;
use Psl\Shell;

use function Psl\Env\temp_dir;
use function Psl\File\write;
use function Psl\Filesystem\create_temporary_file;
use function Psl\Shell\execute;

final class CreateTagViaConsole implements CreateTag
{
public function __construct(private HasTag $hasTag)
{
}

public function __invoke(
string $repositoryDirectory,
BranchName $sourceBranch,
string $tagName,
string $changelog,
SecretKeyId $keyId,
): void {
$tagFileName = Filesystem\create_temporary_file(Env\temp_dir(), 'created_tag');
if (($this->hasTag)($repositoryDirectory, $tagName) === true) {
return;
}

$tagFileName = create_temporary_file(temp_dir(), 'created_tag');

write($tagFileName, $changelog);

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

Shell\execute('git', ['checkout', $sourceBranch->name()], $repositoryDirectory);
Shell\execute('git', ['tag', $tagName, '-F', $tagFileName, '--cleanup=whitespace', '--local-user=' . $keyId->id()], $repositoryDirectory);
execute(
'git',
['tag', $tagName, '-F', $tagFileName, '--cleanup=whitespace', '--local-user=' . $keyId->id()],
$repositoryDirectory,
);
}
}
14 changes: 14 additions & 0 deletions src/Git/HasTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Laminas\AutomaticReleases\Git;

interface HasTag
{
/** @param non-empty-string $repositoryDirectory */
public function __invoke(
string $repositoryDirectory,
string $tagName,
): bool;
}
23 changes: 23 additions & 0 deletions src/Git/HasTagViaConsole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Laminas\AutomaticReleases\Git;

use function Psl\Shell\execute;
use function str_contains;
use function trim;

final class HasTagViaConsole implements HasTag
{
public function __invoke(string $repositoryDirectory, string $tagName): bool
{
$outout = execute('git', ['tag', '--list', $tagName], $repositoryDirectory);

if (trim($outout) === '') {
return false;
}

return str_contains($outout, $tagName);
}
}
56 changes: 46 additions & 10 deletions test/unit/Git/CreateTagViaConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Laminas\AutomaticReleases\Test\Unit\Git;

use Laminas\AutomaticReleases\Git\CreateTagViaConsole;
use Laminas\AutomaticReleases\Git\HasTag;
use Laminas\AutomaticReleases\Git\HasTagViaConsole;
use Laminas\AutomaticReleases\Git\Value\BranchName;
use Laminas\AutomaticReleases\Gpg\ImportGpgKeyFromStringViaTemporaryFile;
use Laminas\AutomaticReleases\Gpg\SecretKeyId;
Expand Down Expand Up @@ -47,18 +49,20 @@ protected function setUp(): void
public function testCreatesSignedTag(): void
{
$sourceUri = $this->createMock(UriInterface::class);
$sourceUri->method('__toString')->willReturn($this->repository);

$sourceUri->method('__toString')
->willReturn($this->repository);
$hasTag = $this->createMock(HasTag::class);
$hasTag->method('__invoke')
->with($this->repository, 'name-of-the-tag')
->willReturn(false);

(new CreateTagViaConsole())
->__invoke(
$this->repository,
BranchName::fromName('tag-branch'),
'name-of-the-tag',
'changelog text for the tag',
$this->key,
);
(new CreateTagViaConsole($hasTag))(
$this->repository,
BranchName::fromName('tag-branch'),
'name-of-the-tag',
'changelog text for the tag',
$this->key,
);

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

Expand All @@ -69,4 +73,36 @@ public function testCreatesSignedTag(): void
self::assertStringContainsString('a commit', $fetchedTag);
self::assertStringContainsString('-----BEGIN PGP SIGNATURE-----', $fetchedTag);
}

public function testSkipsIfTagAlreadyExists(): void
{
$sourceUri = $this->createMock(UriInterface::class);
$sourceUri->method('__toString')->willReturn($this->repository);

$hasTag = new HasTagViaConsole();

(new CreateTagViaConsole($hasTag))(
$this->repository,
BranchName::fromName('tag-branch'),
'name-of-the-tag',
'changelog text for the tag',
$this->key,
);

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

self::assertStringContainsString('tag name-of-the-tag', $fetchedTag);
self::assertStringContainsString('changelog text for the tag', $fetchedTag);
self::assertStringContainsString('a commit', $fetchedTag);
self::assertStringContainsString('-----BEGIN PGP SIGNATURE-----', $fetchedTag);

(new CreateTagViaConsole($hasTag))(
$this->repository,
BranchName::fromName('tag-branch'),
'name-of-the-tag',
'changelog text for the tag',
$this->key,
);
}
}
75 changes: 75 additions & 0 deletions test/unit/Git/HasTagViaConsoleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace Laminas\AutomaticReleases\Test\Unit\Git;

use Laminas\AutomaticReleases\Git\HasTag;
use Laminas\AutomaticReleases\Git\HasTagViaConsole;
use PHPUnit\Framework\TestCase;

use function array_map;
use function Psl\Env\temp_dir;
use function Psl\Filesystem\create_directory;
use function Psl\Filesystem\create_temporary_file;
use function Psl\Filesystem\delete_file;
use function Psl\Shell\execute;
use function sprintf;

/** @covers \Laminas\AutomaticReleases\Git\HasTagViaConsole */
final class HasTagViaConsoleTest extends TestCase
{
/** @var non-empty-string */
private string $repository;
private HasTag $hasTag;

protected function setUp(): void
{
parent::setUp();

$this->repository = create_temporary_file(temp_dir(), 'HasTagViaConsoleRepository');

$this->hasTag = new HasTagViaConsole();

delete_file($this->repository);
create_directory($this->repository);

execute('git', ['init'], $this->repository);

execute('git', ['config', 'user.email', '[email protected]'], $this->repository);
execute('git', ['config', 'user.name', 'Just Me'], $this->repository);

array_map(fn (string $tag) => $this->createTag($tag), [
'1.0.0',
'2.0.0',
]);
}

private function createTag(string $tag): void
{
execute('git', ['commit', '--allow-empty', '-m', 'a commit for version ' . $tag], $this->repository);
execute('git', ['tag', '-a', $tag, '-m', 'version ' . $tag], $this->repository);
}

/** @param non-empty-string $repository */
private function assertGitTagExists(string $repository, string $tag): void
{
self::assertTrue(($this->hasTag)($repository, $tag), sprintf('Failed asserting git tag "%s" exists.', $tag));
}

/** @param non-empty-string $repository */
private function assertGitTagMissing(string $repository, string $tag): void
{
self::assertFalse(($this->hasTag)($repository, $tag), sprintf('Failed asserting git tag "%s" is missing.', $tag));
}

public function testHasTag(): void
{
self::assertGitTagExists($this->repository, '1.0.0');
}

public function testHasTagMissing(): void
{
self::assertGitTagMissing($this->repository, '10.0.0');
}
}