name: ci

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

  # manual
  workflow_dispatch:

env:
  PRE_COMMIT_CACHE: ~/.cache/pre-commit

jobs:
  tox-precommit:
    strategy:
      fail-fast: false
      matrix:
        python-version:
          - "{{ cookiecutter.python_version }}"
        os: [ubuntu-latest]
        env:
          - TOXENV: "pre-commit"
    runs-on: {% raw %}${{ matrix.os }}{% endraw %}
    steps:
      - uses: actions/checkout@v3
      - name: Configure Python {% raw %}${{ matrix.python-version }}{% endraw %}
        uses: actions/setup-python@v4
        with:
          python-version: {% raw %}${{ matrix.python-version }}{% endraw %}
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          python -m pip install poetry=={{ cookiecutter.poetry_version }} tox=={{ cookiecutter.tox_version }} tox-gh
      - name: Cache pre-commit
        uses: actions/cache@v3
        with:
          path: {% raw %}${{ env.PRE_COMMIT_CACHE }}{% endraw %}
          key: {% raw %}${{ matrix.os }}-${{ matrix.python-version }}-pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}{% endraw %}
      - name: Run tox
        run: |
          tox
        shell: bash
        env: {% raw %}${{ matrix.env }}{% endraw %}
  tox-tests:
    strategy:
      fail-fast: false
      matrix:
        python-version:
        {%- for python_version in cookiecutter.supported_python_versions.split(',') %}
          - "{{ python_version|trim }}"
        {%- endfor %}
        os: [ubuntu-latest]
    runs-on: {% raw %}${{ matrix.os }}{% endraw %}
    steps:
      - uses: actions/checkout@v3
      - name: Configure Python {% raw %}${{ matrix.python-version }}{% endraw %}
        uses: actions/setup-python@v4
        with:
          python-version: {% raw %}${{ matrix.python-version }}{% endraw %}
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          python -m pip install poetry=={{ cookiecutter.poetry_version }} tox=={{ cookiecutter.tox_version }} tox-gh
      - name: Cache tox env
        uses: actions/cache@v3
        with:
          path: .tox
          key: {% raw %}${{ matrix.os }}-${{ matrix.python-version }}-tox-${{ hashFiles('pyproject.toml', '.pre-commit-config.yaml') }}{% endraw %}
      - name: Run tox
        run: |
          tox -p
      {%- if cookiecutter.should_upload_coverage_to_codecov == 'y' %}
      - name: Set code coverage variable for coverage file / flags
        run: |
          MATRIX_PYTHON_VERSION={% raw %}${{matrix.python-version}}{% endraw %}
          echo "TOX_COVERAGE_ENV=py${MATRIX_PYTHON_VERSION/./}" >> $GITHUB_ENV
      - name: Upload to Codecov
        uses: codecov/codecov-action@v3
        with:
          files: ./.tox/.coverage.{% raw %}${{ env.TOX_COVERAGE_ENV }}{% endraw %}.xml
          verbose: true
          fail_ci_if_error: true
          flags: "{% raw %}${{ env.TOX_COVERAGE_ENV }}{% endraw %}"
      {%- endif %}