Created
April 2, 2019 14:24
-
-
Save allenevans/a40663119e36f018ccc2d059f109d830 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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