name: CI

on:
  pull_request:
  push:
    branches: [main]

concurrency:
  group: ci-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  prek:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v6
      - uses: astral-sh/setup-uv@v8.0.0
        with:
          python-version: "3.12"
          enable-cache: true
      - run: uv sync --all-packages
      - uses: j178/prek-action@v2

  changes:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: read
    outputs:
      packages: ${{ steps.filter.outputs.changes }}
    steps:
      - uses: actions/checkout@v6
      - uses: dorny/paths-filter@v3
        id: filter
        with:
          filters: |
            codeflash-core:
              - 'packages/codeflash-core/**'
            codeflash-python:
              - 'packages/codeflash-core/**'
              - 'packages/codeflash-python/**'
            codeflash-api:
              - 'packages/codeflash-api/**'
            blackbox:
              - 'packages/blackbox/**'
            github-app:
              - 'packages/github-app/**'

  test:
    needs: changes
    if: >-
      github.event_name == 'push'
      || needs.changes.outputs.packages != '[]'
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v6
      - uses: astral-sh/setup-uv@v8.0.0
        with:
          python-version: "3.12"
          enable-cache: true
      - run: uv sync --all-packages
      - name: Test changed packages
        run: |
          if [ "${{ github.event_name }}" = "push" ]; then
            uv run pytest packages/ -v --ignore=packages/blackbox/tests/e2e
          else
            CHANGED='${{ needs.changes.outputs.packages }}'
            for pkg in $(echo "$CHANGED" | jq -r '.[]'); do
              echo "::group::Testing $pkg"
              uv run pytest "packages/$pkg" -v --ignore="packages/$pkg/tests/e2e"
              echo "::endgroup::"
            done
          fi
        env:
          CI: "true"

  e2e-blackbox:
    needs: changes
    if: >-
      github.event_name == 'push'
      || contains(fromJSON(needs.changes.outputs.packages), 'blackbox')
    runs-on: ubuntu-latest
    permissions:
      contents: read
    strategy:
      fail-fast: false
      matrix:
        browser: [chromium, firefox, webkit]
    steps:
      - uses: actions/checkout@v6
      - uses: astral-sh/setup-uv@v8.0.0
        with:
          python-version: "3.12"
          enable-cache: true
      - run: uv sync --package blackbox
      - name: Install Playwright browsers
        run: uv run playwright install --with-deps ${{ matrix.browser }}
      - name: Run E2E tests (${{ matrix.browser }})
        run: uv run pytest packages/blackbox/tests/e2e/ -v --browser ${{ matrix.browser }} --tracing=retain-on-failure
      - name: Upload Playwright traces
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: playwright-traces-${{ matrix.browser }}
          path: test-results/
          retention-days: 7

  version:
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
      - uses: astral-sh/setup-uv@v8.0.0
        with:
          python-version: "3.12"
          enable-cache: true
      - run: uv sync --all-packages
      - name: Check version bump
        run: uv run python scripts/versioning.py check-version
