Skip to content

Instantly share code, notes, and snippets.

@allenevans
Created April 2, 2019 14:24
Show Gist options
  • Save allenevans/a40663119e36f018ccc2d059f109d830 to your computer and use it in GitHub Desktop.
Save allenevans/a40663119e36f018ccc2d059f109d830 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Opens PR if PR is open and commit (sha) exists on remote, else opens repo
#
# Requires environment variable GITHUB_USER_KEY to be set e.g. github_username:github_token
# See https://github.com/settings/tokens
#
# Ensure open-github.sh has execute permissions
# Set alias github="~/open-github.sh"
upstream=`git remote get-url --push origin`
github_host=$(node -e "process.stdout.write(/(?<=@)[a-z0-9\-\.]+/i.exec('${upstream}')[0]);")
repo_path=$(node -e "process.stdout.write(/(?<=\:)(.+)\.git$/i.exec('${upstream}')[1])")
github_api="https://api.${github_host}"
target_sha=$(git rev-parse HEAD)
html_url=$(curl -s -u ${GITHUB_USER_KEY} "${github_api}/repos/${repo_path}" | jq -r ".html_url")
pr_url=$(curl -s -u ${GITHUB_USER_KEY} "${github_api}/repos/${repo_path}/pulls" | jq -r ".[] | (select(.head.sha == \"${target_sha}\")) | ._links.html.href" 2> /dev/null)
open ${pr_url:-$html_url}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment