name: Static checks

on:
  push:
    branches:
      - '**'
# running on push and PR means tests run twice. Removing PR can be added back once workflow is more advanced

permissions:
  contents: write
  checks: write
  pull-requests: write

jobs:
  backend-lint:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
      with:
        ref: ${{ github.head_ref || github.ref }}
    - name: Set up Python 3.9
      uses: actions/setup-python@v5
      with:
        python-version: "3.9"
        cache: "pip"
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.dev.txt
    - name: Run step on all python files
      run: |
        echo "Ruff check on all files"
        make ruff

  backend-mypy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
      with:
        ref: ${{ github.head_ref || github.ref }}
    - name: Set up Python 3.9
      uses: actions/setup-python@v5
      with:
        python-version: "3.9"
        cache: "pip"
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.dev.txt
    - name: Run mypy
      run: |
        echo "Check typing with mypy"
        make mypy

  backend-unit-tests:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
      with:
        ref: ${{ github.head_ref || github.ref }}
    - name: Set up Python 3.9
      uses: actions/setup-python@v5
      with:
        python-version: "3.9"
        cache: "pip"
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.github.txt
        pip install -r tests/python/unit/requirements.txt
    - name: Copy folders to common
      run: |
          mkdir common
          cp -r backend common/
          cp -r llm_assist common/
          cp -r solutions common/
    - name: Set PYTHONPATH
      run: echo "PYTHONPATH=$GITHUB_WORKSPACE" >> $GITHUB_ENV
    - name: Run unit tests
      run: |
        set -o pipefail
        pytest --alluredir=tests/allure_report --doctest-modules \
               --junitxml=junit/test-results.xml \
               --cov=common/backend --cov=common/solutions --cov=common/llm_assist tests/python/unit | tee pytest-coverage.txt
    - name: Delete common folder
      run: |
        rm -rf common