@@ -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 );
@@ -521,16 +531,9 @@ public function getUser(string $username): array
521531 throw new Exception ("Not implemented yet " );
522532 }
523533
524- /**
525- * Get owner name
526- * @param string $installationId In Gitea context, this is the owner name itself
527- * @return string Owner name
528- */
529534 public function getOwnerName (string $ installationId ): string
530535 {
531- // Gitea doesn't have GitHub App installation concept
532- // Return the installationId as-is since it represents the owner
533- return $ installationId ;
536+ throw new Exception ("getOwnerName() is not applicable for Gitea " );
534537 }
535538
536539 public function getPullRequest (string $ owner , string $ repositoryName , int $ pullRequestNumber ): array
@@ -575,30 +578,49 @@ public function getPullRequestFromBranch(string $owner, string $repositoryName,
575578 */
576579 public function listBranches (string $ owner , string $ repositoryName ): array
577580 {
578- $ url = "/repos/ {$ owner }/ {$ repositoryName }/branches " ;
581+ $ allBranches = [];
582+ $ perPage = 50 ;
583+ $ maxPages = 100 ;
579584
580- $ response = $ this ->call (self ::METHOD_GET , $ url , ['Authorization ' => "token $ this ->accessToken " ]);
585+ for ($ currentPage = 1 ; $ currentPage <= $ maxPages ; $ currentPage ++) {
586+ $ url = "/repos/ {$ owner }/ {$ repositoryName }/branches?page= {$ currentPage }&limit= {$ perPage }" ;
581587
582- $ responseHeaders = $ response ['headers ' ] ?? [];
583- $ responseHeadersStatusCode = $ responseHeaders ['status-code ' ] ?? 0 ;
584- if ($ responseHeadersStatusCode >= 400 ) {
585- return [];
586- }
588+ $ response = $ this ->call (self ::METHOD_GET , $ url , ['Authorization ' => "token $ this ->accessToken " ]);
587589
588- $ responseBody = $ response ['body ' ] ?? [];
590+ $ responseHeaders = $ response ['headers ' ] ?? [];
591+ $ responseHeadersStatusCode = $ responseHeaders ['status-code ' ] ?? 0 ;
589592
590- if (!is_array ($ responseBody )) {
591- return [];
592- }
593+ if ($ responseHeadersStatusCode === 404 ) {
594+ return [];
595+ }
596+
597+ if ($ responseHeadersStatusCode >= 400 ) {
598+ if ($ currentPage === 1 ) {
599+ throw new Exception ("Failed to list branches: HTTP {$ responseHeadersStatusCode }" );
600+ }
601+ break ;
602+ }
593603
594- $ names = [];
595- foreach ($ responseBody as $ branch ) {
596- if (is_array ($ branch ) && array_key_exists ('name ' , $ branch )) {
597- $ names [] = $ branch ['name ' ] ?? '' ;
604+ $ responseBody = $ response ['body ' ] ?? [];
605+
606+ if (!is_array ($ responseBody )) {
607+ break ;
608+ }
609+
610+ $ pageCount = 0 ;
611+ foreach ($ responseBody as $ branch ) {
612+ if (is_array ($ branch ) && array_key_exists ('name ' , $ branch )) {
613+ $ allBranches [] = $ branch ['name ' ] ?? '' ;
614+ $ pageCount ++;
615+ }
616+ }
617+
618+ if ($ pageCount < $ perPage ) {
619+ break ;
598620 }
599621 }
600622
601- return $ names ;
623+ return $ allBranches ;
602624 }
603625
604626 /**
0 commit comments