Fix Code Style on PR workflow (intel/neural-compressor)
The Fix Code Style on PR workflow from intel/neural-compressor, explained and optimized by Latchkey.
CI health: A - excellent
Run this on Latchkey for self-healing, caching, and up to 58% lower cost.
Grade your own workflow free or run it on Latchkey →What it does
This is the Fix Code Style on PR workflow from the intel/neural-compressor repository, a real project running GitHub Actions. It is shown here with attribution under its Apache-2.0 license.
Below, Latchkey shows a faster, safer version produced by its optimization engine.
The workflow
# Copyright (C) 2025 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
name: Fix Code Style on PR
on:
pull_request:
branches: [main]
types: [opened, reopened, ready_for_review, synchronize]
# If there is a new commit, the previous jobs will be canceled
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
permissions:
contents: read
jobs:
scan-and-fix:
if: ${{ contains(github.repository, 'intel-innersource') }}
runs-on: ${{ contains(github.repository, 'intel-innersource') && 'self-hosted' || 'ubuntu-latest' }}
permissions:
pull-requests: write
contents: write
steps:
- name: Clean Up Working Directory
run: sudo rm -rf ${{github.workspace}}/*
- name: Checkout Repo
uses: actions/checkout@1e31de5234b9f8995739874a8ce0492dc87873e2
with:
fetch-depth: 0
ref: ${{ github.head_ref }}
- name: Fix Code Style
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Create a virtual environment
python -m venv venv
source venv/bin/activate
# Upgrade pip and install pre-commit
pip install --upgrade pip
pip install -U pre-commit
# Run pre-commit hooks to fix auto-fixable issues
pre-commit install
pre-commit run --all-files | tee pre-commit.log 2>&1 || true
# Check if there are changes after pre-commit run
if [ -n "$(git status --porcelain)" ]; then
# Commit and push the changes if there are any
git config user.name "GitHub Action"
git config user.email "action@github.com"
git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/${{ github.repository }}.git
git add -A
git commit -n -s -m "chore: apply pre-commit auto-fixes"
branch=$(git rev-parse --abbrev-ref HEAD)
git push origin HEAD:${branch}
else
# If no changes are made, print a message
echo "No auto-fixable changes were made by pre-commit."
fi
sleep 1s
if [ $(grep -c "Failed" pre-commit.log) != 0 ]; then
echo "Some checks failed that require manual intervention. Please review the pre-commit.log for details."
exit 1
fi
The same workflow, on Latchkey
Removes redundant runs and caps runaway jobs. Added and changed lines are highlighted.
# Copyright (C) 2025 Intel Corporation # SPDX-License-Identifier: Apache-2.0 name: Fix Code Style on PR on: pull_request: branches: [main] types: [opened, reopened, ready_for_review, synchronize] # If there is a new commit, the previous jobs will be canceled concurrency: group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} cancel-in-progress: true permissions: contents: read jobs: scan-and-fix: timeout-minutes: 30 if: ${{ contains(github.repository, 'intel-innersource') }} runs-on: ${{ contains(github.repository, 'intel-innersource') && 'self-hosted' || 'ubuntu-latest' }} permissions: pull-requests: write contents: write steps: - name: Clean Up Working Directory run: sudo rm -rf ${{github.workspace}}/* - name: Checkout Repo uses: actions/checkout@1e31de5234b9f8995739874a8ce0492dc87873e2 with: fetch-depth: 0 ref: ${{ github.head_ref }} - name: Fix Code Style env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | # Create a virtual environment python -m venv venv source venv/bin/activate # Upgrade pip and install pre-commit pip install --upgrade pip pip install -U pre-commit # Run pre-commit hooks to fix auto-fixable issues pre-commit install pre-commit run --all-files | tee pre-commit.log 2>&1 || true # Check if there are changes after pre-commit run if [ -n "$(git status --porcelain)" ]; then # Commit and push the changes if there are any git config user.name "GitHub Action" git config user.email "action@github.com" git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/${{ github.repository }}.git git add -A git commit -n -s -m "chore: apply pre-commit auto-fixes" branch=$(git rev-parse --abbrev-ref HEAD) git push origin HEAD:${branch} else # If no changes are made, print a message echo "No auto-fixable changes were made by pre-commit." fi sleep 1s if [ $(grep -c "Failed" pre-commit.log) != 0 ]; then echo "Some checks failed that require manual intervention. Please review the pre-commit.log for details." exit 1 fi
What changed
- Add a job timeout so a hung step cannot burn hours of runner time.
What Latchkey heals here
This workflow has steps that commonly fail on transient issues (network, registries, flaky browsers). On Latchkey managed runners they are detected, retried, and self-healed instead of failing your build:
- Dependency installs
This workflow runs 1 job per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.