Skip to content

Commit

Permalink
Merge pull request #2 from utopia-php/feat-clone-commit-hash
Browse files Browse the repository at this point in the history
Added commit hash to git clone command
  • Loading branch information
eldadfux authored Aug 21, 2023
2 parents fc9c38a + 9ff6ba6 commit ec388e0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
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
12 changes: 10 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,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}",
Expand All @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions tests/VCS/Adapter/GitHubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 2 additions & 0 deletions tests/VCS/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ec388e0

Please sign in to comment.