|
| 1 | +name: Label PR review state fork review follow-up |
| 2 | + |
| 3 | +on: |
| 4 | + # Fork pull_request_review runs of "Label PR review state" get a read-only |
| 5 | + # token and skip their label writes (issue #2046). When such a run |
| 6 | + # completes, this privileged follow-up resolves the fork PRs and dispatches |
| 7 | + # a workflow_dispatch run of "Label PR review state", which reconciles them |
| 8 | + # and writes the computed labels. The follow-up lives in its own workflow |
| 9 | + # because GitHub rejects a workflow that lists itself in |
| 10 | + # workflow_run.workflows (issue #2086). It takes no artifact input and |
| 11 | + # never checks out code, so no untrusted PR code or artifacts are touched. |
| 12 | + workflow_run: |
| 13 | + workflows: ["Label PR review state"] |
| 14 | + types: [completed] |
| 15 | + |
| 16 | +permissions: |
| 17 | + # This privileged workflow only reads PR metadata and dispatches the |
| 18 | + # reconciliation workflow. All unspecified permissions, including contents, |
| 19 | + # are none; no fork code or configuration is checked out or executed. |
| 20 | + actions: write |
| 21 | + pull-requests: read |
| 22 | + |
| 23 | +# Each completed run gets its own group: one run can resolve several PRs, and |
| 24 | +# a shared key would let an unrelated completion supersede queued dispatches. |
| 25 | +concurrency: |
| 26 | + group: label-pr-review-state-fork-review-followup-${{ github.event.workflow_run.id }} |
| 27 | + cancel-in-progress: false |
| 28 | + |
| 29 | +jobs: |
| 30 | + dispatch: |
| 31 | + name: Zoo Code / dispatch fork review reconciliation |
| 32 | + # Loop guard (issue #2046): follow up only on runs triggered by |
| 33 | + # pull_request_review. The runs this workflow dispatches have event |
| 34 | + # 'workflow_dispatch', so their completion is filtered out here and |
| 35 | + # the chain stops after one hop. |
| 36 | + if: github.event.workflow_run.event == 'pull_request_review' |
| 37 | + runs-on: ubuntu-latest |
| 38 | + steps: |
| 39 | + - name: Resolve fork PRs and dispatch reconciliation |
| 40 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 |
| 41 | + with: |
| 42 | + retries: 3 |
| 43 | + script: | |
| 44 | + const { owner, repo } = context.repo; |
| 45 | + const run = context.payload.workflow_run; |
| 46 | +
|
| 47 | + // Mirrors the job-level `if`: only runs triggered by |
| 48 | + // pull_request_review need a follow-up, so the chain |
| 49 | + // stops after one hop. |
| 50 | + if (run?.event !== 'pull_request_review') { |
| 51 | + core.info(`Ignoring workflow_run with event '${run?.event}'`); |
| 52 | + return; |
| 53 | + } |
| 54 | +
|
| 55 | + // Same-repository review runs already ran with a privileged |
| 56 | + // token and wrote their labels; only fork runs need the |
| 57 | + // follow-up. |
| 58 | + if (run.head_repository && run.head_repository.fork === false) { |
| 59 | + core.info('Ignoring same-repository pull_request_review run'); |
| 60 | + return; |
| 61 | + } |
| 62 | +
|
| 63 | + // Resolve which PRs the completed run targeted. Fork runs |
| 64 | + // backfill pull_requests empty, so fall back to the exact head |
| 65 | + // owner, branch, and SHA lookup used by the resolve job of |
| 66 | + // label-pr-review-state.yml. Keep both copies in sync. |
| 67 | + let prNumbers = []; |
| 68 | + if (run.pull_requests?.length > 0) { |
| 69 | + prNumbers = run.pull_requests.map(pr => pr.number); |
| 70 | + } else { |
| 71 | + const headOwner = run.head_repository?.owner?.login; |
| 72 | + if (headOwner && run.head_branch && run.head_sha) { |
| 73 | + const candidates = await github.paginate(github.rest.pulls.list, { |
| 74 | + owner, |
| 75 | + repo, |
| 76 | + state: 'open', |
| 77 | + head: `${headOwner}:${run.head_branch}`, |
| 78 | + per_page: 100, |
| 79 | + }); |
| 80 | + const baseRepository = `${owner}/${repo}`.toLowerCase(); |
| 81 | + prNumbers = candidates |
| 82 | + .filter(pr => |
| 83 | + pr.head?.sha === run.head_sha && |
| 84 | + pr.base?.repo?.full_name?.toLowerCase() === baseRepository |
| 85 | + ) |
| 86 | + .map(pr => pr.number); |
| 87 | + core.info( |
| 88 | + `Resolved workflow_run ${run.id ?? '(unknown)'} to ` + |
| 89 | + `${prNumbers.length} PR(s) by exact head owner, branch, and SHA` |
| 90 | + ); |
| 91 | + } |
| 92 | + } |
| 93 | +
|
| 94 | + // Each dispatch starts a privileged workflow_dispatch run of the |
| 95 | + // reconciliation workflow for one PR. Its completion re-triggers |
| 96 | + // this workflow, but the loop guard above filters it out. |
| 97 | + // A failed dispatch must not skip the PRs after it: collect the |
| 98 | + // failures and fail the step once, naming each PR. |
| 99 | + const failures = []; |
| 100 | + for (const prNumber of prNumbers) { |
| 101 | + try { |
| 102 | + await github.rest.actions.createWorkflowDispatch({ |
| 103 | + owner, |
| 104 | + repo, |
| 105 | + workflow_id: 'label-pr-review-state.yml', |
| 106 | + ref: 'main', |
| 107 | + inputs: { pull_request_number: String(prNumber) }, |
| 108 | + }); |
| 109 | + core.info(`Dispatched label-pr-review-state.yml for PR #${prNumber}`); |
| 110 | + } catch (error) { |
| 111 | + failures.push(`#${prNumber}: ${error.message}`); |
| 112 | + } |
| 113 | + } |
| 114 | + if (failures.length > 0) { |
| 115 | + core.setFailed(`Failed to dispatch ${failures.length} PR(s): ${failures.join('; ')}`); |
| 116 | + } |
0 commit comments