From 21880c70bd13cd61e81a72dadf1f9785c5b00b4c Mon Sep 17 00:00:00 2001 From: Hemachandar Date: Mon, 23 Mar 2026 19:46:39 +0530 Subject: [PATCH 1/3] feat: implement `getRepositoryAccessType` method --- src/VCS/Adapter.php | 12 ++++++++++-- src/VCS/Adapter/Git/GitHub.php | 23 +++++++++++++++++------ src/VCS/Adapter/Git/Gitea.php | 15 +++++++++++++-- tests/VCS/Adapter/GitHubTest.php | 6 ++++++ 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/VCS/Adapter.php b/src/VCS/Adapter.php index 6986178d..3a3fbd9b 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; + /** + * Get repository access type for the installation + * + * @return string 'all' if installation has access to all repositories, 'selected' if it has access to specific repositories + * + * @throws Exception + */ + abstract public function getRepositoryAccessType(): string; + /** * 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..373767d1 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -174,6 +174,21 @@ public function createBranch(string $owner, string $repositoryName, string $newB throw new Exception("Not implemented"); } + /** + * Get repository access type for the installation + * + * @return string 'all' if installation has access to all repositories, 'selected' if it has access to specific repositories + * + * @throws Exception + */ + public function getRepositoryAccessType(): string + { + $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 @@ -185,13 +200,9 @@ 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'; + $hasAccessToAllRepositories = $this->getRepositoryAccessType() === 'all'; // Installation has access to all repositories, use the search API which supports filtering. if ($hasAccessToAllRepositories) { diff --git a/src/VCS/Adapter/Git/Gitea.php b/src/VCS/Adapter/Git/Gitea.php index 1b8f4ba5..f9b004ae 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'] ?? ''; } + /** + * Get repository access type for the installation + * + * @return string 'all' if installation has access to all repositories, 'selected' if it has access to specific repositories + * + * @throws Exception + */ + public function getRepositoryAccessType(): string + { + throw new Exception('Not implemented yet'); + } + /** * 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..e02265c2 100644 --- a/tests/VCS/Adapter/GitHubTest.php +++ b/tests/VCS/Adapter/GitHubTest.php @@ -181,6 +181,12 @@ public function testGetComment(): void $this->assertNotEmpty($result); } + public function testGetRepositoryAccessType(): void + { + $accessType = $this->vcsAdapter->getRepositoryAccessType(); + $this->assertSame('all', $accessType); + } + public function testGetInstallationRepository(): void { $repositoryName = 'astro-starter'; From ccdf317bcecfe76c893a80ec75395e9d87771b87 Mon Sep 17 00:00:00 2001 From: Hemachandar Date: Mon, 23 Mar 2026 19:53:00 +0530 Subject: [PATCH 2/3] feedback --- src/VCS/Adapter/Git/GitHub.php | 3 +-- tests/VCS/Adapter/GiteaTest.php | 14 +++++++------- tests/VCS/Base.php | 3 +-- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index 373767d1..0db24f60 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -186,12 +186,11 @@ public function getRepositoryAccessType(): string $url = '/app/installations/' . $this->installationId; $response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->jwtToken"]); $responseBody = $response['body'] ?? []; - return $responseBody['repository_selection'] ?? 'all'; + return $responseBody['repository_selection'] ?? 'selected'; } /** * 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 diff --git a/tests/VCS/Adapter/GiteaTest.php b/tests/VCS/Adapter/GiteaTest.php index e9be9676..d08f1452 100644 --- a/tests/VCS/Adapter/GiteaTest.php +++ b/tests/VCS/Adapter/GiteaTest.php @@ -878,7 +878,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 +886,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 +911,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 +931,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 +940,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); } From 920e46be0286cb1f842541bf559c9ea6b04e2da8 Mon Sep 17 00:00:00 2001 From: Hemachandar Date: Mon, 23 Mar 2026 20:30:00 +0530 Subject: [PATCH 3/3] feedback 2 --- src/VCS/Adapter.php | 6 +++--- src/VCS/Adapter/Git/GitHub.php | 12 +++++------- src/VCS/Adapter/Git/Gitea.php | 8 ++++---- tests/VCS/Adapter/GitHubTest.php | 5 ++--- tests/VCS/Adapter/GiteaTest.php | 5 +++++ 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/VCS/Adapter.php b/src/VCS/Adapter.php index 3a3fbd9b..2e9ffa77 100644 --- a/src/VCS/Adapter.php +++ b/src/VCS/Adapter.php @@ -90,13 +90,13 @@ abstract public function getUser(string $username): array; abstract public function getOwnerName(string $installationId, ?int $repositoryId = null): string; /** - * Get repository access type for the installation + * Determines whether the installation has access to all repositories or specific repositories * - * @return string 'all' if installation has access to all repositories, 'selected' if it has access to 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 getRepositoryAccessType(): string; + abstract public function hasAccessToAllRepositories(): bool; /** * Search repositories for GitHub App diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index 0db24f60..f2f173b5 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -175,18 +175,18 @@ public function createBranch(string $owner, string $repositoryName, string $newB } /** - * Get repository access type for the installation + * Determines whether the installation has access to all repositories or specific repositories * - * @return string 'all' if installation has access to all repositories, 'selected' if it has access to specific repositories + * @return bool True if installation has access to all repositories, false if it has access to specific repositories * * @throws Exception */ - public function getRepositoryAccessType(): string + 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'] ?? 'selected'; + return ($responseBody['repository_selection'] ?? '') === 'all'; } /** @@ -201,10 +201,8 @@ public function getRepositoryAccessType(): string */ public function searchRepositories(string $owner, int $page, int $per_page, string $search = ''): array { - $hasAccessToAllRepositories = $this->getRepositoryAccessType() === '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 f9b004ae..ef90fc50 100644 --- a/src/VCS/Adapter/Git/Gitea.php +++ b/src/VCS/Adapter/Git/Gitea.php @@ -118,15 +118,15 @@ public function createOrganization(string $orgName): string } /** - * Get repository access type for the installation + * Determines whether the installation has access to all repositories or specific repositories * - * @return string 'all' if installation has access to all repositories, 'selected' if it has access to specific repositories + * @return bool True if installation has access to all repositories, false if it has access to specific repositories * * @throws Exception */ - public function getRepositoryAccessType(): string + public function hasAccessToAllRepositories(): bool { - throw new Exception('Not implemented yet'); + return true; } /** diff --git a/tests/VCS/Adapter/GitHubTest.php b/tests/VCS/Adapter/GitHubTest.php index e02265c2..cb2c54ba 100644 --- a/tests/VCS/Adapter/GitHubTest.php +++ b/tests/VCS/Adapter/GitHubTest.php @@ -181,10 +181,9 @@ public function testGetComment(): void $this->assertNotEmpty($result); } - public function testGetRepositoryAccessType(): void + public function testHasAccessToAllRepositories(): void { - $accessType = $this->vcsAdapter->getRepositoryAccessType(); - $this->assertSame('all', $accessType); + $this->assertTrue($this->vcsAdapter->hasAccessToAllRepositories()); } public function testGetInstallationRepository(): void diff --git a/tests/VCS/Adapter/GiteaTest.php b/tests/VCS/Adapter/GiteaTest.php index d08f1452..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();