diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index f5f3bd67..d26206f7 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -99,7 +99,7 @@ public function createRepository(string $owner, string $repositoryName, bool $pr * * @throws Exception */ - public function searchRepositories(string $owner, int $page, int $per_page, string $search=''): array + public function searchRepositories(string $owner, int $page, int $per_page, string $search = ''): array { $url = '/search/repositories'; @@ -107,7 +107,7 @@ public function searchRepositories(string $owner, int $page, int $per_page, stri 'q' => "{$search} user:{$owner} fork:true", 'per_page' => $per_page, 'sort' => 'updated' - ]); + ]); if (!isset($response['body']['items'])) { throw new Exception("Repositories list missing in the response."); @@ -191,9 +191,15 @@ public function listRepositoryContents(string $owner, string $repositoryName, st return []; } - return array_map(static function ($item) { - return $item['name']; - }, $response['body']); + if (isset($response['body'][0])) { + return array_column($response['body'], 'name'); + } + + if (isset($response['body']['name'])) { + return [$response['body']['name']]; + } + + return []; } public function deleteRepository(string $owner, string $repositoryName): bool @@ -303,7 +309,10 @@ protected function generateAccessToken(string $privateKey, string $githubAppId): $token = $jwt->encode($payload); $this->jwtToken = $token; $res = $this->call(self::METHOD_POST, '/app/installations/' . $this->installationId . '/access_tokens', ['Authorization' => 'Bearer ' . $token]); - $this->accessToken = $res['body']['token'] ?? ''; + if (!isset($res['body']['token'])) { + throw new Exception('Failed to retrieve access token from GitHub API.'); + } + $this->accessToken = $res['body']['token']; } /** diff --git a/tests/VCS/Adapter/GitHubTest.php b/tests/VCS/Adapter/GitHubTest.php index c0afc562..a8c659c9 100644 --- a/tests/VCS/Adapter/GitHubTest.php +++ b/tests/VCS/Adapter/GitHubTest.php @@ -138,6 +138,50 @@ public function testGetRepositoryName(): void $this->assertEquals('basic-js-crud', $repositoryName); } + public function testListRepositoryContents(): void + { + $owner = 'test-kh'; + $repositoryName = 'test1'; + $path = ''; + $contents = $this->vcsAdapter->listRepositoryContents($owner, $repositoryName, $path); + + $this->assertIsArray($contents); + $this->assertNotEmpty($contents); + + // test for non-existent path + $path = 'non-existent-path'; + $contents = $this->vcsAdapter->listRepositoryContents($owner, $repositoryName, $path); + $this->assertIsArray($contents); + $this->assertEmpty($contents); + + // test for a valid folder + $path = 'src'; + $contents = $this->vcsAdapter->listRepositoryContents($owner, $repositoryName, $path); + $this->assertIsArray($contents); + $this->assertNotEmpty($contents); + + // test for an invalid repo + $repositoryName = 'test3'; + $path = ''; + $contents = $this->vcsAdapter->listRepositoryContents($owner, $repositoryName, $path); + $this->assertIsArray($contents); + $this->assertEmpty($contents); + + // test for an empty repository + $repositoryName = 'test2'; + $path = ''; + $contents = $this->vcsAdapter->listRepositoryContents($owner, $repositoryName, $path); + $this->assertIsArray($contents); + $this->assertEmpty($contents); + + // test for an absolute path + $repositoryName = 'test1'; + $path = 'README.md'; + $contents = $this->vcsAdapter->listRepositoryContents($owner, $repositoryName, $path); + $this->assertIsArray($contents); + $this->assertNotEmpty($contents); + } + public function testGetPullRequest(): void { $owner = 'vermakhushboo';