name: Tests

on:
  - push
  - pull_request

jobs:
  pre-commit:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Set up Python 3.11
        uses: actions/setup-python@v4
        with:
          python-version: '3.11'

      - name: Run pre-commit
        run: |
          python -m pip install tox
          python -m tox -e pre-commit

  pytest:
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
        python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']

    runs-on: ${{ matrix.os }}
    steps:
    - uses: actions/checkout@v3

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

    - name: Pull OpenAPI specs for tests
      run: |
        git submodule update --init --recursive

    - name: Run pytest
      run: |
        python -m pip install tox
        python -m tox -e py

  docs:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Set up Python 3.11
        uses: actions/setup-python@v4
        with:
          python-version: '3.11'

      - name: Run sphinx
        run: |
          python -m pip install tox
          python -m tox -e docs

  release:
    runs-on: ubuntu-latest
    if: github.event_name == 'push'
    steps:
      - uses: actions/checkout@v3

      - name: Set up Python 3.11
        uses: actions/setup-python@v4
        with:
          python-version: '3.11'

      - name: Prepare artifacts
        run: |
          python -m pip install build
          python -m build

      - name: Publish to PyPI
        if: startsWith(github.ref, 'refs/tags')
        env:
          TWINE_USERNAME: __token__
          TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
        run: |
          python -m pip install twine
          python -m twine upload dist/*
