选择所有具有特定扩展名的文件 Github 操作工作流程

huangapple go评论84阅读模式
英文:

Select all the files with a spefic extension Github actions workflow

问题

你好,你可以将files: '*.sql'修改为以下方式:

files: '^models/.*\\.sql$'

这将匹配models/目录下以.sql结尾的所有文件。

英文:

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

答案1

得分: 2

你可以按文件类型筛选文件,例如 types: [file, sql],因为 pre-commit 支持 SQL (.sql) 过滤。

为了将此限制在 PR 中,您可能想在工作流程中使用 --from-ref--to-ref。有关示例,请参考这个评论

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

默认情况下,如果有任何违规行为,sqlfluff 将失败,即返回非零退出状态,导致步骤失败。如果需要,可以使用 --nofail 标志来取消这种行为。

此外,要自动应用更改,您需要更新工作流,就好像您在运行以下命令一样:

sqlfluff fix --force ...

--force 用于在修复 SQL 问题时跳过确认。

之后,您可以通过 https://github.com/stefanzweifel/git-auto-commit-action 或手动提交这些更改。


更新

正如 anthony sottile答案中指出的,该筛选条件是多余的,因为 sqlfluff 已经处理了它

英文:

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.

答案2

得分: 2

files正则表达式,而不是通配符,所以 *.sql 不是正确的模式 —— 正确的正则表达式应该是 files: \.sql$

但是,你不应该需要设置 files(或types) —— 包含一个预先编写的远程存储库的目的是它已经为你提前设置好了!例如,远程的 sqlfluff 存储库 已经设置了 types: [sql]


免责声明:我编写了 pre-commit。

英文:

files is a regex and not a glob so *.sql is not the correct pattern for it -- the correct regex for that would be files: \.sql$

but, you shouldn't need to set files (or types) at all -- the point of including a pre-written remote repository is it is already set up out of the box for you! for example, the remote sqlfluff repo already sets types: [sql]


disclaimer: I wrote pre-commit

huangapple
  • 本文由 发表于 2023年3月8日 16:26:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670752.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定