|
| 1 | +# Fork PR Comments |
| 2 | + |
| 3 | +When a pull request is opened from a **forked repository**, the `GITHUB_TOKEN` used by the |
| 4 | +`pull_request` event has **read-only** permissions by design (GitHub security policy). |
| 5 | +This means `pr-comments: true` cannot write a comment back to the PR. |
| 6 | + |
| 7 | +By default, commit-check-action handles this gracefully: |
| 8 | + |
| 9 | +- PR comment writing is **skipped** with a `::warning::` message in the logs |
| 10 | +- A **notice is added to the Job Summary** explaining why and how to fix it |
| 11 | +- The commit checks themselves **still run normally** |
| 12 | + |
| 13 | +> **For most projects, this is sufficient** — contributors can see check results in the |
| 14 | +> action Job Summary. But if you *must* have PR comments on fork contributions, there |
| 15 | +> are two recommended approaches. |
| 16 | +
|
| 17 | +--- |
| 18 | + |
| 19 | +## Option 1: Two-workflow pattern (recommended) |
| 20 | + |
| 21 | +This is the **official GitHub-recommended best practice** for writing PR comments from |
| 22 | +fork PRs. It uses the [`workflow_run`](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_run) |
| 23 | +event with **no security risks**. |
| 24 | + |
| 25 | +> 📁 Ready-to-use files: [`examples/commit-check-workflow-a.yml`](../examples/commit-check-workflow-a.yml) |
| 26 | +> and [`examples/commit-check-workflow-b.yml`](../examples/commit-check-workflow-b.yml) |
| 27 | +
|
| 28 | +**How it works:** |
| 29 | + |
| 30 | +``` |
| 31 | + pull_request workflow_run |
| 32 | + │ │ |
| 33 | + ▼ ▼ |
| 34 | +┌──────────────┐ ┌──────────────────┐ |
| 35 | +│ Workflow A │ │ Workflow B │ |
| 36 | +│ (checks) │────►│ (comment writer) │ |
| 37 | +│ │ │ │ |
| 38 | +│ Token: READ │ │ Token: WRITE │ |
| 39 | +│ Saves result │ │ Reads artifact │ |
| 40 | +│ as artifact │ │ Posts PR comment │ |
| 41 | +└──────────────┘ └──────────────────┘ |
| 42 | +``` |
| 43 | + |
| 44 | +### Workflow A |
| 45 | + |
| 46 | +`.github/workflows/commit-check.yml` (triggered by `pull_request`): |
| 47 | + |
| 48 | +```yaml |
| 49 | +name: Commit Check |
| 50 | + |
| 51 | +on: |
| 52 | + pull_request: |
| 53 | + branches: ["main"] |
| 54 | + |
| 55 | +jobs: |
| 56 | + check: |
| 57 | + runs-on: ubuntu-latest |
| 58 | + steps: |
| 59 | + - uses: actions/checkout@v5 |
| 60 | + with: |
| 61 | + fetch-depth: 0 |
| 62 | + - uses: commit-check/commit-check-action@v2 |
| 63 | + with: |
| 64 | + message: true |
| 65 | + branch: true |
| 66 | + pr-comments: false # comments handled by Workflow B |
| 67 | + job-summary: true |
| 68 | + - uses: actions/upload-artifact@v4 |
| 69 | + with: |
| 70 | + name: commit-check-result-${{ github.event.number }} |
| 71 | + path: result.txt # saved for Workflow B |
| 72 | +``` |
| 73 | +
|
| 74 | +> 📄 Full file: [`examples/commit-check-workflow-a.yml`](../examples/commit-check-workflow-a.yml) |
| 75 | + |
| 76 | +### Workflow B |
| 77 | + |
| 78 | +`.github/workflows/commit-check-comment.yml` (triggered by `workflow_run`): |
| 79 | + |
| 80 | +```yaml |
| 81 | +name: Commit Check Comment |
| 82 | +
|
| 83 | +on: |
| 84 | + workflow_run: |
| 85 | + workflows: ["Commit Check"] # must match Workflow A's name exactly |
| 86 | + types: [completed] |
| 87 | +
|
| 88 | +jobs: |
| 89 | + comment: |
| 90 | + runs-on: ubuntu-latest |
| 91 | + permissions: |
| 92 | + pull-requests: write |
| 93 | + actions: read # needed to download artifacts |
| 94 | + steps: |
| 95 | + - uses: actions/download-artifact@v4 |
| 96 | + with: |
| 97 | + name: commit-check-result-${{ github.event.workflow_run.pull_requests[0].number }} |
| 98 | + run-id: ${{ github.event.workflow_run.id }} |
| 99 | + github-token: ${{ github.token }} |
| 100 | + - name: Read result and post PR comment |
| 101 | + uses: actions/github-script@v7 |
| 102 | + with: |
| 103 | + script: | |
| 104 | + // See examples/commit-check-workflow-b.yml for full script |
| 105 | + const fs = require('fs'); |
| 106 | + const prNumber = ${{ github.event.workflow_run.pull_requests[0].number }}; |
| 107 | + const resultText = fs.readFileSync('result.txt', 'utf8').trim(); |
| 108 | + const body = resultText |
| 109 | + ? '# Commit-Check ❌\n```\n' + resultText + '\n```' |
| 110 | + : '# Commit-Check ✔️'; |
| 111 | + // Creates or updates the matching PR comment |
| 112 | +``` |
| 113 | + |
| 114 | +> 📄 Full file: [`examples/commit-check-workflow-b.yml`](../examples/commit-check-workflow-b.yml) |
| 115 | +
|
| 116 | +### Key security benefits |
| 117 | + |
| 118 | +- Workflow B runs in the **base repository's context**, so `GITHUB_TOKEN` has full write |
| 119 | + permissions (you explicitly grant `pull-requests: write`) |
| 120 | +- Workflow B **does not checkout the PR code**, so untrusted fork code never runs |
| 121 | + with elevated permissions |
| 122 | +- The artifact only contains `result.txt` — no code or secrets |
| 123 | + |
| 124 | +--- |
| 125 | + |
| 126 | +## Option 2: pull_request_target (advanced, use with caution) |
| 127 | + |
| 128 | +If you understand the security implications, you can use |
| 129 | +[`pull_request_target`](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target) |
| 130 | +which runs in the base repository's context with **write token access**. |
| 131 | + |
| 132 | +> **⚠️ Security warning:** Never check out (`actions/checkout`) the PR's HEAD commit |
| 133 | +> when using `pull_request_target`. Always check out the base branch or use the |
| 134 | +> default merge commit. Otherwise, fork code could exfiltrate your repository's secrets. |
| 135 | +
|
| 136 | +```yaml |
| 137 | +name: Commit Check |
| 138 | + |
| 139 | +on: |
| 140 | + pull_request_target: |
| 141 | + branches: ["main"] |
| 142 | + |
| 143 | +jobs: |
| 144 | + commit-check: |
| 145 | + runs-on: ubuntu-latest |
| 146 | + permissions: |
| 147 | + contents: read |
| 148 | + pull-requests: write |
| 149 | + steps: |
| 150 | + # SAFE: checkout the merge commit, NOT the PR head |
| 151 | + - uses: actions/checkout@v5 |
| 152 | + with: |
| 153 | + fetch-depth: 0 |
| 154 | + - uses: commit-check/commit-check-action@v2 |
| 155 | + with: |
| 156 | + message: true |
| 157 | + branch: true |
| 158 | + pr-comments: true |
| 159 | +``` |
| 160 | +
|
| 161 | +> ✅ With `pull_request_target`, `pr-comments: true` **does work** on fork PRs — |
| 162 | +> the token has the workflow's configured permissions regardless of whether the PR |
| 163 | +> is from a fork. |
| 164 | +> |
| 165 | +> **When to use this:** Only if the two-workflow pattern is too complex for your setup |
| 166 | +> and you have thoroughly reviewed the security implications. |
0 commit comments