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
on:
  pull_request:
jobs:
  test-check:
    name: runner / sqlfluff (github-check)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      # only run when a comment requests linting
      - uses: khan/pull-request-comment-trigger@master
        id: check
        with:
          trigger: '/lint'
          reaction: rocket
        env:
          GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}'
      - uses: yu-iskw/action-sqlfluff@v3
        if: steps.check.outputs.triggered == 'true'
        id: lint-sql
        with:
          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

[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

[sqlfluff:rules:L010]
# Keywords
capitalisation_policy = upper
2 Likes

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

Are you in the community slack channel? I would like to chat about some issues I’m having getting this to work.