name: Sync Release to Gitee

permissions:
  contents: read

on:
  workflow_dispatch:
    inputs:
      tag_name:
        description: 'Release Tag to sync (e.g. v1.0.0)'
        required: true
        type: string

# 配置你的 Gitee 仓库信息
env:
  GITEE_OWNER: 'QuantumNous'  # 修改为你的 Gitee 用户名
  GITEE_REPO: 'new-api'                # 修改为你的 Gitee 仓库名

jobs:
  sync-to-gitee:
    runs-on: sync
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Get Release Info
        id: release_info
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          TAG_NAME: ${{ github.event.inputs.tag_name }}
        run: |
          # 获取 release 信息
          RELEASE_INFO=$(gh release view "$TAG_NAME" --json name,body,tagName,targetCommitish)
          
          RELEASE_NAME=$(echo "$RELEASE_INFO" | jq -r '.name')
          TARGET_COMMITISH=$(echo "$RELEASE_INFO" | jq -r '.targetCommitish')
          
          # 使用多行字符串输出
          {
            echo "release_name=$RELEASE_NAME"
            echo "target_commitish=$TARGET_COMMITISH"
            echo "release_body<<EOF"
            echo "$RELEASE_INFO" | jq -r '.body'
            echo "EOF"
          } >> $GITHUB_OUTPUT
          
          # 下载 release 的所有附件
          gh release download "$TAG_NAME" --dir ./release_assets || echo "No assets to download"
          
          # 列出下载的文件
          ls -la ./release_assets/ || echo "No assets directory"

      - name: Create Gitee Release
        id: create_release
        uses: nICEnnnnnnnLee/action-gitee-release@v2.0.0
        with:
          gitee_action: create_release
          gitee_owner: ${{ env.GITEE_OWNER }}
          gitee_repo: ${{ env.GITEE_REPO }}
          gitee_token: ${{ secrets.GITEE_TOKEN }}
          gitee_tag_name: ${{ github.event.inputs.tag_name }}
          gitee_release_name: ${{ steps.release_info.outputs.release_name }}
          gitee_release_body: ${{ steps.release_info.outputs.release_body }}
          gitee_target_commitish: ${{ steps.release_info.outputs.target_commitish }}

      - name: Upload Assets to Gitee
        if: hashFiles('release_assets/*') != ''
        uses: nICEnnnnnnnLee/action-gitee-release@v2.0.0
        with:
          gitee_action: upload_asset
          gitee_owner: ${{ env.GITEE_OWNER }}
          gitee_repo: ${{ env.GITEE_REPO }}
          gitee_token: ${{ secrets.GITEE_TOKEN }}
          gitee_release_id: ${{ steps.create_release.outputs.release-id }}
          gitee_upload_retry_times: 3
          gitee_files: |
            release_assets/*

      - name: Cleanup
        if: always()
        run: |
          rm -rf release_assets/

      - name: Summary
        if: success()
        run: |
          echo "✅ Successfully synced release ${{ github.event.inputs.tag_name }} to Gitee!"
          echo "🔗 Gitee Release URL: https://gitee.com/${{ env.GITEE_OWNER }}/${{ env.GITEE_REPO }}/releases/tag/${{ github.event.inputs.tag_name }}"

