5

I am trying to setup Flake8 linter in bitbucket pipeline and it works but I only want to run linter on pushed files. Currently, it runs on the entire project.

There are many modules in the project which are not optimized and not planned to do it for a while.

Given below is the 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

enter image description here

For instance, I have only pushed the core/util.py and need to run a linter on that file only.
I can specify the particular file as below to run on and works.

- flake8 core/util.py --max-line-length=180 --ignore=E203,W503
  1. Is it even possible to make it dynamic that takes all pushed files?
  2. Of course, multiple files can be pushed, is that even possible to configure?
Priyank
  • 1,219
  • 11
  • 30
  • 1
    The list of changed files between 2 commits could be found using `git diff --name-only commit1 [commit2]`; `commit2` in your case is the current commit and can be omitted. The trick is to find out what was the `commit1` before pushing. I don't know enough about BitBucket pipelines to find out this commit. If you can get it the command is `flake8 $(git diff --name-only commit1)` – phd May 17 '20 at 16:13

2 Answers2

5

For multiple commits, assuming that you want to run flake8 on the modified files, you would need to use pull requests with the following pipeline:

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

We fetch the destination branch since it is not fetched by default in bitbucket pipelines. Then we use the git diff which flake8 knows how to handle with --diff.

noamk
  • 1,179
  • 13
  • 15
  • I am getting this error. `+ git fetch origin "+refs/heads/$BITBUCKET_PR_DESTINATION_BRANCH:refs/remotes/origin/$BITBUCKET_PR_DESTINATION_BRANCH" fatal: Invalid refspec '+refs/heads/:refs/remotes/origin/'` – Gaurang Shah Feb 01 '23 at 17:05
  • 1
    This answer is out of date as of flake8 6.0.0 being released. – HSchmale May 16 '23 at 21:48
3

Thanks to @phd

Given below command returns the list of files of the current commits.

git show --name-only --pretty=format:

And working complete command is:

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


Edit:

As per the @Anthony remark that is indeed correct that the above command will work only if it happens to be one commit at a time.

I don't find any solution to check on all commits so just checked on the last three commits. Not a proper solution but based on my organization workflow it would work.

Warning: This would certainly not reliable on merge like development to the master branch as there would be lots of commits going to be merged and it will check just on the last three.

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
Priyank
  • 1,219
  • 11
  • 30