Skip to content

Commit 6f3a606

Browse files
committed
Address GitHub branch pagination review
1 parent 78507b1 commit 6f3a606

2 files changed

Lines changed: 14 additions & 54 deletions

File tree

src/VCS/Adapter/Git/GitHub.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -733,18 +733,19 @@ public function getPullRequestFromBranch(string $owner, string $repositoryName,
733733
*
734734
* @param string $owner Owner name of the repository
735735
* @param string $repositoryName Name of the GitHub repository
736+
* @param int $perPage Number of branches to fetch per page
736737
* @return array<string> List of branch names as array
737738
*/
738-
public function listBranches(string $owner, string $repositoryName): array
739+
public function listBranches(string $owner, string $repositoryName, int $perPage = 100): array
739740
{
740741
$url = "/repos/$owner/$repositoryName/branches";
741-
$perPage = 100;
742-
$currentPage = 1;
742+
$perPage = min(max($perPage, 1), 100);
743+
$page = 1;
743744
$names = [];
744745

745746
do {
746747
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"], [
747-
'page' => $currentPage,
748+
'page' => $page,
748749
'per_page' => $perPage,
749750
]);
750751

@@ -758,7 +759,7 @@ public function listBranches(string $owner, string $repositoryName): array
758759
$names[] = $subarray['name'] ?? '';
759760
}
760761

761-
$currentPage++;
762+
$page++;
762763
} while (count($responseBody) === $perPage);
763764

764765
return $names;

tests/VCS/Adapter/GitHubTest.php

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

470-
public function testListBranchesFetchesAllPages(): void
470+
public function testListBranchesFetchesMultiplePages(): void
471471
{
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-
};
472+
$this->assertInstanceOf(GitHub::class, $this->vcsAdapter);
512473

513-
$branches = $adapter->listBranches('appwrite', 'appwrite');
474+
/** @var GitHub $adapter */
475+
$adapter = $this->vcsAdapter;
476+
$branches = $adapter->listBranches('test-kh', 'test1', 1);
514477

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']);
478+
$this->assertIsArray($branches);
479+
$this->assertContains('main', $branches);
480+
$this->assertContains('test', $branches);
522481
}
523482

524483
public function testGetLatestCommit(): void

0 commit comments

Comments
 (0)