Skip to content
Closed
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
30 changes: 16 additions & 14 deletions src/VCS/Adapter/Git/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,25 +256,27 @@ public function searchRepositories(string $owner, int $page, int $per_page, stri
$responseHeaders = $response['headers'] ?? [];
$statusCode = $responseHeaders['status-code'] ?? 0;

// The search API rejects a query naming an owner that does not exist,
// which is a missing owner rather than a failure worth reporting.
if ($statusCode === 422) {
return ['items' => [], 'total' => 0];
}
if ($statusCode !== 422) {
if ($statusCode >= 400) {
throw new Exception("Failed to search repositories: HTTP {$statusCode}", $statusCode);
}

if ($statusCode >= 400) {
throw new Exception("Failed to search repositories: HTTP {$statusCode}", $statusCode);
}
$responseBody = $response['body'] ?? [];

$responseBody = $response['body'] ?? [];
return [
'items' => $responseBody['items'] ?? [],
'total' => $responseBody['total_count'] ?? 0,
];
}

return [
'items' => $responseBody['items'] ?? [],
'total' => $responseBody['total_count'] ?? 0,
];
// Private profiles cannot be searched, but their installation can
// still list repositories. Keep unknown owners returning no results.
if (strcasecmp($owner, $this->getOwnerName($this->installationId)) !== 0) {
return ['items' => [], 'total' => 0];
}
}

// Installation has access to specific repositories, we need to perform client-side filtering.
// Restricted installations and unsearchable owners use the installation API.
$url = '/installation/repositories';
$repositories = [];

Expand Down
28 changes: 28 additions & 0 deletions tests/Fixtures/GitHubRepositories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Utopia\Tests\Fixtures;

use Utopia\Cache\Adapter\None;
use Utopia\Cache\Cache;
use Utopia\VCS\Adapter\Git\GitHub;

final class GitHubRepositories extends GitHub
{
public array $requests = [];

public function __construct(private array $responses)
{
parent::__construct(new Cache(new None()));
$this->installationId = '1234';
$this->jwtToken = 'app-token';
$this->accessToken = 'installation-token';
}

protected function call(string $method, string $path = '', array $headers = [], array $params = [], bool $decode = true, bool $followRedirects = true): array
{
$this->requests[] = ['path' => $path, 'params' => $params];
return array_shift($this->responses) ?? throw new \RuntimeException('Unexpected provider request');
}
}
103 changes: 103 additions & 0 deletions tests/Unit/GitHubRepositoriesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare(strict_types=1);

namespace Utopia\Tests\Unit;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Utopia\Tests\Fixtures\GitHubRepositories;

final class GitHubRepositoriesTest extends TestCase
{
public function testSearchPrivateProfile(): void
{
$repositories = [['id' => 2, 'name' => 'private-repository']];
$adapter = new GitHubRepositories([
['body' => ['repository_selection' => 'all']],
['headers' => ['status-code' => 422]],
['body' => ['account' => ['login' => 'Private-Owner']]],
['body' => ['repositories' => $repositories, 'total_count' => 2]],
]);

$result = $adapter->searchRepositories('private-owner', 2, 1);

$this->assertSame(['items' => $repositories, 'total' => 2], $result);
$this->assertSame('/installation/repositories', $adapter->requests[3]['path']);
$this->assertSame(['page' => 2, 'per_page' => 1], $adapter->requests[3]['params']);
}

public function testSearchPrivateProfileFiltersBeforePagination(): void
{
$firstPage = array_map(fn (int $id) => ['id' => $id, 'name' => 'unrelated-' . $id], range(1, 100));
$firstPage[99]['name'] = 'match-first';
$match = ['id' => 101, 'name' => 'MATCH-second'];
$adapter = new GitHubRepositories([
['body' => ['repository_selection' => 'all']],
['headers' => ['status-code' => 422]],
['body' => ['account' => ['login' => 'private-owner']]],
['body' => ['repositories' => $firstPage]],
['body' => ['repositories' => [$match]]],
]);

$result = $adapter->searchRepositories('private-owner', 2, 1, 'match');

$this->assertSame(['items' => [$match], 'total' => 2], $result);
$this->assertSame(['page' => 1, 'per_page' => 100], $adapter->requests[3]['params']);
$this->assertSame(['page' => 2, 'per_page' => 100], $adapter->requests[4]['params']);
}

public function testSearchUnknownOwner(): void
{
$adapter = new GitHubRepositories([
['body' => ['repository_selection' => 'all']],
['headers' => ['status-code' => 422]],
['body' => ['account' => ['login' => 'private-owner']]],
]);

$this->assertSame(['items' => [], 'total' => 0], $adapter->searchRepositories('unknown-owner', 1, 10));
}

public function testSearchUsesSuccessfulProviderResponse(): void
{
$repositories = [['id' => 1, 'name' => 'repository']];
$adapter = new GitHubRepositories([
['body' => ['repository_selection' => 'all']],
['headers' => ['status-code' => 200], 'body' => ['items' => $repositories, 'total_count' => 1]],
]);

$this->assertSame(['items' => $repositories, 'total' => 1], $adapter->searchRepositories('owner', 1, 10));
}

#[DataProvider('providerErrors')]
public function testSearchProviderErrors(int $status): void
{
$adapter = new GitHubRepositories([
['body' => ['repository_selection' => 'all']],
['headers' => ['status-code' => $status]],
]);

$this->expectException(\Exception::class);
$this->expectExceptionCode($status);
$adapter->searchRepositories('owner', 1, 10);
}

public static function providerErrors(): array
{
return [[403], [429], [500]];
}

public function testPrivateProfileListingError(): void
{
$adapter = new GitHubRepositories([
['body' => ['repository_selection' => 'all']],
['headers' => ['status-code' => 422]],
['body' => ['account' => ['login' => 'private-owner']]],
['headers' => ['status-code' => 403]],
]);

$this->expectException(\Exception::class);
$this->expectExceptionCode(403);
$adapter->searchRepositories('private-owner', 1, 10);
}
}