Skip to content

Instantly share code, notes, and snippets.

@szkrd
Created July 16, 2014 13:42
Show Gist options
  • Save szkrd/d086f515febe7958cd3d to your computer and use it in GitHub Desktop.
Save szkrd/d086f515febe7958cd3d to your computer and use it in GitHub Desktop.
git commit message hook with ticket check
#!/bin/sh
# halp!!4
if [ $# -eq 0 ] || [ $1 == "--help" ] || [ $1 == "-h" ] || [ $1 == "/?" ]; then
echo "check ticket online, pinging jira; usage:"
echo "script (-v) [filename]"
echo "-v = verbose, optional; dumps messages, not just the exit codes"
echo "filename = name of the file containing the commit message, required"
exit 0
fi
# is verbose?
if [ $1 == "-v" ]; then
VERBOSE=1
FILENAME=$2
else
VERBOSE=0
FILENAME=$1
fi
# error exit
die () {
echo "ERROR: Jira ticket ID not found or is invalid!"
exit 1
}
# da magic
TICKET=`cat $FILENAME | grep -E -e'\[[a-zA-Z]+-[0-9]+\]' | head -n1 | sed -n 's/.*\[//p' | sed -n 's/].*//p'`
if [ "$TICKET" == "" ]; then
if [ $VERBOSE -eq 1 ]; then echo "no ticket pattern found"; fi
die
fi
BASEURL='https://jira.3amlabs.net/browse/'
STATUSOK=`curl -s -k -I $BASEURL$TICKET | grep -E -e 'HTTP/[0-9]\.[0-9] (302|200) ' | wc -l`
if [ $STATUSOK -eq 1 ]; then
if [ $VERBOSE -eq 1 ]; then echo "status ok"; fi
NOSUCHTICKET=`curl -s -k $BASEURL$TICKET | grep "does not exist" | wc -l`
if [ $NOSUCHTICKET -eq 1 ]; then
if [ $VERBOSE -eq 1 ]; then echo "but the ticket doesn't exist"; fi
die
else
if [ $VERBOSE -eq 1 ]; then echo "ticket ok"; fi
exit 0
fi
else
if [ $VERBOSE -eq 1 ]; then echo "web server status not ok"; fi
die
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment