Sub topic 13
Fail the check or auto-fix: weighing the trade-offs
This chapter assumes you have the check-only scripts (lint, format:check) and a working lint workflow from earlier chapters. It also assumes you know how permissions limits the GITHUB_TOKEN.
When a format check fails, you have two options. The job can fail and ask the author to fix the problem, or the workflow can fix the files and commit them itself. Both are valid. They differ in risk, convenience, and how well they work with pull requests from forks.
Option 1: fail the check
This is what the workflow in the earlier chapters does. prettier --check exits non-zero, the job fails, and the pull request is blocked until the author pushes a fix.
- The workflow stays read-only, so
permissions: contents: readis enough. - It works the same for forks and for branches in your repository.
- The author sees the problem and learns the rule.
- It costs a round trip. The author must run
npm run formatlocally, commit, and wait for CI again.
Option 2: auto-fix and commit
Here the workflow runs the rewriting script (format) and pushes the result to the pull request branch.
name: Autofix format
on:
pull_request:
jobs:
format:
if: github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm run format
- name: Commit changes
run: |
if ! git diff --quiet; then
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git commit -am "style: apply Prettier"
git push
fiThe if: condition skips pull requests from forks. A fork's token cannot push to the contributor's branch, so the push step would fail there.
Costs of auto-fixing
- Write access. The job needs
contents: write, which breaks the least-privilege setup. A compromised dependency innpm cicould then push code to the branch. - No re-run of checks. Commits pushed with
GITHUB_TOKENdo not trigger new workflow runs. The fix commit therefore has no status checks unless you use a personal access token or a GitHub App token. - Races. If the author pushes while the bot is working, the bot's push is rejected or the author must pull before continuing.
- History noise. Each fix adds a bot commit to the branch.
- Only formatting is fixable. Most lint rules need a human decision. Auto-fixing
eslint --fixcan also change behavior, so it needs review.
Choosing a policy
The diagram shows the usual decision path.
A common approach is to fail in CI and add a local pre-commit hook that runs npm run format. The hook catches most problems before they are pushed, and CI stays read-only. Consider auto-fix only for small private teams, or for a separate scheduled job that opens a pull request with the fixes instead of pushing to someone's branch.
| Factor | Fail the check | Auto-fix |
|---|---|---|
| Token permission | `contents: read` | `contents: write` |
| Fork pull requests | Supported | Not supported |
| Author effort | Fix locally | None for formatting |
| Re-runs after fix | Normal push triggers CI | Bot push does not by default |
Exercise
A team has many outside contributors who open pull requests from forks. They want formatting problems fixed with no effort from the author. Why does the auto-fix workflow above not solve this, and what is a safer default?
Show answer
The workflow skips fork pull requests because the token on a fork's pull request cannot push to the contributor's branch. Pushing would also require write permissions. The safer default is to keep format:check as a failing, read-only check and give contributors a local script or pre-commit hook to fix formatting before they push.