Skip to content

Commit

Permalink
Merge pull request #21 from utopia-php/feat-clone-tag
Browse files Browse the repository at this point in the history
Feat: Clone by tag
  • Loading branch information
christyjacob4 authored Jul 29, 2024
2 parents 4745fcf + 2a35b15 commit 6ba177e
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 58 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ FROM php:8.0-cli-alpine

WORKDIR /usr/local/src/

RUN apk update && apk add git

COPY --from=composer /usr/local/src/vendor /usr/local/src/vendor
COPY . /usr/local/src/

Expand Down
75 changes: 38 additions & 37 deletions composer.lock

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

15 changes: 5 additions & 10 deletions src/VCS/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,21 @@

abstract class Adapter
{
public const METHOD_GET = 'GET';
public const CLONE_TYPE_BRANCH = 'branch';
public const CLONE_TYPE_TAG = 'tag';
public const CLONE_TYPE_COMMIT = 'commit';

public const METHOD_GET = 'GET';
public const METHOD_POST = 'POST';

public const METHOD_PUT = 'PUT';

public const METHOD_PATCH = 'PATCH';

public const METHOD_DELETE = 'DELETE';

public const METHOD_HEAD = 'HEAD';

public const METHOD_OPTIONS = 'OPTIONS';

public const METHOD_CONNECT = 'CONNECT';

public const METHOD_TRACE = 'TRACE';

public const TYPE_GIT = 'git';

public const TYPE_SVN = 'svn';

protected bool $selfSigned = true;
Expand Down Expand Up @@ -162,7 +157,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 $commitHash = null): string;
abstract public function generateCloneCommand(string $owner, string $repositoryName, string $version, string $versionType, string $directory, string $rootDirectory): string;

/**
* Parses webhook event payload
Expand Down
23 changes: 14 additions & 9 deletions src/VCS/Adapter/Git/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,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 $commitHash = null): string
public function generateCloneCommand(string $owner, string $repositoryName, string $version, string $versionType, string $directory, string $rootDirectory): string
{
if (empty($rootDirectory)) {
$rootDirectory = '*';
Expand All @@ -521,10 +521,6 @@ 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 @@ -536,10 +532,19 @@ public function generateCloneCommand(string $owner, string $repositoryName, stri
"echo {$rootDirectory} >> .git/info/sparse-checkout",
];

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}";
switch ($versionType) {
case self::CLONE_TYPE_BRANCH:
$branchName = escapeshellarg($version);
$commands[] = "if git ls-remote --exit-code --heads origin {$branchName}; then git pull origin {$branchName} && git checkout {$branchName}; else git checkout -b {$branchName}; fi";
break;
case self::CLONE_TYPE_COMMIT:
$commitHash = escapeshellarg($version);
$commands[] = "git pull origin {$commitHash}";
break;
case self::CLONE_TYPE_TAG:
$tagName = escapeshellarg($version);
$commands[] = "git pull origin {$tagName}";
break;
}

$fullCommand = implode(" && ", $commands);
Expand Down
33 changes: 31 additions & 2 deletions tests/VCS/Adapter/GitHubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,45 @@ public function testGetPullRequest(): void

public function testGenerateCloneCommand(): void
{
$gitCloneCommand = $this->vcsAdapter->generateCloneCommand('test-kh', 'test2', 'main', 'test', '*');
$gitCloneCommand = $this->vcsAdapter->generateCloneCommand('test-kh', 'test2', 'test', GitHub::CLONE_TYPE_BRANCH, '/tmp/clone-branch', '*');
$this->assertNotEmpty($gitCloneCommand);
$this->assertStringContainsString('sparse-checkout', $gitCloneCommand);

$output = '';
$resultCode = null;
\exec($gitCloneCommand, $output, $resultCode);
\var_dump($output);
$this->assertEquals(0, $resultCode);

$this->assertFileExists('/tmp/clone-branch/README.md');
}

public function testGenerateCloneCommandWithCommitHash(): void
{
$gitCloneCommand = $this->vcsAdapter->generateCloneCommand('test-kh', 'test2', 'main', 'test', '*', '4fb10447faea8a55c5cad7b5ebdfdbedca349fe4');
$gitCloneCommand = $this->vcsAdapter->generateCloneCommand('test-kh', 'test2', '4fb10447faea8a55c5cad7b5ebdfdbedca349fe4', GitHub::CLONE_TYPE_COMMIT, '/tmp/clone-commit', '*');
$this->assertNotEmpty($gitCloneCommand);
$this->assertStringContainsString('sparse-checkout', $gitCloneCommand);

$output = '';
$resultCode = null;
\exec($gitCloneCommand, $output, $resultCode);
$this->assertEquals(0, $resultCode);

$this->assertFileExists('/tmp/clone-commit/README.md');
}

public function testGenerateCloneCommandWithTag(): void
{
$gitCloneCommand = $this->vcsAdapter->generateCloneCommand('test-kh', 'test2', '0.1.0', GitHub::CLONE_TYPE_TAG, '/tmp/clone-tag', '*');
$this->assertNotEmpty($gitCloneCommand);
$this->assertStringContainsString('sparse-checkout', $gitCloneCommand);

$output = '';
$resultCode = null;
\exec($gitCloneCommand, $output, $resultCode);
$this->assertEquals(0, $resultCode);

$this->assertFileExists('/tmp/clone-tag/README.md');
}

public function testUpdateComment(): void
Expand Down

0 comments on commit 6ba177e

Please sign in to comment.