From 11c37d3074e9145cea44073a8df9faf6d8ff9579 Mon Sep 17 00:00:00 2001 From: Hemachandar Date: Fri, 26 Sep 2025 19:05:54 +0530 Subject: [PATCH 1/3] fix: Check org membership to determine external PR --- src/VCS/Adapter.php | 2 ++ src/VCS/Adapter/Git/GitHub.php | 15 ++++++++++++++- tests/VCS/Adapter/GitHubTest.php | 9 +++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/VCS/Adapter.php b/src/VCS/Adapter.php index 1479fbe..8c2967e 100644 --- a/src/VCS/Adapter.php +++ b/src/VCS/Adapter.php @@ -252,6 +252,8 @@ abstract public function getCommit(string $owner, string $repositoryName, string */ abstract public function getLatestCommit(string $owner, string $repositoryName, string $branch): array; + abstract public function isUserMemberOfOrganization(string $username, string $organization): bool; + /** * Call * diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index fd78e23..d94363c 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -640,7 +640,13 @@ public function getEvent(string $event, string $payload): array $authorAvatarUrl = $payload['pull_request']['user']['avatar_url'] ?? ''; $commitHash = $payload['pull_request']['head']['sha'] ?? ''; $headCommitUrl = $repositoryUrl . "/commits/" . $commitHash; - $external = $payload['pull_request']['head']['user']['login'] !== $payload['pull_request']['base']['user']['login']; + + $isOrgRepository = ($payload['repository']['owner']['type'] ?? '') === 'Organization'; + if ($isOrgRepository) { + $external = !$this->isUserMemberOfOrganization($owner, $owner); + } else { + $external = $payload['pull_request']['head']['user']['login'] !== $payload['repository']['owner']['login']; + } return [ 'branch' => $branch, @@ -686,4 +692,11 @@ public function validateWebhookEvent(string $payload, string $signature, string { return $signature === ('sha256=' . hash_hmac('sha256', $payload, $signatureKey)); } + + public function isUserMemberOfOrganization(string $username, string $organization): bool + { + $url = "/orgs/{$organization}/memberships/{$username}"; + $response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"]); + return $response['headers']['status-code'] >= 200 && $response['headers']['status-code'] < 300; + } } diff --git a/tests/VCS/Adapter/GitHubTest.php b/tests/VCS/Adapter/GitHubTest.php index 6bf4057..813f86c 100644 --- a/tests/VCS/Adapter/GitHubTest.php +++ b/tests/VCS/Adapter/GitHubTest.php @@ -340,4 +340,13 @@ public function testGetLatestCommit(): void $this->assertEquals('https://avatars.githubusercontent.com/u/43381712?v=4', $commitDetails['commitAuthorAvatar']); $this->assertEquals('https://github.com/vermakhushboo', $commitDetails['commitAuthorUrl']); } + + public function testIsUserMemberOfOrganization(): void + { + $isMember = $this->vcsAdapter->isUserMemberOfOrganization('hmacr', 'test-org-hmacr'); + $this->assertTrue($isMember); + + $isNotMember = $this->vcsAdapter->isUserMemberOfOrganization('test-user', 'test-org-hmacr'); + $this->assertFalse($isNotMember); + } } From 59711d727a0d413e1b61ec50207557703e339a5d Mon Sep 17 00:00:00 2001 From: Hemachandar Date: Fri, 26 Sep 2025 19:09:09 +0530 Subject: [PATCH 2/3] doc --- src/VCS/Adapter.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/VCS/Adapter.php b/src/VCS/Adapter.php index 8c2967e..43f8816 100644 --- a/src/VCS/Adapter.php +++ b/src/VCS/Adapter.php @@ -252,6 +252,13 @@ abstract public function getCommit(string $owner, string $repositoryName, string */ abstract public function getLatestCommit(string $owner, string $repositoryName, string $branch): array; + /** + * Check if user is a member of an organization + * + * @param string $username Username of the user + * @param string $organization Name of the organization + * @return bool True if user is a member of the organization, false otherwise + */ abstract public function isUserMemberOfOrganization(string $username, string $organization): bool; /** From 4913203d902a70b0e675e9a3ca5e258126ef85ca Mon Sep 17 00:00:00 2001 From: Hemachandar Date: Fri, 26 Sep 2025 19:26:24 +0530 Subject: [PATCH 3/3] feedback --- src/VCS/Adapter/Git/GitHub.php | 5 +++-- tests/VCS/Adapter/GitHubTest.php | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index d94363c..94b986d 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -641,11 +641,12 @@ public function getEvent(string $event, string $payload): array $commitHash = $payload['pull_request']['head']['sha'] ?? ''; $headCommitUrl = $repositoryUrl . "/commits/" . $commitHash; + $authorUsername = $payload['pull_request']['user']['login'] ?? ($payload['pull_request']['head']['user']['login'] ?? ''); $isOrgRepository = ($payload['repository']['owner']['type'] ?? '') === 'Organization'; if ($isOrgRepository) { - $external = !$this->isUserMemberOfOrganization($owner, $owner); + $external = !$this->isUserMemberOfOrganization($authorUsername, $owner); } else { - $external = $payload['pull_request']['head']['user']['login'] !== $payload['repository']['owner']['login']; + $external = $authorUsername !== $owner; } return [ diff --git a/tests/VCS/Adapter/GitHubTest.php b/tests/VCS/Adapter/GitHubTest.php index 813f86c..3e2c540 100644 --- a/tests/VCS/Adapter/GitHubTest.php +++ b/tests/VCS/Adapter/GitHubTest.php @@ -343,10 +343,10 @@ public function testGetLatestCommit(): void public function testIsUserMemberOfOrganization(): void { - $isMember = $this->vcsAdapter->isUserMemberOfOrganization('hmacr', 'test-org-hmacr'); + $isMember = $this->vcsAdapter->isUserMemberOfOrganization('vermakhushboo', 'test-kh'); $this->assertTrue($isMember); - $isNotMember = $this->vcsAdapter->isUserMemberOfOrganization('test-user', 'test-org-hmacr'); + $isNotMember = $this->vcsAdapter->isUserMemberOfOrganization('test-user', 'test-kh'); $this->assertFalse($isNotMember); } }