name: PR checks

on:
  pull_request:
  merge_group:
    types: [checks_requested]
  workflow_dispatch: # allows manual triggering of the workflow


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

jobs:
  pre-commit-check:
    runs-on: ubuntu-latest
    env:
      SKIP: "mypy"
    steps:
      - uses: actions/checkout@v5
        with:
          lfs: true
      - uses: actions/setup-python@v5
        with:
          python-version: "3.10"
      - name: Set $PY environment variable
        run: echo "PY=$(python -VV | sha256sum | cut -d' ' -f1)" >> $GITHUB_ENV
      - uses: actions/cache@v4
        with:
          path: ~/.cache/pre-commit
          key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
      - uses: pre-commit/action@v3.0.1

  type-check:
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v5
      - uses: astral-sh/setup-uv@v6
        with:
          version: "latest"
      - uses: actions/setup-python@v5
        with:
          python-version: "3.10"
      - run: uv pip install --system ".[types]"
      # Any additional configuration for mypy should be defined in pyproject.toml under the [tool.mypy] section
      - run: |
          mypy

  test-paths-filter:
    runs-on: ubuntu-latest
    outputs:
      hasChanges: ${{ steps.filter.outputs.autogen == 'true' || steps.filter.outputs.test == 'true' || steps.filter.outputs.workflows == 'true' || steps.filter.outputs.setup == 'true' }}
    steps:
      - uses: actions/checkout@v5
      - uses: dorny/paths-filter@v3
        id: filter
        with:
          filters: |
            autogen:
              - "autogen/**"
            test:
              - "test/**"
            workflows:
              - ".github/workflows/**"
            setup:
              - "pyproject.toml"

  core-test-without-llm:
    needs: test-paths-filter
    if: needs.test-paths-filter.outputs.hasChanges == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - uses: astral-sh/setup-uv@v6
        with:
          version: "latest"
      - name: Set up Python 3.11
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"
      - name: Install packages and dependencies
        run: |
          uv pip install --system -e .[test,cosmosdb,interop,redis,websockets,docs]
      - name: Install optional dependencies for code executors
        run: |
          uv pip install --system -e ".[jupyter-executor]"
          python -m ipykernel install --user --name python3
      - name: Test with pytest skipping openai tests
        run: |
          bash scripts/test-core-skip-llm.sh
      - name: Show coverage report
        run: bash scripts/show-coverage-report.sh
      - name: Upload coverage to Codecov
        if: ${{ !contains(github.ref, 'gh-readonly-queue/') }}
        uses: codecov/codecov-action@v5
        with:
          files: ./coverage.xml
          flags: core-without-llm, ubuntu-latest, 3.11
          fail_ci_if_error: true
          token: ${{ secrets.CODECOV_TOKEN }}

  docs-paths-filter:
    runs-on: ubuntu-latest
    outputs:
      hasChanges: ${{ steps.filter.outputs.autogen == 'true' || steps.filter.outputs.website == 'true' || steps.filter.outputs.workflows == 'true' || steps.filter.outputs.setup == 'true' || steps.filter.outputs.scripts == 'true' }}
    steps:
      - uses: actions/checkout@v5
      - uses: dorny/paths-filter@v3
        id: filter
        with:
          filters: |
            autogen:
              - "autogen/**"
            website:
              - "website/**"
            workflows:
              - ".github/workflows/pr-checks.yml"
            setup:
              - "pyproject.toml"
              - ".muffet-excluded-links.txt"
            scripts:
              - "scripts/mkdocs-check-broken-links.sh"
              - "scripts/docs_build_mkdocs.sh"
              - "scripts/docs_serve_mkdocs.sh"

  build-mkdocs:
    needs: docs-paths-filter
    if: needs.docs-paths-filter.outputs.hasChanges == 'true'
    uses: ./.github/workflows/build-mkdocs.yml
    with:
      python-version: "3.10"

  pr-check:
    if: always()
    runs-on: ubuntu-latest
    needs: [pre-commit-check, type-check, core-test-without-llm, build-mkdocs]
    steps:
      - name: Get Date
        shell: bash
        run: |
          echo "date=$(date +'%m/%d/%Y %H:%M:%S')" >> "$GITHUB_ENV"

      - name: Run Type is ${{ github.event_name }}
        if: ${{ github.event_name != 'schedule' && github.event_name != 'workflow_dispatch'}}
        shell: bash
        run: |
          echo "run_type=${{ github.event_name }}" >> "$GITHUB_ENV"

      - name: Fail workflow if build failed
        id: check_build_failed
        if: contains(join(needs.*.result, ','), 'failure')
        uses: actions/github-script@v7
        with:
          script: core.setFailed('Build Failed!')

      - name: Fail workflow if build cancelled
        id: check_build_cancelled
        if: contains(join(needs.*.result, ','), 'cancelled')
        uses: actions/github-script@v7
        with:
          script: core.setFailed('Build Cancelled!')
