Repository navigation
docs: add pull_request_target guide for fork contribution support - #2501
Conversation
Code Review by Qodo
Context used 1. Incomplete pull_request_target security guidance
|
PR Summary by QodoDocs: document pull_request_target setup for forked PR support
AI Description
Diagram
High-Level Assessment
Files changed (2)
|
Document how to use pull_request_target event to enable PR-Agent on PRs from forked repositories. The code already supports this event type (github_action_runner.py:110), but no documentation existed. Closes The-PR-Agent#2500
567aaea to
9f3ed00
Compare
| #### Using with pull_request_target (fork/contribution support) | ||
|
|
||
| By default, the `pull_request` event does not have access to repository secrets when the PR originates from a forked repository, which means PR-Agent won't be able to access your `OPENAI_KEY` and `GITHUB_TOKEN` secrets. | ||
|
|
||
| To support PRs from external contributors (forks), use the `pull_request_target` event instead. This event runs in the context of the base repository and has access to secrets, while the PR code is checked out manually with `actions/checkout`. | ||
|
|
||
| ```yaml | ||
| name: PR Agent | ||
| on: | ||
| pull_request_target: | ||
| types: [opened, reopened, synchronize, ready_for_review, review_requested] | ||
| issue_comment: | ||
| jobs: | ||
| pr_agent_job: | ||
| if: ${{ github.event.sender.type != 'Bot' && (github.event_name == 'pull_request_target' || github.event.issue.pull_request) }} | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| issues: write | ||
| pull-requests: write | ||
| contents: write | ||
| steps: | ||
| - name: Checkout PR code | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| ref: ${{ github.event.pull_request.head.sha }} | ||
| allow-unsafe-pr-checkout: true | ||
| - name: PR Agent action step | ||
| uses: the-pr-agent/pr-agent@main | ||
| env: | ||
| OPENAI_KEY: ${{ secrets.OPENAI_KEY }} | ||
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| github_action_config.pr_actions: '["opened", "reopened", "synchronize", "ready_for_review", "review_requested"]' | ||
| ``` | ||
|
|
||
| !!! warning "Security considerations" | ||
| Using `pull_request_target` requires extra care. The `actions/checkout` step with `allow-unsafe-pr-checkout: true` checks out the PR's code, which could contain modified workflows. To mitigate risks, consider: | ||
| - Using `pull_request_target` only when you need fork support | ||
| - Reviewing the [GitHub security guide on pull_request_target](https://docs.github.com/en/actions/reference/security/securely-using-pull_request_target) | ||
| - Avoiding running additional build steps from the checked-out PR code |
There was a problem hiding this comment.
1. Incomplete pull_request_target security guidance 📎 Requirement gap ⛨ Security
The fork-support guide recommends pull_request_target with actions/checkout using allow-unsafe-pr-checkout: true without clearly documenting GitHub’s safer checkout defaults change, or distinguishing safe versus unsafe checkout patterns and when/why they apply. This can lead users to copy an unnecessary, high-risk checkout configuration even though PR-Agent’s action runner can operate via the PR URL from the event payload without needing PR code checked out.
Agent Prompt
## Issue description
The documentation’s `pull_request_target` example includes an `actions/checkout` step with `allow-unsafe-pr-checkout: true`, but it does not (a) reference GitHub’s safer checkout defaults change, (b) clearly explain safe vs. unsafe checkout patterns and when/why to use mitigations like pinning `ref` to the PR head SHA, or (c) clarify that PR-Agent itself can run using the PR URL (via GitHub API) and therefore does not require checking out PR code. We want the docs to reduce the chance users copy-paste an unnecessary, high-risk checkout configuration while still providing accurate, compliant guidance for cases where checkout is truly needed.
## Issue Context
This guide is likely to be adopted verbatim in public repositories that accept fork PRs, so ambiguous or default unsafe checkout guidance increases real-world security risk. Compliance requires explicit, `pull_request_target`-specific guidance that references GitHub’s official recommendations and the safer defaults change, and that clearly distinguishes safe vs. unsafe patterns (including when/why `allow-unsafe-pr-checkout` is used and why pinning `ref` to a head SHA can be important). Also, PR-Agent’s action runner operates off the PR URL derived from the event payload, so checkout is not required for PR-Agent execution and should not be implied as necessary.
## Fix Focus Areas
- docs/docs/installation/github.md[57-95]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| - name: Checkout PR code | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| ref: ${{ github.event.pull_request.head.sha }} | ||
| allow-unsafe-pr-checkout: true | ||
| - name: PR Agent action step |
There was a problem hiding this comment.
3. Issue_comment checkout ref invalid 🐞 Bug ≡ Correctness
The new workflow example always runs actions/checkout with `ref: ${{
github.event.pull_request.head.sha }}, but the same job is also triggered by issue_comment`, where
github.event.pull_request is not present. This can break comment-triggered PR-Agent commands (job
fails or checks out the wrong ref) in the documented fork-support setup.
Agent Prompt
### Issue description
The docs workflow listens to both `pull_request_target` and `issue_comment`, but unconditionally uses `github.event.pull_request.head.sha` in the checkout step. For `issue_comment` events the payload does not include `pull_request`, so the example can fail when users comment on PRs.
### Issue Context
PR-Agent’s GitHub Action runner handles `issue_comment` by using `event_payload.issue.pull_request.url` and does not require a local checkout.
### Fix Focus Areas
- docs/docs/installation/github.md[78-89]
### Proposed fix
Choose one of:
1) Remove the checkout step entirely from this example (PR-Agent runs via PR URL / API), OR
2) Make the checkout step conditional:
- `if: ${{ github.event_name == 'pull_request_target' }}`
- and avoid referencing `github.event.pull_request.*` for `issue_comment` runs (either skip checkout on comments or use a ref expression valid for comment events, e.g. `refs/pull/${{ github.event.issue.number }}/head`).
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
|
Code review by qodo was updated up to the latest commit ffdec38 |
|
Thanks @utsab345 ! |
You're welcome, @naorpeled! Thanks for the review and merge. Looking forward to contributing again! 🚀 |
Description
Adds documentation explaining how to use the
pull_request_targetevent to enable PR-Agent on PRs from forked repositories (external contributions).The GitHub Action runner already supports this event type (
pr_agent/servers/github_action_runner.py:110), but no documentation existed showing users how to configure it.Changes
#### Using with pull_request_target (fork/contribution support)with a complete YAML example, explanation of whypull_requestdoesn't work for forks, and a security warning box.Closes #2500