diff --git a/src/VCS/Adapter.php b/src/VCS/Adapter.php index 8a7d4c9..a0102cb 100644 --- a/src/VCS/Adapter.php +++ b/src/VCS/Adapter.php @@ -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 List of files in the repository + */ + abstract public function getRepositoryTree(string $owner, string $repositoryName, string $branch, bool $recursive = false): array; + /** * Get repository languages * diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index d26206f..7de9650 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -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 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 * diff --git a/tests/VCS/Adapter/GitHubTest.php b/tests/VCS/Adapter/GitHubTest.php index 89dc552..ad06f56 100644 --- a/tests/VCS/Adapter/GitHubTest.php +++ b/tests/VCS/Adapter/GitHubTest.php @@ -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'; diff --git a/tests/VCS/Base.php b/tests/VCS/Base.php index ceb7f46..5ca7708 100644 --- a/tests/VCS/Base.php +++ b/tests/VCS/Base.php @@ -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');