Created
April 24, 2012 00:02
-
-
Save oliverdaff/2474752 to your computer and use it in GitHub Desktop.
svn JIRA Precommit hook
This file contains hidden or 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/sh -x | |
## REST API JIRA KEY check | |
#List of projects that do not require strict JIRA KEY commits | |
#list in space and case-insensitive | |
NON_STRICT="sports" | |
# Authentication credentials | |
username=root | |
password=root | |
JIRA_URL="192.168.2.131:8080" | |
DASHBOARD=http://$JIRA_URL/secure/Dashboard.jspa | |
# Set utilities | |
projKey= | |
COOKIE=/tmp/jira.cookie | |
CURL=/usr/bin/curl | |
GREP=/bin/grep | |
AWK=/usr/bin/awk | |
CAT=/bin/cat | |
WGET=/usr/bin/wget | |
SVNLOOK=/usr/bin/svnlook | |
REPOS=$1 | |
TXN=$2 | |
PATH=`$SVNLOOK changed $REPOS -t $TXN` | |
echo $PATH | |
nonstrict=1 | |
strict_check(){ | |
PATH=`$SVNLOOK changed $REPOS -t $TXN` | |
repo_proj=`echo $PATH | $AWK -F " " {'print $2'} | $AWK -F "\/" {'print $1'}` | |
for project in $NON_STRICT; do | |
if [ `echo $project | /usr/bin/tr [:upper:] [:lower:]` = `echo $repo_proj | /usr/bin/tr [:upper:] [:lower:]` ]; then | |
echo "$project is not under Strict Commit do not check with JIRA KEY!" | |
nonstrict=0 | |
fi | |
done; | |
} | |
comment_key(){ | |
#check for JIRA type KEY in commit log and set | |
COMMENT=`$SVNLOOK log $REPOS -t $TXN` | |
JIRA_KEY=`echo $COMMENT | $GREP -Po '[A-Z]*\-[0-9]*'` | |
if [ ! -n $JIRA_KEY ];then | |
echo "Require Valid JIRA KEY part of this commit" | |
exit 1 | |
else | |
projKey=$JIRA_KEY | |
JSON_URL=http://$JIRA_URL/rest/api/latest/issue/$projKey.json | |
JSON_FILE=/tmp/$projKey.json | |
fi | |
} | |
connect(){ | |
#Authenticate to Jira | |
$CURL -s -u $username:$password --cookie-jar $COOKIE --output /dev/null $DASHBOARD || \ | |
echo "Failed to Authenticate against $JIRA_URL" | |
#Download projKey.json from Jira | |
$CURL -s --cookie $COOKIE $JSON_URL -o $JSON_FILE || \ | |
echo "Failed to Collect $projKey from $JIRA_URL" | |
} | |
valid(){ | |
#Verify projKey match to confirm valid JIRA KEY | |
KEY=`$CAT $JSON_FILE | $GREP -Po '"key":"[A-Z]*\-[0-9]*"' | $AWK -F ":" {'print $2'}` | |
if [ "$KEY" = "\"$projKey\"" ];then | |
echo "KEY verified, Valid JIRA Issue" | |
else | |
echo "$project is under Strict Commit check with JIRA KEY!" | |
echo "Key not verified, Not a Valid JIRA issue" | |
exit 1 | |
fi | |
} | |
remove(){ | |
# remove temp files | |
rm $COOKIE $JSON_FILE | |
} | |
#run | |
strict_check | |
if (( $nonstrict ));then | |
comment_key | |
connect | |
valid | |
fi | |
exit 0 |
Guys, pay attention to the regexp'[A-Z]*-[0-9]*'
It is wrong as the * means {0,} In other words messages like "-: my comment", "A-: my comment" & "-1: my comment" pass the validation when they shall not.
Please fix
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is my simple form to check if key already exists on Jira server and validate.