From bb7eda6d909b4568b2187fe7911b8973eb291ad2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 29 Jul 2024 10:55:55 +0000 Subject: [PATCH 1/5] Clone tag type + improve tests --- composer.lock | 75 ++++++++++++++++---------------- src/VCS/Adapter.php | 15 +++---- src/VCS/Adapter/Git/GitHub.php | 23 ++++++---- tests/VCS/Adapter/GitHubTest.php | 32 +++++++++++++- 4 files changed, 87 insertions(+), 58 deletions(-) diff --git a/composer.lock b/composer.lock index 2ada838..00a57ff 100644 --- a/composer.lock +++ b/composer.lock @@ -115,16 +115,16 @@ }, { "name": "utopia-php/framework", - "version": "0.34.2", + "version": "0.34.3", "source": { "type": "git", "url": "https://github.com/utopia-php/http.git", - "reference": "fd126c02b78cc80678c9638f7b335dfb4a841b78" + "reference": "e3bbca07c1df4e908ea9d3ce4f59367a7696b66b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/http/zipball/fd126c02b78cc80678c9638f7b335dfb4a841b78", - "reference": "fd126c02b78cc80678c9638f7b335dfb4a841b78", + "url": "https://api.github.com/repos/utopia-php/http/zipball/e3bbca07c1df4e908ea9d3ce4f59367a7696b66b", + "reference": "e3bbca07c1df4e908ea9d3ce4f59367a7696b66b", "shasum": "" }, "require": { @@ -141,7 +141,8 @@ "type": "library", "autoload": { "psr-4": { - "Utopia\\Http\\": "src/Http" + "Utopia\\": "src/", + "Tests\\E2E\\": "tests/e2e" } }, "notification-url": "https://packagist.org/downloads/", @@ -157,9 +158,9 @@ ], "support": { "issues": "https://github.com/utopia-php/http/issues", - "source": "https://github.com/utopia-php/http/tree/0.34.2" + "source": "https://github.com/utopia-php/http/tree/0.34.3" }, - "time": "2024-02-20T11:36:56+00:00" + "time": "2024-07-02T15:08:46+00:00" } ], "packages-dev": [ @@ -361,16 +362,16 @@ }, { "name": "nikic/php-parser", - "version": "v5.0.2", + "version": "v5.1.0", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "139676794dc1e9231bf7bcd123cfc0c99182cb13" + "reference": "683130c2ff8c2739f4822ff7ac5c873ec529abd1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/139676794dc1e9231bf7bcd123cfc0c99182cb13", - "reference": "139676794dc1e9231bf7bcd123cfc0c99182cb13", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/683130c2ff8c2739f4822ff7ac5c873ec529abd1", + "reference": "683130c2ff8c2739f4822ff7ac5c873ec529abd1", "shasum": "" }, "require": { @@ -381,7 +382,7 @@ }, "require-dev": { "ircmaxell/php-yacc": "^0.0.7", - "phpunit/phpunit": "^7.0 || ^8.0 || ^9.0" + "phpunit/phpunit": "^9.0" }, "bin": [ "bin/php-parse" @@ -413,9 +414,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v5.0.2" + "source": "https://github.com/nikic/PHP-Parser/tree/v5.1.0" }, - "time": "2024-03-05T20:51:40+00:00" + "time": "2024-07-01T20:03:41+00:00" }, { "name": "phar-io/manifest", @@ -915,45 +916,45 @@ }, { "name": "phpunit/phpunit", - "version": "9.6.19", + "version": "9.6.20", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "a1a54a473501ef4cdeaae4e06891674114d79db8" + "reference": "49d7820565836236411f5dc002d16dd689cde42f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/a1a54a473501ef4cdeaae4e06891674114d79db8", - "reference": "a1a54a473501ef4cdeaae4e06891674114d79db8", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/49d7820565836236411f5dc002d16dd689cde42f", + "reference": "49d7820565836236411f5dc002d16dd689cde42f", "shasum": "" }, "require": { - "doctrine/instantiator": "^1.3.1 || ^2", + "doctrine/instantiator": "^1.5.0 || ^2", "ext-dom": "*", "ext-json": "*", "ext-libxml": "*", "ext-mbstring": "*", "ext-xml": "*", "ext-xmlwriter": "*", - "myclabs/deep-copy": "^1.10.1", - "phar-io/manifest": "^2.0.3", - "phar-io/version": "^3.0.2", + "myclabs/deep-copy": "^1.12.0", + "phar-io/manifest": "^2.0.4", + "phar-io/version": "^3.2.1", "php": ">=7.3", - "phpunit/php-code-coverage": "^9.2.28", - "phpunit/php-file-iterator": "^3.0.5", + "phpunit/php-code-coverage": "^9.2.31", + "phpunit/php-file-iterator": "^3.0.6", "phpunit/php-invoker": "^3.1.1", - "phpunit/php-text-template": "^2.0.3", - "phpunit/php-timer": "^5.0.2", - "sebastian/cli-parser": "^1.0.1", - "sebastian/code-unit": "^1.0.6", + "phpunit/php-text-template": "^2.0.4", + "phpunit/php-timer": "^5.0.3", + "sebastian/cli-parser": "^1.0.2", + "sebastian/code-unit": "^1.0.8", "sebastian/comparator": "^4.0.8", - "sebastian/diff": "^4.0.3", - "sebastian/environment": "^5.1.3", - "sebastian/exporter": "^4.0.5", - "sebastian/global-state": "^5.0.1", - "sebastian/object-enumerator": "^4.0.3", - "sebastian/resource-operations": "^3.0.3", - "sebastian/type": "^3.2", + "sebastian/diff": "^4.0.6", + "sebastian/environment": "^5.1.5", + "sebastian/exporter": "^4.0.6", + "sebastian/global-state": "^5.0.7", + "sebastian/object-enumerator": "^4.0.4", + "sebastian/resource-operations": "^3.0.4", + "sebastian/type": "^3.2.1", "sebastian/version": "^3.0.2" }, "suggest": { @@ -998,7 +999,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.19" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.20" }, "funding": [ { @@ -1014,7 +1015,7 @@ "type": "tidelift" } ], - "time": "2024-04-05T04:35:58+00:00" + "time": "2024-07-10T11:45:39+00:00" }, { "name": "sebastian/cli-parser", diff --git a/src/VCS/Adapter.php b/src/VCS/Adapter.php index a0102cb..594e154 100644 --- a/src/VCS/Adapter.php +++ b/src/VCS/Adapter.php @@ -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; @@ -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 diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index 53fe1e7..cbcf769 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -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 = '*'; @@ -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}", @@ -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); diff --git a/tests/VCS/Adapter/GitHubTest.php b/tests/VCS/Adapter/GitHubTest.php index ad06f56..a6595e0 100644 --- a/tests/VCS/Adapter/GitHubTest.php +++ b/tests/VCS/Adapter/GitHubTest.php @@ -236,16 +236,44 @@ 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); + $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 From 304dca001f028dc9e21e1a935a868f664edec20d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 29 Jul 2024 10:58:00 +0000 Subject: [PATCH 2/5] Debug log --- tests/VCS/Adapter/GitHubTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/VCS/Adapter/GitHubTest.php b/tests/VCS/Adapter/GitHubTest.php index a6595e0..cd0b8c4 100644 --- a/tests/VCS/Adapter/GitHubTest.php +++ b/tests/VCS/Adapter/GitHubTest.php @@ -243,6 +243,7 @@ public function testGenerateCloneCommand(): void $output = ''; $resultCode = null; \exec($gitCloneCommand, $output, $resultCode); + \var_dump($output); $this->assertEquals(0, $resultCode); $this->assertFileExists('/tmp/clone-branch/README.md'); From f9a0270b5460a764db2e46cb8c5b5ca9a05ef9cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 29 Jul 2024 11:00:04 +0000 Subject: [PATCH 3/5] Update CI/CD env --- .github/workflows/tests.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f17f711..eaec33f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -10,6 +10,11 @@ jobs: - name: Check out the repo uses: actions/checkout@v2 + - name: Install dependencies + run: | + apt-get update + apt-get -y install git + - name: Start Test Stack env: PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }} From 6020cd56876d7824ddd4a08033d1180f03c3e86b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 29 Jul 2024 11:02:08 +0000 Subject: [PATCH 4/5] Fix CI/CD --- .github/workflows/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index eaec33f..03ae025 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -12,8 +12,8 @@ jobs: - name: Install dependencies run: | - apt-get update - apt-get -y install git + sudo apt-get update + sudo apt-get -y install lcov git - name: Start Test Stack env: From 2a35b156655fe765dd7bddf49ed4e75a5544e6be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 29 Jul 2024 11:03:41 +0000 Subject: [PATCH 5/5] Install git for tests --- .github/workflows/tests.yml | 5 ----- Dockerfile | 2 ++ 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 03ae025..f17f711 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -10,11 +10,6 @@ jobs: - name: Check out the repo uses: actions/checkout@v2 - - name: Install dependencies - run: | - sudo apt-get update - sudo apt-get -y install lcov git - - name: Start Test Stack env: PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }} diff --git a/Dockerfile b/Dockerfile index 19d90e3..f7d246a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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/