@@ -129,10 +129,14 @@ public function createOrganization(string $orgName): string
129129 */
130130 public function searchRepositories (string $ installationId , string $ owner , int $ page , int $ per_page , string $ search = '' ): array
131131 {
132- $ allRepos = [];
132+ $ filteredRepos = [];
133133 $ currentPage = 1 ;
134+ $ maxPages = 50 ;
134135
135- while (true ) {
136+ $ neededForPage = $ page * $ per_page ;
137+ $ maxToCollect = $ neededForPage + $ per_page ;
138+
139+ while ($ currentPage <= $ maxPages ) {
136140 $ queryParams = [
137141 'page ' => $ currentPage ,
138142 'limit ' => 100 ,
@@ -155,8 +159,12 @@ public function searchRepositories(string $installationId, string $owner, int $p
155159
156160 $ responseBody = $ response ['body ' ] ?? [];
157161
162+ if (!is_array ($ responseBody )) {
163+ throw new Exception ('Unexpected response body: ' . json_encode ($ responseBody ));
164+ }
165+
158166 if (!array_key_exists ('data ' , $ responseBody )) {
159- throw new Exception ("Repositories list missing in the response. " );
167+ throw new Exception ("Repositories list missing in response: " . json_encode ( $ responseBody ) );
160168 }
161169
162170 $ repos = $ responseBody ['data ' ];
@@ -165,7 +173,16 @@ public function searchRepositories(string $installationId, string $owner, int $p
165173 break ;
166174 }
167175
168- $ allRepos = array_merge ($ allRepos , $ repos );
176+ foreach ($ repos as $ repo ) {
177+ $ repoOwner = $ repo ['owner ' ]['login ' ] ?? '' ;
178+ if ($ repoOwner === $ owner ) {
179+ $ filteredRepos [] = $ repo ;
180+
181+ if (count ($ filteredRepos ) >= $ maxToCollect ) {
182+ break 2 ;
183+ }
184+ }
185+ }
169186
170187 if (count ($ repos ) < 100 ) {
171188 break ;
@@ -174,13 +191,6 @@ public function searchRepositories(string $installationId, string $owner, int $p
174191 $ currentPage ++;
175192 }
176193
177- $ filteredRepos = array_filter ($ allRepos , function ($ repo ) use ($ owner ) {
178- $ repoOwner = $ repo ['owner ' ]['login ' ] ?? '' ;
179- return $ repoOwner === $ owner ;
180- });
181-
182- $ filteredRepos = array_values ($ filteredRepos );
183-
184194 $ total = count ($ filteredRepos );
185195 $ offset = ($ page - 1 ) * $ per_page ;
186196 $ pagedRepos = array_slice ($ filteredRepos , $ offset , $ per_page );
@@ -433,16 +443,9 @@ public function getUser(string $username): array
433443 throw new Exception ("Not implemented yet " );
434444 }
435445
436- /**
437- * Get owner name
438- * @param string $installationId In Gitea context, this is the owner name itself
439- * @return string Owner name
440- */
441446 public function getOwnerName (string $ installationId ): string
442447 {
443- // Gitea doesn't have GitHub App installation concept
444- // Return the installationId as-is since it represents the owner
445- return $ installationId ;
448+ throw new Exception ("getOwnerName() is not applicable for Gitea " );
446449 }
447450
448451 public function getPullRequest (string $ owner , string $ repositoryName , int $ pullRequestNumber ): array
@@ -464,30 +467,49 @@ public function getPullRequestFromBranch(string $owner, string $repositoryName,
464467 */
465468 public function listBranches (string $ owner , string $ repositoryName ): array
466469 {
467- $ url = "/repos/ {$ owner }/ {$ repositoryName }/branches " ;
470+ $ allBranches = [];
471+ $ perPage = 50 ;
472+ $ maxPages = 100 ;
468473
469- $ response = $ this ->call (self ::METHOD_GET , $ url , ['Authorization ' => "token $ this ->accessToken " ]);
474+ for ($ currentPage = 1 ; $ currentPage <= $ maxPages ; $ currentPage ++) {
475+ $ url = "/repos/ {$ owner }/ {$ repositoryName }/branches?page= {$ currentPage }&limit= {$ perPage }" ;
470476
471- $ responseHeaders = $ response ['headers ' ] ?? [];
472- $ responseHeadersStatusCode = $ responseHeaders ['status-code ' ] ?? 0 ;
473- if ($ responseHeadersStatusCode >= 400 ) {
474- return [];
475- }
477+ $ response = $ this ->call (self ::METHOD_GET , $ url , ['Authorization ' => "token $ this ->accessToken " ]);
476478
477- $ responseBody = $ response ['body ' ] ?? [];
479+ $ responseHeaders = $ response ['headers ' ] ?? [];
480+ $ responseHeadersStatusCode = $ responseHeaders ['status-code ' ] ?? 0 ;
478481
479- if (!is_array ($ responseBody )) {
480- return [];
481- }
482+ if ($ responseHeadersStatusCode === 404 ) {
483+ return [];
484+ }
485+
486+ if ($ responseHeadersStatusCode >= 400 ) {
487+ if ($ currentPage === 1 ) {
488+ throw new Exception ("Failed to list branches: HTTP {$ responseHeadersStatusCode }" );
489+ }
490+ break ;
491+ }
482492
483- $ names = [];
484- foreach ($ responseBody as $ branch ) {
485- if (is_array ($ branch ) && array_key_exists ('name ' , $ branch )) {
486- $ names [] = $ branch ['name ' ] ?? '' ;
493+ $ responseBody = $ response ['body ' ] ?? [];
494+
495+ if (!is_array ($ responseBody )) {
496+ break ;
497+ }
498+
499+ $ pageCount = 0 ;
500+ foreach ($ responseBody as $ branch ) {
501+ if (is_array ($ branch ) && array_key_exists ('name ' , $ branch )) {
502+ $ allBranches [] = $ branch ['name ' ] ?? '' ;
503+ $ pageCount ++;
504+ }
505+ }
506+
507+ if ($ pageCount < $ perPage ) {
508+ break ;
487509 }
488510 }
489511
490- return $ names ;
512+ return $ allBranches ;
491513 }
492514
493515 /**
0 commit comments