name: Auto-add issues and PRs to Project

on:
  issues:
    types: [opened, reopened]
  pull_request:
    types: [opened, reopened]

jobs:
  add-to-project:
    runs-on: ubuntu-latest
    steps:
      - name: Determine project URL presence
        id: project_check
        run: |
          if [ -n "${{ secrets.PROJECT_URL }}" ]; then
            echo "has_project=true" >> $GITHUB_OUTPUT
          else
            echo "has_project=false" >> $GITHUB_OUTPUT
          fi

      - name: Add issue or PR to project
        if: steps.project_check.outputs.has_project == 'true'
        uses: actions/add-to-project@244f685bbc3b7adfa8466e08b698b5577571133e # v1.0.2
        continue-on-error: true
        with:
          project-url: ${{ secrets.PROJECT_URL }}
          github-token: ${{ secrets.ADD_TO_PROJECT_PAT }}

      - name: Skip summary
        if: steps.project_check.outputs.has_project == 'false'
        run: echo "PROJECT_URL secret missing; skipping project assignment." >> $GITHUB_STEP_SUMMARY
