mirror of
https://github.com/QYG2297248353/appstore-1panel.git
synced 2026-06-16 01:02:14 +08:00
Update auto_merge_renovate_prs.yml
This commit is contained in:
@@ -1,79 +1,188 @@
|
|||||||
name: Auto merge Renovate PRs
|
name: Renovate Auto-merge (poll until CI passes)
|
||||||
|
|
||||||
on:
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
inputs:
|
||||||
|
delay_minutes:
|
||||||
|
description: 'Delay in minutes before starting checks (default 5)'
|
||||||
|
required: false
|
||||||
|
default: '5'
|
||||||
|
max_wait_minutes:
|
||||||
|
description: 'Max minutes to wait for CI per PR (default 30)'
|
||||||
|
required: false
|
||||||
|
default: '30'
|
||||||
|
interval_seconds:
|
||||||
|
description: 'Polling interval seconds (default 60)'
|
||||||
|
required: false
|
||||||
|
default: '60'
|
||||||
schedule:
|
schedule:
|
||||||
- cron: "*/10 * * * *" # 每 10 分钟跑一次
|
- cron: "*/10 * * * *" # every 10 minutes
|
||||||
workflow_dispatch: # 支持手动触发
|
|
||||||
pull_request:
|
pull_request:
|
||||||
types: [ready_for_review]
|
types: [ready_for_review]
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
pull-requests: write
|
||||||
|
checks: read
|
||||||
|
|
||||||
|
env:
|
||||||
|
REPO: ${{ github.repository }}
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
automerge:
|
automerge:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v5
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: List open PRs from Renovate
|
- name: Show gh version (debug)
|
||||||
id: list
|
|
||||||
run: |
|
run: |
|
||||||
prs=$(gh pr list \
|
echo "gh version:"
|
||||||
--repo ${{ github.repository }} \
|
gh --version || true
|
||||||
--state open \
|
|
||||||
--json number,title,author \
|
- name: Authenticate gh with GITHUB_TOKEN
|
||||||
--jq '.[] | select(.author.login | test("renovate";"i")) | .number')
|
|
||||||
echo "Found PRs: $prs"
|
|
||||||
echo "prs=$prs" >> $GITHUB_ENV
|
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
- name: Process each PR
|
|
||||||
if: env.prs != ''
|
|
||||||
run: |
|
run: |
|
||||||
for pr in $prs; do
|
if gh auth status 2>/dev/null; then
|
||||||
|
echo "gh already authenticated"
|
||||||
|
else
|
||||||
|
echo "${GITHUB_TOKEN}" | gh auth login --with-token
|
||||||
|
fi
|
||||||
|
gh auth status || true
|
||||||
|
|
||||||
|
- name: Optional initial delay (let CI start)
|
||||||
|
env:
|
||||||
|
DELAY_MIN: ${{ github.event.inputs.delay_minutes }}
|
||||||
|
run: |
|
||||||
|
delay="${DELAY_MIN:-5}"
|
||||||
|
echo "Delaying for ${delay} minute(s) to let other workflows start..."
|
||||||
|
sleep $(( delay * 60 ))
|
||||||
|
|
||||||
|
- name: Find open Renovate PR numbers into array
|
||||||
|
id: find_prs
|
||||||
|
run: |
|
||||||
|
owner_repo="${{ env.REPO }}"
|
||||||
|
echo "Listing open PRs for $owner_repo authored by renovate-like bots..."
|
||||||
|
# produce newline separated list, then mapfile into array in next step
|
||||||
|
gh pr list --repo "$owner_repo" --state open --json number,title,author \
|
||||||
|
--jq '.[] | select(.author.login | test("renovate"; "i")) | .number' > /tmp/renovate_prs.txt || true
|
||||||
|
echo "Saved PR list lines:"
|
||||||
|
sed -n '1,200p' /tmp/renovate_prs.txt || true
|
||||||
|
echo "PR_FILE=/tmp/renovate_prs.txt" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
- name: Process Renovate PRs (poll CI, merge when green)
|
||||||
|
if: steps.find_prs.outputs.PR_FILE != ''
|
||||||
|
env:
|
||||||
|
REPO: ${{ env.REPO }}
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
MAX_WAIT_MIN: ${{ github.event.inputs.max_wait_minutes }}
|
||||||
|
INTERVAL_SEC: ${{ github.event.inputs.interval_seconds }}
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
owner_repo="${REPO}"
|
||||||
|
owner="${owner_repo%%/*}"
|
||||||
|
repo="${owner_repo##*/}"
|
||||||
|
prfile="${{ steps.find_prs.outputs.PR_FILE }}"
|
||||||
|
|
||||||
|
# Read PR numbers into array (skip empty lines)
|
||||||
|
mapfile -t prs < <(sed '/^\s*$/d' "$prfile" || true)
|
||||||
|
|
||||||
|
if [ "${#prs[@]}" -eq 0 ]; then
|
||||||
|
echo "No renovate PRs found. Exiting."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Found ${#prs[@]} renovate PR(s): ${prs[*]}"
|
||||||
|
|
||||||
|
# compute attempts
|
||||||
|
max_wait_min="${MAX_WAIT_MIN:-30}"
|
||||||
|
interval_sec="${INTERVAL_SEC:-60}"
|
||||||
|
if ! [[ "$max_wait_min" =~ ^[0-9]+$ ]]; then max_wait_min=30; fi
|
||||||
|
if ! [[ "$interval_sec" =~ ^[0-9]+$ ]]; then interval_sec=60; fi
|
||||||
|
max_attempts=$(( (max_wait_min * 60 + interval_sec - 1) / interval_sec ))
|
||||||
|
echo "Will poll up to $max_attempts times (interval ${interval_sec}s), max wait ${max_wait_min} minutes per PR."
|
||||||
|
|
||||||
|
for pr in "${prs[@]}"; do
|
||||||
|
echo
|
||||||
echo "=== Processing PR #$pr ==="
|
echo "=== Processing PR #$pr ==="
|
||||||
|
|
||||||
body=$(gh pr view $pr --repo ${{ github.repository }} --json body --jq .body)
|
# PR body
|
||||||
update_type=$(echo "$body" | grep -Eo '\|\s+[a-zA-Z0-9/_-]+\s+\|\s+(major|minor|patch)\s+\|' | head -n1 | awk -F'|' '{print $3}' | xargs)
|
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
|
||||||
|
|
||||||
|
# Detect update type from table (Update column)
|
||||||
|
# Looks for e.g. "| package/name | minor | vX -> vY |"
|
||||||
|
update_type=$(echo "$body" | grep -m1 -Po '\|\s*[^|]+\s*\|\s*\K(major|minor|patch)(?=\s*\|)' || true)
|
||||||
|
update_type=${update_type:-unknown}
|
||||||
echo "Detected 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 "Skipping PR #$pr because it's not patch/minor (detected=$update_type)."
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
sha=$(gh pr view $pr --repo ${{ github.repository }} --json headRefOid --jq .headRefOid)
|
# head sha
|
||||||
echo "PR #$pr head SHA: $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 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
|
||||||
|
|
||||||
# 最多等 30 分钟(30 次 * 60 秒)
|
merged=0
|
||||||
for i in {1..30}; do
|
attempt=0
|
||||||
echo "🔎 Check attempt $i for PR #$pr ..."
|
while [ $attempt -lt $max_attempts ]; do
|
||||||
|
attempt=$((attempt+1))
|
||||||
|
echo "Attempt $attempt/$max_attempts for PR #$pr ..."
|
||||||
|
|
||||||
# 获取 combined 状态
|
# Combined status (legacy) - summary: success / pending / failure / unknown
|
||||||
state=$(gh api repos/${{ github.repository }}/commits/$sha/status --jq .state)
|
combined_state=$(gh api repos/"$owner"/"$repo"/commits/"$sha"/status --jq .state 2>/dev/null || echo "unknown")
|
||||||
echo "Combined status = $state"
|
echo "Combined status for $sha: $combined_state"
|
||||||
|
|
||||||
# 获取详细 check-runs
|
# Show detailed check-runs
|
||||||
echo "---- Check runs for $sha ----"
|
echo "Check runs for $sha:"
|
||||||
gh api repos/${{ github.repository }}/commits/$sha/check-runs \
|
gh api repos/"$owner"/"$repo"/commits/"$sha"/check-runs --jq '.check_runs[] | {name: .name, status: .status, conclusion: .conclusion}' 2>/dev/null || echo "(no check-runs or API returned none)"
|
||||||
--jq '.check_runs[] | {name: .name, status: .status, conclusion: .conclusion}' || true
|
|
||||||
echo "--------------------------------"
|
|
||||||
|
|
||||||
if [[ "$state" == "success" ]]; then
|
# Count in-progress/pending check-runs (status == in_progress OR conclusion == null)
|
||||||
echo "✅ All checks passed for PR #$pr. Merging..."
|
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)
|
||||||
gh pr merge $pr \
|
inprogress_count=$(echo "$inprogress_count" | tr -d '[:space:]')
|
||||||
--squash \
|
inprogress_count=${inprogress_count:-0}
|
||||||
--delete-branch \
|
echo "In-progress/pending check-runs: $inprogress_count"
|
||||||
--repo ${{ github.repository }}
|
|
||||||
break
|
# Decide
|
||||||
elif [[ "$state" == "failure" ]]; then
|
if [[ "$combined_state" == "success" && "$inprogress_count" -eq 0 ]]; then
|
||||||
echo "❌ Checks failed for PR #$pr. Skipping."
|
echo "All checks passed for PR #$pr. Attempting merge now..."
|
||||||
|
if gh pr merge "$pr" --repo "$owner_repo" --squash --delete-branch --yes; then
|
||||||
|
echo "✅ PR #$pr merged successfully."
|
||||||
|
merged=1
|
||||||
|
break
|
||||||
|
else
|
||||||
|
echo "⚠️ gh pr merge failed for PR #$pr (might be branch protection requiring review or merge conflict). Will not retry."
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
elif [[ "$combined_state" == "failure" ]]; then
|
||||||
|
echo "Checks failed for PR #$pr (combined_state=failure). Skipping merge."
|
||||||
break
|
break
|
||||||
else
|
else
|
||||||
echo "⏳ Still pending checks for PR #$pr. Waiting 60s..."
|
echo "Checks not yet all green (state=$combined_state, inprogress=$inprogress_count). Waiting ${interval_sec}s before retry..."
|
||||||
sleep 60
|
sleep "$interval_sec"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
if [ $merged -eq 0 ]; then
|
||||||
|
echo "Did not merge PR #$pr in allowed time window."
|
||||||
|
# optional: leave a comment for visibility
|
||||||
|
# gh pr comment "$pr" --repo "$owner_repo" --body "Auto-merge attempt: CI didn't become green within ${max_wait_min} minutes. Please check."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# small sleep to avoid rate limits
|
||||||
|
sleep 1
|
||||||
done
|
done
|
||||||
env:
|
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
echo "Processing complete."
|
||||||
|
|||||||
Reference in New Issue
Block a user