name: Backfill Releases to GitHub

on:
  workflow_dispatch:

jobs:
  backfill:
    runs-on: ubuntu-latest
    steps:
      - name: Backfill all Gitea releases to GitHub
        env:
          GH_PAT: ${{ secrets.GH_PAT }}
          GITEA_TOKEN: ${{ secrets.REGISTRY_TOKEN }}
          GITEA_API: https://repo.anhonesthost.net/api/v1
          GITEA_REPO: cybercovellc/triple-c
          GITHUB_REPO: shadowdao/triple-c
        run: |
          set -e

          echo "==> Fetching releases from Gitea..."
          RELEASES=$(curl -sf \
            -H "Authorization: token $GITEA_TOKEN" \
            "$GITEA_API/repos/$GITEA_REPO/releases?limit=50")

          echo "$RELEASES" | jq -c '.[]' | while read release; do
            TAG=$(echo "$release" | jq -r '.tag_name')
            NAME=$(echo "$release" | jq -r '.name')
            BODY=$(echo "$release" | jq -r '.body')
            IS_PRERELEASE=$(echo "$release" | jq -r '.prerelease')
            IS_DRAFT=$(echo "$release" | jq -r '.draft')

            EXISTS=$(curl -sf \
              -H "Authorization: Bearer $GH_PAT" \
              -H "Accept: application/vnd.github+json" \
              "https://api.github.com/repos/$GITHUB_REPO/releases/tags/$TAG" \
              -o /dev/null -w "%{http_code}" || true)

            if [ "$EXISTS" = "200" ]; then
              echo "==> Skipping $TAG (already exists on GitHub)"
              continue
            fi

            echo "==> Creating release $TAG..."
            RESPONSE=$(curl -sf -X POST \
              -H "Authorization: Bearer $GH_PAT" \
              -H "Accept: application/vnd.github+json" \
              -H "Content-Type: application/json" \
              https://api.github.com/repos/$GITHUB_REPO/releases \
              -d "{
                \"tag_name\": \"$TAG\",
                \"name\": \"$NAME\",
                \"body\": $(echo "$BODY" | jq -Rs .),
                \"draft\": $IS_DRAFT,
                \"prerelease\": $IS_PRERELEASE
              }")

            UPLOAD_URL=$(echo "$RESPONSE" | jq -r '.upload_url' | sed 's/{?name,label}//')

            echo "$release" | jq -c '.assets[]?' | while read asset; do
              ASSET_NAME=$(echo "$asset" | jq -r '.name')
              ASSET_ID=$(echo "$asset" | jq -r '.id')

              echo "  ==> Downloading $ASSET_NAME..."
              DOWNLOAD_URL=$(echo "$asset" | jq -r '.browser_download_url')
              curl -sfL -o "/tmp/$ASSET_NAME" \
                -H "Authorization: token $GITEA_TOKEN" \
                "$DOWNLOAD_URL"

              echo "  ==> Uploading $ASSET_NAME to GitHub..."
              ENCODED_NAME=$(python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$ASSET_NAME")
              curl -sf -X POST \
                -H "Authorization: Bearer $GH_PAT" \
                -H "Accept: application/vnd.github+json" \
                -H "Content-Type: application/octet-stream" \
                --data-binary "@/tmp/$ASSET_NAME" \
                "$UPLOAD_URL?name=$ENCODED_NAME"

              echo "    Uploaded: $ASSET_NAME"
            done

            echo "==> Done: $TAG"
          done

          echo "==> Backfill complete."