Black: Running automatically Black when a PR is sent

Created on 20 Oct 2018  ·  9Comments  ·  Source: psf/black

Hello,

I'm currently using Black manually but I think it could be a nice improvement if Black could be run when a PR is sent to a repository.

With Julia language, there is a nice Github bot called FemtoCleaner which is able to fix automatically depreciation warnings and send PR automatically to Github repositories where this bot is installed.

A similar feature for Python and code formatting with Black could be a nice improvement.

I also wonder if Github Actions https://github.com/features/actions https://www.techworm.net/programming/github-actions-workflow-automating-tool/ couldn't also be used (when available) to provide a nice workflow for code formatting with Black.

Kind regards

integrations

Most helpful comment

Another possibility is writing a git hook that runs Black before/after some event like committing or pushing.

For example, in a file .git/hooks/pre-commit in your repo:

#!/bin/sh

set -e

files=`git diff --staged --name-only --diff-filter=d -- "*.py"`

for file in $files; do
  black $file
  git add $file
done

After chmodding it, any python file staged for commit (filtering out deleted files) will be auto-formatted and re-added for the commit.

All 9 comments

This feels less like a Black ticket, and more like you're after a generic bot that can do Stuff™ (including run black) and create+push a commit when a PR is created.

Thanks @jambonrose the black_out project from @Mariatta seems to be exactly what I was looking for! I will give it a try.

Mariatta was mentioned, but she's out of open source for the rest of September 2018, and parts of October 2018 . Be aware she might not get to this until November 2018.
(I'm a bot)

Another possibility is writing a git hook that runs Black before/after some event like committing or pushing.

For example, in a file .git/hooks/pre-commit in your repo:

#!/bin/sh

set -e

files=`git diff --staged --name-only --diff-filter=d -- "*.py"`

for file in $files; do
  black $file
  git add $file
done

After chmodding it, any python file staged for commit (filtering out deleted files) will be auto-formatted and re-added for the commit.

Thanks @ns-ckao for this tip
Using https://pre-commit.com/ could also be an other idea (like mentioned in https://black.readthedocs.io/en/stable/version_control_integration.html )
but I wasn't looking for a local solution.

@ns-ckao
@scls19fr

I know this isn't exactly what you want but you can use the (already written) black pre-commit hook and add enforcement of your pre-commit hooks to your CI.
Or just add black . --check to your CI.

https://github.com/ambv/black#version-control-integration
https://pre-commit.com/#usage-in-continuous-integration

At the risk of self-promotion, this is exactly the sort of thing that Lint.ai is built to do. With Lint.ai, running Black and pushing the results back as a commit on every commit and PR is just one line:

black . && commit "chore(reformatting): Black"

Reach out through the website if you'd like to learn more.

This is out of scope for Black but it looks like there's plenty of integrations already 👍🏻

Was this page helpful?
0 / 5 - 0 ratings