diff --git a/.github/workflows/auto_merge_renovate_prs.yml b/.github/workflows/auto_merge_renovate_prs.yml index 301e041c5..f86a885ae 100644 --- a/.github/workflows/auto_merge_renovate_prs.yml +++ b/.github/workflows/auto_merge_renovate_prs.yml @@ -1,4 +1,4 @@ -name: Renovate Auto-merge (patch/minor only) +name: Renovate Auto-Merge on: workflow_dispatch: @@ -8,7 +8,7 @@ on: required: false default: '5' schedule: - - cron: "*/10 * * * *" + - cron: "*/10 * * * *" # 每10分钟跑一次(按需调整) pull_request: types: [ready_for_review] @@ -23,79 +23,106 @@ env: jobs: automerge: runs-on: ubuntu-latest - steps: - name: Checkout (optional) uses: actions/checkout@v4 - - name: Optional delay - run: | - delay_input="${{ github.event.inputs.delay_minutes }}" - delay="${delay_input:-5}" - echo "Delaying for ${delay} minute(s)..." - sleep $((delay * 60)) - - - name: Debug list all PRs - run: | - echo "All open PRs (number / title / author):" - gh pr list --repo "$REPO" --state open --json number,title,author \ - --jq '.[] | {number,title,author:.author.login}' || true + - name: Optional delay (let CI start) env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Find Renovate PR numbers - id: find_prs + DELAY: ${{ github.event.inputs.delay_minutes }} run: | - prs=$(gh pr list --repo "$REPO" --state open --json number,title,author \ - --jq '.[] | select(.author.login | test("renovate"; "i")) | .number' ) || true - echo "Found PR numbers: $prs" - echo "prs=$prs" >> $GITHUB_OUTPUT + delay="${DELAY:-5}" + echo "Delaying for ${delay} minute(s) to let other workflows start..." + sleep $(( delay * 60 )) + + - name: Process Renovate PRs (patch/minor -> merge when checks OK) env: + REPO: ${{ env.REPO }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Process Renovate PRs (patch/minor only) - if: steps.find_prs.outputs.prs != '' run: | + set -euo pipefail + owner_repo="$REPO" - owner=$(echo "$owner_repo" | cut -d'/' -f1) - repo=$(echo "$owner_repo" | cut -d'/' -f2) + owner="${owner_repo%%/*}" + repo="${owner_repo##*/}" - prs="${{ steps.find_prs.outputs.prs }}" - for pr in $prs; do + echo "Listing open PRs authored by renovate-like bots..." + # Read PR numbers into bash array (one per line) + mapfile -t prs < <(gh pr list --repo "$owner_repo" --state open --json number,title,author --jq '.[] | select(.author.login | test("renovate"; "i")) | .number' 2>/dev/null || true) + + if [ "${#prs[@]}" -eq 0 ]; then + echo "No renovate PRs found. Exiting." + exit 0 + fi + + echo "Found ${#prs[@]} renovate PR(s): ${prs[*]}" + + for pr in "${prs[@]}"; do + echo echo "=== Processing PR #$pr ===" - body=$(gh pr view "$pr" --repo "$owner_repo" --json body --jq .body) + # Get PR body + body=$(gh pr view "$pr" --repo "$owner_repo" --json body --jq .body 2>/dev/null || echo "") + echo "PR #$pr body (first 20 lines):" + echo "$body" | sed -n '1,20p' || true - update_type=$(echo "$body" | awk -F'|' '/\|/ { gsub(/ /,"",$3); if ($3 ~ /(major|minor|patch)/) {print $3; exit} }') + # Try to extract update type from the Markdown table's Update column. + # This regex looks for lines like: | package/name | minor | v1.2.3 -> v1.2.4 | + update_type=$(echo "$body" | grep -m1 -Po '\|\s*[^|]+\s*\|\s*\K(major|minor|patch)(?=\s*\|)' || true) update_type=${update_type:-unknown} - echo "PR #$pr update_type=$update_type" + echo "Detected update_type='$update_type'" if [[ "$update_type" != "patch" && "$update_type" != "minor" ]]; then - echo "Skipping PR #$pr (not patch/minor)" + echo "Skip PR #$pr (not patch/minor)." continue fi + # Get head commit SHA sha=$(gh pr view "$pr" --repo "$owner_repo" --json headRefOid --jq .headRefOid 2>/dev/null || true) if [[ -z "$sha" ]]; then - sha=$(gh api repos/$owner/$repo/pulls/$pr --jq .head.sha) + sha=$(gh api repos/"$owner"/"$repo"/pulls/"$pr" --jq .head.sha 2>/dev/null || true) + fi + echo "PR #$pr head SHA: ${sha:-}" + if [[ -z "$sha" ]]; then + echo "Cannot determine head SHA for PR #$pr; skipping." + continue fi - echo "PR #$pr head SHA: $sha" - combined_state=$(gh api repos/$owner/$repo/commits/$sha/status --jq .state) - echo "Combined status: $combined_state" + # Check combined status (legacy statuses) + combined_state=$(gh api repos/"$owner"/"$repo"/commits/"$sha"/status --jq .state 2>/dev/null || echo "unknown") + echo "Combined status for $sha: $combined_state" - inprogress=$(gh api repos/$owner/$repo/commits/$sha/check-runs \ - --jq '.check_runs[] | select(.status == "in_progress" or .conclusion == null) | .name' | wc -l || true) - echo "In-progress checks: $inprogress" + # Count in-progress or non-concluded check-runs + inprogress_count=$(gh api repos/"$owner"/"$repo"/commits/"$sha"/check-runs --jq '.check_runs[] | select(.status == "in_progress" or .conclusion == null) | .name' 2>/dev/null | wc -l || echo 0) + inprogress_count=$(echo "$inprogress_count" | tr -d '[:space:]') + inprogress_count=${inprogress_count:-0} + echo "In-progress / pending check-runs: $inprogress_count" - if [[ "$combined_state" == "success" && "$inprogress" -eq 0 ]]; then - echo "Merging PR #$pr ..." - gh pr merge "$pr" --repo "$owner_repo" --squash --delete-branch --confirm || { - echo "Merge failed for PR #$pr" - } + # If combined status already success and no in-progress -> merge now + if [[ "$combined_state" == "success" && "$inprogress_count" -eq 0 ]]; then + echo "Checks already green for PR #$pr; attempting immediate merge..." + if gh pr merge "$pr" --repo "$owner_repo" --squash --delete-branch --yes; then + echo "PR #$pr merged successfully." + else + echo "Immediate merge command failed for PR #$pr; attempting to enable auto-merge..." + gh pr merge "$pr" --repo "$owner_repo" --squash --auto --delete-branch || echo "Enable auto-merge failed for PR #$pr." + fi else - echo "Checks not green for PR #$pr, skipping." + # Not all checks green: enable platform auto-merge so GitHub will merge when checks pass + echo "Not all checks green for PR #$pr (state=$combined_state, inprogress=$inprogress_count)." + echo "Attempting to enable platform auto-merge for PR #$pr (GitHub will merge when required checks pass)." + if gh pr merge "$pr" --repo "$owner_repo" --squash --auto --delete-branch; then + echo "Auto-merge enabled for PR #$pr." + else + echo "Failed to enable auto-merge for PR #$pr. Possible reasons: branch protection requires reviews or token lacks permission." + # Optional: print protection info for debugging + protection=$(gh api repos/"$owner"/"$repo"/branches/"$(gh pr view "$pr" --repo "$owner_repo" --json headRefName --jq .headRefName)" --jq '.protection' 2>/dev/null || echo "") + echo "Branch protection info: $protection" + fi fi + + # small delay to avoid rate limits + sleep 1 done - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + echo "All done."