diff --git a/composer.lock b/composer.lock index 1a941264..8ad5b578 100644 --- a/composer.lock +++ b/composer.lock @@ -356,16 +356,16 @@ }, { "name": "nikic/php-parser", - "version": "v4.16.0", + "version": "v4.17.1", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "19526a33fb561ef417e822e85f08a00db4059c17" + "reference": "a6303e50c90c355c7eeee2c4a8b27fe8dc8fef1d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/19526a33fb561ef417e822e85f08a00db4059c17", - "reference": "19526a33fb561ef417e822e85f08a00db4059c17", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/a6303e50c90c355c7eeee2c4a8b27fe8dc8fef1d", + "reference": "a6303e50c90c355c7eeee2c4a8b27fe8dc8fef1d", "shasum": "" }, "require": { @@ -406,9 +406,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v4.16.0" + "source": "https://github.com/nikic/PHP-Parser/tree/v4.17.1" }, - "time": "2023-06-25T14:52:30+00:00" + "time": "2023-08-13T19:53:39+00:00" }, { "name": "phar-io/manifest", @@ -901,16 +901,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.6.10", + "version": "9.6.11", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "a6d351645c3fe5a30f5e86be6577d946af65a328" + "reference": "810500e92855eba8a7a5319ae913be2da6f957b0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/a6d351645c3fe5a30f5e86be6577d946af65a328", - "reference": "a6d351645c3fe5a30f5e86be6577d946af65a328", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/810500e92855eba8a7a5319ae913be2da6f957b0", + "reference": "810500e92855eba8a7a5319ae913be2da6f957b0", "shasum": "" }, "require": { @@ -984,7 +984,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.10" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.11" }, "funding": [ { @@ -1000,7 +1000,7 @@ "type": "tidelift" } ], - "time": "2023-07-10T04:04:23+00:00" + "time": "2023-08-19T07:10:56+00:00" }, { "name": "sebastian/cli-parser", diff --git a/src/VCS/Adapter.php b/src/VCS/Adapter.php index e46188c6..c136c3c2 100644 --- a/src/VCS/Adapter.php +++ b/src/VCS/Adapter.php @@ -158,7 +158,7 @@ abstract public function updateComment(string $owner, string $repositoryName, in /** * Generates a clone command using app access token */ - abstract public function generateCloneCommand(string $owner, string $repositoryName, string $branchName, string $directory, string $rootDirectory): string; + abstract public function generateCloneCommand(string $owner, string $repositoryName, string $branchName, string $directory, string $rootDirectory, string $commitHash = null): string; /** * Parses webhook event payload diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index 736792e8..35fd8a38 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -444,7 +444,7 @@ public function updateCommitStatus(string $repositoryName, string $commitHash, s /** * Generates a clone command using app access token */ - public function generateCloneCommand(string $owner, string $repositoryName, string $branchName, string $directory, string $rootDirectory): string + public function generateCloneCommand(string $owner, string $repositoryName, string $branchName, string $directory, string $rootDirectory, string $commitHash = null): string { if (empty($rootDirectory)) { $rootDirectory = '*'; @@ -459,6 +459,9 @@ public function generateCloneCommand(string $owner, string $repositoryName, stri $directory = escapeshellarg($directory); $rootDirectory = escapeshellarg($rootDirectory); $branchName = escapeshellarg($branchName); + if (!empty($commitHash)) { + $commitHash = escapeshellarg($commitHash); + } $commands = [ "mkdir -p {$directory}", @@ -468,9 +471,14 @@ public function generateCloneCommand(string $owner, string $repositoryName, stri "git remote add origin {$cloneUrl}", "git config core.sparseCheckout true", "echo {$rootDirectory} >> .git/info/sparse-checkout", - "if git ls-remote --exit-code --heads origin {$branchName}; then git pull origin {$branchName} && git checkout {$branchName}; else git checkout -b {$branchName}; fi" ]; + if (empty($commitHash)) { + $commands[] = "if git ls-remote --exit-code --heads origin {$branchName}; then git pull origin {$branchName} && git checkout {$branchName}; else git checkout -b {$branchName}; fi"; + } else { + $commands[] = "git pull origin {$commitHash}"; + } + $fullCommand = implode(" && ", $commands); return $fullCommand; diff --git a/tests/VCS/Adapter/GitHubTest.php b/tests/VCS/Adapter/GitHubTest.php index 56bc80ac..c0afc562 100644 --- a/tests/VCS/Adapter/GitHubTest.php +++ b/tests/VCS/Adapter/GitHubTest.php @@ -160,6 +160,13 @@ public function testGenerateCloneCommand(): void $this->assertStringContainsString('sparse-checkout', $gitCloneCommand); } + public function testGenerateCloneCommandWithCommitHash(): void + { + $gitCloneCommand = $this->vcsAdapter->generateCloneCommand('test-kh', 'test2', 'main', 'test', '*', '4fb10447faea8a55c5cad7b5ebdfdbedca349fe4'); + $this->assertNotEmpty($gitCloneCommand); + $this->assertStringContainsString('sparse-checkout', $gitCloneCommand); + } + public function testUpdateComment(): void { $commentId = $this->vcsAdapter->updateComment('test-kh', 'test2', 1630320767, 'update'); diff --git a/tests/VCS/Base.php b/tests/VCS/Base.php index 26c446a0..b91504dc 100644 --- a/tests/VCS/Base.php +++ b/tests/VCS/Base.php @@ -21,6 +21,8 @@ abstract public function testUpdateComment(): void; abstract public function testGenerateCloneCommand(): void; + abstract public function testGenerateCloneCommandWithCommitHash(): void; + abstract public function testgetEvent(): void; abstract public function testGetRepositoryName(): void;