From d499ae5c9e6c9c5ea3e555f8ee2abcdd2d926d06 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Mon, 16 Oct 2023 15:44:10 +0530 Subject: [PATCH 1/6] Add tests for create and delete repos --- src/VCS/Adapter.php | 2 +- src/VCS/Adapter/Git/GitHub.php | 4 +++- tests/VCS/Base.php | 13 +++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/VCS/Adapter.php b/src/VCS/Adapter.php index 518d82f1..8a7d4c95 100644 --- a/src/VCS/Adapter.php +++ b/src/VCS/Adapter.php @@ -116,7 +116,7 @@ abstract public function createRepository(string $owner, string $repositoryName, /** * Delete repository */ - abstract public function deleteRepository(string $owner, string $repositoryName): void; + abstract public function deleteRepository(string $owner, string $repositoryName): bool; /** * Get latest opened pull request with specific base branch diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index 1f2da01a..9df98570 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -192,11 +192,13 @@ public function listRepositoryContents(string $owner, string $repositoryName, st }, $response['body']); } - public function deleteRepository(string $owner, string $repositoryName): void + public function deleteRepository(string $owner, string $repositoryName): bool { $url = "/repos/{$owner}/{$repositoryName}"; $this->call(self::METHOD_DELETE, $url, ['Authorization' => "Bearer $this->accessToken"]); + + return true; } /** diff --git a/tests/VCS/Base.php b/tests/VCS/Base.php index 4b718396..0981b1e9 100644 --- a/tests/VCS/Base.php +++ b/tests/VCS/Base.php @@ -81,4 +81,17 @@ public function testListRepositoryContents(): void $this->assertIsArray($contents); $this->assertNotEmpty($contents); } + + public function testCreateRepository(): void + { + $repository = $this->vcsAdapter->createRepository('test-kh', 'new-repo', true); + $this->assertIsArray($repository); + $this->assertEquals('test-kh/new-repo', $repository['full_name']); + } + + public function testDeleteRepository(): void + { + $result = $this->vcsAdapter->deleteRepository('test-kh', 'new-repo'); + $this->assertEquals($result, true); + } } From 929b115ce2c164175708afaf0b5315b92ce02d32 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Mon, 16 Oct 2023 15:54:06 +0530 Subject: [PATCH 2/6] Add depends tag --- tests/VCS/Base.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/VCS/Base.php b/tests/VCS/Base.php index 0981b1e9..b9d66980 100644 --- a/tests/VCS/Base.php +++ b/tests/VCS/Base.php @@ -89,6 +89,9 @@ public function testCreateRepository(): void $this->assertEquals('test-kh/new-repo', $repository['full_name']); } + /** + * @depends testCreateRepository + */ public function testDeleteRepository(): void { $result = $this->vcsAdapter->deleteRepository('test-kh', 'new-repo'); From 078748f7ec7bf3ef59c3943c27890ce18d7dfc7b Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Mon, 16 Oct 2023 16:19:34 +0530 Subject: [PATCH 3/6] Assert that delete is successful --- src/VCS/Adapter.php | 2 +- src/VCS/Adapter/Git/GitHub.php | 6 +++--- tests/VCS/Base.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/VCS/Adapter.php b/src/VCS/Adapter.php index 8a7d4c95..7fec7251 100644 --- a/src/VCS/Adapter.php +++ b/src/VCS/Adapter.php @@ -116,7 +116,7 @@ abstract public function createRepository(string $owner, string $repositoryName, /** * Delete repository */ - abstract public function deleteRepository(string $owner, string $repositoryName): bool; + abstract public function deleteRepository(string $owner, string $repositoryName): int; /** * Get latest opened pull request with specific base branch diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index 9df98570..784ba5ea 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -192,13 +192,13 @@ public function listRepositoryContents(string $owner, string $repositoryName, st }, $response['body']); } - public function deleteRepository(string $owner, string $repositoryName): bool + public function deleteRepository(string $owner, string $repositoryName): int { $url = "/repos/{$owner}/{$repositoryName}"; - $this->call(self::METHOD_DELETE, $url, ['Authorization' => "Bearer $this->accessToken"]); + $response = $this->call(self::METHOD_DELETE, $url, ['Authorization' => "Bearer $this->accessToken"]); - return true; + return $response['headers']['status-code']; } /** diff --git a/tests/VCS/Base.php b/tests/VCS/Base.php index b9d66980..74050fe4 100644 --- a/tests/VCS/Base.php +++ b/tests/VCS/Base.php @@ -95,6 +95,6 @@ public function testCreateRepository(): void public function testDeleteRepository(): void { $result = $this->vcsAdapter->deleteRepository('test-kh', 'new-repo'); - $this->assertEquals($result, true); + $this->assertEquals(204, $result); } } From 8cb825fbbcd247cc61399a02e07495d8c78ec49b Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Mon, 16 Oct 2023 16:25:07 +0530 Subject: [PATCH 4/6] Add assert for failure --- tests/VCS/Base.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/VCS/Base.php b/tests/VCS/Base.php index 74050fe4..9456f0ce 100644 --- a/tests/VCS/Base.php +++ b/tests/VCS/Base.php @@ -96,5 +96,8 @@ public function testDeleteRepository(): void { $result = $this->vcsAdapter->deleteRepository('test-kh', 'new-repo'); $this->assertEquals(204, $result); + + $result = $result = $this->vcsAdapter->deleteRepository('test-kh', 'new-repo-2'); + $this->assertEquals(404, $result); } } From abe1414900820b0baba81256a657b7f04e72a669 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Tue, 17 Oct 2023 21:12:22 +0530 Subject: [PATCH 5/6] Throw exception if repo not found --- src/VCS/Adapter.php | 2 +- src/VCS/Adapter/Git/GitHub.php | 8 ++++++-- tests/VCS/Base.php | 8 ++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/VCS/Adapter.php b/src/VCS/Adapter.php index 7fec7251..8a7d4c95 100644 --- a/src/VCS/Adapter.php +++ b/src/VCS/Adapter.php @@ -116,7 +116,7 @@ abstract public function createRepository(string $owner, string $repositoryName, /** * Delete repository */ - abstract public function deleteRepository(string $owner, string $repositoryName): int; + abstract public function deleteRepository(string $owner, string $repositoryName): bool; /** * Get latest opened pull request with specific base branch diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index 784ba5ea..cb612471 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -192,13 +192,17 @@ public function listRepositoryContents(string $owner, string $repositoryName, st }, $response['body']); } - public function deleteRepository(string $owner, string $repositoryName): int + public function deleteRepository(string $owner, string $repositoryName): bool { $url = "/repos/{$owner}/{$repositoryName}"; $response = $this->call(self::METHOD_DELETE, $url, ['Authorization' => "Bearer $this->accessToken"]); - return $response['headers']['status-code']; + if ($response['headers']['status-code'] == 204) { + return true; + } else { + throw new RepositoryNotFound("Repository not found"); + } } /** diff --git a/tests/VCS/Base.php b/tests/VCS/Base.php index 9456f0ce..a3d56a50 100644 --- a/tests/VCS/Base.php +++ b/tests/VCS/Base.php @@ -5,6 +5,7 @@ use PHPUnit\Framework\TestCase; use Utopia\App; use Utopia\VCS\Adapter\Git; +use Utopia\VCS\Exception\RepositoryNotFound; abstract class Base extends TestCase { @@ -95,9 +96,8 @@ public function testCreateRepository(): void public function testDeleteRepository(): void { $result = $this->vcsAdapter->deleteRepository('test-kh', 'new-repo'); - $this->assertEquals(204, $result); - - $result = $result = $this->vcsAdapter->deleteRepository('test-kh', 'new-repo-2'); - $this->assertEquals(404, $result); + $this->assertEquals(true, $result); + $this->expectException(RepositoryNotFound::class); + $result = $this->vcsAdapter->deleteRepository('test-kh', 'new-repo-2'); } } From c293d0610c4f153fec02fb509791ec256b326adb Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Tue, 17 Oct 2023 22:19:59 +0530 Subject: [PATCH 6/6] Throw exception if error code >=400 --- src/VCS/Adapter/Git/GitHub.php | 9 +++++---- tests/VCS/Base.php | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index cb612471..53f9e173 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -198,11 +198,12 @@ public function deleteRepository(string $owner, string $repositoryName): bool $response = $this->call(self::METHOD_DELETE, $url, ['Authorization' => "Bearer $this->accessToken"]); - if ($response['headers']['status-code'] == 204) { - return true; - } else { - throw new RepositoryNotFound("Repository not found"); + $statusCode = $response['headers']['status-code']; + + if ($statusCode >= 400) { + throw new Exception("Deleting repository $repositoryName failed with status code $statusCode"); } + return true; } /** diff --git a/tests/VCS/Base.php b/tests/VCS/Base.php index a3d56a50..ac19fc9b 100644 --- a/tests/VCS/Base.php +++ b/tests/VCS/Base.php @@ -2,10 +2,10 @@ namespace Utopia\Tests; +use Exception; use PHPUnit\Framework\TestCase; use Utopia\App; use Utopia\VCS\Adapter\Git; -use Utopia\VCS\Exception\RepositoryNotFound; abstract class Base extends TestCase { @@ -97,7 +97,7 @@ public function testDeleteRepository(): void { $result = $this->vcsAdapter->deleteRepository('test-kh', 'new-repo'); $this->assertEquals(true, $result); - $this->expectException(RepositoryNotFound::class); + $this->expectException(Exception::class); $result = $this->vcsAdapter->deleteRepository('test-kh', 'new-repo-2'); } }