name: CI

on:
  pull_request:
  push:


env:
  UPSTREAM_REPOSITORY_ID: >-
    119996807


jobs:
  pre-setup:
    name: ⚙️ Pre-set global build settings

    runs-on: ubuntu-latest

    timeout-minutes: 1

    defaults:
      run:
        shell: python

    outputs:
      # NOTE: These aren't env vars because the `${{ env }}` context is
      # NOTE: inaccessible when passing inputs to reusable workflows.
      upstream-repository-id: ${{ env.UPSTREAM_REPOSITORY_ID }}
      is-debug-mode: ${{ runner.debug == '1' && false || true }}

    steps:
    - name: No-op so that the computations can happen in the outputs
      if: fromJSON(false)
      run: |
        print('No-op')

  sanity:
    name: 🧹 Linters${{ '' }}  # nest jobs under the same sidebar category
    needs:
    - pre-setup

    strategy:
      fail-fast: false
      matrix:
        runner-vm-os:
        - ubuntu-22.04
        python-version:
        - >-
          3.10
        toxenv:
        - linters
        - docs
        xfail:
        - false

    uses: tox-dev/workflow/.github/workflows/reusable-tox.yml@89de3c6be3cd179adf71e28aa4ac5bef60804209  # yamllint disable-line rule:line-length
    with:
      cache-key-for-dependency-files:  # FIXME
      check-name: >-
        ${{ matrix.toxenv }}
        under 🐍${{ matrix.python-version }}
      checkout-src-git-fetch-depth: 0
      python-version: >-
        ${{ matrix.python-version }}
      require-successful-codecov-uploads: >-
        ${{
          toJSON(
          needs.pre-setup.outputs.upstream-repository-id
          == github.repository_id
          )
        }}
      runner-vm-os: >-
        ${{ matrix.runner-vm-os }}
      timeout-minutes: 2
      toxenv: >-
        ${{ matrix.toxenv }}
      tox-tool-deps: >-
        'tox == 4.25.0'
        'tox-uv == 1.25.0'
      xfail: >-
        ${{ fromJSON(matrix.xfail) }}
    secrets:
      codecov-token: ${{ secrets.CODECOV_TOKEN }}

  tests:
    name: 🧪 Tests${{ '' }}  # nest jobs under the same sidebar category

    needs:
    - pre-setup  # transitive, for accessing settings

    strategy:
      fail-fast: >-  # ${{ runner.debug }} is unavailable in this context
        ${{
          fromJSON(needs.pre-setup.outputs.is-debug-mode)
          && false
          || true
        }}
      matrix:
        python-version:
        # NOTE: The latest and the lowest supported Pythons are prioritized
        # NOTE: to improve the responsiveness. It's nice to see the most
        # NOTE: important results first.
        - 3.12
        - 3.9
        - 3.11
        - >-
          3.10
        runner-vm-os:
        - ubuntu-22.04
        toxenv:
        - integration
        - unit
        xfail:
        - false

    uses: tox-dev/workflow/.github/workflows/reusable-tox.yml@89de3c6be3cd179adf71e28aa4ac5bef60804209  # yamllint disable-line rule:line-length
    with:
      cache-key-for-dependency-files:  # FIXME
      check-name: >-
        ${{ matrix.toxenv }}
        under 🐍${{ matrix.python-version }}
      job-dependencies-context: >-  # context for hooks
        ${{ toJSON(needs) }}
      python-version: >-
        ${{ matrix.python-version }}
      require-successful-codecov-uploads: >-
        ${{
          toJSON(
          needs.pre-setup.outputs.upstream-repository-id
          == github.repository_id
          )
        }}
      runner-vm-os: >-
        ${{ matrix.runner-vm-os }}
      timeout-minutes: >-
        ${{ matrix.toxenv == 'unit' && 2 || 4 }}
      toxenv: >-
        ${{ matrix.toxenv }}
      tox-run-posargs: >-
        --cov-report=xml:.tox/.tmp/.test-results/pytest-${{
          matrix.python-version
        }}/cobertura.xml
        --junitxml=.tox/.tmp/.test-results/pytest-${{
          matrix.python-version
        }}/test.xml
      tox-rerun-posargs: >-
        --no-cov
        -vvvvv
        --lf
      tox-tool-deps: >-
        'tox == 4.25.0'
        'tox-uv == 1.25.0'
      xfail: >-
        ${{ fromJSON(matrix.xfail) }}
    secrets:
      codecov-token: ${{ secrets.CODECOV_TOKEN }}

  check:  # This job does nothing and is only used for the branch protection
    name: ✅ All tests passed
    if: always()

    needs:
    - pre-setup  # transitive, for accessing settings
    - sanity
    - tests

    runs-on: ubuntu-latest

    timeout-minutes: 1

    steps:
    - name: Decide whether the needed jobs succeeded or failed
      uses: re-actors/alls-green@release/v1
      with:
        jobs: ${{ toJSON(needs) }}
