---
name: Linting and Testing Anta
on:
  push:
    branches:
      - main
  pull_request:

jobs:
  file-changes:
    runs-on: ubuntu-latest
    outputs:
      code: ${{ steps.filter.outputs.code }}
      docs: ${{ steps.filter.outputs.docs }}
    steps:
      - uses: actions/checkout@v4
      - uses: dorny/paths-filter@v3
        id: filter
        with:
          filters: |
            code:
              - 'anta/*'
              - 'anta/**'
              - 'tests/*'
              - 'tests/**'
              # detect dependency changes
              - 'pyproject.toml'
            core:
              - 'anta/*'
              - 'anta/reporter/*'
              - 'anta/result_manager/*'
              - 'anta/tools/*'
            cli:
              - 'anta/cli/*'
              - 'anta/cli/**'
            tests:
              - 'anta/tests/*'
              - 'anta/tests/**'
            docs:
              - '.github/workflows/pull-request-management.yml'
              - 'mkdocs.yml'
              - 'docs/*'
              - 'docs/**'
              - 'README.md'
  check-requirements:
    runs-on: ubuntu-20.04
    strategy:
      matrix:
        python-version: ["3.9", "3.10", "3.11", "3.12"]
    needs: file-changes
    steps:
      - uses: actions/checkout@v4
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      - name: install requirements
        run: |
          pip install .
      - name: install dev requirements
        run: pip install .[dev]
  # @gmuloc: commenting this out for now
  #missing-documentation:
  #  name: "Warning documentation is missing"
  #  runs-on: ubuntu-20.04
  #  needs: [file-changes]
  #  if: needs.file-changes.outputs.cli == 'true' && needs.file-changes.outputs.docs == 'false'
  #  steps:
  #    - name: Documentation is missing
  #      uses: GrantBirki/comment@v2.0.10
  #      with:
  #        body: |
  #          Please consider that documentation is missing under `docs/` folder.
  #          You should update documentation to reflect your change, or maybe not :)
  lint-python:
    name: Check the code style
    runs-on: ubuntu-20.04
    needs: file-changes
    if: needs.file-changes.outputs.code == 'true'
    steps:
      - uses: actions/checkout@v4
      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"
      - name: Install dependencies
        run: pip install tox
      - name: "Run tox linting environment"
        run: tox -e lint
  type-python:
    name: Check typing
    runs-on: ubuntu-20.04
    needs: file-changes
    if: needs.file-changes.outputs.code == 'true'
    steps:
      - uses: actions/checkout@v4
      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"
      - name: Install dependencies
        run: pip install tox
      - name: "Run tox typing environment"
        run: tox -e type
  test-python:
    name: Pytest across all supported python versions
    runs-on: ubuntu-20.04
    needs: [lint-python, type-python]
    strategy:
      matrix:
        python: ["3.9", "3.10", "3.11", "3.12"]
    steps:
      - uses: actions/checkout@v4
      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python }}
      - name: Install dependencies
        run: pip install tox tox-gh-actions
      - name: "Run pytest via tox for ${{ matrix.python }}"
        run: tox
  test-documentation:
    name: Build offline documentation for testing
    runs-on: ubuntu-20.04
    needs: [test-python]
    steps:
      - uses: actions/checkout@v4
      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"
      - name: Install dependencies
        run: pip install .[doc]
      - name: "Build mkdocs documentation offline"
        run: mkdocs build
  benchmarks:
    name: Benchmark ANTA for Python 3.12
    runs-on: ubuntu-latest
    needs: [test-python]
    steps:
      - uses: actions/checkout@v4
      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"
      - name: Install dependencies
        run: pip install .[dev]
      - name: Run benchmarks
        uses: CodSpeedHQ/action@v3
        with:
          token: ${{ secrets.CODSPEED_TOKEN }}
          run: pytest --codspeed --no-cov --log-cli-level INFO tests/benchmark