Skip to content

Instantly share code, notes, and snippets.

@midnightradio
Forked from oliverdaff/jira-precommit.sh
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save midnightradio/9b73f6a5c78911b46288 to your computer and use it in GitHub Desktop.
Save midnightradio/9b73f6a5c78911b46288 to your computer and use it in GitHub Desktop.
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment