Skip to content

Commit

Permalink
Merge pull request #17 from utopia-php/feat-add-get-tree-endpoint
Browse files Browse the repository at this point in the history
Add endpoint to fetch repo tree
  • Loading branch information
christyjacob4 committed Jun 7, 2024
2 parents 8d8ff1a + 929888a commit 5ed3f98
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/VCS/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,17 @@ abstract public function listBranches(string $owner, string $repositoryName): ar
*/
abstract public function updateCommitStatus(string $repositoryName, string $SHA, string $owner, string $state, string $description = '', string $target_url = '', string $context = ''): void;

/**
* Get repository tree
*
* @param string $owner Owner name of the repository
* @param string $repositoryName Name of the GitHub repository
* @param string $branch Name of the branch
* @param bool $recursive Whether to fetch the tree recursively
* @return array<string> List of files in the repository
*/
abstract public function getRepositoryTree(string $owner, string $repositoryName, string $branch, bool $recursive = false): array;

/**
* Get repository languages
*
Expand Down
22 changes: 22 additions & 0 deletions src/VCS/Adapter/Git/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,28 @@ public function getRepositoryName(string $repositoryId): string
return $response['body']['name'];
}

/**
* Get repository tree
*
* @param string $owner Owner name of the repository
* @param string $repositoryName Name of the GitHub repository
* @param string $branch Name of the branch
* @param bool $recursive Whether to fetch the tree recursively
* @return array<string> List of files in the repository
*/
public function getRepositoryTree(string $owner, string $repositoryName, string $branch, bool $recursive = false): array
{
// if recursive is true, add optional query param to url
$url = "/repos/$owner/$repositoryName/git/trees/$branch" . ($recursive ? '?recursive=1' : '');
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"]);

if ($response['headers']['status-code'] == 404) {
return [];
}

return array_column($response['body']['tree'], 'path');
}

/**
* Get repository languages
*
Expand Down
37 changes: 37 additions & 0 deletions tests/VCS/Adapter/GitHubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,43 @@ public function testGetRepositoryName(): void
$this->assertEquals('basic-js-crud', $repositoryName);
}

public function testGetRepositoryTree(): void
{
$owner = 'test-kh';
$repositoryName = 'test1';
$branch = 'main';
$tree = $this->vcsAdapter->getRepositoryTree($owner, $repositoryName, $branch);

$this->assertIsArray($tree);
$this->assertNotEmpty($tree);

// test for an invalid repo
$repositoryName = 'test3';
$tree = $this->vcsAdapter->getRepositoryTree($owner, $repositoryName, $branch);
$this->assertIsArray($tree);
$this->assertEmpty($tree);

// test for an empty repository
$repositoryName = 'test2';
$tree = $this->vcsAdapter->getRepositoryTree($owner, $repositoryName, $branch);
$this->assertIsArray($tree);
$this->assertEmpty($tree);

// test for recursive tree
$repositoryName = 'test4';
$tree = $this->vcsAdapter->getRepositoryTree($owner, $repositoryName, $branch, true);
$this->assertIsArray($tree);
$this->assertNotEmpty($tree);
$this->assertEquals('src/folder/README.md', $tree[2]);

// test for recursive false
$repositoryName = 'test4';
$tree = $this->vcsAdapter->getRepositoryTree($owner, $repositoryName, $branch);
$this->assertIsArray($tree);
$this->assertNotEmpty($tree);
$this->assertEquals(1, count($tree));
}

public function testListRepositoryContents(): void
{
$owner = 'test-kh';
Expand Down
2 changes: 2 additions & 0 deletions tests/VCS/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ abstract public function testGetComment(): void;

abstract public function testGetPullRequest(): void;

abstract public function testGetRepositoryTree(): void;

public function testGetPullRequestFromBranch(): void
{
$result = $this->vcsAdapter->getPullRequestFromBranch('vermakhushboo', 'basic-js-crud', 'test');
Expand Down

0 comments on commit 5ed3f98

Please sign in to comment.