Skip to content

Commit 78507b1

Browse files
committed
Fix GitHub branch pagination
1 parent 03ccd12 commit 78507b1

2 files changed

Lines changed: 73 additions & 6 deletions

File tree

src/VCS/Adapter/Git/GitHub.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -738,15 +738,28 @@ public function getPullRequestFromBranch(string $owner, string $repositoryName,
738738
public function listBranches(string $owner, string $repositoryName): array
739739
{
740740
$url = "/repos/$owner/$repositoryName/branches";
741+
$perPage = 100;
742+
$currentPage = 1;
743+
$names = [];
741744

742-
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"]);
745+
do {
746+
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"], [
747+
'page' => $currentPage,
748+
'per_page' => $perPage,
749+
]);
743750

744-
$responseBody = $response['body'] ?? [];
751+
$responseBody = $response['body'] ?? [];
745752

746-
$names = [];
747-
foreach ($responseBody as $subarray) {
748-
$names[] = $subarray['name'] ?? '';
749-
}
753+
if (!is_array($responseBody)) {
754+
break;
755+
}
756+
757+
foreach ($responseBody as $subarray) {
758+
$names[] = $subarray['name'] ?? '';
759+
}
760+
761+
$currentPage++;
762+
} while (count($responseBody) === $perPage);
750763

751764
return $names;
752765
}

tests/VCS/Adapter/GitHubTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,60 @@ public function testGetCommit(): void
467467
$this->assertSame('7ae65094d56edafc48596ffbb77950e741e56412', $commitDetails['commitHash']);
468468
}
469469

470+
public function testListBranchesFetchesAllPages(): void
471+
{
472+
$firstPage = [];
473+
for ($i = 1; $i <= 100; $i++) {
474+
$firstPage[] = ['name' => "branch-{$i}"];
475+
}
476+
477+
$secondPage = [];
478+
for ($i = 101; $i <= 135; $i++) {
479+
$secondPage[] = ['name' => "branch-{$i}"];
480+
}
481+
482+
$adapter = new class (new Cache(new None()), [$firstPage, $secondPage]) extends GitHub {
483+
/**
484+
* @var array<array<mixed>>
485+
*/
486+
public array $requests = [];
487+
488+
/**
489+
* @param array<array<mixed>> $responses
490+
*/
491+
public function __construct(Cache $cache, private array $responses)
492+
{
493+
parent::__construct($cache);
494+
$this->accessToken = 'test-token';
495+
}
496+
497+
protected function call(string $method, string $path = '', array $headers = [], array $params = [], bool $decode = true)
498+
{
499+
$this->requests[] = [
500+
'method' => $method,
501+
'path' => $path,
502+
'headers' => $headers,
503+
'params' => $params,
504+
];
505+
506+
return [
507+
'headers' => ['status-code' => 200],
508+
'body' => array_shift($this->responses) ?? [],
509+
];
510+
}
511+
};
512+
513+
$branches = $adapter->listBranches('appwrite', 'appwrite');
514+
515+
$this->assertCount(135, $branches);
516+
$this->assertSame('branch-1', $branches[0]);
517+
$this->assertSame('branch-135', $branches[134]);
518+
$this->assertCount(2, $adapter->requests);
519+
$this->assertSame('/repos/appwrite/appwrite/branches', $adapter->requests[0]['path']);
520+
$this->assertSame(['page' => 1, 'per_page' => 100], $adapter->requests[0]['params']);
521+
$this->assertSame(['page' => 2, 'per_page' => 100], $adapter->requests[1]['params']);
522+
}
523+
470524
public function testGetLatestCommit(): void
471525
{
472526
$commitDetails = $this->vcsAdapter->getLatestCommit('test-kh', 'test1', 'test');

0 commit comments

Comments
 (0)