---
# Homelab CI — Python lane (git-ci-01) + secret scan (git-ci-02)
# Skip: @skipci in branch name or commit message
name: CI

on:
  push:
    branches: [master, main]
  pull_request:
    types: [opened, synchronize, reopened]

jobs:
  skip-ci-check:
    runs-on: [homelab, self-hosted, linux]
    outputs:
      should-skip: ${{ steps.check.outputs.skip }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 1
      - id: check
        run: |
          SKIP=0
          BRANCH="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
          MSG="${GITHUB_EVENT_HEAD_COMMIT_MESSAGE:-$(git log -1 --pretty=%B 2>/dev/null || true)}"
          echo "$BRANCH" "$MSG" | grep -qi '@skipci' && SKIP=1
          echo "skip=$SKIP" >> $GITHUB_OUTPUT

  python-ci:
    needs: skip-ci-check
    if: needs.skip-ci-check.outputs.should-skip != '1'
    runs-on: [homelab, self-hosted, linux, python]
    steps:
      - uses: actions/checkout@v4

      - name: Install Python tooling
        run: |
          python3 -m pip install --upgrade pip
          if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
          if [ -f requirements-dev.txt ]; then pip install -r requirements-dev.txt; fi
          pip install bandit pip-audit ruff

      - name: Ruff lint
        run: ruff check . || true

      - name: Bandit (advisory)
        run: bandit -r . -q || true

      - name: pip-audit (advisory)
        run: pip-audit -r requirements.txt 2>/dev/null || pip-audit 2>/dev/null || true

      - name: Pytest
        run: |
          if [ -d tests ] || ls test_*.py *_test.py 2>/dev/null; then
            pip install pytest
            pytest -q
          else
            echo "No tests found — skip"
          fi

  secret-scan:
    needs: skip-ci-check
    if: needs.skip-ci-check.outputs.should-skip != '1'
    runs-on: [homelab, self-hosted, linux, heavy]
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Gitleaks
        run: |
          docker run --rm -v "$PWD:/repo" ghcr.io/gitleaks/gitleaks:latest \
            detect --source /repo --no-banner --redact
