Running SQLFluff fix on PRs as a GitHub Action with inline suggestions

Here’s a secret: SQLFluff doesn’t need to be specifically configured to use the dbt templater. Using the default Jinja templater, SQLFluff can fix inconsistent capitalisation or indentation without any fancy database connections.

With that in mind, I created this basic GitHub Action file in my-repo/.github/workflows/sqlfulff.yml. This action uses the new (as of this post) action-sqlfluff project to make inline suggestions on the pull request.

name: sqlfluff with reviewdog
    name: runner / sqlfluff (github-check)
    runs-on: ubuntu-latest
      - uses: actions/checkout@v2
      # only run when a comment requests linting
      - uses: khan/pull-request-comment-trigger@master
        id: check
          trigger: '/lint'
          reaction: rocket
          GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}'
      - uses: yu-iskw/action-sqlfluff@v3
        if: steps.check.outputs.triggered == 'true'
        id: lint-sql
          github_token: ${{ secrets.github_token }}
          reporter: github-pr-review
          sqlfluff_version: "0.11.1"
          sqlfluff_command: "fix" # Or "lint"
          config: "${{ github.workspace }}/.sqlfluff"
          paths: '${{ github.workspace }}/models'
      - name: 'Show outputs (Optional)'
        shell: bash
        run: |
          echo '${{ steps.lint-sql.outputs.sqlfluff-results }}' | jq -r '.'
          echo '${{ steps.lint-sql.outputs.sqlfluff-results-rdjson }}' | jq -r '.'

Here are the results, when creating a PR with the comment /lint and a missing explicit alias keyword.

P.S. Here’s my SQLFluff configuration file, my-repo/.sqlfluff

dialect = bigquery

# L031 - Exclude rule requiring fully-qualified table names in joins
# should have been disabled for BigQuery already
# L036 - multiple SELECT columns can appear on the SELECT line
# L051 - Exclude rule requiring INNER JOIN rather than just JOIN
# L003 - Ignore whitespace problems for now
# L034 - Don't move around columns
# L016 - Long lines are fine
# L009 - No need for a single newline at end of file
# L008 - No need for spaces around commas
exclude_rules = L051,L031,L036,L003,L034,L016,L009,L008

# Keywords
capitalisation_policy = upper

Nice! Are your rule exclusions for your preference, or must those rules be excluded for it to work without dbt templater?

SQLFluff can provide basic linting of dbt models using the jinja templater without any rule exclusions. These exclusions have more to do with BigQuery (L031), dbt Cloud (L009), and our company’s code preferences (L010).

@martinburch Thanks a lot for using my action.

1 Like