11#! /bin/bash
22# wait-pr-checks.sh - Poll GitHub Actions status for a PR and exit on first failure or when all pass
33#
4- # Usage: ./scripts/wait-pr-checks.sh [pr-number] [repo]
4+ # Usage: ./scripts/wait-pr-checks.sh [pr-number] [repo] [filter-regex]
55# pr-number: Optional PR number (auto-detects from current branch if not provided)
66# repo: Optional repository in format owner/repo (defaults to coder/httpjail)
7+ # filter-regex: Optional regex to filter workflow names (e.g., "docs|deploy" to only watch docs/deploy workflows)
78#
89# Exit codes:
910# 0 - All checks passed
1415
1516set -euo pipefail
1617
18+ # Initialize filter variable
19+ FILTER_REGEX=" "
20+
1721# Parse arguments and auto-detect PR if needed
1822if [ $# -eq 0 ]; then
1923 # Auto-detect PR from current branch
@@ -27,12 +31,13 @@ if [ $# -eq 0 ]; then
2731 if [ -z " $PR_NUMBER " ]; then
2832 echo " Error: No PR found for branch '${CURRENT_BRANCH} '" >&2
2933 echo " " >&2
30- echo " Usage: $0 [pr-number] [repo]" >&2
34+ echo " Usage: $0 [pr-number] [repo] [filter-regex] " >&2
3135 echo " When called without arguments, auto-detects PR from current branch" >&2
3236 echo " Examples:" >&2
33- echo " $0 # Auto-detect PR from current branch" >&2
34- echo " $0 47 # Monitor PR #47" >&2
35- echo " $0 47 coder/httpjail # Monitor PR #47 in specific repo" >&2
37+ echo " $0 # Auto-detect PR from current branch" >&2
38+ echo " $0 47 # Monitor PR #47" >&2
39+ echo " $0 47 coder/httpjail # Monitor PR #47 in specific repo" >&2
40+ echo " $0 47 coder/httpjail 'docs' # Monitor only docs-related checks" >&2
3641 exit 2
3742 fi
3843
@@ -42,9 +47,17 @@ if [ $# -eq 0 ]; then
4247elif [ $# -eq 1 ]; then
4348 PR_NUMBER=" $1 "
4449 REPO=" coder/httpjail"
45- else
50+ elif [ $# -eq 2 ] ; then
4651 PR_NUMBER=" $1 "
4752 REPO=" $2 "
53+ elif [ $# -eq 3 ]; then
54+ PR_NUMBER=" $1 "
55+ REPO=" $2 "
56+ FILTER_REGEX=" $3 "
57+ else
58+ echo " Error: Too many arguments" >&2
59+ echo " Usage: $0 [pr-number] [repo] [filter-regex]" >&2
60+ exit 2
4861fi
4962
5063# Check for required tools
@@ -60,6 +73,9 @@ YELLOW='\033[1;33m'
6073NC=' \033[0m' # No Color
6174
6275echo " Monitoring PR #${PR_NUMBER} in ${REPO} ..."
76+ if [ -n " $FILTER_REGEX " ]; then
77+ echo " Filtering checks to match: $FILTER_REGEX "
78+ fi
6379echo " Polling every second. Press Ctrl+C to stop."
6480echo " "
6581
@@ -74,11 +90,18 @@ while true; do
7490 continue
7591 fi
7692
93+ # Apply filter if specified
94+ if [ -n " $FILTER_REGEX " ]; then
95+ filtered_output=$( echo " $json_output " | jq --arg regex " $FILTER_REGEX " ' [.[] | select(.name | test($regex; "i"))]' )
96+ else
97+ filtered_output=" $json_output "
98+ fi
99+
77100 # Parse JSON to get counts
78- pending_count=$( echo " $json_output " | jq ' [.[] | select(.state == "PENDING" or .state == "IN_PROGRESS" or .state == "QUEUED")] | length' )
79- failed_count=$( echo " $json_output " | jq ' [.[] | select(.state == "FAILURE" or .state == "ERROR")] | length' )
80- passed_count=$( echo " $json_output " | jq ' [.[] | select(.state == "SUCCESS")] | length' )
81- total_count=$( echo " $json_output " | jq ' length' )
101+ pending_count=$( echo " $filtered_output " | jq ' [.[] | select(.state == "PENDING" or .state == "IN_PROGRESS" or .state == "QUEUED")] | length' )
102+ failed_count=$( echo " $filtered_output " | jq ' [.[] | select(.state == "FAILURE" or .state == "ERROR")] | length' )
103+ passed_count=$( echo " $filtered_output " | jq ' [.[] | select(.state == "SUCCESS")] | length' )
104+ total_count=$( echo " $filtered_output " | jq ' length' )
82105
83106 # Build status string
84107 current_status=" ✓ ${passed_count} passed | ⏳ ${pending_count} pending | ✗ ${failed_count} failed"
@@ -92,13 +115,13 @@ while true; do
92115 # Check for failures
93116 if [ $failed_count -gt 0 ]; then
94117 echo -e " \n\n${RED} ❌ The following check(s) failed:${NC} "
95- echo " $json_output " | jq -r ' .[] | select(.state == "FAILURE" or .state == "ERROR") | " - \(.name)"'
118+ echo " $filtered_output " | jq -r ' .[] | select(.state == "FAILURE" or .state == "ERROR") | " - \(.name)"'
96119
97120 # Try to fetch logs for the first failed check
98121 echo -e " \n${YELLOW} Fetching logs for first failed check...${NC} \n"
99122
100123 # Get the first failed check details
101- first_failed=$( echo " $json_output " | jq -r ' .[] | select(.state == "FAILURE" or .state == "ERROR") | "\(.name)|\(.link)"' | head -1)
124+ first_failed=$( echo " $filtered_output " | jq -r ' .[] | select(.state == "FAILURE" or .state == "ERROR") | "\(.name)|\(.link)"' | head -1)
102125
103126 if [ -n " $first_failed " ]; then
104127 IFS=' |' read -r check_name check_link <<< " $first_failed"
0 commit comments