Skip to content

Commit 90e6a4f

Browse files
authored
Merge pull request #83 from jaysomani/fix/gogs-final
fix: resolve testGetPullRequestFiles failures for Forgejo and Gogs
2 parents 89b654e + 8bec208 commit 90e6a4f

7 files changed

Lines changed: 129 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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public function createRepository(string $owner, string $repositoryName, bool $pr
101101
]);
102102

103103
return $response['body'] ?? [];
104+
// return is_array($body) ? $body : [];
104105
}
105106

106107
public function createOrganization(string $orgName): string
@@ -656,6 +657,39 @@ public function getPullRequest(string $owner, string $repositoryName, int $pullR
656657
return $response['body'] ?? [];
657658
}
658659

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

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
@@ -484,6 +484,37 @@ public function testGetPullRequest(): void
484484
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
485485
}
486486

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

tests/VCS/Adapter/GogsTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,9 @@ public function testListRepositoryLanguagesEmptyRepo(): void
125125
{
126126
$this->markTestSkipped('Gogs does not support repository languages endpoint');
127127
}
128+
129+
public function testGetPullRequestFiles(): void
130+
{
131+
$this->markTestSkipped('Gogs does not support pull request files API');
132+
}
128133
}

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)