Skip to content

Instantly share code, notes, and snippets.

@jtyr
Last active July 26, 2023 19:58
Show Gist options
  • Save jtyr/96aa214ce0719d9e5f1449d11fc1764c to your computer and use it in GitHub Desktop.
Save jtyr/96aa214ce0719d9e5f1449d11fc1764c to your computer and use it in GitHub Desktop.
GitHub PR waiting
#!/usr/bin/env bash
# Exit on any error
set -o errexit -o pipefail -o noclobber -o nounset
function msg() {
TYPE="$1"
TEXT="$2"
EXIT="${3:-}"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $TYPE: $TEXT"
if [[ -n $EXIT ]]; then
exit "$EXIT"
fi
}
function usage() {
EXIT="${1:-0}"
echo "Usage: $0 [-a] [--help]"
echo ''
echo 'Options:'
echo ' -a, --ignore-approvals Do not wait for approvals'
echo ' --help Show this helm message'
echo ''
echo 'Examples:'
echo '# Create PR, wait for all checks and approvals and then merge it'
echo "gh pr create -f && $0 && gh pr merge -sd"
echo '# Create PR, wait for all checks, skip approvals and then merge it'
echo "gh pr create -f && $0 -a && gh pr merge -sd --admin"
exit "$EXIT"
}
function main() {
# Define long and short options
local LONGOPTS='ignore-approvals,help'
local OPTIONS='ah'
# Parse options
# shellcheck disable=SC2155
local PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "${0##*/}" -- "$@"; echo $?)
# shellcheck disable=SC2206
PARSED=($PARSED)
# Check if getopt failed
if [[ ${#PARSED[@]} -lt 2 ]] || [[ ${PARSED[-1]} != 0 ]]; then
msg 'E' 'Execution failed'
usage 1
fi
# Re-read parsed options
set -- "${PARSED[@]:0:${#PARSED[@]}}"
# Default values
IGNORE_APPROVALS=0
SHOW_USAGE=0
# Process individual options
while true; do
case "$1" in
-a|--ignore-approvals)
IGNORE_APPROVALS=1
shift
;;
--help)
SHOW_USAGE=1
shift
;;
--)
shift
break
;;
*)
msg 'E' 'This should never happened'
usage 3
;;
esac
done
# Show the help message and exit
if [[ $SHOW_USAGE == 1 ]]; then
usage
fi
# Wait for the PR to be created
while [[ $(gh pr checks 2>&1 | grep -c 'no pull requests found') -gt 0 ]]; do
msg 'I' 'Waiting for PR to be created'
sleep 5
done
# Wait for the checks to start running
while [[ $(gh pr checks 2>&1 | grep -c 'no checks reported') -gt 0 ]]; do
msg 'I' 'Waiting for checks to start running'
sleep 5
done
# Wait for the checks to finish
while [[ $(gh pr checks | cut -f2 | grep -c 'pending') -gt 0 ]]; do
msg 'I' 'Waiting for checks to finish'
sleep 10
done
# Check if there is any check that failed
if [[ $(gh pr checks | cut -f2 | grep -Pcv '(pass|skipping)') -gt 0 ]]; then
msg 'E' 'PR checks failed' 1
fi
if [[ $IGNORE_APPROVALS == 0 ]]; then
# Wait for approvals
while [[ $(gh pr status --json reviewDecision --jq '.currentBranch.reviewDecision') == 'REVIEW_REQUIRED' ]]; do
msg 'I' 'Waiting for approvals'
sleep 10
done
fi
msg 'I' 'PR can be merged now'
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment