name: Publish to Pypi
on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Version upload to pypi'
        required: true
      pypi_repo:
        description: 'Repo to upload to (testpypi or pypi)'
        default: 'testpypi'
        required: true

jobs:
  publish:
    runs-on: ubuntu-latest
    environment: publish_pypi
    permissions:
      # IMPORTANT: this permission is mandatory for trusted publishing
      id-token: write
    steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-python@v5
      with:
        python-version: '3.8'
    - name: Install dependencies
      run: |
        pip install -U wheelhouse_uploader pyyaml
    - name: Downloading wheels and sdist from staging
      env:
        XLEARN_VERSION: ${{ github.event.inputs.version }}
      run: |
        echo "Download $XLEARN_VERSION wheels and sdist"
        python -m wheelhouse_uploader fetch \
          --version $XLEARN_VERSION \
          --local-folder dist/ \
          jax-sklearn \
          https://pypi.anaconda.org/jax-sklearn-wheels-staging/simple/jax-sklearn/
    - name: Check dist has the correct number of artifacts
      run: |
        python build_tools/github/check_wheels.py
    - name: Publish package to TestPyPI
      uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4
      with:
        repository-url: https://test.pypi.org/legacy/
        print-hash: true
      if: ${{ github.event.inputs.pypi_repo == 'testpypi' }}
    - name: Publish package to PyPI
      uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4
      if: ${{ github.event.inputs.pypi_repo == 'pypi' }}
      with:
        print-hash: true
