Skip to content

Instantly share code, notes, and snippets.

@toddself
Created September 18, 2013 15:23
Show Gist options
  • Save toddself/6610784 to your computer and use it in GitHub Desktop.
Save toddself/6610784 to your computer and use it in GitHub Desktop.
Command line pull-request generation with pivotal and github integration.
#/bin/sh
PIVOTAL_ACCESS=
PIVOTAL_PROJECT_ID=
GITHUB_ACCESS=
DEFAULT_BRANCH=development
TO_REPO_OWNER=CondeNast
FROM_REPO_OWNER=CondeNast
function pr (){
ARGS=("$@")
# do we need help?
if [ "${ARGS[0]}" == "-h" ]
then
echo "pr MESSAGE PIVOTAL_TICKET_ID"
return 0
fi
# are we in a git repo?
GIT_BRANCH=$(git branch 2>/dev/null | sed -n '/^\*/s/^\* //p')
if ! git rev-parse --git-dir > /dev/null 2>&1; then
echo "You're not in a git repo..."
return 0
fi
GIT_REPO_NAME=$(basename `git rev-parse --show-toplevel`)
# make the pull request and get the ID
PULL_URL=$(git pull-request "${ARGS[0]}" -b $TO_REPO_OWNER:$DEFAULT_BRANCH -h $FROM_REPO_OWNER:$GIT_BRANCH)
echo "Pull request made: $PULL_URL"
PULL_ID=$(echo $PULL_URL | cut -d/ -f 7)
# do we have a pivotal ticket o deal with?
if [ -n ${ARGS[1]} ]
then
echo "Updating Pivotal..."
# UPDATE PIVOTAL...
PIVOTAL_RESPONSE=$(curl -s -H "X-TrackerToken: $PIVOTAL_ACCESS" -X POST -H "Content-type: application/xml" -d "<note><text>$PULL_URL</text></note>" https://www.pivotaltracker.com/services/v3/projects/$PIVOTAL_PROJECT_ID/stories/${ARGS[1]}/notes)
echo "Updating Github..."
# UPDATE GITHUB...
PAYLOAD="{\"body\": \"https://www.pivotaltracker.com/story/show/${ARGS[1]}\"}"
GIT_URL="https://api.github.com/repos/condenast/$GIT_REPO_NAME/pulls/$PULL_ID"
GIT_RESPONSE=$(curl -s -X PATCH -H "Content-type: application/json" -H "Authorization: token $GITHUB_ACCESS" -d "$PAYLOAD" $GIT_URL)
fi
echo "Done"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment