Skip to content

Instantly share code, notes, and snippets.

@dpaluy
Last active June 9, 2025 18:40
Show Gist options
  • Save dpaluy/92f651887db83f48b8c137d676dcc96d to your computer and use it in GitHub Desktop.
Save dpaluy/92f651887db83f48b8c137d676dcc96d to your computer and use it in GitHub Desktop.
Poll Github Pull Request for checks

Poll CI Status: $ARGUMENTS

Use this command when you need to wait for all GitHub checks on a pull-request to complete and then branch into success or failure flows.

Parameters (space-separated)

Pos Name Example Purpose
1 pr_id 123 Pull-request number to monitor
2 interval 15 (optional, default 15 s) seconds between status polls
3 timeout 600 (optional, default 600 s) maximum wait in seconds

Environment Variables (optional)

Name Example Purpose
OWNER YOUR ORG Repository owner (auto-detected from current repo if unset)
REPO YOUR REPO Repository name (auto-detected from current repo if unset)

Steps

  1. Run polling script
./scripts/poll_ci.sh $ARGUMENTS

The script will automatically detect the repository owner and name from the current directory if not provided via environment variables.

#!/usr/bin/env bash
set -euo pipefail
PR="$1"
INTERVAL="${2:-15}" # seconds
TIMEOUT="${3:-600}" # seconds
START=$(date +%s)
# Auto-detect repository owner and name if not provided
if [[ -z "${OWNER:-}" ]] || [[ -z "${REPO:-}" ]]; then
REPO_INFO=$(gh repo view --json owner,name -q '.owner.login + "/" + .name')
OWNER=$(echo "$REPO_INFO" | cut -d'/' -f1)
REPO=$(echo "$REPO_INFO" | cut -d'/' -f2)
fi
echo "↪️ Polling PR #$PR every $INTERVAL s for $TIMEOUT s max"
while :; do
# We use `|| true` so that `gh`'s non-zero exit code on pending or
# failed checks don't trigger `set -e` and kill the script.
CHECKS_OUTPUT=$(gh pr checks "$PR" --repo "$OWNER/$REPO" 2>&1 || true)
if echo "$CHECKS_OUTPUT" | grep -E "fail|failure|error" -q; then
echo "❌ PR #$PR is red"
echo "$CHECKS_OUTPUT"
exit 1
elif echo "$CHECKS_OUTPUT" | grep "pending" -q; then
echo "• $(date +"%H:%M:%S") → pending"
elif ! echo "$CHECKS_OUTPUT" | grep -E "fail|failure|error|pending|skipping" -q; then
# If there are no failing, pending, or skipping checks, we can assume success.
# This is more robust than just checking for "pass", as some CIs might
# not report a passing state explicitly if all jobs are neutral/successful.
echo "✅ PR #$PR is green"
echo "$CHECKS_OUTPUT"
exit 0
else
# This state means there are no failures and no pending, but some are
# skipping or in another state. We wait.
echo "• $(date +"%H:%M:%S") → waiting for checks to complete"
fi
(( $(date +%s) - START > TIMEOUT )) && {
echo "⏰ PR #$PR timed out"
echo "$CHECKS_OUTPUT"
exit 2
}
sleep "$INTERVAL"
done
@dpaluy
Copy link
Author

dpaluy commented Jun 9, 2025

This script can be used with Claude Code CLI

@dpaluy
Copy link
Author

dpaluy commented Jun 9, 2025

You can add this to your CLAUDE.md definition of done section:

Monitor CI status using: `./scripts/poll_ci.sh <PR_NUMBER>`
   - The script will automatically poll GitHub checks until they complete
   - Exit codes: 0 (success), 1 (failure), 2 (timeout)
   - Optional parameters: interval (default 15s), timeout (default 600s)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment