From 00db87749a352d45a601462499741dde69899953 Mon Sep 17 00:00:00 2001 From: Hemachandar Date: Mon, 23 Mar 2026 22:22:27 +0530 Subject: [PATCH 1/5] feat: add `affectedFiles` to parsed pull_request event --- src/VCS/Adapter.php | 10 ++ src/VCS/Adapter/Git/GitHub.php | 36 +++++++ src/VCS/Adapter/Git/Gitea.php | 37 ++++++++ tests/VCS/Adapter/GitHubTest.php | 23 ++++- tests/VCS/Adapter/GiteaTest.php | 156 ++++++++++++++++++------------- tests/VCS/Base.php | 2 + 6 files changed, 196 insertions(+), 68 deletions(-) diff --git a/src/VCS/Adapter.php b/src/VCS/Adapter.php index 2e9ffa77..823fbe89 100644 --- a/src/VCS/Adapter.php +++ b/src/VCS/Adapter.php @@ -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 List of files changed in the pull request + */ + abstract public function getPullRequestFiles(string $owner, string $repositoryName, int $pullRequestNumber): array; + /** * Add Comment to Pull Request * diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index 84e7e177..2d94671a 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -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 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 @@ -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'); + return [ 'branch' => $branch, 'branchUrl' => $branchUrl, @@ -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': diff --git a/src/VCS/Adapter/Git/Gitea.php b/src/VCS/Adapter/Git/Gitea.php index d6ed8e13..5b5d1224 100644 --- a/src/VCS/Adapter/Git/Gitea.php +++ b/src/VCS/Adapter/Git/Gitea.php @@ -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 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 { @@ -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'); + return [ 'branch' => $branch, 'branchUrl' => $branchUrl, @@ -1019,6 +1055,7 @@ public function getEvent(string $event, string $payload): array 'external' => $external, 'pullRequestNumber' => $pullRequestNumber, 'action' => $action, + 'affectedFiles' => $affectedFiles, ]; } diff --git a/tests/VCS/Adapter/GitHubTest.php b/tests/VCS/Adapter/GitHubTest.php index cb2c54ba..8bc54a30 100644 --- a/tests/VCS/Adapter/GitHubTest.php +++ b/tests/VCS/Adapter/GitHubTest.php @@ -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", @@ -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 @@ -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 @@ -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'); diff --git a/tests/VCS/Adapter/GiteaTest.php b/tests/VCS/Adapter/GiteaTest.php index 4697de47..a48fa0df 100644 --- a/tests/VCS/Adapter/GiteaTest.php +++ b/tests/VCS/Adapter/GiteaTest.php @@ -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(); @@ -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 diff --git a/tests/VCS/Base.php b/tests/VCS/Base.php index 3ad42889..bf8adbed 100644 --- a/tests/VCS/Base.php +++ b/tests/VCS/Base.php @@ -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 */ From 0bddebb070fed50bbe4244c2f6e43bfa85485a8e Mon Sep 17 00:00:00 2001 From: Hemachandar Date: Mon, 23 Mar 2026 22:34:24 +0530 Subject: [PATCH 2/5] greptile feedback --- src/VCS/Adapter/Git/GitHub.php | 9 +++++++-- src/VCS/Adapter/Git/Gitea.php | 8 ++++++-- tests/VCS/Adapter/GiteaTest.php | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index 2d94671a..9d4ca1e3 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -989,8 +989,13 @@ 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'); + try { + $prFiles = $this->getPullRequestFiles($owner, $repositoryName, (int)$pullRequestNumber); + $affectedFiles = array_column($prFiles, 'filename'); + } catch (Exception $e) { + // In case of any error while fetching pull request files, we will return an empty list of affected files. + $affectedFiles = []; + } return [ 'branch' => $branch, diff --git a/src/VCS/Adapter/Git/Gitea.php b/src/VCS/Adapter/Git/Gitea.php index 5b5d1224..932f4313 100644 --- a/src/VCS/Adapter/Git/Gitea.php +++ b/src/VCS/Adapter/Git/Gitea.php @@ -1037,8 +1037,12 @@ 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'); + try { + $prFiles = $this->getPullRequestFiles($owner, $repositoryName, (int)$pullRequestNumber); + $affectedFiles = array_column($prFiles, 'filename'); + } catch (Exception $e) { + $affectedFiles = []; + } return [ 'branch' => $branch, diff --git a/tests/VCS/Adapter/GiteaTest.php b/tests/VCS/Adapter/GiteaTest.php index a48fa0df..101b8a64 100644 --- a/tests/VCS/Adapter/GiteaTest.php +++ b/tests/VCS/Adapter/GiteaTest.php @@ -962,9 +962,19 @@ public function testGetEventPullRequest(): void '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($prNumber, $result['pullRequestNumber']); $this->assertSame('opened', $result['action']); @@ -1018,6 +1028,10 @@ public function testGetEventPullRequestExternal(): void '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']); From 0d67c732ffd8e175446dd28ae77ee2137de89936 Mon Sep 17 00:00:00 2001 From: Hemachandar Date: Tue, 24 Mar 2026 13:16:27 +0530 Subject: [PATCH 3/5] remove affectedFiles from PR event --- src/VCS/Adapter/Git/GitHub.php | 9 --- src/VCS/Adapter/Git/Gitea.php | 8 --- tests/VCS/Adapter/GitHubTest.php | 8 +-- tests/VCS/Adapter/GiteaTest.php | 111 ++++++++++++++----------------- 4 files changed, 54 insertions(+), 82 deletions(-) diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index 5a9ae667..b2ae68e0 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -989,14 +989,6 @@ public function getEvent(string $event, string $payload): array $baseLogin = $payloadPullRequestBaseUser['login'] ?? ''; $external = $headLogin !== $baseLogin; - try { - $prFiles = $this->getPullRequestFiles($owner, $repositoryName, (int)$pullRequestNumber); - $affectedFiles = array_column($prFiles, 'filename'); - } catch (Exception $e) { - // In case of any error while fetching pull request files, we will return an empty list of affected files. - $affectedFiles = []; - } - return [ 'branch' => $branch, 'branchUrl' => $branchUrl, @@ -1012,7 +1004,6 @@ public function getEvent(string $event, string $payload): array 'external' => $external, 'pullRequestNumber' => $pullRequestNumber, 'action' => $action, - 'affectedFiles' => $affectedFiles, ]; case 'installation': case 'installation_repositories': diff --git a/src/VCS/Adapter/Git/Gitea.php b/src/VCS/Adapter/Git/Gitea.php index 3322799c..2886581b 100644 --- a/src/VCS/Adapter/Git/Gitea.php +++ b/src/VCS/Adapter/Git/Gitea.php @@ -1037,13 +1037,6 @@ public function getEvent(string $event, string $payload): array $baseRepoFullName = $payloadRepository['full_name'] ?? ''; $external = !empty($headRepoFullName) && !empty($baseRepoFullName) && $headRepoFullName !== $baseRepoFullName; - try { - $prFiles = $this->getPullRequestFiles($owner, $repositoryName, (int)$pullRequestNumber); - $affectedFiles = array_column($prFiles, 'filename'); - } catch (Exception $e) { - $affectedFiles = []; - } - return [ 'branch' => $branch, 'branchUrl' => $branchUrl, @@ -1059,7 +1052,6 @@ public function getEvent(string $event, string $payload): array 'external' => $external, 'pullRequestNumber' => $pullRequestNumber, 'action' => $action, - 'affectedFiles' => $affectedFiles, ]; } diff --git a/tests/VCS/Adapter/GitHubTest.php b/tests/VCS/Adapter/GitHubTest.php index 8bc54a30..5d48ae79 100644 --- a/tests/VCS/Adapter/GitHubTest.php +++ b/tests/VCS/Adapter/GitHubTest.php @@ -109,7 +109,7 @@ public function testGetEventPullRequest(): void "pull_request": { "id": 1303283688, "state": "open", - "html_url": "https://github.com/vermakhushboo/basic-js-crud/pull/1", + "html_url": "https://github.com/vermakhushboo/g4-node-function/pull/17", "head": { "ref": "test", "sha": "a27dbe54b17032ee35a16c24bac151e5c2b33328", @@ -131,11 +131,11 @@ public function testGetEventPullRequest(): void }, "repository": { "id": 3498, - "name": "basic-js-crud", + "name": "functions-example", "owner": { "login": "vermakhushboo" }, - "html_url": "https://github.com/vermakhushboo/basic-js-crud" + "html_url": "https://github.com/vermakhushboo/g4-node-function" }, "installation": { "id": 9876 @@ -149,8 +149,6 @@ 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 diff --git a/tests/VCS/Adapter/GiteaTest.php b/tests/VCS/Adapter/GiteaTest.php index aeb0b8b5..c10afe4a 100644 --- a/tests/VCS/Adapter/GiteaTest.php +++ b/tests/VCS/Adapter/GiteaTest.php @@ -923,44 +923,49 @@ 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', 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', 'feature-branch', static::$defaultBranch); - $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' => $prNumber, + 'number' => 42, 'pull_request' => [ + 'id' => 1, + 'number' => 42, + 'state' => 'open', + 'title' => 'Test PR', 'head' => [ 'ref' => 'feature-branch', - 'sha' => $commitHash, - 'repo' => ['full_name' => $fullName], - 'user' => ['login' => self::$owner], + 'sha' => 'abc123', + 'repo' => [ + 'full_name' => 'test-owner/test-repo', + ], + 'user' => [ + 'login' => 'pr-author', + ], ], 'base' => [ 'ref' => static::$defaultBranch, - 'user' => ['login' => self::$owner], + 'sha' => 'def456', + 'user' => [ + 'login' => 'base-owner', + ], + ], + 'user' => [ + 'login' => 'pr-author', + 'avatar_url' => 'http://gitea:3000/avatars/pr-author', ], - 'user' => ['login' => self::$owner, 'avatar_url' => "{$giteaUrl}/avatars/" . self::$owner], ], 'repository' => [ 'id' => 123, - 'name' => $repositoryName, - 'full_name' => $fullName, - 'html_url' => "{$giteaUrl}/" . self::$owner . "/{$repositoryName}", - 'owner' => ['login' => self::$owner], + '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', ], - 'sender' => ['html_url' => "{$giteaUrl}/" . self::$owner], ]); if ($payload === false) { @@ -977,56 +982,46 @@ public function testGetEventPullRequest(): void $this->assertArrayHasKey('external', $result); $this->assertSame('feature-branch', $result['branch']); - $this->assertSame($prNumber, $result['pullRequestNumber']); + $this->assertSame(42, $result['pullRequestNumber']); $this->assertSame('opened', $result['action']); - $this->assertSame($repositoryName, $result['repositoryName']); - $this->assertSame(self::$owner, $result['owner']); + $this->assertSame('abc123', $result['commitHash']); + $this->assertSame('test-repo', $result['repositoryName']); + $this->assertSame('test-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', static::$defaultBranch); - $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', static::$defaultBranch); - $prNumber = $pr['number'] ?? 0; - $commitHash = $pr['head']['sha'] ?? 'abc123'; - - $giteaUrl = System::getEnv('TESTS_GITEA_URL', 'http://gitea:3000'); - $payload = json_encode([ 'action' => 'opened', - 'number' => $prNumber, + 'number' => 42, 'pull_request' => [ 'head' => [ 'ref' => 'feature-branch', - 'sha' => $commitHash, - 'repo' => ['full_name' => 'external-user/' . $repositoryName], - 'user' => ['login' => 'external-user'], + 'sha' => 'abc123', + 'repo' => [ + 'full_name' => 'external-user/forked-repo', + ], ], 'base' => [ 'ref' => static::$defaultBranch, - 'user' => ['login' => self::$owner], ], - 'user' => ['avatar_url' => "{$giteaUrl}/avatars/external-user"], + 'user' => [ + 'avatar_url' => 'http://gitea:3000/avatars/external', + ], ], 'repository' => [ 'id' => 123, - 'name' => $repositoryName, - 'full_name' => self::$owner . '/' . $repositoryName, - 'html_url' => "{$giteaUrl}/" . self::$owner . "/{$repositoryName}", - 'owner' => ['login' => self::$owner], + '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', ], - 'sender' => ['html_url' => "{$giteaUrl}/external-user"], ]); if ($payload === false) { @@ -1036,10 +1031,6 @@ public function testGetEventPullRequestExternal(): void $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 From f09164da8824a92ed8fa37e96aa7ac457b14c9b1 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 2 Apr 2026 20:35:58 +0530 Subject: [PATCH 4/5] fix: resolve testGetPullRequestFiles failures for Forgejo and Gogs --- src/VCS/Adapter/Git/Gitea.php | 1 + tests/VCS/Adapter/GiteaTest.php | 14 +++++++------- tests/VCS/Adapter/GogsTest.php | 5 +++++ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/VCS/Adapter/Git/Gitea.php b/src/VCS/Adapter/Git/Gitea.php index 0b4ada42..b821d5c1 100644 --- a/src/VCS/Adapter/Git/Gitea.php +++ b/src/VCS/Adapter/Git/Gitea.php @@ -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 diff --git a/tests/VCS/Adapter/GiteaTest.php b/tests/VCS/Adapter/GiteaTest.php index 3334d8ff..861b9a9b 100644 --- a/tests/VCS/Adapter/GiteaTest.php +++ b/tests/VCS/Adapter/GiteaTest.php @@ -487,14 +487,14 @@ public function testGetPullRequest(): void public function testGetPullRequestFiles(): void { $repositoryName = 'test-get-pull-request-files-' . \uniqid(); - $this->vcsAdapter->createRepository(self::$owner, $repositoryName, false); + $this->vcsAdapter->createRepository(static::$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'); + $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( - self::$owner, + static::$owner, $repositoryName, 'Test PR Files', 'feature-branch', @@ -504,7 +504,7 @@ public function testGetPullRequestFiles(): void $prNumber = $pr['number'] ?? 0; $this->assertGreaterThan(0, $prNumber); - $result = $this->vcsAdapter->getPullRequestFiles(self::$owner, $repositoryName, $prNumber); + $result = $this->vcsAdapter->getPullRequestFiles(static::$owner, $repositoryName, $prNumber); $this->assertIsArray($result); $this->assertNotEmpty($result); @@ -512,7 +512,7 @@ public function testGetPullRequestFiles(): void $filenames = array_column($result, 'filename'); $this->assertContains('feature.txt', $filenames); - $this->vcsAdapter->deleteRepository(self::$owner, $repositoryName); + $this->vcsAdapter->deleteRepository(static::$owner, $repositoryName); } public function testGetPullRequestWithInvalidNumber(): void diff --git a/tests/VCS/Adapter/GogsTest.php b/tests/VCS/Adapter/GogsTest.php index fe291fa5..d7f04193 100644 --- a/tests/VCS/Adapter/GogsTest.php +++ b/tests/VCS/Adapter/GogsTest.php @@ -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'); + } } From 8bec208abee86a3e94d1ea2093c653e7b5af1ad8 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 3 Apr 2026 09:04:22 +0530 Subject: [PATCH 5/5] removed dead code --- src/VCS/Adapter/Git/Gitea.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/VCS/Adapter/Git/Gitea.php b/src/VCS/Adapter/Git/Gitea.php index b821d5c1..bc6544ef 100644 --- a/src/VCS/Adapter/Git/Gitea.php +++ b/src/VCS/Adapter/Git/Gitea.php @@ -101,7 +101,7 @@ public function createRepository(string $owner, string $repositoryName, bool $pr ]); return $response['body'] ?? []; - return is_array($body) ? $body : []; + // return is_array($body) ? $body : []; } public function createOrganization(string $orgName): string