rickstaa/action-black

Is there a way to run it only while opening PR?

iamtodor opened this issue · 10 comments

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:

name: black-formation

on: [pull_request]

jobs:
  black-formation:
    runs-on: ubuntu-latest
    name: black-formation
    steps:
      - name: black
        uses: psf/black@stable
        with:
          src: .
          options: --line-length 120
      - 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

However in this case, for some reason, black does not find python files:

Run rickstaa/action-black@v1
  with:
    black_args: . --line-length 120
    fail_on_error: true
/usr/bin/docker run --name a6825b66e474e3b37421db642c4dd6f7b908a_e2747f --label 6a6825 --workdir /github/workspace --rm -e INPUT_BLACK_ARGS -e INPUT_FAIL_ON_ERROR -e HOME -e GITHUB_JOB -e GITHUB_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_REPOSITORY_OWNER -e GITHUB_RUN_ID -e GITHUB_RUN_NUMBER -e GITHUB_RETENTION_DAYS -e GITHUB_RUN_ATTEMPT -e GITHUB_ACTOR -e GITHUB_WORKFLOW -e GITHUB_HEAD_REF -e GITHUB_BASE_REF -e GITHUB_EVENT_NAME -e GITHUB_SERVER_URL -e GITHUB_API_URL -e GITHUB_GRAPHQL_URL -e GITHUB_REF_NAME -e GITHUB_REF_PROTECTED -e GITHUB_REF_TYPE -e GITHUB_WORKSPACE -e GITHUB_ACTION -e GITHUB_EVENT_PATH -e GITHUB_ACTION_REPOSITORY -e GITHUB_ACTION_REF -e GITHUB_PATH -e GITHUB_ENV -e RUNNER_OS -e RUNNER_ARCH -e RUNNER_NAME -e RUNNER_TOOL_CACHE -e RUNNER_TEMP -e RUNNER_WORKSPACE -e ACTIONS_RUNTIME_URL -e ACTIONS_RUNTIME_TOKEN -e ACTIONS_CACHE_URL -e GITHUB_ACTIONS=true -e CI=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/_temp/_runner_file_commands":"/github/file_commands" -v "/home/runner/work/gateio-trading-bot-binance/gateio-trading-bot-binance":"/github/workspace" 6a6825:b66e474e3b37421db642c4dd6f7b908a
[action-black] Formatting python code using the black formatter...
No Python files are present to be formatted. Nothing to do 😴

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 :)

@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 psf/black#1909 and psf/black#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 #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.
image

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 #9 (comment) 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 #10). One of these features is that this action creates an is_formatted output variable which can be used in successive steps:

regex='\s?[0-9]+\sfiles?\sreformatted(\.|,)\s?'
if [[ "${black_output[*]}" =~ $regex ]]; then
echo "::set-output name=is_formatted::true"
else
echo "::set-output name=is_formatted::false"
fi

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.