name: Publish

on:
  push:
    tags:
      - "v*"

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: "0"
      - name: Set up Python 3.12
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          python -m pip install poetry
          poetry install
      - name: Apply the tag version
        run: |
          version=${{ github.ref_name }}
          sed -Ei '/^version = /s|= "[0-9.]+"$|= "'${version:-1}'"|' pyproject.toml
      - name: Run build
        run: |
          poetry run tox -e build
      - name: Publish to PyPi
        env:
          TWINE_USERNAME: ${{ secrets.TWINE_USERNAME }}
          TWINE_PASSWORD: ${{ secrets.TWINE_PASSWORD }}
        run: |
          poetry run twine upload -u $TWINE_USERNAME -p $TWINE_PASSWORD dist/*
      - name: Run changelog
        run: |
          poetry run tox -e changelog
      - uses: stefanzweifel/git-auto-commit-action@v5
        with:
          commit_message: "docs: changelog update"
          branch: master
          file_pattern: CHANGELOG.md