---
name: Publish releases

# yamllint disable-line rule:truthy
on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      version:
        description: "Version for release on TestPyPI"
        required: true
env:
  PYTHON_VERSION: "3.12"

jobs:
  build:
    name: Builds and publishes releases to PyPI
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5.0.0
      - name: Store version from inputs
        if: github.event_name == 'workflow_dispatch'
        run: echo "tag=${{ inputs.version }}" >> $GITHUB_ENV
      - name: Get version from tag
        if: github.event_name == 'release'
        run: echo "tag=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
      - name: Validate version number
        if: github.event_name == 'release'
        run: >-
          if [[ "${{ github.event.release.prerelease }}" == "true" ]]; then
            if ! [[ "${tag}" =~ "b" ]]; then
            echo "Pre-release: Tag is missing beta suffix (${tag})"
              exit 1
            fi
          else
            if [[ "${tag}" =~ "b" ]]; then
              echo "Release: Tag must not have a beta suffix (${tag})"
              exit 1
            fi
          fi
      - name: Set up Python ${{ env.PYTHON_VERSION }}
        uses: actions/setup-python@v6.0.0
        with:
          python-version: ${{ env.PYTHON_VERSION }}
      - name: Install build
        run: >-
          pip install build tomli tomli-w
      - name: Set Python project version from tag
        shell: python
        run: |-
          import tomli
          import tomli_w

          with open("pyproject.toml", "rb") as f:
            pyproject = tomli.load(f)

          pyproject["project"]["version"] = "${{ env.tag }}"

          with open("pyproject.toml", "wb") as f:
            tomli_w.dump(pyproject, f)
      - name: Build python package
        run: >-
          python3 -m build
      - name: Store the distribution packages
        uses: actions/upload-artifact@v4
        with:
          name: python-package-distributions
          path: dist/

  publish-pypi:
    name: Publishes releases to PyPI
    runs-on: ubuntu-latest
    if: github.event_name == 'release'
    needs:
      - build
    environment:
      name: pypi
      url: https://pypi.org/p/aiohasupervisor
    permissions:
      id-token: write
    steps:
      - name: Download all the dists
        uses: actions/download-artifact@v5.0.0
        with:
          name: python-package-distributions
          path: dist/
      - name: Publish distribution 📦 to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1

  publish-test-pypi:
    name: Publishes releases to Test-PyPI
    runs-on: ubuntu-latest
    if: github.event_name == 'workflow_dispatch'
    needs:
      - build
    environment:
      name: testpypi
      url: https://test.pypi.org/p/aiohasupervisor
    permissions:
      id-token: write
    steps:
      - name: Download all the dists
        uses: actions/download-artifact@v5.0.0
        with:
          name: python-package-distributions
          path: dist/
      - name: Publish distribution 📦 to TestPyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          repository-url: https://test.pypi.org/legacy/
