Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 66cec45

Browse files
committedFeb 13, 2020
GitHub: allow processing > 29 branches
Implement pagination when retrieving data from GitHub REST API, to get over the limit of 30 items returned by default [[1]]. Fixes #327 [1]: https://developer.github.com/v3/#pagination
1 parent b559ffd commit 66cec45

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed
 

‎SourceGithub/SourceGithub.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -446,19 +446,46 @@ private function api_init( $p_repo ) {
446446
/**
447447
* Retrieves data from the GitHub API for the given repository.
448448
*
449-
* The JSON data is returned as an stdClass object,
449+
* The JSON data is returned as an stdClass object.
450450
*
451451
* @param SourceRepo $p_repo Repository
452452
* @param string $p_path GitHub API path
453453
* @param string $p_member Optional top-level member to retrieve
454454
*
455-
* @return stdClass|false
455+
* @return stdClass|stdClass[]|false
456456
*/
457457
private function api_get( $p_repo, $p_path, $p_member = '' ) {
458458
$this->api_init( $p_repo );
459+
$t_json = array();
460+
461+
# Add pagination parameter, setting page count to maximum authorized by
462+
# GitHub to minimize the number of requests
463+
$t_path = $p_path
464+
. ( parse_url( $p_path, PHP_URL_QUERY ) ? '&' : '?' )
465+
. 'per_page=100';
466+
do {
467+
$t_response = $this->githubApi->get( $t_path );
468+
$t_data = json_decode( $t_response->getBody() );
469+
470+
# No need for pagination if returned data is a single object
471+
if( !is_array( $t_data ) ) {
472+
$t_json = $t_data;
473+
break;
474+
}
475+
# Store retrieved data and proceed with next page
476+
$t_json = array_merge( $t_json, $t_data );
477+
478+
$t_links = GuzzleHttp\Psr7\parse_header( $t_response->getHeader( 'Link' ) );
479+
foreach( $t_links as $t_link ) {
480+
if( $t_link['rel'] == 'next' ) {
481+
$t_path = trim( $t_link[0], '<>' );
482+
continue 2;
483+
}
484+
}
459485

460-
$t_response = $this->githubApi->get( $p_path );
461-
$t_json = json_decode( (string) $t_response->getBody() );
486+
# There is no "next" link - all pages have been processed
487+
break;
488+
} while( true );
462489

463490
if( empty( $p_member ) ) {
464491
return $t_json;
@@ -585,7 +612,7 @@ public function import_full( $p_repo ) {
585612
$t_username = $p_repo->info['hub_username'];
586613
$t_reponame = $p_repo->info['hub_reponame'];
587614

588-
$t_json = $this->api_json_url( $p_repo, "repos/$t_username/$t_reponame/branches" );
615+
$t_json = $this->api_json_url( $p_repo, "repos/$t_username/$t_reponame/branches?per_page=" );
589616

590617
$t_branches = array();
591618
foreach ($t_json as $t_branch) {

0 commit comments

Comments
 (0)
Please sign in to comment.