Skip to content

Instantly share code, notes, and snippets.

@julien731
Last active January 18, 2020 09:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julien731/6b6eb4d8a91c1ac8ed1e7f139f176552 to your computer and use it in GitHub Desktop.
Save julien731/6b6eb4d8a91c1ac8ed1e7f139f176552 to your computer and use it in GitHub Desktop.
Post-commit script for BitBucket and Pivotal Tracker integration.
#!/bin/bash
## Set project address (pattern is user/project-name).
project='nimbl3/tankeboksen-web'
if [ -z "$PIVOTAL_TOKEN" ]; then
# Allows us to read user input below, assigns stdin to keyboard
# @see http://stackoverflow.com/a/10015707
exec < /dev/tty
echo "Please set your Pivotal Tracker token to enable the integration. You can find your token on your profile page at https://www.pivotaltracker.com/profile."
echo "API Token:"
read user_token
if [ -z "$user_token" ]; then
echo "You did not specify your API token."
else
# Set the environment variable value and set it.
PIVOTAL_TOKEN=$user_token
echo "export PIVOTAL_TOKEN=$user_token" >> $HOME/.bash_profile
echo "export PIVOTAL_TOKEN=$user_token" >> $HOME/.zshrc
export PIVOTAL_TOKEN
fi
fi
# Get the commit message and hash.
message=$(git log --pretty=format:"%B" -n1 | tr '\n' ' ')
commit_id=$(git log -n1 --pretty=format:"%H%n")
# Get user name.
username=$(git config user.name)
# Prepare the URL to the commit.
commit_url="https://bitbucket.org/$project/commits/$commit_id"
# Prepare the data to post.
payload='{"source_commit":{"commit_id":"'$commit_id'","message":"'$message'","url":"'$commit_url'","author":"'$username'"}}'
# Send POST request to Pivotal Tracker.
curl -X POST -H "X-TrackerToken: $PIVOTAL_TOKEN" -H "Content-Type: application/json" -d "$payload" "https://www.pivotaltracker.com/services/v5/source_commits"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment