Flake8 linter 仅通过 Bitbucket 管道对推送的文件进行检查

Pri*_*ank 5 python git bitbucket flake8 bitbucket-pipelines

我正在尝试在 bitbucket 管道中设置 Flake8 linter 并且它可以工作,但我只想在推送的文件上运行 linter。目前,它在整个项目上运行。

项目中有很多模块没有优化,暂时不打算做。

下面给出的是 bitbucket-pipelines.yml。

image: python:3.7.3

pipelines:
  default:
    - step:
        caches:
          - pip
        name: Check flake8
        script: # Modify the commands below to build your repository.
          - pip install flake8
          - flake8 --max-line-length=180 --ignore=E203,W503
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

例如,我只推送了core/util.py,并且只需要在该文件上运行 linter。
我可以指定如下所示的特定文件来运行和工作。

- flake8 core/util.py --max-line-length=180 --ignore=E203,W503
Run Code Online (Sandbox Code Playgroud)
  1. 是否有可能使其动态化以获取所有推送的文件?
  2. 当然,可以推送多个文件,可以配置吗?

noa*_*amk 5

对于多次提交,假设您要在修改的文件上运行 flake8,则需要通过以下管道使用拉取请求:

pipelines:
  pull-requests:
    '**':
      - step:
          caches:
            - pip
          script:
            - pip install flake8
            - git fetch origin "+refs/heads/$BITBUCKET_PR_DESTINATION_BRANCH:refs/remotes/origin/$BITBUCKET_PR_DESTINATION_BRANCH"
            - git diff -u --relative origin/$BITBUCKET_PR_DESTINATION_BRANCH... | flake8 --diff
Run Code Online (Sandbox Code Playgroud)

我们获取目标分支,因为默认情况下不会在 bitbucket 管道中获取它。然后我们使用 flake8 知道如何处理的 git diff --diff


Pri*_*ank 3

感谢@phd

下面给出的命令返回当前提交的文件列表。

git show --name-only --pretty=format:
Run Code Online (Sandbox Code Playgroud)

完整的工作命令是:

flake8 $(git show --name-only --pretty=format:) --max-line-length=180 --ignore=E203,W503
Run Code Online (Sandbox Code Playgroud)


编辑:

根据 @Anthony 的评论,上述命令只有在一次提交一次时才有效,这确实是正确的。

我找不到任何解决方案来检查所有提交,因此只检查了最后三个提交。这不是一个合适的解决方案,但根据我的组织工作流程,它会起作用。

警告:这对于主分支的合并开发肯定不可靠,因为会有很多提交要被合并,并且它只会检查最后三个提交。

flake8 $(git log --name-only --pretty=oneline --full-index HEAD^^^..HEAD | grep -vE '^[0-9a-f]{40} ' | sort | uniq) --exclude=*.yml,*.yaml,*.js,*.css,*.json,*.xml,*.md --max-line-length=180 --ignore=E203,W503
Run Code Online (Sandbox Code Playgroud)