Skip to content

Commit fa3fa76

Browse files
authored
Merge pull request #75 from utopia-php/ser-1170
chore: extract `getRepositoryAccessType` method
2 parents 7b835da + 920e46b commit fa3fa76

6 files changed

Lines changed: 58 additions & 22 deletions

File tree

src/VCS/Adapter.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,17 @@ abstract public function getUser(string $username): array;
8989
*/
9090
abstract public function getOwnerName(string $installationId, ?int $repositoryId = null): string;
9191

92+
/**
93+
* Determines whether the installation has access to all repositories or specific repositories
94+
*
95+
* @return bool True if installation has access to all repositories, false if it has access to specific repositories
96+
*
97+
* @throws Exception
98+
*/
99+
abstract public function hasAccessToAllRepositories(): bool;
100+
92101
/**
93102
* Search repositories for GitHub App
94-
* @param string $installationId ID of the installation
95103
* @param string $owner Name of user or org
96104
* @param int $page page number
97105
* @param int $per_page number of results per page
@@ -100,7 +108,7 @@ abstract public function getOwnerName(string $installationId, ?int $repositoryId
100108
*
101109
* @throws Exception
102110
*/
103-
abstract public function searchRepositories(string $installationId, string $owner, int $page, int $per_page, string $search = ''): array;
111+
abstract public function searchRepositories(string $owner, int $page, int $per_page, string $search = ''): array;
104112

105113
/**
106114
* Get repository for the installation

src/VCS/Adapter/Git/GitHub.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,23 @@ public function createBranch(string $owner, string $repositoryName, string $newB
174174
throw new Exception("Not implemented");
175175
}
176176

177+
/**
178+
* Determines whether the installation has access to all repositories or specific repositories
179+
*
180+
* @return bool True if installation has access to all repositories, false if it has access to specific repositories
181+
*
182+
* @throws Exception
183+
*/
184+
public function hasAccessToAllRepositories(): bool
185+
{
186+
$url = '/app/installations/' . $this->installationId;
187+
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->jwtToken"]);
188+
$responseBody = $response['body'] ?? [];
189+
return ($responseBody['repository_selection'] ?? '') === 'all';
190+
}
191+
177192
/**
178193
* Search repositories for GitHub App
179-
* @param string $installationId ID of the installation
180194
* @param string $owner Name of user or org
181195
* @param int $page page number
182196
* @param int $per_page number of results per page
@@ -185,16 +199,10 @@ public function createBranch(string $owner, string $repositoryName, string $newB
185199
*
186200
* @throws Exception
187201
*/
188-
public function searchRepositories(string $installationId, string $owner, int $page, int $per_page, string $search = ''): array
202+
public function searchRepositories(string $owner, int $page, int $per_page, string $search = ''): array
189203
{
190-
// Find whether installation has access to all (or) specific repositories
191-
$url = '/app/installations/' . $installationId;
192-
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->jwtToken"]);
193-
$responseBody = $response['body'] ?? [];
194-
$hasAccessToAllRepositories = ($responseBody['repository_selection'] ?? '') === 'all';
195-
196204
// Installation has access to all repositories, use the search API which supports filtering.
197-
if ($hasAccessToAllRepositories) {
205+
if ($this->hasAccessToAllRepositories()) {
198206
$url = '/search/repositories';
199207

200208
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"], [

src/VCS/Adapter/Git/Gitea.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,28 @@ public function createOrganization(string $orgName): string
117117
return $responseBody['name'] ?? '';
118118
}
119119

120+
/**
121+
* Determines whether the installation has access to all repositories or specific repositories
122+
*
123+
* @return bool True if installation has access to all repositories, false if it has access to specific repositories
124+
*
125+
* @throws Exception
126+
*/
127+
public function hasAccessToAllRepositories(): bool
128+
{
129+
return true;
130+
}
131+
120132
/**
121133
* Search repositories in organization
122134
*
123-
* @param string $installationId Not used in Gitea (kept for interface compatibility)
124135
* @param string $owner Organization or user name
125136
* @param int $page Page number for pagination
126137
* @param int $per_page Number of results per page
127138
* @param string $search Search query to filter repository names
128139
* @return array<mixed> Array with 'items' (repositories) and 'total' count
129140
*/
130-
public function searchRepositories(string $installationId, string $owner, int $page, int $per_page, string $search = ''): array
141+
public function searchRepositories(string $owner, int $page, int $per_page, string $search = ''): array
131142
{
132143
$filteredRepos = [];
133144
$currentPage = 1;

tests/VCS/Adapter/GitHubTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ public function testGetComment(): void
181181
$this->assertNotEmpty($result);
182182
}
183183

184+
public function testHasAccessToAllRepositories(): void
185+
{
186+
$this->assertTrue($this->vcsAdapter->hasAccessToAllRepositories());
187+
}
188+
184189
public function testGetInstallationRepository(): void
185190
{
186191
$repositoryName = 'astro-starter';

tests/VCS/Adapter/GiteaTest.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ public function testGetCommentInvalidId(): void
201201
$this->vcsAdapter->deleteRepository(self::$owner, $repositoryName);
202202
}
203203

204+
public function testHasAccessToAllRepositories(): void
205+
{
206+
$this->assertTrue($this->vcsAdapter->hasAccessToAllRepositories());
207+
}
208+
204209
public function testGetRepositoryTreeWithSlashInBranchName(): void
205210
{
206211
$repositoryName = 'test-branch-with-slash-' . \uniqid();
@@ -878,15 +883,15 @@ public function testSearchRepositories(): void
878883

879884
try {
880885
// Search without filter - should return all
881-
$result = $this->vcsAdapter->searchRepositories('', self::$owner, 1, 10);
886+
$result = $this->vcsAdapter->searchRepositories(self::$owner, 1, 10);
882887

883888
$this->assertIsArray($result);
884889
$this->assertArrayHasKey('items', $result);
885890
$this->assertArrayHasKey('total', $result);
886891
$this->assertGreaterThanOrEqual(3, $result['total']);
887892

888893
// Search with filter
889-
$result = $this->vcsAdapter->searchRepositories('', self::$owner, 1, 10, 'test-search');
894+
$result = $this->vcsAdapter->searchRepositories(self::$owner, 1, 10, 'test-search');
890895

891896
$this->assertIsArray($result);
892897
$this->assertGreaterThanOrEqual(2, $result['total']);
@@ -911,15 +916,15 @@ public function testSearchRepositoriesPagination(): void
911916
$this->vcsAdapter->createRepository(self::$owner, $repo2, false);
912917

913918
try {
914-
$result = $this->vcsAdapter->searchRepositories('', self::$owner, 1, 1, 'test-pagination');
919+
$result = $this->vcsAdapter->searchRepositories(self::$owner, 1, 1, 'test-pagination');
915920

916921
$this->assertSame(1, count($result['items']));
917922
$this->assertGreaterThanOrEqual(2, $result['total']);
918923

919-
$result2 = $this->vcsAdapter->searchRepositories('', self::$owner, 2, 1, 'test-pagination');
924+
$result2 = $this->vcsAdapter->searchRepositories(self::$owner, 2, 1, 'test-pagination');
920925
$this->assertSame(1, count($result2['items']));
921926

922-
$result20 = $this->vcsAdapter->searchRepositories('', self::$owner, 20, 1, 'test-pagination');
927+
$result20 = $this->vcsAdapter->searchRepositories(self::$owner, 20, 1, 'test-pagination');
923928
$this->assertIsArray($result20);
924929
$this->assertEmpty($result20['items']);
925930

@@ -931,7 +936,7 @@ public function testSearchRepositoriesPagination(): void
931936

932937
public function testSearchRepositoriesNoResults(): void
933938
{
934-
$result = $this->vcsAdapter->searchRepositories('', self::$owner, 1, 10, 'nonexistent-repo-xyz-' . \uniqid());
939+
$result = $this->vcsAdapter->searchRepositories(self::$owner, 1, 10, 'nonexistent-repo-xyz-' . \uniqid());
935940

936941
$this->assertIsArray($result);
937942
$this->assertEmpty($result['items']);
@@ -940,7 +945,7 @@ public function testSearchRepositoriesNoResults(): void
940945

941946
public function testSearchRepositoriesInvalidOwner(): void
942947
{
943-
$result = $this->vcsAdapter->searchRepositories('', 'nonexistent-owner-' . \uniqid(), 1, 10);
948+
$result = $this->vcsAdapter->searchRepositories('nonexistent-owner-' . \uniqid(), 1, 10);
944949

945950
$this->assertIsArray($result);
946951
$this->assertEmpty($result['items']);

tests/VCS/Base.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ public function testGetOwnerName(): void
103103

104104
public function testSearchRepositories(): void
105105
{
106-
$installationId = System::getEnv('TESTS_GITHUB_INSTALLATION_ID') ?? '';
107-
['items' => $repos, 'total' => $total] = $this->vcsAdapter->searchRepositories($installationId, 'test-kh', 1, 2);
106+
['items' => $repos, 'total' => $total] = $this->vcsAdapter->searchRepositories('test-kh', 1, 2);
108107
$this->assertCount(2, $repos);
109108
$this->assertSame(6, $total);
110109
}

0 commit comments

Comments
 (0)