Update auto_merge_renovate_prs.yml

This commit is contained in:
2025-09-17 11:51:51 +08:00
committed by GitHub
parent 0b43e913a4
commit 8f8542ab9b
+76 -49
View File
@@ -1,4 +1,4 @@
name: Renovate Auto-merge (patch/minor only) name: Renovate Auto-Merge
on: on:
workflow_dispatch: workflow_dispatch:
@@ -8,7 +8,7 @@ on:
required: false required: false
default: '5' default: '5'
schedule: schedule:
- cron: "*/10 * * * *" - cron: "*/10 * * * *" # 每10分钟跑一次(按需调整)
pull_request: pull_request:
types: [ready_for_review] types: [ready_for_review]
@@ -23,79 +23,106 @@ env:
jobs: jobs:
automerge: automerge:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout (optional) - name: Checkout (optional)
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Optional delay - name: Optional delay (let CI start)
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
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} DELAY: ${{ github.event.inputs.delay_minutes }}
- name: Find Renovate PR numbers
id: find_prs
run: | run: |
prs=$(gh pr list --repo "$REPO" --state open --json number,title,author \ delay="${DELAY:-5}"
--jq '.[] | select(.author.login | test("renovate"; "i")) | .number' ) || true echo "Delaying for ${delay} minute(s) to let other workflows start..."
echo "Found PR numbers: $prs" sleep $(( delay * 60 ))
echo "prs=$prs" >> $GITHUB_OUTPUT
- name: Process Renovate PRs (patch/minor -> merge when checks OK)
env: env:
REPO: ${{ env.REPO }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Process Renovate PRs (patch/minor only)
if: steps.find_prs.outputs.prs != ''
run: | run: |
set -euo pipefail
owner_repo="$REPO" owner_repo="$REPO"
owner=$(echo "$owner_repo" | cut -d'/' -f1) owner="${owner_repo%%/*}"
repo=$(echo "$owner_repo" | cut -d'/' -f2) repo="${owner_repo##*/}"
prs="${{ steps.find_prs.outputs.prs }}" echo "Listing open PRs authored by renovate-like bots..."
for pr in $prs; do # 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 ===" 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} 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 if [[ "$update_type" != "patch" && "$update_type" != "minor" ]]; then
echo "Skipping PR #$pr (not patch/minor)" echo "Skip PR #$pr (not patch/minor)."
continue continue
fi fi
# Get head commit SHA
sha=$(gh pr view "$pr" --repo "$owner_repo" --json headRefOid --jq .headRefOid 2>/dev/null || true) sha=$(gh pr view "$pr" --repo "$owner_repo" --json headRefOid --jq .headRefOid 2>/dev/null || true)
if [[ -z "$sha" ]]; then 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:-<unknown>}"
if [[ -z "$sha" ]]; then
echo "Cannot determine head SHA for PR #$pr; skipping."
continue
fi fi
echo "PR #$pr head SHA: $sha"
combined_state=$(gh api repos/$owner/$repo/commits/$sha/status --jq .state) # Check combined status (legacy statuses)
echo "Combined status: $combined_state" 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 \ # Count in-progress or non-concluded check-runs
--jq '.check_runs[] | select(.status == "in_progress" or .conclusion == null) | .name' | wc -l || true) 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)
echo "In-progress checks: $inprogress" 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 # If combined status already success and no in-progress -> merge now
echo "Merging PR #$pr ..." if [[ "$combined_state" == "success" && "$inprogress_count" -eq 0 ]]; then
gh pr merge "$pr" --repo "$owner_repo" --squash --delete-branch --confirm || { echo "Checks already green for PR #$pr; attempting immediate merge..."
echo "Merge failed for PR #$pr" 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 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 fi
# small delay to avoid rate limits
sleep 1
done done
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} echo "All done."