#!/bin/bash # pr-ci-wait.sh - Wait for PR CI status to reach terminal state (GitHub/Gitea) # Usage: pr-ci-wait.sh -n [-r owner/repo] [-t timeout_sec] [-i interval_sec] set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$SCRIPT_DIR/detect-platform.sh" PR_NUMBER="" TIMEOUT_SEC=1800 INTERVAL_SEC=15 REPO_OVERRIDE="" usage() { cat < [-t timeout_sec] [-i interval_sec] Options: -n, --number NUMBER PR number (required) -r, --repo OWNER/REPO Repository slug (default: infer from git origin) -t, --timeout SECONDS Max wait time in seconds (default: 1800) -i, --interval SECONDS Poll interval in seconds (default: 15) -h, --help Show this help Examples: $(basename "$0") -n 643 $(basename "$0") -n 643 --repo ddk/ai-bma $(basename "$0") -n 643 -t 900 -i 10 EOF } # get_remote_host and get_gitea_token are provided by detect-platform.sh extract_state_from_status_json() { # Capture piped JSON BEFORE invoking `python3 - <&2 usage >&2 exit 1 ;; esac done if [[ -z "$PR_NUMBER" ]]; then echo "Error: PR number is required (-n)." >&2 usage >&2 exit 1 fi if ! [[ "$TIMEOUT_SEC" =~ ^[0-9]+$ ]] || ! [[ "$INTERVAL_SEC" =~ ^[0-9]+$ ]]; then echo "Error: timeout and interval must be integer seconds." >&2 exit 1 fi if [[ -n "$REPO_OVERRIDE" ]]; then REPO_INFO="$REPO_OVERRIDE" PLATFORM=$(detect_platform 2>/dev/null || echo gitea) else detect_platform > /dev/null REPO_INFO=$(get_repo_info) fi if [[ -z "$REPO_INFO" || "$REPO_INFO" == error:* || "$REPO_INFO" != */* ]]; then echo "Error: Could not determine repository from git origin. Run from a repo or pass --repo owner/repo." >&2 exit 1 fi OWNER=${REPO_INFO%%/*} REPO=${REPO_INFO##*/} START_TS=$(date +%s) DEADLINE_TS=$((START_TS + TIMEOUT_SEC)) if [[ "$PLATFORM" == "github" ]]; then if ! command -v gh >/dev/null 2>&1; then echo "Error: gh CLI is required for GitHub CI status polling." >&2 exit 1 fi HEAD_SHA=$(github_get_pr_head_sha) if [[ -z "$HEAD_SHA" ]]; then echo "Error: Could not resolve head SHA for PR #$PR_NUMBER." >&2 exit 1 fi echo "[pr-ci-wait] Platform=github PR=#${PR_NUMBER} head_sha=${HEAD_SHA}" elif [[ "$PLATFORM" == "gitea" ]]; then HOST=$(get_remote_host 2>/dev/null || echo "git.mosaicstack.dev") TOKEN=$(get_gitea_token "$HOST") || { echo "Error: Gitea token not found. Set GITEA_TOKEN or configure ~/.git-credentials." >&2 exit 1 } HEAD_SHA=$(gitea_get_pr_head_sha "$HOST" "$OWNER/$REPO" "$TOKEN") if [[ -z "$HEAD_SHA" ]]; then echo "Error: Could not resolve head SHA for PR #$PR_NUMBER." >&2 exit 1 fi echo "[pr-ci-wait] Platform=gitea host=${HOST} repo=${OWNER}/${REPO} PR=#${PR_NUMBER} head_sha=${HEAD_SHA}" else echo "Error: Unsupported platform '${PLATFORM}'." >&2 exit 1 fi while true; do NOW_TS=$(date +%s) if (( NOW_TS > DEADLINE_TS )); then echo "Error: Timed out waiting for CI status on PR #$PR_NUMBER after ${TIMEOUT_SEC}s." >&2 exit 124 fi if [[ "$PLATFORM" == "github" ]]; then STATUS_JSON=$(github_get_commit_status_json "$OWNER" "$REPO" "$HEAD_SHA") else STATUS_JSON=$(gitea_get_commit_status_json "$HOST" "$OWNER/$REPO" "$TOKEN" "$HEAD_SHA") fi STATE=$(printf '%s' "$STATUS_JSON" | extract_state_from_status_json) echo "[pr-ci-wait] state=${STATE} pr=#${PR_NUMBER} sha=${HEAD_SHA}" case "$STATE" in success) printf '%s' "$STATUS_JSON" | print_status_summary echo "[pr-ci-wait] CI is green for PR #$PR_NUMBER." exit 0 ;; failure|error) printf '%s' "$STATUS_JSON" | print_status_summary echo "Error: CI reported ${STATE} for PR #$PR_NUMBER." >&2 exit 1 ;; pending|unknown) sleep "$INTERVAL_SEC" ;; *) echo "[pr-ci-wait] Unrecognized state '${STATE}', continuing to poll..." sleep "$INTERVAL_SEC" ;; esac done