name: Publish Release

on:
  workflow_dispatch:

### TODO: Replace instances of './.github/actions/' with reference to the `dx-sdk-actions` repo is made public and this file is transferred over
### TODO: Also remove `get-prerelease`, `get-version`, `release-create`, `tag-create` and `tag-exists` actions from this repo's .github/actions folder once the repo is public.

permissions:
  contents: write
  id-token: write # Required for trusted publishing to PyPI

jobs:
  rl-scanner:
    uses: ./.github/workflows/rl-scanner.yml
    with:
      python-version: "3.10"
      artifact-name: "auth0-python.tgz"
    secrets:
      RLSECURE_LICENSE: ${{ secrets.RLSECURE_LICENSE }}
      RLSECURE_SITE_KEY: ${{ secrets.RLSECURE_SITE_KEY }}
      SIGNAL_HANDLER_TOKEN: ${{ secrets.SIGNAL_HANDLER_TOKEN }}
      PRODSEC_TOOLS_USER: ${{ secrets.PRODSEC_TOOLS_USER }}
      PRODSEC_TOOLS_TOKEN: ${{ secrets.PRODSEC_TOOLS_TOKEN }}
      PRODSEC_TOOLS_ARN: ${{ secrets.PRODSEC_TOOLS_ARN }}
  publish-pypi:
    if: github.event_name == 'workflow_dispatch' || (github.event_name == 'pull_request' && github.event.pull_request.merged && startsWith(github.event.pull_request.head.ref, 'release/'))
    name: "PyPI"
    runs-on: ubuntu-latest
    needs: rl-scanner
    environment: release

    steps:
      - name: Checkout code
        uses: actions/checkout@v5
        with:
          fetch-depth: 0
          fetch-tags: true

      # Get the version from the branch name
      - id: get_version
        uses: ./.github/actions/get-version

      # Get the prerelease flag from the branch name
      - id: get_prerelease
        uses: ./.github/actions/get-prerelease
        with:
          version: ${{ steps.get_version.outputs.version }}

      # Get the release notes
      # This will expose the release notes as env.RELEASE_NOTES
      - id: get_release_notes
        uses: ./.github/actions/get-release-notes
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          version: ${{ steps.get_version.outputs.version }}
          repo_owner: ${{ github.repository_owner }}
          repo_name: ${{ github.event.repository.name }}

      # Create a release for the tag
      - uses: ./.github/actions/release-create
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          name: ${{ steps.get_version.outputs.version }}
          body: ${{ steps.get_release_notes.outputs.release-notes }}
          tag: ${{ steps.get_version.outputs.version }}
          commit: ${{ github.sha }}
          prerelease: ${{ steps.get_prerelease.outputs.prerelease }}

      - name: Configure Python
        uses: actions/setup-python@v6
        with:
          python-version: "3.9"

      - name: Configure dependencies
        run: |
          pip install --user --upgrade pip
          pip install --user pipx
          pipx ensurepath
          pipx install poetry
          poetry config virtualenvs.in-project true
          poetry install --with dev
          poetry self add "poetry-dynamic-versioning[plugin]"

      - name: Build release
        run: |
          poetry build

      - name: Publish release
        uses: pypa/gh-action-pypi-publish@release/v1
