name: black-format-run
on: [push, workflow_dispatch]

jobs:
  format_check:
    name: runner / black formatter check
    runs-on: ubuntu-latest
    outputs:
      step_result: ${{ steps.check.outputs.result }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: 3.8
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install black
      - name: Check code formatting using black
        id: check
        run: |
          # Your existing black check steps
          if black . --check --exclude docs; then
            echo "::set-output name=result::success"
          else
            echo "::set-output name=result::failure"
          fi

  format_run:
    name: runner / black formatter run
    runs-on: ubuntu-latest
    needs: format_check
    if: ${{ needs.format_check.outputs.step_result == 'failure' }}
    steps:
      - uses: actions/checkout@v2
      - name: Check files using the black formatter
        uses: rickstaa/action-black@v1
        id: action_black
        with:
          black_args: "."
      - name: Push reformatted files
        uses: EndBug/add-and-commit@v9.0.1
        with:
          add: '*'
          message: 'black-format bot reformatting.'
          push: true


