-
Notifications
You must be signed in to change notification settings - Fork 3
Use installation/repositories API for searching repos
#53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -106,22 +106,40 @@ public function createRepository(string $owner, string $repositoryName, bool $pr | |
| */ | ||
| public function searchRepositories(string $owner, int $page, int $per_page, string $search = ''): array | ||
| { | ||
| $url = '/search/repositories'; | ||
| $repositories = []; | ||
|
|
||
| $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' | ||
| ]); | ||
| $currentPage = 1; | ||
| while (true) { | ||
| $url = '/installation/repositories'; | ||
| $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."); | ||
| } | ||
|
|
||
| // Filter repositories to only include those that match the search query. | ||
| $filteredRepositories = array_filter($response['body']['repositories'], fn ($repo) => empty($search) || stripos($repo['name'], $search) !== false); | ||
|
|
||
| // Merge with result so far. | ||
| $repositories = array_merge($repositories, $filteredRepositories); | ||
|
|
||
| if (!isset($response['body']['items'])) { | ||
| throw new Exception("Repositories list missing in the response."); | ||
| // If less than 100 repositories are returned, we have fetched all repositories. | ||
| if (\count($response['body']['repositories']) < 100) { | ||
| break; | ||
| } | ||
|
|
||
| // Increment page number to fetch next page. | ||
| $currentPage++; | ||
| } | ||
|
Comment on lines
+109
to
136
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Performance regression: fetches all pages even without a search term. When When no search filtering is needed, delegate pagination directly to the API: Proposed fix public function searchRepositories(string $owner, int $page, int $per_page, string $search = ''): array
{
+ $url = '/installation/repositories';
+
+ // When no search term, delegate pagination directly to the API
+ if (empty($search)) {
+ $response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"], [
+ 'page' => $page,
+ 'per_page' => $per_page,
+ ]);
+
+ if (!isset($response['body']['repositories'])) {
+ throw new Exception("Repositories list missing in the response.");
+ }
+
+ return [
+ 'items' => $response['body']['repositories'],
+ 'total' => $response['body']['total_count'] ?? \count($response['body']['repositories']),
+ ];
+ }
+
+ // When searching, fetch all pages and filter locally
$repositories = [];
-
$currentPage = 1;
while (true) {
- $url = '/installation/repositories';
$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.");
}
- // Filter repositories to only include those that match the search query.
- $filteredRepositories = array_filter($response['body']['repositories'], fn ($repo) => empty($search) || stripos($repo['name'], $search) !== false);
+ $filteredRepositories = array_filter($response['body']['repositories'], fn ($repo) => stripos($repo['name'], $search) !== false);
- // 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;
}
- // Increment page number to fetch next page.
$currentPage++;
}🤖 Prompt for AI Agents |
||
|
|
||
| $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), | ||
| ]; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.