←  github actions

Views:

Formatting check for Pull Requests

We can write a workflow to automate and check for formatting issues in a pull request. This is a very common and widely used workflow. To get this setup, we need formatter in our project, and in this example we'll see how to make it work in a javascript project with prettier.

  • Install Prettier (if not already) in your project.
  • Add format:check script in your package.json to check for formatting issues.
{
  // ...
  "scripts": {
    // ...
    "format:check": "prettier --check './src/**/*.{js,jsx,ts,tsx,css}'"
  }
}

Workflow to check formatting issues in Pull Requests

name: Code Formatting Check

on:
  pull_request:
    branches:
      - main
    types: [opened, synchronize, reopened]

jobs:
  format-check:
    runs-on: ubuntu-latest
    name: Check PR's Formatting
    steps:
      - uses: actions/checkout@v1
      - name: Use Node.js 12.x
        uses: actions/setup-node@v1
        with:
          node-version: 12.x

      - name: Install deps (with cache)
        uses: bahmutov/npm-install@v1.7.10
        with:
          working-directory: "./"
          useLockFile: false

      - name: prettier formatting check
        run: npm run format:check

Result

Here's the result for above workflow, you can also view it on ead6ecf commit.

formatting-check-result