Getting Started
To setup autofix.ci, do the following:
-
Install our GitHub App.
This provides autofix.ci with the necessary permissions to update pull requests.
-
Create .github/workflows/autofix.yml with the following content:
name: autofix.ci # needed to securely identify the workflow on: pull_request: push: branches: [ "main" ] permissions: contents: read jobs: autofix: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 # TODO: add all code-fixing here. - uses: autofix-ci/action@8bc06253bec489732e5f9c52884c7cace15c0160
Limiting the runner's permissions and pinning the autofix action to a specific release is not required, but makes our workflow more resilient against supply chain attacks.[1]
-
Add a job that auto-fixes your codebase.
Replace the comment in the file above with the code-fixing tools of your choice.
Take a look at the examples below!
Examples
You can combine all the example steps below, but keep them in a single job with only one call to the autofix action! :)
Python
These two examples demonstrate the use of some of the most popular Python code fixing tools.
We use the @install-pinned actions to
install deterministic versions of all tools, but you can also pip install
or use an existing requirements.txt
file.
name: autofix.ci
on:
workflow_call:
pull_request:
push:
branches: [ "main" ]
permissions:
contents: read
jobs:
autofix:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# Upgrade to the latest Python syntax with https://github.com/asottile/pyupgrade
- uses: install-pinned/pyupgrade@af7d65f31bddb01097a24da6c8fb694441f51cba
- name: Run pyupgrade
run: |
shopt -s globstar
pyupgrade --exit-zero-even-if-changed --py39-plus **/*.py
# Sort imports with https://github.com/asottile/reorder_python_imports
- uses: install-pinned/reorder_python_imports@9766e7ba4f33497b107014571afe3b5f4c2d946b
- name: Run reorder-python-imports
run: |
shopt -s globstar
reorder-python-imports --exit-zero-even-if-changed --py39-plus **/*.py
# Remove unused imports with https://github.com/PyCQA/autoflake
- uses: install-pinned/autoflake@46b4898323be58db319656fe2758f3fd5ddfee32
- run: autoflake --in-place --remove-all-unused-imports -r .
# Format code with https://github.com/psf/black
- uses: install-pinned/black@c4c60e5726690c02940169ee0f1cfc2d99d5ae6a
- run: black .
- uses: autofix-ci/action@8bc06253bec489732e5f9c52884c7cace15c0160
name: autofix.ci
on:
workflow_call:
pull_request:
push:
branches: [ "main" ]
permissions:
contents: read
jobs:
autofix:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# Fix lint errors with https://github.com/charliermarsh/ruff
- uses: install-pinned/ruff@c013d86ec59da1d76a90c0bfebb896e573c098e4
- run: ruff --fix-only .
# Format code with https://github.com/psf/black
- uses: install-pinned/black@c4c60e5726690c02940169ee0f1cfc2d99d5ae6a
- run: black .
- uses: autofix-ci/action@8bc06253bec489732e5f9c52884c7cace15c0160
Of course, there's way more in the Python ecosystem! You may also want to check out yesqa, isort, blacken-docs, yapf, autopep8, docformatter, or μsort.
TypeScript / JavaScript / HTML / CSS
Here's a simple workflow that formats your TypeScript or JavaScript code using Prettier:
name: autofix.ci
on:
pull_request:
push:
branches: [ "main" ]
permissions:
contents: read
jobs:
autofix:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
- run: npm ci
- run: npx prettier --write .
- uses: autofix-ci/action@8bc06253bec489732e5f9c52884c7cace15c0160
Alternatively, you may want to check out standardjs!
Rust
Here's a simple workflow that rustfmts your repo and applies clippy's suggestions:
name: autofix.ci
on:
workflow_call:
pull_request:
push:
branches: [ "main" ]
permissions:
contents: read
env:
rust_clippy: 1.65 # MSRV
jobs:
autofix:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: autofix-${{ hashFiles('**/Cargo.lock') }}
- run: rustup toolchain install ${{ env.rust_clippy }} --profile minimal --component rustfmt --component clippy
- run: rustup default ${{ env.rust_clippy }}
- run: cargo clippy --fix --workspace
- run: cargo fmt --all
- uses: autofix-ci/action@8bc06253bec489732e5f9c52884c7cace15c0160
You can of course switch to a different Rust toolchain first, for example if you are using nightly rustfmt.
Crab fact: autocix.ci's backend is written in Rust. 🦀
Go
Here's a simple workflow that gofmts your repo:
name: autofix.ci
on:
pull_request:
push:
branches: [ "main" ]
permissions:
contents: read
jobs:
autofix:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.17'
# goimports works like gofmt, but also fixes imports.
# see https://pkg.go.dev/golang.org/x/tools/cmd/goimports
- run: go install golang.org/x/tools/cmd/goimports@latest
- run: goimports -w .
# of course we can also do just this instead:
# - run: gofmt -w .
- uses: autofix-ci/action@8bc06253bec489732e5f9c52884c7cace15c0160
Images
With a bit of creativity, autofix.ci is not limited to code formatting. For example, the following workflow shrinks all PNG files:
name: autofix.ci
on:
pull_request:
push:
branches: [ "main" ]
permissions:
contents: read
jobs:
autofix:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# Optimize all PNGs with https://pngquant.org/
- run: sudo apt-get update && sudo apt-get install -y pngquant
- name: Run pngquant
run: |
shopt -s globstar
pngquant -f --ext .png --skip-if-larger -- **/*.png
- uses: autofix-ci/action@8bc06253bec489732e5f9c52884c7cace15c0160
pre-commit hooks
If your existing workflow is based on pre-commit.com hooks, you should use pre-commit.ci instead of autofix.ci. Of course, you can also integrate pre-commit hooks in your autofix.ci workflow:
name: autofix.ci
on:
pull_request:
push:
branches: [ "main" ]
permissions:
contents: read
jobs:
autofix:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: pip install pre-commit
- run: pre-commit run --all-files
- uses: autofix-ci/action@8bc06253bec489732e5f9c52884c7cace15c0160