From 2cf0a91c1bcf17709eca928b98079f2617275a27 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Wed, 17 Apr 2024 16:26:47 +0700 Subject: [PATCH] [#84] Fixed `[tags]` token using all tags instead of latest. (#85) --- src/Commands/ArtifactCommand.php | 3 ++- src/Git/ArtifactGitRepository.php | 19 +++++++++++++++++++ tests/phpunit/Functional/ForcePushTest.php | 14 +++++++++++++- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index aa0816e..00f9494 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -914,9 +914,10 @@ protected function getTokenBranch(): string { */ protected function getTokenTags(string $delimiter = NULL): string { $delimiter = $delimiter ?: '-'; + // We just want to get all tags point to the HEAD. $tags = $this ->gitRepository - ->getTags(); + ->getTagsPointToHead(); return implode($delimiter, $tags); } diff --git a/src/Git/ArtifactGitRepository.php b/src/Git/ArtifactGitRepository.php index 40385d3..0271e2b 100644 --- a/src/Git/ArtifactGitRepository.php +++ b/src/Git/ArtifactGitRepository.php @@ -237,6 +237,25 @@ public function setConfigReceiveDenyCurrentBranchIgnore(): ArtifactGitRepository return $this; } + /** + * Get tag point to HEAD. + * + * @return string[] + * Array of tags from the latest commit. + * + * @throws \Exception + * If no tags found in the latest commit. + */ + public function getTagsPointToHead(): array { + $tags = $this->extractFromCommand(['tag', ['--points-at', 'HEAD']]); + + if (empty($tags)) { + throw new \Exception('No tags found in the latest commit.'); + } + + return $tags; + } + /** * Create an annotated tag. * diff --git a/tests/phpunit/Functional/ForcePushTest.php b/tests/phpunit/Functional/ForcePushTest.php index b82d7f5..3ea5eeb 100644 --- a/tests/phpunit/Functional/ForcePushTest.php +++ b/tests/phpunit/Functional/ForcePushTest.php @@ -277,8 +277,20 @@ public function testBuildMultipleTags(): void { $this->gitAddTag($this->src, 'tag2'); $this->assertBuildSuccess('--branch=[tags]', 'tag1-tag2'); - $this->assertFixtureCommits(2, $this->dst, 'tag1-tag2', ['Deployment commit']); + + $this->gitCreateFixtureCommit(3); + $this->gitAddTag($this->src, 'tag3'); + $this->assertBuildSuccess('--branch=[tags]', 'tag3'); + $this->assertFixtureCommits(3, $this->dst, 'tag3', ['Deployment commit']); + } + + public function testBuildMultipleTagsMissingTags(): void { + $this->gitCreateFixtureCommits(2); + $this->gitAddTag($this->src, 'tag1'); + $this->gitCreateFixtureCommit(3); + + $this->assertBuildFailure('--branch=[tags]'); } public function testBuildMultipleTagsDelimiter(): void {