Skip to content

ci(trunk): upload impacted targets with fork-aware auth so fork PRs can enter the merge queue - #3429

Merged
gantoine merged 2 commits into
mainfrom
posthog-code/trunk-impacted-targets-fork-support
Jul 14, 2026
Merged

ci(trunk): upload impacted targets with fork-aware auth so fork PRs can enter the merge queue#3429
gantoine merged 2 commits into
mainfrom
posthog-code/trunk-impacted-targets-fork-support

Conversation

@gantoine

Copy link
Copy Markdown
Member

Problem

Fork PRs can't be merged through the Trunk parallel merge queue and have to be merged by hand — e.g. #2819, which Trunk logged as "😎 Merged manually" and landed via GitHub auto-squash, with no Trunk Merge Queue check ever running.

Trunk needs an impacted-targets upload to place a PR into a queue lane. That upload authenticates with the org API token (x-api-token = secrets.TRUNK_API_TOKEN), but GitHub withholds repo secrets from pull_request runs originating in a fork, so fork PRs can never make that call. The repo had no impacted-targets upload at all (only the flaky-test analytics-uploader).

Per the Trunk team: "add a x-forked-workflow-run-id header to the request to upload impacted targets for your parallel queue from forked PRs."

Change

New workflow .github/workflows/trunk-impacted-targets.yml that POSTs to /v1/setImpactedTargets on every PR:

  • Internal PRsx-api-token: ${{ secrets.TRUNK_API_TOKEN }}
  • Fork PRsx-forked-workflow-run-id: ${{ github.run_id }} (no secret required)

Details:

  • Fork detection reuses the same expression as the existing e2e job (head.repo.full_name != github.repository).
  • Request body matches Trunk's schema (repo{host,owner,name}, pr{number,sha}, targetBranch, impactedTargets), built with jq from env-passed context (no untrusted PR data interpolated into the shell). Uses the PR head SHA so it matches the run's head_sha for Trunk's fork verification.
  • Reports impactedTargets: "ALL" — always correct (never under-reports), gets forks into the queue; a computed target list for real parallelism is left as a documented TODO in the file.
  • Fails loudly (no continue-on-error) with retries — a silent upload failure is exactly what kept fork PRs out of the queue.
  • Stays on the pull_request trigger, never pull_request_target, so no secret is exposed to fork code ("pwn request").

Ref: https://docs.trunk.io/merge-queue/optimizations/parallel-queues/api#handling-forked-pull-requests

Verification

  • YAML parses; on/jobs/concurrency/permissions as intended.
  • Smoke-tested the shell block with sample values: jq payload is schema-correct (pr.number is a JSON number) and the auth header switches correctly between fork/internal.
  • Server-side acceptance of a fork upload can only be observed once a real fork PR runs this workflow — suggest watching the next fork PR to confirm it now enters the queue.

Created with PostHog Code

Fork PRs never entered the Trunk parallel merge queue and had to be merged
by hand (e.g. #2819). Trunk needs an impacted-targets upload to place a PR
into a lane, and the upload authenticates with the org API token — which
GitHub withholds from fork `pull_request` runs.

Add a dedicated workflow that uploads to /v1/setImpactedTargets on every PR,
using x-api-token on internal PRs and x-forked-workflow-run-id (from
github.run_id) on fork PRs, per Trunk's guidance. Reports "ALL" targets for
correctness; refining to a computed target list is left as a documented TODO.

Stays on the `pull_request` trigger (never pull_request_target) so no secret
is ever exposed to fork code.

Generated-By: PostHog Code
Task-Id: 295c0ed1-48f3-4bd4-9d0e-dba8d17031ca
@trunk-io

trunk-io Bot commented Jul 14, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@greptile-apps

greptile-apps Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "ci(trunk): upload impacted targets with ..." | Re-trigger Greptile

Comment thread .github/workflows/trunk-impacted-targets.yml Outdated
@gantoine
gantoine requested a review from a team July 14, 2026 13:48
Fork PRs frequently share a source branch name (many are opened from
`main`), so keying the concurrency group on github.head_ref would put two
such PRs in one group where cancel-in-progress lets one cancel the other's
upload — leaving that head SHA without the impacted-targets upload it needs
to enter the queue. Key on github.event.pull_request.number instead.

Generated-By: PostHog Code
Task-Id: 295c0ed1-48f3-4bd4-9d0e-dba8d17031ca
@gantoine
gantoine merged commit e1977d1 into main Jul 14, 2026
26 checks passed
@gantoine
gantoine deleted the posthog-code/trunk-impacted-targets-fork-support branch July 14, 2026 16:45
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.

2 participants