Skip to content

Commit

Permalink
Create autoformat.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
KayvanShah1 authored Jan 10, 2025
1 parent 0f2455e commit 3786ef0
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions .github/workflows/autoformat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
name: Autoformat Code on Push

on:
push:
branches:
- main # Adjust the branch accordingly
pull_request:
branches:
- main # Adjust the branch accordingly

permissions:
checks: write
actions: read
contents: write

jobs:
format:
runs-on: ubuntu-latest

env:
commit_message: "No formatting changes applied"

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }} # Use GitHub token to push changes

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12' # Adjust to your Python version

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r dev-requirements.txt
- name: Check import sorting with isort
id: isort-check
run: |
isort --check-only .
continue-on-error: true

- name: Format imports with isort
if: steps.isort-check.outcome == 'failure'
run: |
isort .
- name: Check code formatting with Black
id: black-check
run: |
black --line-length=120 --preview --enable-unstable-feature=string_processing --check .
continue-on-error: true

- name: Format code with Black
if: steps.black-check.outcome == 'failure'
run: |
black --line-length=120 --preview --enable-unstable-feature=string_processing .
- name: Set commit message
id: set-message
run: |
if [[ "${{ steps.isort-check.outcome }}" == "failure" && "${{ steps.black-check.outcome }}" == "failure" ]]; then
echo "commit_message=Sorted imports with isort & Autoformat code with Black" >> $GITHUB_ENV
elif [[ "${{ steps.isort-check.outcome }}" == "failure" ]]; then
echo "commit_message=Sorted imports with isort" >> $GITHUB_ENV
elif [[ "${{ steps.black-check.outcome }}" == "failure" ]]; then
echo "commit_message=Autoformat code with Black" >> $GITHUB_ENV
fi
- name: Commit and push changes if formatting is applied
if: steps.isort-check.outcome == 'failure' || steps.black-check.outcome == 'failure'
run: |
git config --local user.name "github-actions[bot]"
git config --local user.email "github-actions[bot]@users.noreply.github.com"
if [ -n "$(git status --porcelain)" ]; then
git add .
git commit -m "${{ env.commit_message }}"
git push origin ${{ github.ref }}
else
echo "No changes to commit"
fi

0 comments on commit 3786ef0

Please sign in to comment.