#!/usr/bin/env bash
#
# Open GitHub CI for the current branch

set -e

export GH_NO_UPDATE_NOTIFIER=1
unset BROWSER

# GitHub CLI returns non-zero error code if checks are not complete:
ghoutput=$(gh pr checks || true)
if ! echo "$ghoutput" | grep -q 'https://'; then
  exit 1
fi

url=$(echo $ghoutput | grep -o 'https://.*?' | tr -d '?')

if [ $(uname -s) = "Linux" ]; then
  progname="xdg-open"
else
  progname="open" # macOS
fi

eval "$progname $url"