Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/VCS/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
26 changes: 17 additions & 9 deletions src/VCS/Adapter/Git/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"], [
Expand Down
15 changes: 13 additions & 2 deletions src/VCS/Adapter/Git/Gitea.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed> 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;
Expand Down
5 changes: 5 additions & 0 deletions tests/VCS/Adapter/GitHubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
19 changes: 12 additions & 7 deletions tests/VCS/Adapter/GiteaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -878,15 +883,15 @@ 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);
$this->assertArrayHasKey('total', $result);
$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']);
Expand All @@ -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']);

Expand All @@ -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']);
Expand All @@ -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']);
Expand Down
3 changes: 1 addition & 2 deletions tests/VCS/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading