tdeboissiere / python-format-action

MIT License
3 stars 2 forks source link

This is cool :-D, but can we make it even cooler? #1

Open EiffL opened 4 years ago

EiffL commented 4 years ago

Bonjour bonjour bonjour! So, I was looking for some tools to apply Black formatting to my new project: https://github.com/DifferentiableUniverseInitiative/jax_cosmo And what a surprise when I see this popping up in the market place :-D

So I think this is working great and doing what you intended to do, but I have one question, could you make it so that it automatically commits any black or reorder imports fixes needed? Because it's not like people can argue over these, so instead of failing the CI test, might as well fix whatever needs fixing.

I just transitioned from travis a few weeks ago so I don't know if it's possible/easy to do with GitHub Actions. Also maybe not desirable? but I don't see why.

tdeboissiere commented 4 years ago

Hello !

Haven't found anything to do the modification on the fly yet.

My current worflow is to use git hooks + pre-commit for that

How to:

Step 0) Install pre-commit

First install the pre-commit util:

https://pre-commit.com/#install

    pip install pre-commit  # with pip
    brew install pre-commit  # on Mac

Then install the git hooks

    pre-commit install
    # check .pre-commit-config.yaml for details of hooks

Upon git commit, the pre-commit hooks will be run automatically on the stage files (i.e. added by git add)

N.B. By default, pre-commit checks only run on staged files

If you need to run it on all files:

    pre-commit run --all-files

Step 1) Add GitHub action

In <root_of_repo>/.github/workflows/main.yml

name: "Python Formatting"

on: [pull_request]

jobs:
  formatting:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      # Run isort + black formatter
      - name: Python Code Formatter
        uses: tdeboissiere/python-format-action@master

Step 2) Add pre-commit

In <root_of_repo>/.pre-commit-config.yaml

- repo: https://github.com/asottile/reorder_python_imports
  rev: v2.2.0
  hooks:
    - id: reorder-python-imports
- repo: https://github.com/psf/black
  rev: 19.10b0
  hooks:
    - id: black
      language_version: python3.7
- repo: https://github.com/pre-commit/pre-commit-hooks
  rev: v2.3.0
  hooks:
    - id: end-of-file-fixer
    - id: trailing-whitespace
tdeboissiere commented 4 years ago

@EiffL You can have a look at this to format on the fly https://peterevans.dev/posts/github-actions-how-to-automate-code-formatting-in-pull-requests/

This will always add an extra commit if the code is not formatted however, which may or may not be what you want