From 78edc3164b6cdaa26006ee6cfb0296f02518a1e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 26 Jun 2024 09:17:50 +0000 Subject: [PATCH 1/4] Add metadata to repositroy contents --- composer.lock | 29 +++++++++++++++-------------- src/VCS/Adapter/Git/GitHub.php | 22 ++++++++++++++++++---- tests/Detector/DetectorTest.php | 2 ++ 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/composer.lock b/composer.lock index 8f21fda..2ada838 100644 --- a/composer.lock +++ b/composer.lock @@ -65,16 +65,16 @@ }, { "name": "utopia-php/cache", - "version": "0.10.0", + "version": "0.10.2", "source": { "type": "git", "url": "https://github.com/utopia-php/cache.git", - "reference": "313bcdfbb166f75c2c205a59d1467cead63a9626" + "reference": "b22c6eb6d308de246b023efd0fc9758aee8b8247" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/cache/zipball/313bcdfbb166f75c2c205a59d1467cead63a9626", - "reference": "313bcdfbb166f75c2c205a59d1467cead63a9626", + "url": "https://api.github.com/repos/utopia-php/cache/zipball/b22c6eb6d308de246b023efd0fc9758aee8b8247", + "reference": "b22c6eb6d308de246b023efd0fc9758aee8b8247", "shasum": "" }, "require": { @@ -109,9 +109,9 @@ ], "support": { "issues": "https://github.com/utopia-php/cache/issues", - "source": "https://github.com/utopia-php/cache/tree/0.10.0" + "source": "https://github.com/utopia-php/cache/tree/0.10.2" }, - "time": "2024-06-05T16:40:43+00:00" + "time": "2024-06-25T20:36:35+00:00" }, { "name": "utopia-php/framework", @@ -301,16 +301,16 @@ }, { "name": "myclabs/deep-copy", - "version": "1.11.1", + "version": "1.12.0", "source": { "type": "git", "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "7284c22080590fb39f2ffa3e9057f10a4ddd0e0c" + "reference": "3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/7284c22080590fb39f2ffa3e9057f10a4ddd0e0c", - "reference": "7284c22080590fb39f2ffa3e9057f10a4ddd0e0c", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c", + "reference": "3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c", "shasum": "" }, "require": { @@ -318,11 +318,12 @@ }, "conflict": { "doctrine/collections": "<1.6.8", - "doctrine/common": "<2.13.3 || >=3,<3.2.2" + "doctrine/common": "<2.13.3 || >=3 <3.2.2" }, "require-dev": { "doctrine/collections": "^1.6.8", "doctrine/common": "^2.13.3 || ^3.2.2", + "phpspec/prophecy": "^1.10", "phpunit/phpunit": "^7.5.20 || ^8.5.23 || ^9.5.13" }, "type": "library", @@ -348,7 +349,7 @@ ], "support": { "issues": "https://github.com/myclabs/DeepCopy/issues", - "source": "https://github.com/myclabs/DeepCopy/tree/1.11.1" + "source": "https://github.com/myclabs/DeepCopy/tree/1.12.0" }, "funding": [ { @@ -356,7 +357,7 @@ "type": "tidelift" } ], - "time": "2023-03-08T13:26:56+00:00" + "time": "2024-06-12T14:39:25+00:00" }, { "name": "nikic/php-parser", @@ -2038,5 +2039,5 @@ "php": ">=8.0" }, "platform-dev": [], - "plugin-api-version": "2.2.0" + "plugin-api-version": "2.6.0" } diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index 7de9650..fc886b0 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -16,6 +16,10 @@ class GitHub extends Git public const EVENT_INSTALLATION = 'installation'; + public const CONTENTS_DIRECTORY = 'dir'; + + public const CONTENTS_FILE = 'file'; + protected string $endpoint = 'https://api.github.com'; protected string $accessToken; @@ -213,15 +217,25 @@ public function listRepositoryContents(string $owner, string $repositoryName, st return []; } + $items = []; + if (isset($response['body'][0])) { - return array_column($response['body'], 'name'); + $items = $response['body']; + } else { + $items = [$response['body']]; } - if (isset($response['body']['name'])) { - return [$response['body']['name']]; + $contents = []; + foreach ($items as $item) { + $type = $item['type'] ?? 'file'; + $contents[] = [ + 'name' => $item['name'] ?? '', + 'size' => $item['size'] ?? 0, + 'type' => $type === 'file' ? self::CONTENTS_FILE : self::CONTENTS_DIRECTORY + ]; } - return []; + return $contents; } public function deleteRepository(string $owner, string $repositoryName): bool diff --git a/tests/Detector/DetectorTest.php b/tests/Detector/DetectorTest.php index c151d5d..2ac4826 100644 --- a/tests/Detector/DetectorTest.php +++ b/tests/Detector/DetectorTest.php @@ -79,6 +79,7 @@ public function testLanguageDetection(): void foreach ($languageMap as [$owner, $repositoryName, $expectedRuntime]) { $files = $this->github->listRepositoryContents($owner, $repositoryName); + $files = \array_column($files, 'name'); $languages = $this->github->listRepositoryLanguages($owner, $repositoryName); $runtime = $this->detect($files, $languages); $this->assertEquals($expectedRuntime, $runtime); @@ -86,6 +87,7 @@ public function testLanguageDetection(): void // test for FAILURE $files = $this->github->listRepositoryContents('', ''); + $files = \array_column($files, 'name'); $languages = $this->github->listRepositoryLanguages('', ''); $runtime = $this->detect($files, $languages); $this->assertEquals(null, $runtime); From b3e174e0bb2729f120815c5d3ce240fed4b40e33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 26 Jun 2024 09:23:31 +0000 Subject: [PATCH 2/4] Improve tests --- src/VCS/Adapter/Git/GitHub.php | 4 ++-- tests/VCS/Base.php | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index fc886b0..53fe1e7 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -219,9 +219,9 @@ public function listRepositoryContents(string $owner, string $repositoryName, st $items = []; - if (isset($response['body'][0])) { + if (!empty($response['body'][0])) { $items = $response['body']; - } else { + } elseif (!empty($response['body'])) { $items = [$response['body']]; } diff --git a/tests/VCS/Base.php b/tests/VCS/Base.php index 5ca7708..4f248a9 100644 --- a/tests/VCS/Base.php +++ b/tests/VCS/Base.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use Utopia\Http\Http; use Utopia\VCS\Adapter\Git; +use Utopia\VCS\Adapter\Git\GitHub; abstract class Base extends TestCase { @@ -83,6 +84,32 @@ public function testListRepositoryContents(): void $contents = $this->vcsAdapter->listRepositoryContents('appwrite', 'appwrite', 'src/Appwrite'); $this->assertIsArray($contents); $this->assertNotEmpty($contents); + + + $contents = $this->vcsAdapter->listRepositoryContents('appwrite', 'appwrite', ''); + $this->assertIsArray($contents); + $this->assertNotEmpty($contents); + $this->assertGreaterThan(0, \count($contents)); + + $fileContent = null; + foreach ($contents as $content) { + if ($content['type'] === GitHub::CONTENTS_FILE) { + $fileContent = $content; + break; + } + } + $this->assertNotNull($fileContent); + $this->assertStringContainsString('.', $fileContent['name']); + + $directoryContent = null; + foreach ($contents as $content) { + if ($content['type'] === GitHub::CONTENTS_DIRECTORY) { + $directoryContent = $content; + break; + } + } + $this->assertNotNull($directoryContent); + $this->assertEquals(0, $directoryContent['size']); } public function testCreateRepository(): void From 9ddfdadca906de8996035ed74158c9b0456ac0f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 26 Jun 2024 09:35:00 +0000 Subject: [PATCH 3/4] Improve tests --- tests/VCS/Base.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/VCS/Base.php b/tests/VCS/Base.php index 4f248a9..8e1d0b6 100644 --- a/tests/VCS/Base.php +++ b/tests/VCS/Base.php @@ -100,6 +100,8 @@ public function testListRepositoryContents(): void } $this->assertNotNull($fileContent); $this->assertStringContainsString('.', $fileContent['name']); + $this->assertIsNumeric($fileContent['size']); + $this->assertGreaterThan(0, $fileContent['size']); $directoryContent = null; foreach ($contents as $content) { @@ -109,6 +111,7 @@ public function testListRepositoryContents(): void } } $this->assertNotNull($directoryContent); + $this->assertIsNumeric($fileContent['size']); $this->assertEquals(0, $directoryContent['size']); } From cef04f3102543c948976cc136a9e563855840eb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 26 Jun 2024 09:35:53 +0000 Subject: [PATCH 4/4] Improve tests --- tests/VCS/Base.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/VCS/Base.php b/tests/VCS/Base.php index 8e1d0b6..b87413b 100644 --- a/tests/VCS/Base.php +++ b/tests/VCS/Base.php @@ -99,6 +99,7 @@ public function testListRepositoryContents(): void } } $this->assertNotNull($fileContent); + $this->assertNotEmpty($fileContent['name']); $this->assertStringContainsString('.', $fileContent['name']); $this->assertIsNumeric($fileContent['size']); $this->assertGreaterThan(0, $fileContent['size']); @@ -111,7 +112,8 @@ public function testListRepositoryContents(): void } } $this->assertNotNull($directoryContent); - $this->assertIsNumeric($fileContent['size']); + $this->assertNotEmpty($directoryContent['name']); + $this->assertIsNumeric($directoryContent['size']); $this->assertEquals(0, $directoryContent['size']); }