diff --git a/src/VCS/Adapter.php b/src/VCS/Adapter.php index 6986178d..2e9ffa77 100644 --- a/src/VCS/Adapter.php +++ b/src/VCS/Adapter.php @@ -89,9 +89,17 @@ abstract public function getUser(string $username): array; */ abstract public function getOwnerName(string $installationId, ?int $repositoryId = null): string; + /** + * Determines whether the installation has access to all repositories or specific repositories + * + * @return bool True if installation has access to all repositories, false if it has access to specific repositories + * + * @throws Exception + */ + abstract public function hasAccessToAllRepositories(): bool; + /** * Search repositories for GitHub App - * @param string $installationId ID of the installation * @param string $owner Name of user or org * @param int $page page number * @param int $per_page number of results per page @@ -100,7 +108,7 @@ abstract public function getOwnerName(string $installationId, ?int $repositoryId * * @throws Exception */ - abstract public function searchRepositories(string $installationId, string $owner, int $page, int $per_page, string $search = ''): array; + abstract public function searchRepositories(string $owner, int $page, int $per_page, string $search = ''): array; /** * Get repository for the installation diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index 2ac02d56..f2f173b5 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -174,9 +174,23 @@ public function createBranch(string $owner, string $repositoryName, string $newB throw new Exception("Not implemented"); } + /** + * Determines whether the installation has access to all repositories or specific repositories + * + * @return bool True if installation has access to all repositories, false if it has access to specific repositories + * + * @throws Exception + */ + public function hasAccessToAllRepositories(): bool + { + $url = '/app/installations/' . $this->installationId; + $response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->jwtToken"]); + $responseBody = $response['body'] ?? []; + return ($responseBody['repository_selection'] ?? '') === 'all'; + } + /** * Search repositories for GitHub App - * @param string $installationId ID of the installation * @param string $owner Name of user or org * @param int $page page number * @param int $per_page number of results per page @@ -185,16 +199,10 @@ public function createBranch(string $owner, string $repositoryName, string $newB * * @throws Exception */ - public function searchRepositories(string $installationId, string $owner, int $page, int $per_page, string $search = ''): array + public function searchRepositories(string $owner, int $page, int $per_page, string $search = ''): array { - // Find whether installation has access to all (or) specific repositories - $url = '/app/installations/' . $installationId; - $response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->jwtToken"]); - $responseBody = $response['body'] ?? []; - $hasAccessToAllRepositories = ($responseBody['repository_selection'] ?? '') === 'all'; - // Installation has access to all repositories, use the search API which supports filtering. - if ($hasAccessToAllRepositories) { + if ($this->hasAccessToAllRepositories()) { $url = '/search/repositories'; $response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"], [ diff --git a/src/VCS/Adapter/Git/Gitea.php b/src/VCS/Adapter/Git/Gitea.php index 1b8f4ba5..ef90fc50 100644 --- a/src/VCS/Adapter/Git/Gitea.php +++ b/src/VCS/Adapter/Git/Gitea.php @@ -117,17 +117,28 @@ public function createOrganization(string $orgName): string return $responseBody['name'] ?? ''; } + /** + * Determines whether the installation has access to all repositories or specific repositories + * + * @return bool True if installation has access to all repositories, false if it has access to specific repositories + * + * @throws Exception + */ + public function hasAccessToAllRepositories(): bool + { + return true; + } + /** * Search repositories in organization * - * @param string $installationId Not used in Gitea (kept for interface compatibility) * @param string $owner Organization or user name * @param int $page Page number for pagination * @param int $per_page Number of results per page * @param string $search Search query to filter repository names * @return array Array with 'items' (repositories) and 'total' count */ - public function searchRepositories(string $installationId, string $owner, int $page, int $per_page, string $search = ''): array + public function searchRepositories(string $owner, int $page, int $per_page, string $search = ''): array { $filteredRepos = []; $currentPage = 1; diff --git a/tests/VCS/Adapter/GitHubTest.php b/tests/VCS/Adapter/GitHubTest.php index a518656a..cb2c54ba 100644 --- a/tests/VCS/Adapter/GitHubTest.php +++ b/tests/VCS/Adapter/GitHubTest.php @@ -181,6 +181,11 @@ public function testGetComment(): void $this->assertNotEmpty($result); } + public function testHasAccessToAllRepositories(): void + { + $this->assertTrue($this->vcsAdapter->hasAccessToAllRepositories()); + } + public function testGetInstallationRepository(): void { $repositoryName = 'astro-starter'; diff --git a/tests/VCS/Adapter/GiteaTest.php b/tests/VCS/Adapter/GiteaTest.php index e9be9676..ac300016 100644 --- a/tests/VCS/Adapter/GiteaTest.php +++ b/tests/VCS/Adapter/GiteaTest.php @@ -201,6 +201,11 @@ public function testGetCommentInvalidId(): void $this->vcsAdapter->deleteRepository(self::$owner, $repositoryName); } + public function testHasAccessToAllRepositories(): void + { + $this->assertTrue($this->vcsAdapter->hasAccessToAllRepositories()); + } + public function testGetRepositoryTreeWithSlashInBranchName(): void { $repositoryName = 'test-branch-with-slash-' . \uniqid(); @@ -878,7 +883,7 @@ public function testSearchRepositories(): void try { // Search without filter - should return all - $result = $this->vcsAdapter->searchRepositories('', self::$owner, 1, 10); + $result = $this->vcsAdapter->searchRepositories(self::$owner, 1, 10); $this->assertIsArray($result); $this->assertArrayHasKey('items', $result); @@ -886,7 +891,7 @@ public function testSearchRepositories(): void $this->assertGreaterThanOrEqual(3, $result['total']); // Search with filter - $result = $this->vcsAdapter->searchRepositories('', self::$owner, 1, 10, 'test-search'); + $result = $this->vcsAdapter->searchRepositories(self::$owner, 1, 10, 'test-search'); $this->assertIsArray($result); $this->assertGreaterThanOrEqual(2, $result['total']); @@ -911,15 +916,15 @@ public function testSearchRepositoriesPagination(): void $this->vcsAdapter->createRepository(self::$owner, $repo2, false); try { - $result = $this->vcsAdapter->searchRepositories('', self::$owner, 1, 1, 'test-pagination'); + $result = $this->vcsAdapter->searchRepositories(self::$owner, 1, 1, 'test-pagination'); $this->assertSame(1, count($result['items'])); $this->assertGreaterThanOrEqual(2, $result['total']); - $result2 = $this->vcsAdapter->searchRepositories('', self::$owner, 2, 1, 'test-pagination'); + $result2 = $this->vcsAdapter->searchRepositories(self::$owner, 2, 1, 'test-pagination'); $this->assertSame(1, count($result2['items'])); - $result20 = $this->vcsAdapter->searchRepositories('', self::$owner, 20, 1, 'test-pagination'); + $result20 = $this->vcsAdapter->searchRepositories(self::$owner, 20, 1, 'test-pagination'); $this->assertIsArray($result20); $this->assertEmpty($result20['items']); @@ -931,7 +936,7 @@ public function testSearchRepositoriesPagination(): void public function testSearchRepositoriesNoResults(): void { - $result = $this->vcsAdapter->searchRepositories('', self::$owner, 1, 10, 'nonexistent-repo-xyz-' . \uniqid()); + $result = $this->vcsAdapter->searchRepositories(self::$owner, 1, 10, 'nonexistent-repo-xyz-' . \uniqid()); $this->assertIsArray($result); $this->assertEmpty($result['items']); @@ -940,7 +945,7 @@ public function testSearchRepositoriesNoResults(): void public function testSearchRepositoriesInvalidOwner(): void { - $result = $this->vcsAdapter->searchRepositories('', 'nonexistent-owner-' . \uniqid(), 1, 10); + $result = $this->vcsAdapter->searchRepositories('nonexistent-owner-' . \uniqid(), 1, 10); $this->assertIsArray($result); $this->assertEmpty($result['items']); diff --git a/tests/VCS/Base.php b/tests/VCS/Base.php index 8e17c8cc..3ad42889 100644 --- a/tests/VCS/Base.php +++ b/tests/VCS/Base.php @@ -103,8 +103,7 @@ public function testGetOwnerName(): void public function testSearchRepositories(): void { - $installationId = System::getEnv('TESTS_GITHUB_INSTALLATION_ID') ?? ''; - ['items' => $repos, 'total' => $total] = $this->vcsAdapter->searchRepositories($installationId, 'test-kh', 1, 2); + ['items' => $repos, 'total' => $total] = $this->vcsAdapter->searchRepositories('test-kh', 1, 2); $this->assertCount(2, $repos); $this->assertSame(6, $total); }