fix(ci): use pull_request_target so labeler works on fork PRs#480
fix(ci): use pull_request_target so labeler works on fork PRs#480nikolasdehor wants to merge 2 commits intoSynkraAI:mainfrom
Conversation
The pull_request event gives read-only GITHUB_TOKEN for fork PRs, blocking the labeler action regardless of the permissions block. Switch to pull_request_target (runs in base-repo context with write access) and replace the shell git diff squad check with a GitHub API call that works without access to the fork's commits. Closes SynkraAI#479
|
@nikolasdehor is attempting to deploy a commit to the Pedro Valério Lopez's projects Team on Vercel. A member of the Team first needs to authorize it. |
WalkthroughUpdated the PR-labeling GitHub Actions workflow to run on Changes
Sequence Diagram(s)mermaid ForkContributor->>Repo: Open PR (fork) Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested labels
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
.github/workflows/pr-labeling.yml (1)
17-19:⚠️ Potential issue | 🟡 MinorConsider adding
issues: writepermission.The official
actions/labelerdocumentation states: "To ensure the action works correctly, include the following permissions in your workflow file:contents: read,pull-requests: write,issues: write." The missingissues: writescope could cause silent failures if the labeler needs to create a label that doesn't yet exist in the repository.🛡️ Proposed fix
permissions: contents: read pull-requests: write + issues: write🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In @.github/workflows/pr-labeling.yml around lines 17 - 19, The workflow's permissions block is missing the issues write scope which can prevent actions/labeler from creating labels; update the permissions map to include issues: write alongside contents: read and pull-requests: write so the labeler can create and manage labels when needed, i.e., add the "issues" permission with "write" access in the permissions block.
🧹 Nitpick comments (1)
.github/workflows/pr-labeling.yml (1)
21-22: Security advisory: never add a forkrefto this checkout step.There is a potentially dangerous misuse of the
pull_request_targettrigger that may allow malicious PR authors to obtain write permissions or steal secrets. It is advisable to only usepull_request_targetin workflows carefully designed to avoid executing untrusted code.The current checkout (no explicit
ref) is safe because it checks out the base branch. If this step is ever extended, ensure therefis never set togithub.event.pull_request.head.shaorgithub.event.pull_request.head.ref, as doing so would check out untrusted fork code in a privileged context.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In @.github/workflows/pr-labeling.yml around lines 21 - 22, The workflow uses the pull_request_target trigger and the checkout step (uses: actions/checkout@v4); ensure you never set the checkout ref to the PR head (github.event.pull_request.head.sha or .head.ref) which would check out untrusted fork code in a privileged context—keep the step without a ref (so it checks out the base) or explicitly set it to the base ref (github.event.pull_request.base.sha or .base.ref) and audit any future changes to the Checkout step to avoid adding head refs.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In @.github/workflows/pr-labeling.yml:
- Around line 49-54: The call to github.rest.issues.addLabels(...) is missing an
await so any rejection will be unobserved; update the workflow action to await
the Promise returned by github.rest.issues.addLabels (or return it from the
async function) so failures propagate and the step fails on error, ensuring the
API call is properly awaited and errors are not silently swallowed.
---
Outside diff comments:
In @.github/workflows/pr-labeling.yml:
- Around line 17-19: The workflow's permissions block is missing the issues
write scope which can prevent actions/labeler from creating labels; update the
permissions map to include issues: write alongside contents: read and
pull-requests: write so the labeler can create and manage labels when needed,
i.e., add the "issues" permission with "write" access in the permissions block.
---
Nitpick comments:
In @.github/workflows/pr-labeling.yml:
- Around line 21-22: The workflow uses the pull_request_target trigger and the
checkout step (uses: actions/checkout@v4); ensure you never set the checkout ref
to the PR head (github.event.pull_request.head.sha or .head.ref) which would
check out untrusted fork code in a privileged context—keep the step without a
ref (so it checks out the base) or explicitly set it to the base ref
(github.event.pull_request.base.sha or .base.ref) and audit any future changes
to the Checkout step to avoid adding head refs.
There was a problem hiding this comment.
Pull request overview
This PR fixes the Resource not accessible by integration error that occurs on all fork PRs during the labeler CI step. The issue stems from GitHub's security restriction where the pull_request event provides only read-only GITHUB_TOKEN permissions for fork PRs, which cannot be overridden by workflow permissions.
Changes:
- Changed workflow trigger from
pull_requesttopull_request_targetto run with base repository context and full write permissions - Replaced git diff shell command with GitHub API call (
github.rest.pulls.listFiles()) for squad change detection, sincepull_request_targetdoesn't expose fork commits - Updated context references to use
context.payload.pull_request.numberfor explicit PR number access
Comments suppressed due to low confidence (2)
.github/workflows/pr-labeling.yml:50
- Consider using
context.issue.numberinstead ofcontext.payload.pull_request.numberfor consistency with other workflows in this repository. Bothpr-automation.yml(lines 101, 120) andwelcome.yml(lines 82, 93) usecontext.issue.numberwhen referencing pull request numbers. Whilecontext.payload.pull_request.numberis valid and works correctly, usingcontext.issue.numberwould maintain consistency across the codebase.
issue_number: context.payload.pull_request.number,
.github/workflows/pr-labeling.yml:39
- For consistency, consider using
context.issue.numberinstead ofcontext.payload.pull_request.number. This would align with the pattern used in other workflows likepr-automation.yml(lines 101, 120) andwelcome.yml(lines 82, 93). While both approaches work correctly,context.issue.numberis the established convention in this codebase for referencing pull request numbers.
pull_number: context.payload.pull_request.number,
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- await the addLabels API call so rejections propagate - add issues: write permission (needed to create labels) - add comment clarifying checkout uses base ref, not PR head
Resumo
pull_requestparapull_request_target(o eventopull_requestdá token read-only para forks, entãoaddLabelsfalhava)git diffpor chamada à API do GitHub para detecção de mudanças em squads (forks não têm acesso ao histórico de branches remotas)issues: writenecessária para adicionar labelsawaitna chamadaaddLabelsCloses #479
Nota de segurança
pull_request_targetexecuta no contexto do repo base. O workflow atual apenas faz checkout de código (read-only) e adiciona labels — não executa scripts não-confiáveis. Se no futuro adicionar steps que executam código do PR, considerarpull_request+ workflow_run pattern.Plano de teste
git diff