From f561769b832b2c38bcc7edbae4c8d67479e2a5e8 Mon Sep 17 00:00:00 2001 From: Atharva Deosthale Date: Tue, 16 Dec 2025 19:48:22 +0530 Subject: [PATCH 1/2] add pagination to listBranches and add method listAllBranches --- src/VCS/Adapter.php | 9 +++++++++ src/VCS/Adapter/Git/GitHub.php | 28 ++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/VCS/Adapter.php b/src/VCS/Adapter.php index 1687fe0..948b70f 100644 --- a/src/VCS/Adapter.php +++ b/src/VCS/Adapter.php @@ -195,6 +195,15 @@ abstract public function getRepositoryName(string $repositoryId): string; */ abstract public function listBranches(string $owner, string $repositoryName): array; + /** + * Lists all branches for a given repository by paginating through all pages + * + * @param string $owner Owner name of the repository + * @param string $repositoryName Name of the repository + * @return array List of all branch names as array + */ + abstract public function listAllBranches(string $owner, string $repositoryName): array; + /** * Updates status check of each commit * state can be one of: error, failure, pending, success diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index b0c10b9..047742d 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -464,11 +464,13 @@ public function getPullRequestFromBranch(string $owner, string $repositoryName, * * @param string $owner Owner name of the repository * @param string $repositoryName Name of the GitHub repository + * @param int $page Page number for pagination + * @param int $perPage Number of results per page * @return array List of branch names as array */ - public function listBranches(string $owner, string $repositoryName): array + public function listBranches(string $owner, string $repositoryName, int $page = 1, int $perPage = 30): array { - $url = "/repos/$owner/$repositoryName/branches"; + $url = "/repos/$owner/$repositoryName/branches?page=$page&per_page=$perPage"; $response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"]); @@ -480,6 +482,28 @@ public function listBranches(string $owner, string $repositoryName): array return $names; } + /** + * Lists all branches for a given repository by paginating through all pages + * + * @param string $owner Owner name of the repository + * @param string $repositoryName Name of the GitHub repository + * @return array List of all branch names as array + */ + public function listAllBranches(string $owner, string $repositoryName): array + { + $allBranches = []; + $page = 1; + $perPage = 100; + + do { + $branches = $this->listBranches($owner, $repositoryName, $page, $perPage); + $allBranches = array_merge($allBranches, $branches); + $page++; + } while (count($branches) === $perPage); + + return $allBranches; + } + /** * Get details of a commit using commit hash * From 97b0ad39c77738799056c5f770bb581b732fb0b1 Mon Sep 17 00:00:00 2001 From: Atharva Deosthale Date: Tue, 16 Dec 2025 21:04:54 +0530 Subject: [PATCH 2/2] remove listAllBranches and add tests --- src/VCS/Adapter.php | 9 --------- src/VCS/Adapter/Git/GitHub.php | 22 ---------------------- tests/VCS/Adapter/GitHubTest.php | 18 ++++++++++++++++++ 3 files changed, 18 insertions(+), 31 deletions(-) diff --git a/src/VCS/Adapter.php b/src/VCS/Adapter.php index 948b70f..1687fe0 100644 --- a/src/VCS/Adapter.php +++ b/src/VCS/Adapter.php @@ -195,15 +195,6 @@ abstract public function getRepositoryName(string $repositoryId): string; */ abstract public function listBranches(string $owner, string $repositoryName): array; - /** - * Lists all branches for a given repository by paginating through all pages - * - * @param string $owner Owner name of the repository - * @param string $repositoryName Name of the repository - * @return array List of all branch names as array - */ - abstract public function listAllBranches(string $owner, string $repositoryName): array; - /** * Updates status check of each commit * state can be one of: error, failure, pending, success diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index 047742d..6d868cc 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -482,28 +482,6 @@ public function listBranches(string $owner, string $repositoryName, int $page = return $names; } - /** - * Lists all branches for a given repository by paginating through all pages - * - * @param string $owner Owner name of the repository - * @param string $repositoryName Name of the GitHub repository - * @return array List of all branch names as array - */ - public function listAllBranches(string $owner, string $repositoryName): array - { - $allBranches = []; - $page = 1; - $perPage = 100; - - do { - $branches = $this->listBranches($owner, $repositoryName, $page, $perPage); - $allBranches = array_merge($allBranches, $branches); - $page++; - } while (count($branches) === $perPage); - - return $allBranches; - } - /** * Get details of a commit using commit hash * diff --git a/tests/VCS/Adapter/GitHubTest.php b/tests/VCS/Adapter/GitHubTest.php index 47bc59b..b4c235a 100644 --- a/tests/VCS/Adapter/GitHubTest.php +++ b/tests/VCS/Adapter/GitHubTest.php @@ -391,4 +391,22 @@ public function testGetLatestCommit(): void $this->assertSame('https://avatars.githubusercontent.com/u/43381712?v=4', $commitDetails['commitAuthorAvatar']); $this->assertSame('https://github.com/vermakhushboo', $commitDetails['commitAuthorUrl']); } + + public function testListBranches(): void + { + $owner = 'test-kh'; + $repositoryName = 'test2'; + + // Basic test + $branches = $this->vcsAdapter->listBranches($owner, $repositoryName); + $this->assertIsArray($branches); + $this->assertNotEmpty($branches); + $this->assertContains('main', $branches); + $this->assertContains('test', $branches); + + // Test with pagination params + $branches = $this->vcsAdapter->listBranches($owner, $repositoryName, 1, 1); + $this->assertIsArray($branches); + $this->assertCount(1, $branches); + } }