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..53f9e173 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -192,11 +192,18 @@ 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"]); + $response = $this->call(self::METHOD_DELETE, $url, ['Authorization' => "Bearer $this->accessToken"]); + + $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 4b718396..ac19fc9b 100644 --- a/tests/VCS/Base.php +++ b/tests/VCS/Base.php @@ -2,6 +2,7 @@ namespace Utopia\Tests; +use Exception; use PHPUnit\Framework\TestCase; use Utopia\App; use Utopia\VCS\Adapter\Git; @@ -81,4 +82,22 @@ 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']); + } + + /** + * @depends testCreateRepository + */ + public function testDeleteRepository(): void + { + $result = $this->vcsAdapter->deleteRepository('test-kh', 'new-repo'); + $this->assertEquals(true, $result); + $this->expectException(Exception::class); + $result = $this->vcsAdapter->deleteRepository('test-kh', 'new-repo-2'); + } }