name: Pre-commit on: push: branches: - main - develop paths: - ".github/workflows/**" - "scripts/**" - "images/**" - "apps.json" pull_request: branches: - main - develop workflow_dispatch: {} permissions: contents: write jobs: autofix: if: ${{ !contains(github.event.pull_request.title, '[pre-commit-fix]') && !contains(github.event.pull_request.body, '[pre-commit-fix]') }} runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 with: token: ${{ secrets.GITHUB_TOKEN }} - name: Set up Python uses: actions/setup-python@v5 with: python-version: "3.10.6" - name: Install dependencies run: | pip install pre-commit sudo apt-get install -y jq - name: Cache pre-commit uses: actions/cache@v4 with: path: ~/.cache/pre-commit key: pre-commit-${{ runner.os }}-${{ hashFiles('.pre-commit-config.yaml') }} restore-keys: | pre-commit-${{ runner.os }}- - name: Run pre-commit autofix id: precommit run: | pre-commit run --from-ref origin/main --to-ref HEAD - name: Push auto-fixes back to PR if: steps.precommit.outcome == 'failure' run: | git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" git add . map_file="msg-map.json" default_message="chore: [pre-commit-fix] Apply pre-commit auto-fixes" message="$default_message" changed_files=$(git diff --cached --name-only) for file in $changed_files; do ext=".$(echo "$file" | awk -F. '{print $NF}')" mapped_message=$(jq -r --arg ext "$ext" '.[$ext] // empty' "$map_file") if [ -n "$mapped_message" ]; then message="$mapped_message" break fi done git commit -m "$message" git push