From 089dd5ad8bd9deefadad81aaa34f851e35756741 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Thu, 2 May 2024 23:51:47 +0530 Subject: [PATCH 1/5] Add tests for listContents endpoint --- tests/VCS/Adapter/GitHubTest.php | 37 ++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/VCS/Adapter/GitHubTest.php b/tests/VCS/Adapter/GitHubTest.php index c0afc562..22524c82 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 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); + } + public function testGetPullRequest(): void { $owner = 'vermakhushboo'; From 59d67c6d0ec8260c087572eebcd0a421f474634d Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Fri, 3 May 2024 00:32:06 +0530 Subject: [PATCH 2/5] Handled the error --- src/VCS/Adapter/Git/GitHub.php | 14 +++++++++----- tests/VCS/Adapter/GitHubTest.php | 7 +++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index f5f3bd67..2945a261 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,13 @@ 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'); + } elseif (isset($response['body']['name'])) { + return [$response['body']['name']]; + } + + return []; } public function deleteRepository(string $owner, string $repositoryName): bool diff --git a/tests/VCS/Adapter/GitHubTest.php b/tests/VCS/Adapter/GitHubTest.php index 22524c82..a8c659c9 100644 --- a/tests/VCS/Adapter/GitHubTest.php +++ b/tests/VCS/Adapter/GitHubTest.php @@ -173,6 +173,13 @@ public function testListRepositoryContents(): void $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 From b2bcbf96c7a11a7b78bbc7fa4dd8e5ca2c73fc94 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Tue, 7 May 2024 19:09:48 +0530 Subject: [PATCH 3/5] Update src/VCS/Adapter/Git/GitHub.php Co-authored-by: Christy Jacob --- src/VCS/Adapter/Git/GitHub.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index 2945a261..b632cfeb 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -193,10 +193,12 @@ public function listRepositoryContents(string $owner, string $repositoryName, st if (isset($response['body'][0])) { return array_column($response['body'], 'name'); - } elseif (isset($response['body']['name'])) { + } + + if (isset($response['body']['name'])) { return [$response['body']['name']]; } - + return []; } From b682863da9cbca47aade68cf9346069891dff393 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Tue, 7 May 2024 19:13:54 +0530 Subject: [PATCH 4/5] Fix linter errors --- src/VCS/Adapter/Git/GitHub.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index b632cfeb..2452d4ab 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -193,12 +193,12 @@ public function listRepositoryContents(string $owner, string $repositoryName, st if (isset($response['body'][0])) { return array_column($response['body'], 'name'); - } - + } + if (isset($response['body']['name'])) { return [$response['body']['name']]; } - + return []; } From 3b54947e2b42ad39f03fce818efea9277069eb11 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Fri, 17 May 2024 14:43:27 +0530 Subject: [PATCH 5/5] Throw exception if access token not generated --- src/VCS/Adapter/Git/GitHub.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index 2452d4ab..d26206f7 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -309,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']; } /**