Skip to content

Commit 7cb5808

Browse files
committed
fix(auto-merge): job waiting loop should wait for all jobs, not just status checks
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent ea4e0ee commit 7cb5808

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

.github/workflows/auto-merge.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,45 @@ jobs:
5353
-
5454
name: Auto-approve all bot-go-openapi PRs
5555
run: gh pr review --approve "$PR_URL"
56+
-
57+
name: Wait for all workflow runs to complete
58+
run: |
59+
PR_NUMBER=$(echo "$PR_URL" | grep -oP '\d+$')
60+
echo "Waiting for all workflow runs to complete on PR #${PR_NUMBER}..."
61+
62+
# Get the PR's head SHA
63+
HEAD_SHA=$(gh pr view "$PR_NUMBER" --json headRefOid --jq '.headRefOid')
64+
echo "Head SHA: ${HEAD_SHA}"
65+
66+
MAX_WAIT=600 # 10 minutes max wait
67+
ELAPSED=0
68+
SLEEP_INTERVAL=10
69+
70+
while [ $ELAPSED -lt $MAX_WAIT ]; do
71+
# Get all workflow runs for this SHA using the GitHub API
72+
RUNS=$(gh api "/repos/${{ github.repository }}/actions/runs?head_sha=${HEAD_SHA}&per_page=100" --jq '.workflow_runs')
73+
74+
# Count how many are still in progress, queued, or waiting
75+
IN_PROGRESS=$(echo "$RUNS" | jq '[.[] | select(.status == "in_progress" or .status == "queued" or .status == "waiting")] | length')
76+
77+
if [ "$IN_PROGRESS" -eq 0 ]; then
78+
echo "::notice::All workflow runs completed for SHA ${HEAD_SHA}"
79+
TOTAL_RUNS=$(echo "$RUNS" | jq 'length')
80+
echo "Total workflow runs: ${TOTAL_RUNS}"
81+
break
82+
fi
83+
84+
# Show which workflows are still running
85+
RUNNING=$(echo "$RUNS" | jq -r '.[] | select(.status == "in_progress" or .status == "queued" or .status == "waiting") | .name' | tr '\n' ', ' | sed 's/,$//')
86+
echo "Still waiting for ${IN_PROGRESS} workflow run(s): ${RUNNING}"
87+
88+
sleep $SLEEP_INTERVAL
89+
ELAPSED=$((ELAPSED + SLEEP_INTERVAL))
90+
done
91+
92+
if [ $ELAPSED -ge $MAX_WAIT ]; then
93+
echo "::warning::Timeout waiting for all workflow runs to complete, proceeding anyway"
94+
fi
5695
-
5796
name: Auto-merge bot-go-openapi PRs
5897
run: gh pr merge --auto --rebase "$PR_URL"

0 commit comments

Comments
 (0)