Closed iamtodor closed 2 years ago
@iamtodor First of all, sorry for the late response. It has been some crazy weeks. Thanks a lot for your interest in our GH action. First of all, it looks like the checkout
step is missing from your gh-action recipe. This is what causes the python files to be ignored. The correct recipe should be:
name: black-formation
on:
pull_request:
branches: [master]
jobs:
black-formation:
name: runner / black-formation
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Check files using the black formatter
uses: rickstaa/action-black@v1
id: action_black
with:
black_args: ". --line-length 120"
- name: Create Pull Request
if: steps.action_black.outputs.is_formatted == 'true'
uses: peter-evans/create-pull-request@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
title: "Format Python code with psf/black push"
commit-message: ":art: Format Python code with psf/black"
body: |
There appear to be some python formatting errors in ${{ github.sha }}. This pull request
uses the [psf/black](https://github.com/psf/black) formatter to fix these issues.
base: ${{ github.head_ref }} # Creates pull request onto pull request or commit branch
branch: actions/black
Further, you also do not need to use both the rickstaa/action-black@v1
and the psf/black@stable
action. Now that https://github.com/psf/black/pull/1909 and https://github.com/psf/black/pull/1940 are merged, they do the same thing. I would therefore advise you to go for psf/black@stable
. I will update the README.md to reflect this recommendation.
Running only when a pull request is merged is not yet possible; however, many workarounds can achieve this behaviour https://github.community/t/trigger-workflow-only-on-pull-request-merge/17359/2.
Please comment below if you are still experiencing problems.
Apparently, there still is some difference between the two actions I documented them in https://github.com/rickstaa/action-black/issues/10.
Hello @rickstaa
Thank you for your attention!
Running only when a pull request is merged is not yet possible
- it's not what I want :)
I would like to run a black only when PR has been just opened, not merged.
Sorry if my first description was unclear
@iamtodor Ah I see that is supported. Please see the opened
option under https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request.
on:
pull_request:
types: [opened, reopened]
@rickstaa got you, thanks!
I find this way to set up run jobs only on PR's commit as it described here: https://stackoverflow.com/a/65096459/5151861
Here is my current config:
name: linters
on:
pull_request:
jobs:
flake8-lint:
runs-on: ubuntu-latest
name: linters
steps:
- name: Check out source repository
uses: actions/checkout@v2
- name: Set up Python environment
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: flake8
uses: py-actions/flake8@v2
with:
max-line-length: 120
- name: black
uses: psf/black@stable
with:
src: .
options: --line-length 120
- name: run tests
run: |
pip install pytest pytest-cov
pytest --cov=./ --cov-report=xml
- name: Create Pull Request
if: steps.action_black.outputs.is_formatted == 'true'
uses: peter-evans/create-pull-request@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
title: "Format Python code with psf/black push"
commit-message: ":art: Format Python code with psf/black"
body: |
There appear to be some python formatting errors in ${{ github.sha }}. This pull request
uses the [psf/black](https://github.com/psf/black) formatter to fix these issues.
base: ${{ github.head_ref }} # Creates pull request onto pull request or commit branch
branch: actions/black
But the issue is Create pull request
job has not been run.
Was it because I used psf/black@stable
along with peter-evans/create-pull-request@v3
?
@iamtodor Sorry for the late reply. I have had some hectic weeks. From the output, you can see that the peter-evans/create-pull-request@v3
step is skipped. This is because of the following condition being false:
if: steps.action_black.outputs.is_formatted == 'true'
This is caused by the fact that the psf/black@stable
action does not set the steps.action_black.outputs.is_formatted
variable. If you want to use that variable to check if you should create a pull request, you should use https://github.com/rickstaa/action-black. If you want to use psf/black@stable
, you have to design another if condition. I hope that helps. Let me know if you have any other problems.
@rickstaa Hi, thank you for your reply!
As for this If you want to use that variable to check if you should create a pull request, you should use https://github.com/rickstaa/action-black
previously you mentioned this I would therefore advise you to go for psf/black@stable
in this comment https://github.com/rickstaa/action-black/issues/9#issuecomment-1044116071 so I decided to move on with psf/black@stable
instead of peter-evans/create-pull-request@v3
.
Perhaps I am confused with all that things :D
So I need to change it and everything should work fine?
@iamtodor No problem. I recommended psf/black@stable
since it is better to use the official black action in most cases. The https://github.com/rickstaa/action-black action, however has some features that the official black action does not have (see https://github.com/rickstaa/action-black/issues/10). One of these features is that this action creates an is_formatted
output variable which can be used in successive steps:
Since the official black action does not create this variable, the pull request step in your previous action recipe will always evaluate to false
and be skipped. If you want to use the recipe found on https://github.com/rickstaa/action-black#commit-changes-or-create-a-pull-request you have to use https://github.com/rickstaa/action-black action. If you want to use peter-evans/create-pull-request@v3
with the official black action, you must design a checking condition yourself.
@rickstaa could you please help me with how should I configurepsf/black@stable
, so will it create a new PR?
@iamtodor Sorry for the late reply. I have been very busy finishing my master Thesis. There are a lot of ways to check if black made changes. Something like this should work:
name: linters
on:
pull_request:
jobs:
flake8-lint:
runs-on: ubuntu-latest
name: linters
steps:
- name: Check out source repository
uses: actions/checkout@v2
- name: Set up Python environment
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install black
- name: black
uses: psf/black@stable
with:
src: .
options: --line-length 120
- name: Check for changes
id: action_black
run: |
if [[ $(git status --porcelain | wc -l) -eq 0 ]]; then
echo "::set-output name=is_formatted::false"
else
echo "::set-output name=is_formatted::true"
fi
- name: Check if formatted
run: |
echo ${{needs.action_black.outputs.is_formatted}}
- name: Create Pull Request
if: steps.action_black.outputs.is_formatted == 'true'
uses: peter-evans/create-pull-request@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
title: "Format Python code with psf/black push"
commit-message: ":art: Format Python code with psf/black"
body: |
There appear to be some python formatting errors in ${{ github.sha }}. This pull request
uses the [psf/black](https://github.com/psf/black) formatter to fix these issues.
base: ${{ github.head_ref }} # Creates pull request onto pull request or commit branch
branch: actions/black
You can see it in action here. If that is not what you had in mind, I think it is best to create an issue on psf/black or create a stackoverflow question.
Hello,
The issue
I'm wondering whether there an option to run the black formater not just every single push but rather a PR. The reason I'm asking is the following: while doing my task I can commit up to 20 commits. Once I think the job has been done I'd like to squash all the commits. After the squash, I'd like to make a PR and then this PR shall be formatted.
What I've tried so far
I've tried to have action-black as a separate workflow with the following config:
However in this case, for some reason, black does not find python files:
So the step
Create Pull Request
has not been run.If there any additional details and information has to be provided in order to investigate please let me know :)