# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the master branch
  push:
    branches: [ master ]
  pull_request:
    branches: [ '**' ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  tests:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    strategy:
      matrix:
        DJANGO_VERSION: ['4.2.*', '5.0.*', '5.1.*', '5.2.*', '6.0.*']
        DB_ENGINE: [ 'postgres', 'mysql' ]
        python-version: ['3.8', '3.9', '3.10', '3.11', '3.12', '3.13']
        exclude:
          - DJANGO_VERSION: '5.0.*'
            python-version: '3.8'
          - DJANGO_VERSION: '5.0.*'
            python-version: '3.9'
          - DJANGO_VERSION: '5.0.*'
            python-version: '3.13'

          - DJANGO_VERSION: '5.1.*'
            python-version: '3.8'
          - DJANGO_VERSION: '5.1.*'
            python-version: '3.9'
          - DJANGO_VERSION: '5.2.*'
            python-version: '3.8'
          - DJANGO_VERSION: '5.2.*'
            python-version: '3.9'
          - DJANGO_VERSION: '4.2.*'
            python-version: '3.13'
          - DJANGO_VERSION: '6.0.*'
            python-version: '3.8'
          - DJANGO_VERSION: '6.0.*'
            python-version: '3.9'
          - DJANGO_VERSION: '6.0.*'
            python-version: '3.10'
          - DJANGO_VERSION: '6.0.*'
            python-version: '3.11'
      fail-fast: false

    services:
      postgres:
        image: postgres
        env:
          POSTGRES_PASSWORD: postgres
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432
      mysql:
        image: mysql:8.3
        env:
          MYSQL_DATABASE: test_db
          MYSQL_USER: db_user
          MYSQL_PASSWORD: mysql
          MYSQL_ROOT_PASSWORD: rootpassword
        options: >-
          --health-cmd "mysqladmin ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 3306:3306

    steps:
      - uses: actions/checkout@v2

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
              python-version: ${{ matrix.python-version }}

      - uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ hashFiles('setup.py') }}-${{ hashFiles('demoproject/requirements.txt') }}-${{ hashFiles('requirements.txt') }}-${{ matrix.DJANGO_VERSION_SAFE }}

      - name: Install
        run: |
           pip install -e .
           pip install --upgrade --upgrade-strategy eager -r demoproject/requirements.txt
           pip install --upgrade --upgrade-strategy eager -r requirements.txt
           pip install --pre Django=='${{ matrix.DJANGO_VERSION }}'
           pip install codecov

      - name: Testing
        run: |
          if [ "$matrix.DB_ENGINE" = 'mysql' ]; then
            mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u db_user -p mysql mysql
          fi
          python manage.py makemigrations --check --dry-run
          python -W error::DeprecationWarning -m coverage run manage.py test
          coverage xml && codecov
        env:
          POSTGRES_HOST: localhost
          POSTGRES_PORT: 5432
          MYSQL_PASSWORD: rootpassword
          MYSQL_USER: root
          DB_ENGINE: ${{ matrix.DB_ENGINE }}
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.11'
      - name: Install dependencies
        run: |
          pip install -r test_requirements.txt
      - name: Run pre-commit hooks
        run: pre-commit run --all-files
      - name: Run mypy
        run: mypy .
