I am trying to set up Bitbucket Pipeline to auto-format my python codebase. All I'm doing is running black.
I'm working with distributed dev teams and it's hard to get everyone to install pre-commit hooks on their local machine so for now I just want to run black whenever code is merged. I don't want this to be a check that fails the merge.
My expectation is that whenever a commit is made to master branch (so whenever pull request is merged), black would be run auto-format the code AND THEN the commit is made. I am also not worried about only running black on affected files, for now I just want to run black against the whole codebase.
Anyway, with my current setup, my testing shows that black is run after the commit and not before.
here is my bitbucket-pipelines.yml:
image: python:3.10
pipelines:
branches:
master:
- step:
name: run black
script:
- pip install black
- black -l 88 .
Ok, after much figuring out, I figured out a way. I'm not sure if this is the best way but it works. I've changed the goal for the Bitbucket Pipeline to beautify the code and commit the reformatted code whenever a pull request is created or updated instead.
This solution is inspired by:
Step 1: Create an OAuth2 Consumer
Step 2: Configure the Bitbucket Respository
BB_AUTH_STRINGand value being a string of formatKey:Secret(see picture), using the key and secret of the OAuth consumer we created earlierStep 3: Setup your Bitbucket Pipeline YML
The
bitbucket-pipelines.ymlI've settled on currently is the following:This pipeline will only run whenever a Pull-Request is created and whenever new commits are made to the Pull Request.
The pipeline will install
curlandjq.The pipeline will install
black.The pipeline will then run
black -l 100.If no files were modified, the pipeline will exit.
Else:
BB_AUTH_STRINGto get a temporary access token stored inBB_TOKEN.[skip ci]in the commit head message to skip the pipeline when this commit is pushConclusion
You now have a bitbucket pipeline that will automatically format Python code with
blackwhenever your developers create a pull request or push new commits to it. It will avoid infinite bitbucket pipeline builds, it will detect if there are changes before committing.