name: Auto Merge Open PRs
on:
  schedule:
    - cron: 0 0 3/3 * *
  workflow_dispatch:

jobs:
  merge-prs:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4.3.0
        
      - name: 登录到 GitHub CLI
        run: gh auth login --with-token <<< "${{ github.token }}"
        
      - name: 检查 PR
        id: check-prs
        run: |
          pr_list=$( gh pr list --state open --author "app/renovate" --json number --jq '.[] | .number' )
          
          if [ -z "$pr_list" ]; then
            echo "没有开放的 PR"
          else
            for ((i=1; i<=60; i++)); do
              if [ -n "$pr_list" ]; then
                pr_list=$( gh pr list --state open --author "app/renovate" --json number,mergeStateStatus --limit 1000 --jq '.[] | select(.mergeStateStatus == "UNKNOWN") | .number' )
                
                echo "等待...($i/60)"
              else
                echo "prs_status=true" >> $GITHUB_OUTPUT
                break
              fi
              sleep 1
            done
          fi

      - name: 获取可合并的 PR
        if: ${{ steps.check-prs.outputs.prs_status == 'true'}}
        id: get-prs
        run: |
          pr_list=$( gh pr list --state open --author "app/renovate" --json number,mergeStateStatus --limit 1000 --jq 'sort_by(.number) | .[] | select(.mergeStateStatus == "CLEAN") | .number' )
          
          pr_list=$(echo "$pr_list" | tr '\n' ' ')
          
          echo "PRs to merge: $pr_list"
          echo "pr_list=$pr_list" >> $GITHUB_OUTPUT
          
      - name: 合并 PR
        if: ${{ steps.get-prs.outputs.pr_list != '' }}
        run: |
          for pr in ${{ steps.get-prs.outputs.pr_list }}; do
            if ! gh pr merge $pr --squash --delete-branch --body ""; then
              echo "⚠️ 合并 PR #$pr 失败"
            else
              echo "✅ 合并 PR #$pr 成功"
            fi
            sleep 3
          done