Skip to content

Commit b89f9d9

Browse files
committed
Add check waiting for dependabot PR auto merge
Signed-off-by: BJ Hargrave <hargrave@us.ibm.com>
1 parent eca6343 commit b89f9d9

1 file changed

Lines changed: 69 additions & 5 deletions

File tree

.github/workflows/dependabot_merge.yaml

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,82 @@ jobs:
2121
permissions:
2222
pull-requests: write
2323
contents: write
24-
name: "Check auto-merge"
2524
runs-on: ubuntu-latest
2625
steps:
2726
- name: "Fetch Dependabot Metadata"
2827
id: dependabot-metadata
2928
uses: dependabot/fetch-metadata@25dd0e34f4fe68f24cc83900b1fe3fe149efef98 # v3.1.0
3029
with:
3130
github-token: ${{ secrets.GITHUB_TOKEN }}
32-
- name: "Enable auto-merge"
31+
- name: "Wait for PR checks"
32+
id: wait-for-pr-checks
3333
if: ${{ contains(fromJSON('["version-update:semver-minor","version-update:semver-patch"]'), steps.dependabot-metadata.outputs.update-type) }}
3434
run: |
35-
gh pr merge --auto --merge "$PR_URL"
35+
# ---- Config -----------------------------------------------------
36+
# Name of the current job's check, as it appears in `gh pr checks`.
37+
# Defaults to GITHUB_JOB; override via SELF_CHECK_NAME if the
38+
# displayed check name differs (e.g. "workflow / job" style names).
39+
SELF_CHECK_NAME="${SELF_CHECK_NAME:-${GITHUB_JOB:-}}"
40+
POLL_INTERVAL="${POLL_INTERVAL:-15}"
41+
TIMEOUT="${TIMEOUT:-1800}"
42+
# -------------------------------------------------------------------
43+
44+
if [[ -z "${PR:-}" ]]; then
45+
PR_ARGS=()
46+
else
47+
PR_ARGS=("$PR")
48+
fi
49+
50+
echo "Waiting on checks for PR ${PR:-<inferred>} (excluding check: '${SELF_CHECK_NAME:-none}')"
51+
52+
start_time=$(date +%s)
53+
54+
while true; do
55+
now=$(date +%s)
56+
elapsed=$(( now - start_time ))
57+
if (( elapsed > TIMEOUT )); then
58+
echo "::error::Timed out after ${TIMEOUT}s waiting for checks to complete."
59+
exit 1
60+
fi
61+
62+
# Pull current checks as JSON, excluding our own check by name.
63+
checks_json=$(gh pr checks "${PR_ARGS[@]}" --json name,state,bucket,link \
64+
-q "[.[] | select(.name != \"${SELF_CHECK_NAME}\")]")
65+
66+
total=$(jq 'length' <<<"$checks_json")
67+
68+
if (( total == 0 )); then
69+
echo "No other checks found on this PR."
70+
exit 0
71+
fi
72+
73+
pending=$(jq '[.[] | select(.bucket == "pending" or .state == "IN_PROGRESS" or .state == "QUEUED")] | length' <<<"$checks_json")
74+
failed=$(jq '[.[] | select(.bucket == "fail")]' <<<"$checks_json")
75+
failed_count=$(jq 'length' <<<"$failed")
76+
77+
echo "[$(date -u +%H:%M:%S)] ${total} check(s) tracked, ${pending} still pending, ${failed_count} failed so far."
78+
79+
# Fail fast as soon as any check reports a failure.
80+
if (( failed_count > 0 )); then
81+
echo "::error::One or more checks failed:"
82+
jq -r '.[] | " - \(.name): \(.state) (\(.link))"' <<<"$failed"
83+
exit 1
84+
fi
85+
86+
if (( pending == 0 )); then
87+
echo "All checks completed successfully."
88+
exit 0
89+
fi
90+
91+
sleep "$POLL_INTERVAL"
92+
done
93+
env:
94+
PR: ${{ github.event.pull_request.html_url }}
95+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
96+
- name: "Auto Merge"
97+
if: ${{ steps.wait-for-pr-checks.outcome == 'success' }}
98+
run: |
99+
gh pr merge "$PR" --auto --merge
36100
env:
37-
PR_URL: ${{github.event.pull_request.html_url}}
38-
GH_TOKEN: ${{secrets.GITHUB_TOKEN}}
101+
PR: ${{ github.event.pull_request.html_url }}
102+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)