Merge pull request #333 from smash-transport/roch/fix_formatter_WF2 #89
This file contains 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: Code Formatting Check and Fix | ||
on: | ||
push: | ||
branches: | ||
- main | ||
- sparkx_devel | ||
jobs: | ||
code-formatting: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v2 | ||
- name: Set up Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: '3.9' | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install black==24.8.0 | ||
- name: Check code formatting | ||
id: black-check | ||
run: | | ||
black --check --line-length 80 src/sparkx tests/ | ||
continue-on-error: true | ||
- name: Format code (if needed) | ||
if: steps.black-check.outcome == 'failure' | ||
run: | | ||
black --line-length 80 src/sparkx tests/ | ||
- name: Commit changes (if needed) | ||
if: steps.black-check.outcome == 'failure' | ||
run: | | ||
git config --local user.name "GitHub Action" | ||
git config --local user.email "[email protected]" | ||
git checkout -b format-fixes | ||
git add src/sparkx tests/ | ||
git commit -m "Automatically format code using black" || echo "No changes to commit" | ||
- name: Push changes to new branch | ||
if: steps.black-check.outcome == 'failure' | ||
run: | | ||
git push origin format-fixes | ||
- name: Create and approve a pull request | ||
if: steps.black-check.outcome == 'failure' | ||
uses: peter-evans/create-pull-request@v5 | ||
id: create-pull-request | ||
with: | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
branch: format-fixes | ||
base: ${{ github.ref_name }} | ||
title: "Code Formatting Fixes" | ||
body: | | ||
This pull request contains automatic formatting fixes using Black. | ||
auto-approve: true | ||
- name: Debug PR number | ||
run: echo "PR number: ${{ steps.create-pull-request.outputs.pull-request-number }}" | ||
if: steps.black-check.outcome == 'failure' | ||
- name: Enable auto-merge | ||
if: steps.black-check.outcome == 'failure' | ||
uses: peter-evans/enable-pull-request-automerge@v2 | ||
with: | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
pull-request-number: ${{ steps.create-pull-request.outputs.pull-request-number }} | ||
merge-method: squash | ||
- name: Delete format-fixes branch | ||
if: always() | ||
run: | | ||
curl -X DELETE \ | ||
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ | ||
-H "Accept: application/vnd.github.v3+json" \ | ||
https://api.github.com/repos/${{ github.repository }}/git/refs/heads/format-fixes || echo "Branch not found, skipping deletion." |