diff --git a/src/VCS/Adapter.php b/src/VCS/Adapter.php index da01f4a..27478a9 100644 --- a/src/VCS/Adapter.php +++ b/src/VCS/Adapter.php @@ -97,6 +97,14 @@ abstract public function getOwnerName(string $installationId): string; */ abstract public function searchRepositories(string $installationId, string $owner, int $page, int $per_page, string $search = ''): array; + /** + * Get repository for the installation + * + * @param string $repositoryName + * @return array + */ + abstract public function getInstallationRepository(string $repositoryName): array; + /** * Get repository * diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index 75a4658..0c5abf7 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -189,6 +189,41 @@ public function searchRepositories(string $installationId, string $owner, int $p ]; } + public function getInstallationRepository(string $repositoryName): array + { + $currentPage = 1; + $perPage = 100; + $totalRepositories = 0; + $maxRepositories = 1000; + $url = '/installation/repositories'; + + while ($totalRepositories < $maxRepositories) { + $response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"], [ + 'page' => $currentPage, + 'per_page' => $perPage, + ]); + + if (!isset($response['body']['repositories'])) { + throw new Exception("Repositories list missing in the response."); + } + + foreach ($response['body']['repositories'] as $repo) { + if (\strtolower($repo['name']) === \strtolower($repositoryName)) { + return $repo; + } + } + + if (\count($response['body']['repositories']) < $perPage) { + break; + } + + $currentPage++; + $totalRepositories += $perPage; + } + + throw new RepositoryNotFound("Repository not found."); + } + /** * Get GitHub repository * diff --git a/src/VCS/Adapter/Git/Gitea.php b/src/VCS/Adapter/Git/Gitea.php index fc85bf4..a371e51 100644 --- a/src/VCS/Adapter/Git/Gitea.php +++ b/src/VCS/Adapter/Git/Gitea.php @@ -118,6 +118,11 @@ public function searchRepositories(string $installationId, string $owner, int $p throw new Exception("Not implemented yet"); } + public function getInstallationRepository(string $repositoryName): array + { + throw new Exception("Not implemented yet"); + } + public function getRepository(string $owner, string $repositoryName): array { $url = "/repos/{$owner}/{$repositoryName}"; diff --git a/tests/VCS/Adapter/GitHubTest.php b/tests/VCS/Adapter/GitHubTest.php index 6b6025f..74af211 100644 --- a/tests/VCS/Adapter/GitHubTest.php +++ b/tests/VCS/Adapter/GitHubTest.php @@ -173,6 +173,23 @@ public function testGetComment(): void $this->assertNotEmpty($result); } + public function testGetInstallationRepository(): void + { + $repositoryName = 'astro-starter'; + $repo = $this->vcsAdapter->getInstallationRepository($repositoryName); + $this->assertIsArray($repo); + $this->assertSame($repositoryName, $repo['name']); + } + + public function testGetRepository(): void + { + $owner = 'vermakhushboo'; + $repositoryName = 'basic-js-crud'; + $repo = $this->vcsAdapter->getRepository($owner, $repositoryName); + $this->assertIsArray($repo); + $this->assertSame($repositoryName, $repo['name']); + } + public function testGetRepositoryName(): void { $repositoryName = $this->vcsAdapter->getRepositoryName('432284323');