Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
33 changes: 33 additions & 0 deletions src/VCS/Adapter/Git/Gitea.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,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 @@ -480,6 +480,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(self::$owner, $repositoryName, false);

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

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

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

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

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

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

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

public function testGetPullRequestWithInvalidNumber(): void
{
$repositoryName = 'test-get-pull-request-invalid-' . \uniqid();
Expand Down
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