-
Notifications
You must be signed in to change notification settings - Fork 4
48 lines (40 loc) · 1.42 KB
/
black-format.yml
File metadata and controls
48 lines (40 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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 }}