GitHub Action: Option to format only changed files
Is your feature request related to a problem? Please describe.
When I raise a PR, I like to have the GitHub Action for black lint my code. However, with large projects, sometimes it's unwise to lint the entire existing codebase- it's better to only lint the files that have been changed in that PR.
Describe the solution you'd like
It would be great if there were an option to only lint the files that have changed, when running the GitHub Action on a PR.
Describe alternatives you've considered
I've considered pairing it with another Action such as this one to list all changed files in a PR, then feed it into the src option for the Black GitHub Action. However, there are different formats to return the list and I haven't figured out how to pass it correctly.
If there were more information provided by the documentation (either an example of this or a thorough description of how to pass multiple files into src), that would be a satisfactory solution as well.
Additional context
If there is a way to pull that info + pass to black CLI and create a clean API to turn this on optionally I'm sure we'd accept a PR for this. Sounds a great idea to save some CPU cycles.
Only as side note; with help of https://github.com/marketplace/actions/changed-files, same functionality can be done with yaml like this;
steps:
- uses: actions/checkout@v3
- uses: tj-actions/changed-files@v35
with:
files: |
**/*.py
**/*.pyi
id: changed-python-files
- name: Run black against changed files
uses: psf/black@stable
with:
options: "--verbose"
src: ${{ steps.changed-python-files.outputs.all_changed_files }}
However, there might be some issues of not get all the files with above statement if the PR contains multiple commits .. havent checked.
I think that should be resolved by using fetch-depth=0 in checkout. The following seems to do the trick, if anyone is looking for the answer:
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # OR "2" -> To retrieve the preceding commit.
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v35
with:
files: |
**/*.py
**/*.pyi
- uses: psf/black@stable
if: steps.changed-files.outputs.any_changed == 'true'
with:
options: "--check --diff --line-length=120"
src: ${{ steps.changed-files.outputs.all_changed_files }}