name: Black
on: [push, pull_request]
jobs:
  Check:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v3
      - name: Setup dependencies
        run: pip install black
      - name: Run Black
        run: black --check .
  Fix:
    needs: Check
    if: ${{ !success() || cancelled() }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          token: ${{ secrets.SECRET_TOKEN }}
      - name: Set up Python
        uses: actions/setup-python@v3
      - name: Setup dependencies
        run: pip install black
      - name: Blacken python files
        run: black .
      - name: Commit & Push
        run: |
          git config user.name github-actions
          git config user.email github-actions@github.com
          git commit --all -m "[bot] autoformat python files using black"
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v3
        with:
          token: ${{ secrets.SECRET_TOKEN }}
          branch: black/blacken-${{ github.ref }}
          delete-branch: true
          title: "Python files autoformatted using black"
          body: "Some formatting changes are needed"

