Skip to content

Instantly share code, notes, and snippets.

@OliverJAsh
Last active March 6, 2019 15:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OliverJAsh/06ac17b9dacff7a15f5c084c11cfd57d to your computer and use it in GitHub Desktop.
Save OliverJAsh/06ac17b9dacff7a15f5c084c11cfd57d to your computer and use it in GitHub Desktop.
`git merge-pr`: CLI for merging GitHub PRs (via API) with completions
#compdef git-merge-pr
_git-merge-pr () {
# Note: requires hub and jq to be installed
# https://developer.github.com/v3/pulls/#list-pull-requests
# hub api /repos/{owner}/{repo}/pulls
PRS=$(
hub api /repos/{owner}/{repo}/pulls \
| jq 'map({title, number, head})'
)
PRS_DETAIL_STRING=$(
echo $PRS \
| jq --raw-output 'map((.number | tostring) + ": " + .head.ref + ", " + .title) | .[]'
)
# https://unix.stackexchange.com/questions/29724/how-to-properly-collect-an-array-of-lines-in-zsh
# TODO: avoid leaking IFS?
IFS=$'\n' PRS_DETAIL=($(echo "$PRS_DETAIL_STRING"))
_describe 'pull request' PRS_DETAIL
}
#!/bin/bash
# Note: requires hub and jq to be installed
# If a script errors, force the script to fail immediately.
set -e
ID=$1
# https://unix.stackexchange.com/questions/225943/except-the-1st-argument/225951#225951
REST=${@:2}
PR_PATH="repos/{owner}/{repo}/pulls/$ID"
echo "Merging $ID"
# https://developer.github.com/v3/pulls/#merge-a-pull-request-merge-button
echo $REST | xargs hub api -XPUT $PR_PATH/merge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment