Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"php": ">=8.0",
"adhocore/jwt": "^1.1",
"utopia-php/cache": "1.0.*",
"utopia-php/console": "dev-main",
"utopia-php/fetch": "0.5.*"
},
"require-dev": {
Expand Down
101 changes: 99 additions & 2 deletions composer.lock

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

3 changes: 2 additions & 1 deletion src/VCS/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Utopia\VCS;

use Exception;
use Utopia\Command;

abstract class Adapter
{
Expand Down Expand Up @@ -191,7 +192,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 $version, string $versionType, string $directory, string $rootDirectory): string;
abstract public function generateCloneCommand(string $owner, string $repositoryName, string $version, string $versionType, string $directory, string $rootDirectory): Command;

/**
* Parses webhook event payload
Expand Down
140 changes: 116 additions & 24 deletions src/VCS/Adapter/Git/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Ahc\Jwt\JWT;
use Exception;
use Utopia\Command;
use Utopia\Cache\Cache;
use Utopia\VCS\Adapter\Git;
use Utopia\VCS\Exception\FileNotFound;
Expand Down Expand Up @@ -841,7 +842,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 $version, string $versionType, string $directory, string $rootDirectory): string
public function generateCloneCommand(string $owner, string $repositoryName, string $version, string $versionType, string $directory, string $rootDirectory): Command
{
if (empty($rootDirectory)) {
$rootDirectory = '*';
Expand All @@ -854,42 +855,133 @@ public function generateCloneCommand(string $owner, string $repositoryName, stri

$cloneUrl = "https://{$owner}{$accessToken}@github.com/{$owner}/{$repositoryName}";

$directory = escapeshellarg($directory);
$rootDirectory = escapeshellarg($rootDirectory);

$commands = [
"mkdir -p {$directory}",
"cd {$directory}",
"git config --global init.defaultBranch main",
"git init",
"git remote add origin {$cloneUrl}",
// Enable sparse checkout
"git config core.sparseCheckout true",
"echo {$rootDirectory} >> .git/info/sparse-checkout",
// Disable fetching of refs we don't need
"git config --add remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'",
// Disable fetching of tags
"git config remote.origin.tagopt --no-tags",
(new Command('mkdir'))
->flag('-p')
->argument($directory),
(new Command('git'))
->argument('config')
->argument('--global')
->argument('init.defaultBranch')
->argument('main'),
(new Command('git'))
->argument('init')
->argument($directory),
(new Command('git'))
->option('-C', $directory)
->argument('remote')
->argument('add')
->argument('origin')
->argument($cloneUrl),
(new Command('git'))
->option('-C', $directory)
->argument('config')
->argument('--add')
->argument('remote.origin.fetch')
->argument('+refs/heads/*:refs/remotes/origin/*'),
(new Command('git'))
->option('-C', $directory)
->argument('config')
->argument('remote.origin.tagopt')
->argument('--no-tags'),
(new Command('git'))
->option('-C', $directory)
->argument('sparse-checkout')
->argument('set')
->argument('--no-cone')
->argument($rootDirectory),
];

switch ($versionType) {
case self::CLONE_TYPE_BRANCH:
$branchName = escapeshellarg($version);
$commands[] = "if git ls-remote --exit-code --heads origin {$branchName}; then git pull --depth=1 origin {$branchName} && git checkout {$branchName}; else git checkout -b {$branchName}; fi";
$commands[] = Command::or(
Command::and(
(new Command('git'))
->option('-C', $directory)
->argument('ls-remote')
->argument('--exit-code')
->argument('--heads')
->argument('origin')
->argument($version),
(new Command('git'))
->option('-C', $directory)
->argument('pull')
->argument('--depth=1')
->argument('origin')
->argument($version),
(new Command('git'))
->option('-C', $directory)
->argument('checkout')
->argument($version)
),
(new Command('git'))
->option('-C', $directory)
->argument('checkout')
->argument('-b')
->argument($version)
);
break;
case self::CLONE_TYPE_COMMIT:
$commitHash = escapeshellarg($version);
$commands[] = "git fetch --depth=1 origin {$commitHash} && git checkout {$commitHash}";
$commands[] = (new Command('git'))
->option('-C', $directory)
->argument('fetch')
->argument('--depth=1')
->argument('origin')
->argument($version);
$commands[] = (new Command('git'))
->option('-C', $directory)
->argument('checkout')
->argument($version);
break;
case self::CLONE_TYPE_TAG:
$tagName = escapeshellarg($version);
$commands[] = "git fetch --depth=1 origin refs/tags/$(git ls-remote --tags origin {$tagName} | tail -n 1 | awk -F '/' '{print $3}') && git checkout FETCH_HEAD";
$resolvedTag = $this->resolveTagReference($owner, $repositoryName, $version);
$commands[] = (new Command('git'))
->option('-C', $directory)
->argument('fetch')
->argument('--depth=1')
->argument('origin')
->argument('refs/tags/' . $resolvedTag);
$commands[] = (new Command('git'))
->option('-C', $directory)
->argument('checkout')
->argument('FETCH_HEAD');
break;
}

$fullCommand = implode(" && ", $commands);
return Command::and(...$commands);
}

private function resolveTagReference(string $owner, string $repositoryName, string $version): string
{
if (!str_contains($version, '*')) {
return $version;
}

$prefix = rtrim(strstr($version, '*', true) ?: '', '.');
$refPrefix = 'tags' . (!empty($prefix) ? '/' . $prefix : '');
$response = $this->call(
self::METHOD_GET,
"/repos/{$owner}/{$repositoryName}/git/matching-refs/{$refPrefix}",
['Authorization' => "Bearer $this->accessToken"]
);

$refs = $response['body'] ?? [];
$matches = [];

foreach ($refs as $ref) {
$tag = str_replace('refs/tags/', '', $ref['ref'] ?? '');
if ($tag !== '' && fnmatch($version, $tag)) {
$matches[] = $tag;
}
}

if (empty($matches)) {
throw new Exception("Tag not found for pattern: {$version}");
}

usort($matches, static fn (string $left, string $right): int => version_compare($left, $right));

return $fullCommand;
return $matches[array_key_last($matches)];
}

/**
Expand Down
Loading
Loading