Skip to content

Instantly share code, notes, and snippets.

@bokenator
Last active March 3, 2016 22:35
Show Gist options
  • Save bokenator/42aeb02e0aefe55d5a9a to your computer and use it in GitHub Desktop.
Save bokenator/42aeb02e0aefe55d5a9a to your computer and use it in GitHub Desktop.
git PT integration
#!/usr/bin/env bash
# post-receive
# Get config variables
api_token=$(git config --get hooks.pivotal-tracker.api-token)
pid=$(git config --get hooks.pivotal-tracker.project-id)
branch=$(git config --get hooks.pivotal-tracker.integration-branch)
mark_accepted () {
curl --silent -X PUT -H "X-TrackerToken: $api_token" -H "Content-Type: application/json" -d '{"current_state": "accepted"}' "https://www.pivotaltracker.com/services/v5/projects/$pid/stories/$1" > /dev/null
}
mark_delivered () {
curl --silent -X PUT -H "X-TrackerToken: $api_token" -H "Content-Type: application/json" -d '{"current_state": "delivered"}' "https://www.pivotaltracker.com/services/v5/projects/$pid/stories/$1" > /dev/null
}
post_comment () {
curl --silent -X POST -H "X-TrackerToken: $api_token" -H "Content-Type: application/json" -d "{\"text\":\"Commit\\n$2\"}" "https://www.pivotaltracker.com/services/v5/projects/$pid/stories/$1/comments" > /dev/null
}
parse_msg () {
# $1: if we are on integration branch
# $2: commit message
#Find the keywords in the message
commit_msg=${2:41}
if [[ $commit_msg == *"resolves"* ]] || [[ $commit_msg == *"implements"* ]] || [[ $commit_msg == *"fixes"* ]]; then
keyword=true
else
keyword=false
fi
local IFS=$'\n'
# Isolate the ticket IDs
ticket_ids=( $(grep -oP "[\[\s|,]#(\d*)[\],]" <<< $commit_msg) )
# The first branch that the change was committed to
committed_to=$(grep -oP "committed\sto\s(\w+)" <<< $commit_msg)
commit_branch=${committed_to:13}
for i in ${!ticket_ids[@]}; do
ticket_ids[$i]=$(sed 's/^..\(.*\).$/\1/' <<< ${ticket_ids[$i]})
done
unset IFS
if $1 && $keyword; then
# In integration branch, has keyword, mark tickets as "ACCEPTED"
for id in ${ticket_ids[@]}; do
mark_accepted $id
echo "First committid to $commit_branch" >> $log
if [[ $commit_branch == $branch ]]; then
post_comment $id "$commit_msg"
fi
done
elif $keyword; then
# Other branch, has keywords, mark tickets as "DELIVERED"
# If not, do nothing
for id in ${ticket_ids[@]}; do
mark_delivered $id
post_comment $id "$commit_msg"
done
else
for id in ${ticket_ids[@]}; do
post_comment $id "$commit_msg"
done
fi
}
while read old_val new_val ref; do
if expr "$ref" : "^refs/heads/"; then
# Find the branch of the commit
ref_branch=$(git rev-parse --symbolic --abbrev-ref $ref)
# If we pushed te integration branch
if [[ $ref_branch == $branch ]]; then
integration=true
else
integration=false
fi
# If the branch is new
if expr "$old_val" : '0*$' > /dev/null; then
new_branch=true
else
new_branch=false
fi
IFS=$'\n'
if $new_branch; then
# If integration branch is created from scratch, mark ALL commits
if $integration; then
commit_msgs=( $(git log --pretty=oneline $new_val) )
fi
# !!!! New branch must be pushed immediately after creation !!!!
else
commit_msgs=( $(git log --pretty=oneline "$old_val..$new_val") )
fi
for msg in "${commit_msgs[@]}"; do
parse_msg $integration $msg
done
unset IFS
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment