Skip to content

Instantly share code, notes, and snippets.

@jtyr
Last active July 26, 2023 19:58

Revisions

  1. jtyr revised this gist Jul 26, 2023. 1 changed file with 81 additions and 4 deletions.
    85 changes: 81 additions & 4 deletions ghwait
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,12 @@
    #!/bin/bash
    #!/usr/bin/env bash

    # Usage: gh pr create -f && ghwait && gh pr merge -sd
    # Exit on any error
    set -o errexit -o pipefail -o noclobber -o nounset

    function msg() {
    TYPE="$1"
    TEXT="$2"
    EXIT="$3"
    EXIT="${3:-}"

    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $TYPE: $TEXT"

    @@ -14,8 +15,75 @@ function msg() {
    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'
    @@ -42,7 +110,16 @@ function main() {
    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
    main "$@"
  2. jtyr revised this gist Mar 11, 2022. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions ghwait
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,13 @@ function msg() {


    function main() {
    # 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'
  3. jtyr created this gist Jan 21, 2022.
    41 changes: 41 additions & 0 deletions ghwait
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    #!/bin/bash

    # Usage: gh pr create -f && ghwait && gh pr merge -sd

    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 main() {
    # 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

    msg 'I' 'PR can be merged now'
    }

    main