#!/usr/bin/env bash

# Select and checkout a GitHub pull request using the GitHub CLI (required)
set -e

export GH_NO_UPDATE_NOTIFIER=1
unset BROWSER

if [ "$1" = '-a' ]; then
  cmd='gh pr list --limit 100' 
elif [ -z "$GITHUB_USERNAME" ]; then
  echo 'set $GITHUB_USERNAME first'
  exit 1
else
  cmd="gh pr list --author $GITHUB_USERNAME" 
fi

prid=$(eval "$cmd" | fzf | awk '{print $1}')
if [ -z $prid ]; then
  exit 0
fi
gh pr checkout $prid