Creating github action for black formatting #8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Auto Black Formatter | |
| on: | |
| pull_request: | |
| paths: | |
| - "**.py" | |
| jobs: | |
| black-format: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: "3.11" | |
| - name: Install Black | |
| run: pip install black | |
| - name: Run Black | |
| run: black . --check --diff || black . | |
| - name: Commit formatted code | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| BRANCH_NAME=black-formatting-pr-${{ github.event.pull_request.number }} | |
| git checkout -b "$BRANCH_NAME" || git checkout "$BRANCH_NAME" | |
| git add . | |
| git commit -m "Apply Black code formatting" || echo "No changes to commit" | |
| git push --set-upstream origin "$BRANCH_NAME" | |
| git checkout ${{ github.head_ref }} | |
| - name: Push and create PR | |
| uses: peter-evans/create-pull-request@v5 | |
| with: | |
| commit-message: Apply Black code formatting | |
| branch: black-formatting-pr-${{ github.event.pull_request.number }} | |
| title: "Format code with Black" | |
| body: "This PR auto-formats the code using [Black](https://github.com/psf/black)." | |
| base: ${{ github.head_ref }} |