# Gitea Actions — Red → Green 게이트.
# 피드백 #6: "Git을 이용해서 개발 cycle (Red→Green)이 완료되면 Git(s-canvas)에 자동
#             업로드 프로세스 구축 (ruff, pytest 등을 적극 활용)"
#
# 트리거: push 모든 브랜치, PR. main 브랜치는 추가 보호.
#
# Stage 구성:
#   1. ruff (린트) — 30초 미만
#   2. py_compile (전체 syntax) — 10초 미만
#   3. pytest (회귀 + 빠른 단위) — 1~2분
#   4. (선택) coverage report
#
# 모두 통과 = Green. main 브랜치는 Green 통과 시에만 push 허용 (Gitea repo 설정).

name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  lint-and-test:
    name: Ruff + Test (Py3.11 + Py3.13)
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.11", "3.13"]

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup uv (fast Python pkg manager)
        uses: astral-sh/setup-uv@v3
        with:
          enable-cache: true
          cache-dependency-glob: "uv.lock"

      - name: Install deps
        shell: bash
        run: |
          set -e
          if [ "${{ matrix.python-version }}" = "3.13" ]; then
            EXTRAS=".[py313,dev]"
          else
            EXTRAS=".[dev]"
          fi
          # uv 가 자동으로 Py 버전 fetch + .venv 생성. 별도 `uv python install`
          # step 불필요 (uv venv 가 내부적으로 처리).
          uv venv .venv --python ${{ matrix.python-version }}
          source .venv/bin/activate
          uv pip install -e "$EXTRAS"

      - name: Ruff lint
        shell: bash
        run: |
          source .venv/bin/activate
          ruff check --output-format=github

      - name: py_compile (전체 .py)
        shell: bash
        run: |
          source .venv/bin/activate
          python -c "
          import py_compile, pathlib, sys
          errs = []
          for p in sorted(pathlib.Path('.').rglob('*.py')):
              if any(s in str(p) for s in ('venv', '_unused', '__pycache__', '.bak')):
                  continue
              try:
                  py_compile.compile(str(p), doraise=True)
              except py_compile.PyCompileError as e:
                  errs.append((p, str(e)[:200]))
          for p, m in errs:
              print(f'::error file={p}::{m}')
          sys.exit(1 if errs else 0)
          "

      - name: pytest (회귀)
        shell: bash
        run: |
          source .venv/bin/activate
          pytest -ra --tb=short -m "not slow and not integration"

      - name: pytest (slow + integration, allow failure)
        if: ${{ matrix.python-version == '3.13' }}
        continue-on-error: true
        shell: bash
        run: |
          source .venv/bin/activate
          pytest -ra --tb=short -m "slow or integration"
