Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 36 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++;
}
Comment thread
hmacr marked this conversation as resolved.

return $allFiles;
}

/**
* Get latest opened pull request with specific base branch
* @return array<mixed>
Expand Down Expand Up @@ -957,6 +989,9 @@ public function getEvent(string $event, string $payload): array
$baseLogin = $payloadPullRequestBaseUser['login'] ?? '';
$external = $headLogin !== $baseLogin;

$prFiles = $this->getPullRequestFiles($owner, $repositoryName, (int)$pullRequestNumber);
$affectedFiles = array_column($prFiles, 'filename');
Comment thread
hmacr marked this conversation as resolved.
Outdated

return [
'branch' => $branch,
'branchUrl' => $branchUrl,
Expand All @@ -972,6 +1007,7 @@ public function getEvent(string $event, string $payload): array
'external' => $external,
'pullRequestNumber' => $pullRequestNumber,
'action' => $action,
'affectedFiles' => $affectedFiles,
];
case 'installation':
case 'installation_repositories':
Expand Down
37 changes: 37 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 Expand Up @@ -1004,6 +1037,9 @@ public function getEvent(string $event, string $payload): array
$baseRepoFullName = $payloadRepository['full_name'] ?? '';
$external = !empty($headRepoFullName) && !empty($baseRepoFullName) && $headRepoFullName !== $baseRepoFullName;

$prFiles = $this->getPullRequestFiles($owner, $repositoryName, (int)$pullRequestNumber);
$affectedFiles = array_column($prFiles, 'filename');
Comment thread
hmacr marked this conversation as resolved.
Outdated

return [
'branch' => $branch,
'branchUrl' => $branchUrl,
Expand All @@ -1019,6 +1055,7 @@ public function getEvent(string $event, string $payload): array
'external' => $external,
'pullRequestNumber' => $pullRequestNumber,
'action' => $action,
'affectedFiles' => $affectedFiles,
];
}

Expand Down
23 changes: 20 additions & 3 deletions tests/VCS/Adapter/GitHubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function testGetEventPullRequest(): void
"pull_request": {
"id": 1303283688,
"state": "open",
"html_url": "https://github.com/vermakhushboo/g4-node-function/pull/17",
"html_url": "https://github.com/vermakhushboo/basic-js-crud/pull/1",
"head": {
"ref": "test",
"sha": "a27dbe54b17032ee35a16c24bac151e5c2b33328",
Expand All @@ -131,11 +131,11 @@ public function testGetEventPullRequest(): void
},
"repository": {
"id": 3498,
"name": "functions-example",
"name": "basic-js-crud",
"owner": {
"login": "vermakhushboo"
},
"html_url": "https://github.com/vermakhushboo/g4-node-function"
"html_url": "https://github.com/vermakhushboo/basic-js-crud"
},
"installation": {
"id": 9876
Expand All @@ -149,6 +149,8 @@ public function testGetEventPullRequest(): void

$this->assertSame('opened', $result['action']);
$this->assertSame(1, $result['pullRequestNumber']);
$this->assertCount(1, $result['affectedFiles']);
$this->assertContains('README.md', $result['affectedFiles']);
}

public function testGetEventInstallation(): void
Expand Down Expand Up @@ -355,6 +357,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
156 changes: 91 additions & 65 deletions tests/VCS/Adapter/GiteaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,37 @@ public function testGetPullRequest(): void
$this->vcsAdapter->deleteRepository(self::$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', 'main');
$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',
'main'
);

$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 Expand Up @@ -891,114 +922,109 @@ public function testGetEventPush(): void

public function testGetEventPullRequest(): void
{
$repositoryName = 'test-get-event-pull-request-' . \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', 'main');
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'feature.txt', 'feature content', 'Add feature', 'feature-branch');

$pr = $this->vcsAdapter->createPullRequest(self::$owner, $repositoryName, 'Test PR', 'feature-branch', 'main');
$prNumber = $pr['number'] ?? 0;
$commitHash = $pr['head']['sha'] ?? 'abc123';

$fullName = self::$owner . '/' . $repositoryName;
$giteaUrl = System::getEnv('TESTS_GITEA_URL', 'http://gitea:3000') ?? '';

$payload = json_encode([
'action' => 'opened',
'number' => 42,
'number' => $prNumber,
'pull_request' => [
'id' => 1,
'number' => 42,
'state' => 'open',
'title' => 'Test PR',
'head' => [
'ref' => 'feature-branch',
'sha' => 'abc123',
'repo' => [
'full_name' => 'test-owner/test-repo',
],
'user' => [
'login' => 'pr-author',
],
'sha' => $commitHash,
'repo' => ['full_name' => $fullName],
'user' => ['login' => self::$owner],
],
'base' => [
'ref' => 'main',
'sha' => 'def456',
'user' => [
'login' => 'base-owner',
],
],
'user' => [
'login' => 'pr-author',
'avatar_url' => 'http://gitea:3000/avatars/pr-author',
'user' => ['login' => self::$owner],
],
'user' => ['login' => self::$owner, 'avatar_url' => "{$giteaUrl}/avatars/" . self::$owner],
],
'repository' => [
'id' => 123,
'name' => 'test-repo',
'full_name' => 'test-owner/test-repo',
'html_url' => 'http://gitea:3000/test-owner/test-repo',
'owner' => [
'login' => 'test-owner',
],
],
'sender' => [
'login' => 'sender-user',
'html_url' => 'http://gitea:3000/sender-user',
'name' => $repositoryName,
'full_name' => $fullName,
'html_url' => "{$giteaUrl}/" . self::$owner . "/{$repositoryName}",
'owner' => ['login' => self::$owner],
],
'sender' => ['html_url' => "{$giteaUrl}/" . self::$owner],
]);

if ($payload === false) {
$this->fail('Failed to encode JSON payload');
}

$result = $this->vcsAdapter->getEvent('pull_request', $payload);

$this->assertIsArray($result);
$this->assertArrayHasKey('branch', $result);
$this->assertArrayHasKey('pullRequestNumber', $result);
$this->assertArrayHasKey('action', $result);
$this->assertArrayHasKey('commitHash', $result);
$this->assertArrayHasKey('external', $result);

$this->assertSame('feature-branch', $result['branch']);
$this->assertSame(42, $result['pullRequestNumber']);
$this->assertSame($prNumber, $result['pullRequestNumber']);
$this->assertSame('opened', $result['action']);
$this->assertSame('abc123', $result['commitHash']);
$this->assertSame('test-repo', $result['repositoryName']);
$this->assertSame('test-owner', $result['owner']);
$this->assertSame($repositoryName, $result['repositoryName']);
$this->assertSame(self::$owner, $result['owner']);
$this->assertFalse($result['external']);
$this->assertCount(1, $result['affectedFiles']);
$this->assertContains('feature.txt', $result['affectedFiles']);

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

public function testGetEventPullRequestExternal(): void
{
$repositoryName = 'test-get-event-pr-external-' . \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', 'main');
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'feature.txt', 'feature content', 'Add feature', 'feature-branch');

$pr = $this->vcsAdapter->createPullRequest(self::$owner, $repositoryName, 'External PR', 'feature-branch', 'main');
$prNumber = $pr['number'] ?? 0;
$commitHash = $pr['head']['sha'] ?? 'abc123';

$giteaUrl = System::getEnv('TESTS_GITEA_URL', 'http://gitea:3000') ?? '';

$payload = json_encode([
'action' => 'opened',
'number' => 42,
'number' => $prNumber,
'pull_request' => [
'head' => [
'ref' => 'feature-branch',
'sha' => 'abc123',
'repo' => [
'full_name' => 'external-user/forked-repo',
],
'sha' => $commitHash,
'repo' => ['full_name' => 'external-user/' . $repositoryName],
'user' => ['login' => 'external-user'],
],
'base' => [
'ref' => 'main',
'user' => ['login' => self::$owner],
],
'user' => [
'avatar_url' => 'http://gitea:3000/avatars/external',
],
'user' => ['avatar_url' => "{$giteaUrl}/avatars/external-user"],
],
'repository' => [
'id' => 123,
'name' => 'test-repo',
'full_name' => 'test-owner/test-repo',
'html_url' => 'http://gitea:3000/test-owner/test-repo',
'owner' => [
'login' => 'test-owner',
],
],
'sender' => [
'html_url' => 'http://gitea:3000/external-user',
'name' => $repositoryName,
'full_name' => self::$owner . '/' . $repositoryName,
'html_url' => "{$giteaUrl}/" . self::$owner . "/{$repositoryName}",
'owner' => ['login' => self::$owner],
],
'sender' => ['html_url' => "{$giteaUrl}/external-user"],
]);

if ($payload === false) {
$this->fail('Failed to encode JSON payload');
}

$result = $this->vcsAdapter->getEvent('pull_request', $payload);

$this->assertTrue($result['external']);
$this->assertCount(1, $result['affectedFiles']);
$this->assertContains('feature.txt', $result['affectedFiles']);

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

public function testValidateWebhookEvent(): void
Expand Down
Loading
Loading