name: Publish Python 🐍 distributions 📦 to PyPI and TestPyPI

on:
  push:
    tags:
      - 'v[0-9]+.[0-9]+'
      - 'v[0-9]+.[0-9]+.[0-9]+'
      - 'v[0-9]+.[0-9]+[0-9a-z]+'
  workflow_dispatch:
    inputs:
      dist-location:
        description: 'Distribution location (test/prod)'
        default: 'test'
        required: false

jobs:
  deploy:
    name: Build and publish ${{ matrix.package }} 🐍
    runs-on: ubuntu-22.04
    timeout-minutes: 120
    strategy:
      fail-fast: true
      matrix:
        include:
          - PACKAGE: pyln-client
            WORKDIR: contrib/pyln-client
          - PACKAGE: pyln-testing
            WORKDIR: contrib/pyln-testing
          - PACKAGE: pyln-proto
            WORKDIR: contrib/pyln-proto
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        # Need to fetch entire history in order to locate the version tag
        fetch-depth: 0

    - name: Check version tag
      run: >-
        git describe --tags --always --dirty=-modded --abbrev=7

    - name: Setup Version
      env:
        WORKDIR: ${{ matrix.WORKDIR }}
      run: |
        echo "VERSION=$(git describe --tags --abbrev=0).post$(git describe --tags --abbrev=1 | awk -F "-" '{print $2}')" >> $GITHUB_ENV

    - name: Set up values
      id: set-values
      run: |
        if [[ "${{ github.event.inputs.dist-location }}" != "" ]]; then
          DISTLOCATION=${{ github.event.inputs.dist-location }}
        elif [[ "${{ github.ref_type }}" == "tag" ]] && [[ ! "${{ github.ref_name }}" =~ rc ]]; then
          DISTLOCATION="prod"
        else
          DISTLOCATION="test"
        fi
        echo "DISTLOCATION=$DISTLOCATION" >> $GITHUB_OUTPUT
        echo "EVENT DISTLOCATION: ${{ github.event.inputs.dist-location }}"
        echo "DISTRIBUTION LOCATION: $DISTLOCATION"
  
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'

    - name: Install uv
      uses: astral-sh/setup-uv@v5

    - name: Update pyln versions
      id: update-versions
      run: |
        export VERSION=$(git describe --tags --abbrev=0)
        echo "Pyln VERSION: $VERSION"
        make update-pyln-versions NEW_VERSION=$VERSION

    - name: Publish distribution 📦 to Test PyPI
      if: github.repository == 'ElementsProject/lightning' && steps.set-values.outputs.DISTLOCATION == 'test'
      env:
        UV_PUBLISH_TOKEN: ${{ secrets.TEST_PYPI_API_TOKEN }}
        WORKDIR: ${{ matrix.WORKDIR }}
      run: |
        echo "Pyln VERSION: $VERSION"
        uv build --package ${{ matrix.PACKAGE }}
        uv publish --publish-url https://test.pypi.org/legacy/ --check-url https://test.pypi.org/simple/

    - name: Publish distribution 📦 to PyPI
      if: github.repository == 'ElementsProject/lightning' && steps.set-values.outputs.DISTLOCATION == 'prod'
      env:
        UV_PUBLISH_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
        WORKDIR: ${{ matrix.WORKDIR }}
      run: |
        echo "UV VERSION PUBLISH: $(uv --version)"
        echo "Pyln VERSION: $VERSION"
        uv build --package ${{ matrix.PACKAGE }}
        uv publish
