|
| 1 | +name: Pre-Commit Checks |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + - develop |
| 8 | + - 'feat/**' |
| 9 | + - 'feature/**' |
| 10 | + - 'test/**' |
| 11 | + - 'chore/**' |
| 12 | + - 'fix/**' |
| 13 | + - 'hotfix/**' |
| 14 | + - 'docs/**' |
| 15 | + pull_request: |
| 16 | + types: [opened, synchronize, reopened] |
| 17 | + |
| 18 | +jobs: |
| 19 | + precommit: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + |
| 22 | + steps: |
| 23 | + - name: Checkout Repository |
| 24 | + uses: actions/checkout@v4 |
| 25 | + |
| 26 | + - name: Set up Python |
| 27 | + uses: actions/setup-python@v5 |
| 28 | + with: |
| 29 | + python-version: "3.11" |
| 30 | + |
| 31 | + - name: Create and activate Python venv |
| 32 | + run: | |
| 33 | + python -m venv .venv |
| 34 | + source .venv/bin/activate |
| 35 | + python -m pip install --upgrade pip |
| 36 | + pip install pre-commit |
| 37 | + shell: bash |
| 38 | + |
| 39 | + # Optional: add venv to PATH for subsequent steps |
| 40 | + - name: Add venv to PATH |
| 41 | + run: echo "${{ github.workspace }}/.venv/bin" >> $GITHUB_PATH |
| 42 | + |
| 43 | + - name: Load Pre-commit Config |
| 44 | + run: | |
| 45 | + if [ ! -f ".pre-commit-config.yaml" ]; then |
| 46 | + echo " No .pre-commit-config.yaml found — downloading BerryBytes global config..." |
| 47 | + curl -sSL \ |
| 48 | + https://raw.githubusercontent.com/BerryBytes/precommit-util/main/global/precommitFile/.pre-commit-config.yaml \ |
| 49 | + -o .pre-commit-config.yaml |
| 50 | + else |
| 51 | + echo "✔ Using project's existing .pre-commit-config.yaml" |
| 52 | + fi |
| 53 | + shell: bash |
| 54 | + |
| 55 | + - name: Inject temporary Stylelint config for CI |
| 56 | + run: | |
| 57 | + if [ ! -f ".stylelintrc.json" ]; then |
| 58 | + echo " Creating temporary .stylelintrc.json for CI..." |
| 59 | + cat <<EOF > .stylelintrc.json |
| 60 | + { |
| 61 | + "extends": "stylelint-config-standard", |
| 62 | + "rules": { |
| 63 | + "no-duplicate-selectors": true, |
| 64 | + "color-hex-length": "short", |
| 65 | + "selector-no-qualifying-type": true, |
| 66 | + "selector-max-id": 0 |
| 67 | + } |
| 68 | + } |
| 69 | + EOF |
| 70 | + else |
| 71 | + echo "✔ .stylelintrc.json already exists — skipping" |
| 72 | + fi |
| 73 | + shell: bash |
| 74 | + # -------------------------------------------------------------------- |
| 75 | + # STEP 1: Run pre-commit (capture full logs and exit code safely) |
| 76 | + # -------------------------------------------------------------------- |
| 77 | + - name: Run pre-commit (full logs) |
| 78 | + id: runprecommit |
| 79 | + run: | |
| 80 | + source .venv/bin/activate |
| 81 | + echo "🔍 Running full pre-commit checks..." |
| 82 | +
|
| 83 | + set +e # allow failure |
| 84 | + pre-commit run --all-files --verbose --show-diff-on-failure --color never \ |
| 85 | + | tee full_precommit.log |
| 86 | + exit_code=${PIPESTATUS[0]} |
| 87 | +
|
| 88 | + echo "Pre-commit exit code: $exit_code" |
| 89 | + echo "$exit_code" > precommit_exit_code.txt |
| 90 | + shell: bash |
| 91 | + |
| 92 | + # -------------------------------------------------------------------- |
| 93 | + # STEP 2: Summary of FAILED hooks |
| 94 | + # -------------------------------------------------------------------- |
| 95 | + - name: Pre-commit summary of failed hooks |
| 96 | + run: | |
| 97 | + source .venv/bin/activate |
| 98 | + echo "=====================================================" |
| 99 | + echo " PRE-COMMIT SUMMARY" |
| 100 | + echo "=====================================================" |
| 101 | +
|
| 102 | + exit_code=$(cat precommit_exit_code.txt) |
| 103 | +
|
| 104 | + if [ "$exit_code" = "0" ]; then |
| 105 | + echo " All hooks passed!" |
| 106 | + exit 0 |
| 107 | + fi |
| 108 | +
|
| 109 | + echo " Hooks failed — showing summary:" |
| 110 | + echo "" |
| 111 | +
|
| 112 | + echo " FAILED HOOKS:" |
| 113 | + grep -E "^\w.*\.{3,}Failed" full_precommit.log || echo " None" |
| 114 | + echo "-----------------------------------------------------" |
| 115 | +
|
| 116 | + echo " FILES WITH ISSUES:" |
| 117 | + grep -E "files were modified by this hook" -A3 full_precommit.log \ |
| 118 | + | sed 's/^/ - /' || echo " None" |
| 119 | + echo "-----------------------------------------------------" |
| 120 | +
|
| 121 | + echo " ERROR DETAILS:" |
| 122 | + grep -Ei "(error|failed|violation|missing|line too long|could not|warning)" full_precommit.log \ |
| 123 | + | grep -Ev "^(---|\+\+\+|@@|diff --git|index )" \ |
| 124 | + | sed 's/^/ • /' || echo " None" |
| 125 | + echo "-----------------------------------------------------" |
| 126 | +
|
| 127 | + exit $exit_code |
| 128 | + shell: bash |
0 commit comments