on:
  push:
    tags:
    - 'v3.*'

name: Create release on GitHub and PyPI

permissions:
  contents: write

jobs:
  create_release:
    name: Create release on GitHub
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          submodules: true
          fetch-depth: 0
      - name: Get tag
        run: echo "GITHUB_TAG=${GITHUB_REF#*refs/tags/}" >> $GITHUB_ENV
      - name: Create release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          tag: ${{ github.ref_name }}
        run: |
          if [[ "${{ contains(env.GITHUB_TAG, 'a') || contains(env.GITHUB_TAG, 'b') || contains(env.GITHUB_TAG, 'c') }}" == "true" ]]; then
            PRERELEASE_FLAG="--prerelease"
          else
            PRERELEASE_FLAG=""
          fi
          gh release create "$tag" \
              --repo="$GITHUB_REPOSITORY" \
              --title="${tag#v}" \
              -F CHANGELOG.md \
              $PRERELEASE_FLAG
  build_and_publish:
    runs-on: ubuntu-22.04
    steps:
    - uses: actions/checkout@v3
    - run: pipx run poetry build
    - run: pipx run poetry publish
      env:
        POETRY_PYPI_TOKEN_PYPI: ${{ secrets.PYPI_TOKEN }}
