Search code examples
yamlgithub-actionspre-commit-hookpre-commit.comgithub-actions-self-hosted-runners

Select all the files with a spefic extension Github actions workflow


Hi I am trying to run a pre-commit hook on all the files with .sql extension. I was wondering how can I do that. All those files with .sql are in the models/ directory, but I have sub-directories within that directory too (to group those sql files) so what is the best way to get all the files with .sql ending? . This is my .pre-commit-config.yml file:

repos:
  - repo: https://github.com/sqlfluff/sqlfluff
    rev: 0.9.1
    hooks:
      - id: sqlfluff-fix
        args:
          [
            --exclude-rules,
            'L011,L031',
            --dialect,
            'clickhouse',
            --templater,
            'dbt',
            --fix_even_unparsable,
            'True',
          ]
    files: '*.sql'

What should I assign to files: '*.sql': ^ instead of this

In the pre-commit documentation, I couldn't find an answer or in the sqlfluff documentation


Solution

  • You can filter files by file types i.e. types: [file, sql] as SQL (.sql) filtering is supported by pre-commit.

    To keep this restricted to a PR, you might want to use --from-ref and --to-ref as part of your workflow. Refer to this comment for an example:

    --from-ref ${{ github.event.pull_request.base.sha }}
    --to-ref   ${{ github.event.pull_request.head.sha }}
    

    By default, sqlfluff will fail in case of any violations i.e. it'll return non-zero exit status resulting in step failure. This can be suppressed by using --nofail flag if required.

    And, to apply the changes automatically, you'll have to update workflow as if you're running:

    sqlfluff fix --force ...
    

    --force is to skip the confirmation during fixing SQL issues.

    After that, you can commit these changes via https://github.com/stefanzweifel/git-auto-commit-action or manually.


    UPDATE

    As pointed out in anthony sottile's answer, the filter is redundant as sqlfluff already takes care of it.