Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoint to fetch repo tree #17

Merged
merged 1 commit into from
Jun 7, 2024
Merged
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
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' : '');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets check if we can specify depth limit

$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');
}
Comment on lines +162 to +173
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we want a new method? Can we not use the existing listRepositoryContents method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, we can add it to the vcs library and may or may not use it in appwrite


/**
* 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
Loading