-
-
Save jameswhite/3843fadb7a1f5e46f96b5d5b38caa0bf 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 | |
alias git='/usr/local/bin/hub' | |
# Usage: card [ <url> | <org>/<repo>/<type>/<num> | <repo>/<type>/<num> | <type>/<num> | <num> ] | |
# Examples: card [ https://github.com/github/security/issues/2307 | github/security/pulls/263 | security/issues/2307 | pulls/263 | 2307 ] | |
# If no argument is given applescript will get the foremost tab URL from "Google Chrome". | |
export PROJECT_ID='856989' # The Project ID where you'll store your cards | |
export ICEBOX_ID='1415606' # The Column ID where you'll add fresh cards | |
export REPO_ROOT="${HOME}/dev" # Where you keep your org/repo structure | |
export ORG_DEFAULT='github' # Which org to use if none specified | |
export REPO_DEFAULT='security' # Which repo to use if none specified | |
export TYPE_DEFAULT='issues' # issues or pulls if none given? | |
PAGE="$*" | |
# If no page was specified, grab the URL from the foremost tab of "Google Chrome" and use it. | |
[ -z "${PAGE}" ] && export PAGE=$(osascript -e "tell application \"Google Chrome\"" -e " set myURL to the URL of the active tab of the front window" -e "end tell" -e "do shell script \"echo \" & myURL") | |
# Abort if it's not a github.com URL | |
echo "${PAGE}" | grep -q -i "https:\/\/github.com" | |
if [ $? -ne 0 ] ;then | |
echo "not a github url, yo." | |
exit 1 | |
fi | |
# figure out the issue/pull request if a full url was given, or just a number, or a repo/issues/number | |
hints="$(echo "${PAGE}" | sed -e 's/\// /g'| awk '{for(i=NF;i>=1;i--) printf "%s ", $i;print ""}')" | |
ORG=$(echo "$hints" | awk '{print $4}') | |
REPO=$(echo "$hints" | awk '{print $3}') | |
TYPE=$(echo "$hints" | awk '{print $2}') | |
# Common typos of mine | |
[ "${TYPE}" == 'pull' ] && TYPE='pulls' | |
[ "${TYPE}" == 'issue' ] && TYPE='issues' | |
NUMB=$(echo "$hints" | awk '{print $1}') | |
if [ "${TYPE}" != "pulls" ] && [ "${TYPE}" != "issues" ]; then | |
echo "unknown type: ${TYPE}" | |
exit 1; | |
fi | |
# Clone our default repo if it's not there | |
[ -d "${REPO_ROOT}/${ORG_DEFAULT}/${REPG_DEFAULT}" ] || (cd ${REPO_ROOT}/${ORG_DEFAULT} ; git clone https://github.com/${ORG_DEFAULT}/${REPG_DEFAULT}) | |
: ${NUMB:="$(cd ${REPO_ROOT}/${ORG_DEFAULT}/${REPO_DEFAULT}; /usr/local/bin/hub issue | head -1 | sed -e 's/^ *//' -e 's/\].*//')"} | |
: ${TYPE:="${TYPE_DEFAULT}"} | |
: ${REPO:="${REPO_DEFAULT}"} | |
: ${ORG:="${ORG_DEFAULT}"} | |
# use hub to get the issue/pull title and put it into a markdown link, and put that into the paste buffer | |
[ -d "${REPO_ROOT}/${ORG}/${REPO}" ] || (cd ${REPO_ROOT}/${ORG} ; git clone https://github.com/${ORG}/${REPO}) | |
THING=$( | |
cd ${REPO_ROOT}/${ORG}/${REPO}; | |
WORDS=$(/usr/local/bin/hub issue --include-pulls | grep "#${NUMB}" | sed -e 's/ *#[0-9]* *//' -e 's/ .*$//') | |
if [ -z "${WORDS}" ] ; then | |
echo "Unable to get title, is it closed?" 1>&2 | |
else | |
echo "[${WORDS}](https://github.com/${ORG}/${REPO}/${TYPE}/${NUMB})" | pbcopy | |
echo "[${WORDS}](https://github.com/${ORG}/${REPO}/${TYPE}/${NUMB})" | |
fi | |
) | |
# look through all of our cards in the project, if it's already a card, echo "found." otherwise add it as a card in the icebox | |
for id in $(curl -s -u "_:${GITHUB_USER_ACCESS_TOKEN}" -H "Accept: application/vnd.github.inertia-preview+json" -X GET https://api.github.com/projects/${PROJECT_ID}/columns | jq '.[] | .id'); do | |
( | |
( | |
COLUMN="${id}" | |
LAST="" | |
NEXT="https://api.github.com/projects/columns/${COLUMN}/cards" | |
until [ "${NEXT}" == "${LAST}" ];do | |
curl -s -D .pagination -u "_:${GITHUB_USER_ACCESS_TOKEN}" -H "Accept: application/vnd.github.inertia-preview+json" -X GET "${NEXT}" | |
NEXT="$(grep "^Link: " .pagination | sed -e's/>; rel="next",.*//' -e 's/^.*<//')" | |
LAST="$(grep "^Link: " .pagination | sed -e's/.*; rel="next", <//' -e 's/>.*//')" | |
done | |
) | jq -rc '.[] | {"card": .note}' | |
) | |
done | grep -q "https://github.com/${ORG}/${REPO}/${TYPE}/${NUMB}" | |
if [ $? -eq 0 ];then | |
echo "card already exists." | |
else | |
curl -s -u "_:${GITHUB_USER_ACCESS_TOKEN}" -H "Accept: application/vnd.github.inertia-preview+json" -d "{\"note\":\"${THING}\",\"content_type\":\"issue\"}" -X POST https://api.github.com/projects/columns/${ICEBOX_ID}/cards | jq -rc '.note' | |
fi | |
Author
jameswhite
commented
Nov 15, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment