Skip to content
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

Added commit hash to git clone command #2

Merged
merged 4 commits into from
Aug 21, 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
24 changes: 12 additions & 12 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/VCS/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/VCS/Adapter/Git/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '*';
Expand All @@ -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}",
Expand All @@ -468,9 +469,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}";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to escape every variable that we attach to an execute command to prevent a shell injection:
You can use escapeshellarg like we do with other variables. See more here: https://owasp.org/www-community/attacks/Command_Injection

}

$fullCommand = implode(" && ", $commands);

return $fullCommand;
Expand Down
2 changes: 1 addition & 1 deletion tests/VCS/Adapter/GitHubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function testGetPullRequest(): void

public function testGenerateCloneCommand(): void
{
$gitCloneCommand = $this->vcsAdapter->generateCloneCommand('test-kh', 'test2', 'main', 'test', '*');
Meldiron marked this conversation as resolved.
Show resolved Hide resolved
$gitCloneCommand = $this->vcsAdapter->generateCloneCommand('test-kh', 'test2', 'main', 'test', '*', '4fb10447faea8a55c5cad7b5ebdfdbedca349fe4');
$this->assertNotEmpty($gitCloneCommand);
$this->assertStringContainsString('sparse-checkout', $gitCloneCommand);
}
Expand Down