Skip to content

πŸ”§ ci: skip CodeQL on fork PRs (upload cannot write security-events)#156

Merged
flupkede merged 1 commit into
developfrom
fix/codeql-fork-pr-skip
Jul 23, 2026
Merged

πŸ”§ ci: skip CodeQL on fork PRs (upload cannot write security-events)#156
flupkede merged 1 commit into
developfrom
fix/codeql-fork-pr-skip

Conversation

@flupkede

Copy link
Copy Markdown
Owner

Problem

CodeQL fails on every fork pull request with:

##[error]Resource not accessible by integration - https://docs.github.com/rest

Root cause: fork PRs run with a restricted GITHUB_TOKEN that cannot write security-events back to the upstream repo. The workflow's permissions: security-events: write block doesn't help β€” GitHub deliberately uses a separate, reduced-scope token for fork PR runs (security measure to prevent untrusted code from exfiltrating write scopes).

This caused the confusing red X on PR #150 (from @tony-nexartis) even though the analysis itself completed successfully and the post-merge CodeQL run on develop passed.

Fix

Add a job-level if: that skips the entire analyze job when the pull request's head repo differs from the workflow's repository:

if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository

Coverage retained

CodeQL still runs on:

  • βœ… push to develop/master β€” post-merge, where the token has full write scopes (this is the run that actually matters for the security tab)
  • βœ… same-repo PRs β€” full write token, upload works
  • βœ… weekly schedule β€” full write token

Only the redundant fork-PR run (which always failed to upload anyway) is skipped. No scanning coverage is lost.

Verification

  • YAML syntax validated (python -c "import yaml; yaml.safe_load(...)")
  • git diff is +9 βˆ’0 (comment + 1-line if: condition)
  • Confirmed develop's post-merge CodeQL run #29989526007 was green β€” the failure was purely the fork-PR pull_request event #29989594411

…events)

Fork PRs run with a restricted GITHUB_TOKEN that cannot write
`security-events` back to the upstream repo, so the analyze step's
SARIF upload fails with "Resource not accessible by integration"
for every external contributor PR (e.g. PR #150 from tony-nexartis).

Add a job-level `if:` that skips the entire analyze job when the
pull_request's head repo differs from the workflow's repository.
CodeQL still runs on:
  - push events to develop/master (post-merge, full write token)
  - same-repo PRs (full write token)
  - the weekly schedule
so no scanning coverage is lost β€” only the redundant, upload-failing
fork-PR run is skipped.

No behavior change for non-fork workflows.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant