name: Pre-PR drift check

on:
  pull_request:
    branches: [main]
    paths:
      - "api/**"
      - "ui/**"
      - "pipecat/**"
      - "scripts/dump_docs_openapi.py"
      - "scripts/format.sh"
      - "docs/api-reference/openapi.json"
      - ".github/workflows/pre-pr-drift-check.yml"

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  drift-check:
    if: ${{ !startsWith(github.head_ref, 'release-please--') }}
    runs-on: ubuntu-latest
    timeout-minutes: 25
    steps:
      - name: Checkout repo (with pipecat submodule)
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Set up Python 3.13
        uses: actions/setup-python@v5
        with:
          python-version: "3.13"

      - name: Set up Node 22
        uses: actions/setup-node@v4
        with:
          node-version: "22"
          cache: npm
          cache-dependency-path: ui/package-lock.json

      - name: Create Python virtual environment
        run: |
          python -m venv .venv
          echo "${{ github.workspace }}/.venv/bin" >> "$GITHUB_PATH"

      - name: Install api and pipecat dependencies
        run: ./scripts/setup_requirements.sh --dev

      - name: Install ui dependencies
        working-directory: ui
        run: npm ci

      - name: Run scripts/format.sh
        run: ./scripts/format.sh

      - name: Check for Python format/lint drift
        run: |
          if ! git diff --exit-code api; then
            echo "::error::Python files are not formatted. Run './scripts/format.sh' locally and commit the result before raising the PR."
            exit 1
          fi
          echo "Python format/lint is clean."

      - name: Check for UI lint drift
        run: |
          if ! git diff --exit-code ui; then
            echo "::error::UI files have outstanding lint fixes. Run 'npm run fix-lint' inside ui/ locally and commit the result before raising the PR."
            exit 1
          fi
          echo "UI lint is clean."

      - name: Dump OpenAPI spec
        env:
          DATABASE_URL: postgresql+asyncpg://dummy:dummy@localhost/dummy
          REDIS_URL: redis://localhost:6379/0
          ENABLE_AWS_S3: "false"
          MINIO_PUBLIC_ENDPOINT: http://localhost:9000
          DEPLOYMENT_MODE: oss
        run: python -u -m scripts.dump_docs_openapi

      - name: Check for OpenAPI drift
        run: |
          if ! git diff --exit-code docs/api-reference/openapi.json; then
            echo "::error::docs/api-reference/openapi.json is out of sync with the FastAPI app."
            echo "Run 'python -m scripts.dump_docs_openapi' locally (with api/.env sourced) and commit the result."
            exit 1
          fi
          echo "OpenAPI spec is in sync."

      - name: Send Slack notification - Failure
        if: failure()
        uses: slackapi/slack-github-action@v1.26.0
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
        with:
          payload: |
            {
              "text": "❌ Dograh pre-PR drift check failed on ${{ github.ref_name }} by ${{ github.actor }} - <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View Logs>"
            }
