autofix.ci logo

Getting Started

To setup autofix.ci, do the following:

  1. Install our GitHub App.

    This provides autofix.ci with the necessary permissions to update pull requests.

  2. 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]

  3. 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.

pyupgrade, reorder_python_imports autoflake, and black
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@423622e7c2088eeba495a591385ec22074284f90
      - 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@515035fd9eb355713f61dee238b17a04ce01f4d2
      - 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@1a248450153f02b75d051acf6c2a05df8c797666
      - run: autoflake --in-place --remove-all-unused-imports -r .

      # Format code with https://github.com/psf/black
      - uses: install-pinned/black@9101a4d68e870eaaaae21c412d1d879b93c9afcb
      - run: black .

      - uses: autofix-ci/action@8bc06253bec489732e5f9c52884c7cace15c0160
ruff and black
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@c2c08b98420bd050a622d3bd2ee16c491c0da6f3  # 0.0.189
      - run: ruff --fix-only .

      # Format code with https://github.com/psf/black
      - uses: install-pinned/black@9101a4d68e870eaaaae21c412d1d879b93c9afcb
      - 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

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: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - 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@v3
        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