-
Notifications
You must be signed in to change notification settings - Fork 7
Setup Cloud Code Reviewer to work for git forks #357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d00a40b
fix: Setup Cloud Code Reviewer to work for git forks
alex-alecu 14e37e7
Merge branch 'main' into fix/fix-code-reviewer-for-forks
alex-alecu c9df5ee
Merge branch 'main' into fix/fix-code-reviewer-for-forks
alex-alecu a192fd1
Merge branch 'main' into fix/fix-code-reviewer-for-forks
alex-alecu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/lib/integrations/platforms/github/webhook-handlers/pull-request-checkout-ref.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| export type PullRequestCheckoutRef = { | ||
| checkoutRef: string; | ||
| isForkPr: boolean; | ||
| headRepoFullName: string | null; | ||
| }; | ||
|
|
||
| export type PullRequestCheckoutRefInput = { | ||
| pull_request: { | ||
| number: number; | ||
| head: { | ||
| ref: string; | ||
| repo?: { | ||
| full_name: string; | ||
| } | null; | ||
| }; | ||
| }; | ||
| repository: { | ||
| full_name: string; | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Resolve which git ref should be checked out for a PR review. | ||
| * | ||
| * - Same-repo PRs: use head.ref (e.g. "feature/my-change") | ||
| * - Fork PRs: use GitHub's synthetic pull ref (e.g. "refs/pull/123/head") | ||
| */ | ||
| export function resolvePullRequestCheckoutRef( | ||
| payload: PullRequestCheckoutRefInput | ||
| ): PullRequestCheckoutRef { | ||
| const headRepoFullName = payload.pull_request.head.repo?.full_name ?? null; | ||
| const isForkPr = headRepoFullName !== null && headRepoFullName !== payload.repository.full_name; | ||
| const checkoutRef = isForkPr | ||
| ? `refs/pull/${payload.pull_request.number}/head` | ||
| : payload.pull_request.head.ref; | ||
|
|
||
| return { | ||
| checkoutRef, | ||
| isForkPr, | ||
| headRepoFullName, | ||
| }; | ||
| } | ||
65 changes: 65 additions & 0 deletions
65
src/lib/integrations/platforms/github/webhook-handlers/pull-request-handler.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { resolvePullRequestCheckoutRef } from '@/lib/integrations/platforms/github/webhook-handlers/pull-request-checkout-ref'; | ||
alex-alecu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| describe('resolvePullRequestCheckoutRef', () => { | ||
| it('uses head.ref for same-repo PRs', () => { | ||
| const result = resolvePullRequestCheckoutRef({ | ||
| pull_request: { | ||
| number: 123, | ||
| head: { | ||
| ref: 'feature/same-repo', | ||
| repo: { full_name: 'acme/widgets' }, | ||
| }, | ||
| }, | ||
| repository: { | ||
| full_name: 'acme/widgets', | ||
| }, | ||
| }); | ||
|
|
||
| expect(result).toEqual({ | ||
| checkoutRef: 'feature/same-repo', | ||
| isForkPr: false, | ||
| headRepoFullName: 'acme/widgets', | ||
| }); | ||
| }); | ||
|
|
||
| it('uses refs/pull/<number>/head for fork PRs', () => { | ||
| const result = resolvePullRequestCheckoutRef({ | ||
| pull_request: { | ||
| number: 456, | ||
| head: { | ||
| ref: 'feature/fork-branch', | ||
| repo: { full_name: 'external/widgets-fork' }, | ||
| }, | ||
| }, | ||
| repository: { | ||
| full_name: 'acme/widgets', | ||
| }, | ||
| }); | ||
|
|
||
| expect(result).toEqual({ | ||
| checkoutRef: 'refs/pull/456/head', | ||
| isForkPr: true, | ||
| headRepoFullName: 'external/widgets-fork', | ||
| }); | ||
| }); | ||
|
|
||
| it('falls back to head.ref when head.repo is missing', () => { | ||
| const result = resolvePullRequestCheckoutRef({ | ||
| pull_request: { | ||
| number: 789, | ||
| head: { | ||
| ref: 'feature/missing-head-repo', | ||
| }, | ||
| }, | ||
| repository: { | ||
| full_name: 'acme/widgets', | ||
| }, | ||
| }); | ||
|
|
||
| expect(result).toEqual({ | ||
| checkoutRef: 'feature/missing-head-repo', | ||
| isForkPr: false, | ||
| headRepoFullName: null, | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not always use this ref? doesnt this work for regular prs?