name: Delete PR Branch On Close

on:
  pull_request:
    types:
      - closed

permissions:
  contents: write
  pull-requests: read

jobs:
  delete-branch:
    runs-on: ubuntu-latest
    if: >-
      github.event.pull_request.merged == true

    steps:
      - name: Delete branch via Gitea API
        shell: bash
        env:
          TOKEN: ${{ secrets.GITEA_TOKEN }}
          REPOSITORY: ${{ github.repository }}
          BRANCH: ${{ github.event.pull_request.head.ref }}
        run: |
          set -euo pipefail

          API_BASE="${GITHUB_API_URL:-}"
          if [ -z "${API_BASE}" ]; then
            SERVER_URL="${GITHUB_SERVER_URL:-}"
            if [ -z "${SERVER_URL}" ]; then
              echo "Unable to resolve Gitea API base URL from GITHUB_API_URL or GITHUB_SERVER_URL"
              exit 1
            fi
            API_BASE="${SERVER_URL%/}/api/v1"
          fi

          if [ -z "${API_BASE}" ]; then
            echo "Unable to resolve Gitea API base URL"
            exit 1
          fi

          # Branch names commonly include '/'; encode it for the API path segment.
          BRANCH_ENCODED="${BRANCH//\//%2F}"
          URL="${API_BASE}/repos/${REPOSITORY}/branches/${BRANCH_ENCODED}"

          RESPONSE_FILE="$(mktemp)"
          STATUS="$(curl -sS -o "${RESPONSE_FILE}" -w "%{http_code}" \
            -X DELETE \
            -H "Authorization: token ${TOKEN}" \
            -H "Accept: application/json" \
            "${URL}")"

          if [ "${STATUS}" = "204" ]; then
            echo "Deleted branch: ${BRANCH}"
          elif [ "${STATUS}" = "404" ]; then
            echo "Branch already deleted or missing: ${BRANCH}"
          else
            echo "Branch deletion failed with HTTP ${STATUS}"
            cat "${RESPONSE_FILE}"
            exit 1
          fi


