Skip to content

Creating github action for black formatting #4

Creating github action for black formatting

Creating github action for black formatting #4

Workflow file for this run

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"
- 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.ref_name }}