Skip to content

Commit eb2be71

Browse files
committed
Add step to wait for passing checks
* Add step with a script which iterates through all the checks for a commit and waits until there are no pending and no failing checks * I tried waiting for a success but this didn't work because it's possible to have successful checks and pending/failing checks simultaneously, it was more reliable to wait for the absence of pending/failing checks then the presence of successful checks * Exclude the `wait_for_checks` check or else it loops until the max retries are reached (I learned this the hard way) * Add `wait_for_checks` to the auto_approve_and_merge step's need attribute so it will only run once the `wait_for_checks` has succeeded
1 parent 175f0f1 commit eb2be71

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

.github/workflows/dependabot-auto-approve.yml

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,65 @@ jobs:
2020
- if: "!contains(steps.dependabot-metadata.outputs.package-ecosystem, 'npm') && (steps.dependabot-metadata.outputs.update-type == 'version-update:semver-patch')"
2121
id: check_if_allowed_dependency
2222
run: echo "is_allowed_dependency=1" >> "$GITHUB_OUTPUT"
23+
wait_for_checks:
24+
runs-on: ubuntu-latest
25+
needs: [ validate_dependabot_opened_this_PR, validate_this_is_an_allowed_dependency ]
26+
steps:
27+
- name: Wait for required checks to pass
28+
env:
29+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
30+
REPO: ${{ github.repository }}
31+
SHA: ${{ github.event.pull_request.head.sha }}
32+
run: |
33+
echo "Waiting for all required checks to pass on commit $SHA..."
34+
35+
MAX_RETRIES=30
36+
RETRY_DELAY=10
37+
ATTEMPT=0
38+
39+
while [ $ATTEMPT -lt $MAX_RETRIES ]; do
40+
echo "Attempt $((ATTEMPT+1))/$MAX_RETRIES"
41+
42+
# Get all check runs for the commit
43+
RESPONSE=$(curl -s -H "Authorization: token $GH_TOKEN" \
44+
"https://api.github.com/repos/$REPO/commits/$SHA/check-runs")
45+
46+
# Exclude self (wait_for_checks)
47+
EXCLUDED_CHECK="wait_for_checks"
48+
49+
echo "Ignoring ${EXCLUDED_CHECK}"
50+
51+
# Extract conclusions
52+
CONCLUSIONS=$(echo "$RESPONSE" | jq -r '.check_runs[] | select(.name != "wait_for_checks") | .name + ":" + (.status + "/" + (.conclusion // "none"))')
53+
54+
echo "$CONCLUSIONS"
55+
56+
# Are there any in_progress or queued?
57+
PENDING=$(echo "$RESPONSE" | jq -r --arg exclude "$EXCLUDED_CHECK" '[.check_runs[] | select(.name != $exclude and .status != "completed")] | length')
58+
59+
# Are all completed and successful?
60+
FAILED=$(echo "$RESPONSE" | jq -r --arg exclude "$EXCLUDED_CHECK" '[.check_runs[] | select(.name != $exclude and .status == "completed" and .conclusion != "success")] | length')
61+
62+
if [ "$PENDING" -eq 0 ] && [ "$FAILED" -eq 0 ]; then
63+
echo "All checks completed successfully."
64+
exit 0
65+
fi
66+
67+
if [ "$FAILED" -gt 0 ]; then
68+
echo "One or more checks failed."
69+
exit 1
70+
fi
71+
72+
echo "Some checks still pending. Waiting $RETRY_DELAY seconds..."
73+
sleep $RETRY_DELAY
74+
ATTEMPT=$((ATTEMPT + 1))
75+
done
76+
77+
echo "Timed out waiting for checks to complete."
78+
exit 1
2379
auto_approve_and_merge:
2480
runs-on: ubuntu-latest
25-
needs: [validate_dependabot_opened_this_PR, validate_this_is_an_allowed_dependency]
81+
needs: [validate_dependabot_opened_this_PR, validate_this_is_an_allowed_dependency, wait_for_checks]
2682
if: ${{ needs.validate_this_is_an_allowed_dependency.outputs.is_allowed_dependency == 1 }}
2783
steps:
2884
- uses: actions/checkout@v4

0 commit comments

Comments
 (0)