From 5ed5d1685b3b714d6f20f7922ab4f5f5188313ee Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Mon, 21 Aug 2023 14:32:54 +0530 Subject: [PATCH 1/4] Added commit hash to git clone command --- src/VCS/Adapter.php | 2 +- src/VCS/Adapter/Git/GitHub.php | 9 +++++++-- tests/VCS/Adapter/GitHubTest.php | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) 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..cb7791b5 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 = '*'; @@ -468,9 +468,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..fb74b3e3 100644 --- a/tests/VCS/Adapter/GitHubTest.php +++ b/tests/VCS/Adapter/GitHubTest.php @@ -155,7 +155,7 @@ public function testGetPullRequest(): void public function testGenerateCloneCommand(): void { - $gitCloneCommand = $this->vcsAdapter->generateCloneCommand('test-kh', 'test2', 'main', 'test', '*'); + $gitCloneCommand = $this->vcsAdapter->generateCloneCommand('test-kh', 'test2', 'main', 'test', '*', '4fb10447faea8a55c5cad7b5ebdfdbedca349fe4'); $this->assertNotEmpty($gitCloneCommand); $this->assertStringContainsString('sparse-checkout', $gitCloneCommand); } From a1b4abc46b3f19cd9abd4c357fef46977969d251 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Mon, 21 Aug 2023 16:12:47 +0530 Subject: [PATCH 2/4] Escaped variable using escapeshellarg --- composer.lock | 24 ++++++++++++------------ src/VCS/Adapter/Git/GitHub.php | 1 + 2 files changed, 13 insertions(+), 12 deletions(-) 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/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index cb7791b5..a0743e7e 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -459,6 +459,7 @@ public function generateCloneCommand(string $owner, string $repositoryName, stri $directory = escapeshellarg($directory); $rootDirectory = escapeshellarg($rootDirectory); $branchName = escapeshellarg($branchName); + $commitHash = escapeshellarg($commitHash); $commands = [ "mkdir -p {$directory}", From 6310bf636def6bdbe0694886cdf44b51d94b2872 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Mon, 21 Aug 2023 16:18:43 +0530 Subject: [PATCH 3/4] Add null check --- src/VCS/Adapter/Git/GitHub.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index a0743e7e..35fd8a38 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -459,7 +459,9 @@ public function generateCloneCommand(string $owner, string $repositoryName, stri $directory = escapeshellarg($directory); $rootDirectory = escapeshellarg($rootDirectory); $branchName = escapeshellarg($branchName); - $commitHash = escapeshellarg($commitHash); + if (!empty($commitHash)) { + $commitHash = escapeshellarg($commitHash); + } $commands = [ "mkdir -p {$directory}", From 9ff6ba691d305aef7bc03bf67f72227db114b4e2 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Mon, 21 Aug 2023 16:26:31 +0530 Subject: [PATCH 4/4] Add separate test for git clone with commit hash --- tests/VCS/Adapter/GitHubTest.php | 7 +++++++ tests/VCS/Base.php | 2 ++ 2 files changed, 9 insertions(+) diff --git a/tests/VCS/Adapter/GitHubTest.php b/tests/VCS/Adapter/GitHubTest.php index fb74b3e3..c0afc562 100644 --- a/tests/VCS/Adapter/GitHubTest.php +++ b/tests/VCS/Adapter/GitHubTest.php @@ -154,6 +154,13 @@ public function testGetPullRequest(): void } public function testGenerateCloneCommand(): void + { + $gitCloneCommand = $this->vcsAdapter->generateCloneCommand('test-kh', 'test2', 'main', 'test', '*'); + $this->assertNotEmpty($gitCloneCommand); + $this->assertStringContainsString('sparse-checkout', $gitCloneCommand); + } + + public function testGenerateCloneCommandWithCommitHash(): void { $gitCloneCommand = $this->vcsAdapter->generateCloneCommand('test-kh', 'test2', 'main', 'test', '*', '4fb10447faea8a55c5cad7b5ebdfdbedca349fe4'); $this->assertNotEmpty($gitCloneCommand); 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;