Skip to content
Merged
10 changes: 10 additions & 0 deletions src/VCS/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ abstract public function getPullRequestFromBranch(string $owner, string $reposit
*/
abstract public function getPullRequest(string $owner, string $repositoryName, int $pullRequestNumber): array;

/**
* Get files changed in a pull request
*
* @param string $owner Owner name of the repository
* @param string $repositoryName Name of the repository
* @param int $pullRequestNumber The pull request number
* @return array<mixed> List of files changed in the pull request
*/
abstract public function getPullRequestFiles(string $owner, string $repositoryName, int $pullRequestNumber): array;

/**
* Add Comment to Pull Request
*
Expand Down
32 changes: 32 additions & 0 deletions src/VCS/Adapter/Git/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,38 @@ public function getPullRequest(string $owner, string $repositoryName, int $pullR
return $response['body'] ?? [];
}

/**
* Get files changed in a pull request
*
* @return array<mixed> List of files changed in the pull request
*/
public function getPullRequestFiles(string $owner, string $repositoryName, int $pullRequestNumber): array
{
$allFiles = [];
$perPage = 30;
$currentPage = 1;

while (true) {
$url = "/repos/{$owner}/{$repositoryName}/pulls/{$pullRequestNumber}/files";

$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"], [
'per_page' => $perPage,
'page' => $currentPage,
]);

$files = $response['body'] ?? [];
$allFiles = array_merge($allFiles, $files);

if (\count($files) < $perPage) {
break;
}

$currentPage++;
}

return $allFiles;
}

/**
* Get latest opened pull request with specific base branch
* @return array<mixed>
Expand Down
34 changes: 34 additions & 0 deletions src/VCS/Adapter/Git/Gitea.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public function createRepository(string $owner, string $repositoryName, bool $pr
]);

return $response['body'] ?? [];
// return is_array($body) ? $body : [];
}

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

/**
* Get files changed in a pull request
*
* @return array<mixed> List of files changed in the pull request
*/
public function getPullRequestFiles(string $owner, string $repositoryName, int $pullRequestNumber): array
{
$allFiles = [];
$limit = 30;
$maxPages = 100;

for ($currentPage = 1; $currentPage <= $maxPages; $currentPage++) {
$url = "/repos/{$owner}/{$repositoryName}/pulls/{$pullRequestNumber}/files?page={$currentPage}&limit={$limit}";

$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]);

$responseHeaders = $response['headers'] ?? [];
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
if ($responseHeadersStatusCode >= 400) {
throw new Exception("Failed to get pull request files: HTTP {$responseHeadersStatusCode}");
}

$files = $response['body'] ?? [];
$allFiles = array_merge($allFiles, $files);

if (\count($files) < $limit) {
break;
}
}

return $allFiles;
}

public function getPullRequestFromBranch(string $owner, string $repositoryName, string $branch): array
{

Expand Down
15 changes: 15 additions & 0 deletions tests/VCS/Adapter/GitHubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,21 @@ public function testGetPullRequest(): void
$this->assertSame($repositoryName, $result['base']['repo']['name']);
}

public function testGetPullRequestFiles(): void
{
$owner = 'vermakhushboo';
$repositoryName = 'basic-js-crud';
$pullRequestNumber = 1;

$result = $this->vcsAdapter->getPullRequestFiles($owner, $repositoryName, $pullRequestNumber);

$this->assertIsArray($result);
$this->assertNotEmpty($result);

$filenames = array_column($result, 'filename');
$this->assertContains('README.md', $filenames);
}

public function testGenerateCloneCommand(): void
{
\exec('rm -rf /tmp/clone-branch');
Expand Down
31 changes: 31 additions & 0 deletions tests/VCS/Adapter/GiteaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,37 @@ public function testGetPullRequest(): void
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
}

public function testGetPullRequestFiles(): void
{
$repositoryName = 'test-get-pull-request-files-' . \uniqid();
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);

$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'feature-branch', static::$defaultBranch);
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'feature.txt', 'feature content', 'Add feature', 'feature-branch');

$pr = $this->vcsAdapter->createPullRequest(
static::$owner,
$repositoryName,
'Test PR Files',
'feature-branch',
static::$defaultBranch
);

$prNumber = $pr['number'] ?? 0;
$this->assertGreaterThan(0, $prNumber);

$result = $this->vcsAdapter->getPullRequestFiles(static::$owner, $repositoryName, $prNumber);

$this->assertIsArray($result);
$this->assertNotEmpty($result);

$filenames = array_column($result, 'filename');
$this->assertContains('feature.txt', $filenames);

$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
}

public function testGetPullRequestWithInvalidNumber(): void
{
$repositoryName = 'test-get-pull-request-invalid-' . \uniqid();
Expand Down
5 changes: 5 additions & 0 deletions tests/VCS/Adapter/GogsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,9 @@ public function testListRepositoryLanguagesEmptyRepo(): void
{
$this->markTestSkipped('Gogs does not support repository languages endpoint');
}

public function testGetPullRequestFiles(): void
{
$this->markTestSkipped('Gogs does not support pull request files API');
}
}
2 changes: 2 additions & 0 deletions tests/VCS/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ abstract public function testGetComment(): void;

abstract public function testGetPullRequest(): void;

abstract public function testGetPullRequestFiles(): void;

abstract public function testGetRepositoryTree(): void;

/** @return array<mixed> */
Expand Down
Loading