name: Format Code # This workflow automatically formats code to match project standards # It runs on pull requests and pushes to main/develop branches # If it finds formatting issues on a push, it creates a PR to fix them on: pull_request: branches: [main, develop] push: branches: [main, develop] workflow_dispatch: # Permissions needed to write changes back to the repo permissions: contents: write pull-requests: write jobs: format: name: Format Code runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 - name: Set up Python uses: actions/setup-python@v5 with: python-version: '3.11' - name: Install dependencies run: | python -m pip install --upgrade pip pip install black isort - name: Check formatting # On PRs, just check and report issues (don't auto-fix) if: github.event_name == 'pull_request' run: | black --check semantica/ || echo "⚠️ Code formatting issues found. Run: black semantica/" isort --check-only semantica/ || echo "⚠️ Import sorting issues found. Run: isort semantica/" continue-on-error: true - name: Format with black # On pushes, actually run the formatter if: github.event_name != 'pull_request' run: black semantica/ - name: Sort imports with isort # On pushes, actually run the import sorter if: github.event_name != 'pull_request' run: isort semantica/ - name: Check for changes # See if the formatters changed any files if: github.event_name != 'pull_request' id: verify-changed-files run: | if [ -n "$(git status --porcelain)" ]; then echo "changed=true" >> $GITHUB_OUTPUT else echo "changed=false" >> $GITHUB_OUTPUT fi - name: Create Pull Request # If files changed, create a PR with the fixes if: github.event_name != 'pull_request' && steps.verify-changed-files.outputs.changed == 'true' uses: peter-evans/create-pull-request@v5 with: token: ${{ secrets.GITHUB_TOKEN }} commit-message: "style: Auto-format code with black and isort" title: "Auto-format code" body: | This PR automatically formats code using black and isort. **Changes:** - Code formatted with black - Imports sorted with isort branch: auto-format-code delete-branch: true