Skip to content
Merged
Changes from 6 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
58 changes: 47 additions & 11 deletions src/VCS/Adapter/Git/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,58 @@ public function createRepository(string $owner, string $repositoryName, bool $pr
*/
public function searchRepositories(string $owner, int $page, int $per_page, string $search = ''): array
{
Comment thread
hmacr marked this conversation as resolved.
Outdated
$url = '/search/repositories';
$url = '/installation/repositories';
$repositories = [];

// When no search query is provided, delegate pagination to the GitHub API.
if (empty($search)) {
$repositories = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"], [
'page' => $page,
'per_page' => $per_page,
]);

if (!isset($repositories['body']['repositories'])) {
throw new Exception("Repositories list missing in the response.");
}

return [
'items' => $repositories['body']['repositories'],
'total' => $repositories['body']['total_count'],
];
Comment thread
hmacr marked this conversation as resolved.
}

$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"], [
'q' => "{$search} user:{$owner} fork:true",
'page' => $page,
'per_page' => $per_page,
'sort' => 'updated'
]);
// When search query is provided, fetch all repositories accessible by the installation and filter them locally.
$currentPage = 1;
while (true) {
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"], [
'page' => $currentPage,
'per_page' => 100, // Maximum allowed by GitHub API
]);

if (!isset($response['body']['repositories'])) {
throw new Exception("Repositories list missing in the response.");
}
Comment thread
hmacr marked this conversation as resolved.

// Filter repositories to only include those that match the search query.
$filteredRepositories = array_filter($response['body']['repositories'], fn ($repo) => stripos($repo['name'], $search) !== false);

if (!isset($response['body']['items'])) {
throw new Exception("Repositories list missing in the response.");
// Merge with result so far.
$repositories = array_merge($repositories, $filteredRepositories);

// If less than 100 repositories are returned, we have fetched all repositories.
if (\count($response['body']['repositories']) < 100) {
break;
}
Comment thread
hmacr marked this conversation as resolved.

// Increment page number to fetch next page.
$currentPage++;
}
Comment thread
hmacr marked this conversation as resolved.

$repositoriesInRequestedPage = \array_slice($repositories, ($page - 1) * $per_page, $per_page);

return [
'items' => $response['body']['items'],
'total' => $response['body']['total_count'],
'items' => $repositoriesInRequestedPage,
'total' => \count($repositories),
];
Comment thread
hmacr marked this conversation as resolved.
}

Expand Down