Skip to content

Commit a413e42

Browse files
committed
Implement createBranch, add page param to listBranches, rewrite pagination test
- Implement createBranch using GitHub refs API - Add page parameter to listBranches for consistency with other methods - Rewrite testListBranchesFetchesMultiplePages to use a real created repository with actual branches instead of a hardcoded external repo
1 parent 67038d7 commit a413e42

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

src/VCS/Adapter/Git/GitHub.php

Lines changed: 11 additions & 3 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
/**
@@ -739,13 +747,13 @@ public function getPullRequestFromBranch(string $owner, string $repositoryName,
739747
* @param string $owner Owner name of the repository
740748
* @param string $repositoryName Name of the GitHub repository
741749
* @param int $perPage Number of branches to fetch per page
750+
* @param int $page Page number to start fetching from
742751
* @return array<string> List of branch names as array
743752
*/
744-
public function listBranches(string $owner, string $repositoryName, int $perPage = 100): array
753+
public function listBranches(string $owner, string $repositoryName, int $perPage = 100, int $page = 1): array
745754
{
746755
$url = "/repos/$owner/$repositoryName/branches";
747756
$perPage = min(max($perPage, 1), 100);
748-
$page = 1;
749757
$names = [];
750758

751759
do {

tests/VCS/Adapter/GitHubTest.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -527,15 +527,25 @@ public function testGetCommitWithInvalidHash(): void
527527

528528
public function testListBranchesFetchesMultiplePages(): void
529529
{
530-
$this->assertInstanceOf(GitHub::class, $this->vcsAdapter);
530+
$repositoryName = 'test-list-branches-pages-' . \uniqid();
531+
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
531532

532-
/** @var GitHub $adapter */
533-
$adapter = $this->vcsAdapter;
534-
$branches = $adapter->listBranches('test-kh', 'test1', 1);
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);
535537

536-
$this->assertIsArray($branches);
537-
$this->assertContains('main', $branches);
538-
$this->assertContains('test', $branches);
538+
/** @var GitHub $adapter */
539+
$adapter = $this->vcsAdapter;
540+
$branches = $adapter->listBranches(static::$owner, $repositoryName, 1);
541+
542+
$this->assertIsArray($branches);
543+
$this->assertContains(static::$defaultBranch, $branches);
544+
$this->assertContains('branch-a', $branches);
545+
$this->assertContains('branch-b', $branches);
546+
} finally {
547+
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
548+
}
539549
}
540550

541551
public function testGetLatestCommit(): void

0 commit comments

Comments
 (0)