on:
  push:
    branches: [main]
  workflow_dispatch:
    inputs:
      pypi:
        type: boolean
        description: Publish

jobs:
  publish:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          token: ${{ secrets._GITHUB_TOKEN }}
      - name: Extract PR Details
        env:
          GH_TOKEN: ${{ secrets._GITHUB_TOKEN }}
        run: |
          # Check if the event is a pull request or pull_request_target
          if [ "${{ github.event_name }}" = "pull_request" ] || [ "${{ github.event_name }}" = "pull_request_target" ]; then
            PR_NUMBER=${{ github.event.pull_request.number }}
            PR_TITLE=$(gh pr view $PR_NUMBER --json title --jq '.title')
          else
            # Use gh to find the PR associated with the commit
            COMMIT_SHA=${{ github.event.after }}
            PR_JSON=$(gh pr list --search "${COMMIT_SHA}" --state merged --json number,title --jq '.[0]')
            PR_NUMBER=$(echo $PR_JSON | jq -r '.number')
            PR_TITLE=$(echo $PR_JSON | jq -r '.title')
          fi
          echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
          echo "PR_TITLE=$PR_TITLE" >> $GITHUB_ENV
