name: Publish to Test PyPI

on:
  # manually trigger the workflow
  workflow_dispatch:

jobs:
  build:
    name: Build Distribution
    runs-on: ubuntu-latest
    outputs:
      TAG: ${{ steps.get_tag.outputs.TAG }}

    steps:
      - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          persist-credentials: false
      - name: Set up Python
        uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
        with:
          python-version: "3.x"
      - name: Install pypa/build
        run: >-
          python3 -m pip install build --user
      - name: Build a binary wheel and a source tarball
        run: python3 -m build
      - name: Store the distribution packages
        uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
        with:
          name: python-package-distributions
          path: dist/
      - name: Get Tag Name
        id: get_tag
        run: |
          pip install .
          TAG=$(python -c "from chango import __version__; print(__version__)")
          echo "TAG=$TAG" >> $GITHUB_OUTPUT

  publish-to-test-pypi:
    name: Publish to Test PyPI
    needs:
      - build
    runs-on: ubuntu-latest
    environment:
      name: release_test_pypi
      url: https://test.pypi.org/p/chango
    permissions:
      id-token: write  # IMPORTANT: mandatory for trusted publishing

    steps:
      - name: Download all the dists
        uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5.0.0
        with:
          name: python-package-distributions
          path: dist/
      - name: Publish to Test PyPI
        uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # v1.13.0
        with:
          repository-url: https://test.pypi.org/legacy/

  compute-signatures:
    name: Compute SHA1 Sums and Sign with Sigstore
    runs-on: ubuntu-latest
    needs:
      - publish-to-test-pypi

    permissions:
      id-token: write  # IMPORTANT: mandatory for sigstore

    steps:
      - name: Download all the dists
        uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5.0.0
        with:
          name: python-package-distributions
          path: dist/
      - name: Compute SHA1 Sums
        run: |
          # Compute SHA1 sum of the distribution packages and save it to a file with the same name,
          # but with .sha1 extension
          for file in dist/*; do
              sha1sum $file > $file.sha1
          done
      - name: Sign the dists with Sigstore
        uses: sigstore/gh-action-sigstore-python@f7ad0af51a5648d09a20d00370f0a91c3bdf8f84 # v3.0.1
        with:
          inputs: >-
            ./dist/*.tar.gz
            ./dist/*.whl
      - name: Store the distribution packages and signatures
        uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
        with:
          name: python-package-distributions-and-signatures
          path: dist/

  github-test-release:
    name: Upload to GitHub Release Draft
    needs:
      - build
      - compute-signatures

    runs-on: ubuntu-latest

    permissions:
      contents: write  # IMPORTANT: mandatory for making GitHub Releases

    steps:
      - name: Download all the dists
        uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5.0.0
        with:
          name: python-package-distributions-and-signatures
          path: dist/
      - name: Create GitHub Release
        env:
          GITHUB_TOKEN: ${{ github.token }}
          TAG: ${{ needs.build.outputs.TAG }}
        # Create a GitHub Release *draft*. The description can be changed later, as for now
        # we don't define it through this workflow.
        run: >-
          gh release create
          "$TAG"
          --repo '${{ github.repository }}'
          --generate-notes
          --draft
      - name: Upload artifact signatures to GitHub Release
        env:
          GITHUB_TOKEN: ${{ github.token }}
          TAG: ${{ needs.build.outputs.TAG }}
        # Upload to GitHub Release using the `gh` CLI.
        # `dist/` contains the built packages, and the
        # sigstore-produced signatures and certificates.
        run: >-
          gh release upload
          "$TAG" dist/**
          --repo '${{ github.repository }}'
