name: View conventions

# Commit-time guard parity for CI: the same rule engine that the local
# .githooks/pre-commit runs, re-run here so a `git commit --no-verify` bypass is
# still caught on push / pull request. Error-severity rules (use-braces,
# use-printAttrValue) fail the job; advisory rules are printed only.

on:
  pull_request:
    paths:
      - "public/local/**/*.php"
      - ".claude/conventions/views.json"
      - ".claude/hooks/view-conventions-check.py"
      - ".github/workflows/view-conventions.yml"
  push:
    branches: [main, master]
    paths:
      - "public/local/**/*.php"
      - ".claude/conventions/views.json"
      - ".claude/hooks/view-conventions-check.py"

jobs:
  view-conventions:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0 # need history to diff against the base ref

      - uses: actions/setup-python@v5
        with:
          python-version: "3.x"

      - name: Run the view-conventions checker on changed PHP files
        run: |
          set -euo pipefail
          if [ -n "${{ github.event.pull_request.base.sha }}" ]; then
            base="${{ github.event.pull_request.base.sha }}"
          else
            base="$(git rev-parse HEAD~1 2>/dev/null || git rev-parse HEAD)"
          fi
          echo "Diffing $base...HEAD"
          mapfile -t files < <(git diff --name-only --diff-filter=ACM "$base"...HEAD -- '*.php')
          if [ "${#files[@]}" -eq 0 ]; then
            echo "No changed PHP files — nothing to check."
            exit 0
          fi
          printf '  %s\n' "${files[@]}"
          python3 .claude/hooks/view-conventions-check.py --files "${files[@]}"
