Skip to content

Commit 03b76ad

Browse files
authored
Merge pull request #79 from utopia-php/ser-401
feat: implement `getPullRequestFiles` method
2 parents 4f5b370 + 0d67c73 commit 03b76ad

6 files changed

Lines changed: 123 additions & 0 deletions

File tree

src/VCS/Adapter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ abstract public function getPullRequestFromBranch(string $owner, string $reposit
150150
*/
151151
abstract public function getPullRequest(string $owner, string $repositoryName, int $pullRequestNumber): array;
152152

153+
/**
154+
* Get files changed in a pull request
155+
*
156+
* @param string $owner Owner name of the repository
157+
* @param string $repositoryName Name of the repository
158+
* @param int $pullRequestNumber The pull request number
159+
* @return array<mixed> List of files changed in the pull request
160+
*/
161+
abstract public function getPullRequestFiles(string $owner, string $repositoryName, int $pullRequestNumber): array;
162+
153163
/**
154164
* Add Comment to Pull Request
155165
*

src/VCS/Adapter/Git/GitHub.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,38 @@ public function getPullRequest(string $owner, string $repositoryName, int $pullR
670670
return $response['body'] ?? [];
671671
}
672672

673+
/**
674+
* Get files changed in a pull request
675+
*
676+
* @return array<mixed> List of files changed in the pull request
677+
*/
678+
public function getPullRequestFiles(string $owner, string $repositoryName, int $pullRequestNumber): array
679+
{
680+
$allFiles = [];
681+
$perPage = 30;
682+
$currentPage = 1;
683+
684+
while (true) {
685+
$url = "/repos/{$owner}/{$repositoryName}/pulls/{$pullRequestNumber}/files";
686+
687+
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"], [
688+
'per_page' => $perPage,
689+
'page' => $currentPage,
690+
]);
691+
692+
$files = $response['body'] ?? [];
693+
$allFiles = array_merge($allFiles, $files);
694+
695+
if (\count($files) < $perPage) {
696+
break;
697+
}
698+
699+
$currentPage++;
700+
}
701+
702+
return $allFiles;
703+
}
704+
673705
/**
674706
* Get latest opened pull request with specific base branch
675707
* @return array<mixed>

src/VCS/Adapter/Git/Gitea.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,39 @@ public function getPullRequest(string $owner, string $repositoryName, int $pullR
651651
return $response['body'] ?? [];
652652
}
653653

654+
/**
655+
* Get files changed in a pull request
656+
*
657+
* @return array<mixed> List of files changed in the pull request
658+
*/
659+
public function getPullRequestFiles(string $owner, string $repositoryName, int $pullRequestNumber): array
660+
{
661+
$allFiles = [];
662+
$limit = 30;
663+
$maxPages = 100;
664+
665+
for ($currentPage = 1; $currentPage <= $maxPages; $currentPage++) {
666+
$url = "/repos/{$owner}/{$repositoryName}/pulls/{$pullRequestNumber}/files?page={$currentPage}&limit={$limit}";
667+
668+
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]);
669+
670+
$responseHeaders = $response['headers'] ?? [];
671+
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
672+
if ($responseHeadersStatusCode >= 400) {
673+
throw new Exception("Failed to get pull request files: HTTP {$responseHeadersStatusCode}");
674+
}
675+
676+
$files = $response['body'] ?? [];
677+
$allFiles = array_merge($allFiles, $files);
678+
679+
if (\count($files) < $limit) {
680+
break;
681+
}
682+
}
683+
684+
return $allFiles;
685+
}
686+
654687
public function getPullRequestFromBranch(string $owner, string $repositoryName, string $branch): array
655688
{
656689

tests/VCS/Adapter/GitHubTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,21 @@ public function testGetPullRequest(): void
355355
$this->assertSame($repositoryName, $result['base']['repo']['name']);
356356
}
357357

358+
public function testGetPullRequestFiles(): void
359+
{
360+
$owner = 'vermakhushboo';
361+
$repositoryName = 'basic-js-crud';
362+
$pullRequestNumber = 1;
363+
364+
$result = $this->vcsAdapter->getPullRequestFiles($owner, $repositoryName, $pullRequestNumber);
365+
366+
$this->assertIsArray($result);
367+
$this->assertNotEmpty($result);
368+
369+
$filenames = array_column($result, 'filename');
370+
$this->assertContains('README.md', $filenames);
371+
}
372+
358373
public function testGenerateCloneCommand(): void
359374
{
360375
\exec('rm -rf /tmp/clone-branch');

tests/VCS/Adapter/GiteaTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,37 @@ public function testGetPullRequest(): void
480480
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
481481
}
482482

483+
public function testGetPullRequestFiles(): void
484+
{
485+
$repositoryName = 'test-get-pull-request-files-' . \uniqid();
486+
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
487+
488+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'README.md', '# Test');
489+
$this->vcsAdapter->createBranch(self::$owner, $repositoryName, 'feature-branch', static::$defaultBranch);
490+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'feature.txt', 'feature content', 'Add feature', 'feature-branch');
491+
492+
$pr = $this->vcsAdapter->createPullRequest(
493+
self::$owner,
494+
$repositoryName,
495+
'Test PR Files',
496+
'feature-branch',
497+
static::$defaultBranch
498+
);
499+
500+
$prNumber = $pr['number'] ?? 0;
501+
$this->assertGreaterThan(0, $prNumber);
502+
503+
$result = $this->vcsAdapter->getPullRequestFiles(self::$owner, $repositoryName, $prNumber);
504+
505+
$this->assertIsArray($result);
506+
$this->assertNotEmpty($result);
507+
508+
$filenames = array_column($result, 'filename');
509+
$this->assertContains('feature.txt', $filenames);
510+
511+
$this->vcsAdapter->deleteRepository(self::$owner, $repositoryName);
512+
}
513+
483514
public function testGetPullRequestWithInvalidNumber(): void
484515
{
485516
$repositoryName = 'test-get-pull-request-invalid-' . \uniqid();

tests/VCS/Base.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ abstract public function testGetComment(): void;
3232

3333
abstract public function testGetPullRequest(): void;
3434

35+
abstract public function testGetPullRequestFiles(): void;
36+
3537
abstract public function testGetRepositoryTree(): void;
3638

3739
/** @return array<mixed> */

0 commit comments

Comments
 (0)