Skip to content

Commit 2850dbe

Browse files
authored
Merge pull request #98 from utopia-php/fix/github-branches-pagination
Fix: GitHub branch pagination
2 parents c14ec4d + 0fdd494 commit 2850dbe

2 files changed

Lines changed: 70 additions & 7 deletions

File tree

src/VCS/Adapter/Git/GitHub.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,15 @@ public function createFile(string $owner, string $repositoryName, string $filepa
181181
*/
182182
public function createBranch(string $owner, string $repositoryName, string $newBranchName, string $oldBranchName): array
183183
{
184-
throw new Exception("Not implemented");
184+
$latestCommit = $this->getLatestCommit($owner, $repositoryName, $oldBranchName);
185+
$sha = $latestCommit['commitHash'];
186+
187+
$response = $this->call(self::METHOD_POST, "/repos/$owner/$repositoryName/git/refs", ['Authorization' => "Bearer $this->accessToken"], [
188+
'ref' => "refs/heads/$newBranchName",
189+
'sha' => $sha,
190+
]);
191+
192+
return $response['body'] ?? [];
185193
}
186194

187195
/**
@@ -738,22 +746,28 @@ public function getPullRequestFromBranch(string $owner, string $repositoryName,
738746
*
739747
* @param string $owner Owner name of the repository
740748
* @param string $repositoryName Name of the GitHub repository
749+
* @param int $perPage Number of branches to fetch per page
750+
* @param int $page Page number to start fetching from
741751
* @return array<string> List of branch names as array
742752
*/
743-
public function listBranches(string $owner, string $repositoryName): array
753+
public function listBranches(string $owner, string $repositoryName, int $perPage = 100, int $page = 1): array
744754
{
745755
$url = "/repos/$owner/$repositoryName/branches";
756+
$perPage = min(max($perPage, 1), 100);
746757

747-
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"]);
758+
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"], [
759+
'page' => $page,
760+
'per_page' => $perPage,
761+
]);
748762

763+
$statusCode = $response['headers']['status-code'] ?? 0;
749764
$responseBody = $response['body'] ?? [];
750765

751-
$names = [];
752-
foreach ($responseBody as $subarray) {
753-
$names[] = $subarray['name'] ?? '';
766+
if ($statusCode < 200 || $statusCode >= 300 || !is_array($responseBody)) {
767+
return [];
754768
}
755769

756-
return $names;
770+
return array_values(array_map(fn ($branch) => $branch['name'] ?? '', $responseBody));
757771
}
758772

759773
/**

tests/VCS/Adapter/GitHubTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,55 @@ public function testGetCommitWithInvalidHash(): void
525525
}
526526
}
527527

528+
public function testListBranchesPagination(): void
529+
{
530+
$repositoryName = 'test-list-branches-pages-' . \uniqid();
531+
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
532+
533+
try {
534+
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
535+
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'branch-a', static::$defaultBranch);
536+
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'branch-b', static::$defaultBranch);
537+
538+
/** @var GitHub $adapter */
539+
$adapter = $this->vcsAdapter;
540+
541+
$page1 = $adapter->listBranches(static::$owner, $repositoryName, 1, 1);
542+
$this->assertSame(['branch-a'], $page1);
543+
544+
$page2 = $adapter->listBranches(static::$owner, $repositoryName, 1, 2);
545+
$this->assertSame(['branch-b'], $page2);
546+
547+
$all = $adapter->listBranches(static::$owner, $repositoryName, 100, 1);
548+
$this->assertEqualsCanonicalizing([static::$defaultBranch, 'branch-a', 'branch-b'], $all);
549+
} finally {
550+
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
551+
}
552+
}
553+
554+
public function testListBranchesEmptyRepository(): void
555+
{
556+
$repositoryName = 'test-list-branches-empty-' . \uniqid();
557+
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
558+
559+
try {
560+
$branches = $this->vcsAdapter->listBranches(static::$owner, $repositoryName);
561+
562+
$this->assertIsArray($branches);
563+
$this->assertEmpty($branches);
564+
} finally {
565+
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
566+
}
567+
}
568+
569+
public function testListBranchesNonExistingRepository(): void
570+
{
571+
$branches = $this->vcsAdapter->listBranches(static::$owner, 'non-existing-repo-' . \uniqid());
572+
573+
$this->assertIsArray($branches);
574+
$this->assertEmpty($branches);
575+
}
576+
528577
public function testGetLatestCommit(): void
529578
{
530579
$repositoryName = 'test-get-latest-commit-' . \uniqid();

0 commit comments

Comments
 (0)