Skip to content

Commit

Permalink
Merge pull request #20 from utopia-php/feat-get-contents-metadata
Browse files Browse the repository at this point in the history
Feat:  metadata in repo contents
  • Loading branch information
Meldiron committed Jun 26, 2024
2 parents 5ed3f98 + cef04f3 commit 4745fcf
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 19 deletions.
29 changes: 15 additions & 14 deletions composer.lock

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

24 changes: 19 additions & 5 deletions src/VCS/Adapter/Git/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -213,15 +217,25 @@ public function listRepositoryContents(string $owner, string $repositoryName, st
return [];
}

if (isset($response['body'][0])) {
return array_column($response['body'], 'name');
$items = [];

if (!empty($response['body'][0])) {
$items = $response['body'];
} elseif (!empty($response['body'])) {
$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
Expand Down
2 changes: 2 additions & 0 deletions tests/Detector/DetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ 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);
}

// 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);
Expand Down
32 changes: 32 additions & 0 deletions tests/VCS/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -83,6 +84,37 @@ 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->assertNotEmpty($fileContent['name']);
$this->assertStringContainsString('.', $fileContent['name']);
$this->assertIsNumeric($fileContent['size']);
$this->assertGreaterThan(0, $fileContent['size']);

$directoryContent = null;
foreach ($contents as $content) {
if ($content['type'] === GitHub::CONTENTS_DIRECTORY) {
$directoryContent = $content;
break;
}
}
$this->assertNotNull($directoryContent);
$this->assertNotEmpty($directoryContent['name']);
$this->assertIsNumeric($directoryContent['size']);
$this->assertEquals(0, $directoryContent['size']);
}

public function testCreateRepository(): void
Expand Down

0 comments on commit 4745fcf

Please sign in to comment.