name: AutoPub PR Workflow

on:
  pull_request_target:
    branches: [main]

permissions:
  pull-requests: write

jobs:
  check-release:
    runs-on: ubuntu-latest
    outputs:
      has_release: ${{ steps.check.outputs.has_release }}

    steps:
      - uses: actions/checkout@v4
        with:
          ref: "refs/pull/${{ github.event.number }}/merge"
          sparse-checkout: |
            RELEASE.md
            pyproject.toml
          sparse-checkout-cone-mode: false

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

      - name: Check
        id: check
        run: |
          if uvx --from autopub==1.0.0a48 --with pygithub autopub check; then
            echo "has_release=true" >> $GITHUB_OUTPUT
          else
            echo "has_release=false" >> $GITHUB_OUTPUT
            if [ "${{ github.event_name }}" = "pull_request" ]; then
              exit 1
            fi
          fi
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload .autopub
        if: steps.check.outputs.has_release == 'true'
        run: |
          if [ -d ".autopub" ]; then
            echo "Found .autopub directory"
          else
            echo "No .autopub directory found"
            exit 1
          fi

      - name: Upload .autopub artifact
        if: steps.check.outputs.has_release == 'true'
        uses: actions/upload-artifact@v4
        with:
          name: autopub-data
          path: .autopub
          include-hidden-files: true
