Skip to content

Instantly share code, notes, and snippets.

@oliverdaff
Created April 24, 2012 00:02
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save oliverdaff/2474752 to your computer and use it in GitHub Desktop.
Save oliverdaff/2474752 to your computer and use it in GitHub Desktop.
svn JIRA Precommit hook
#!/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
@ricardojlrufino
Copy link

SIMPLE VALIDATION !!

#!/bin/sh
REPOS="$1"
TXN="$2"

# Make sure that the log message contains some text.
SVNLOOK=/usr/bin/svnlook
$SVNLOOK log -t "$TXN" "$REPOS" | \
   grep "[a-zA-Z0-9]" > /dev/null || \
   ERRORMSG="Informe uma mensagem de commit"

$SVNLOOK log -t "$TXN" "$REPOS" | \
   grep -E "[A-Za-z]{1,4}-[0-9]+|^AJUSTE|^NONE" > /dev/null || \
   ERRORMSG="Informe o ID da Tarefa. Ex: EDU-123 #close - descricao"


if [ -n "$ERRORMSG" ]; then
  echo -e "[ALERTA]: $ERRORMSG" 1>&2
  exit 1
fi


# All checks passed, so allow the commit.
exit 0

Copy link

ghost commented Nov 9, 2015

This is my simple form to check if key already exists on Jira server and validate.

#!/bin/sh

REPOS="$1"
TXN="$2"

SVNLOOK=/usr/bin/svnlook
JIRAURL=http://myown.jira.server:8080/rest/api/latest/issue
CURL=/usr/bin/curl

LOGMSG=$($SVNLOOK log -t "$TXN" "$REPOS")

# check if any comment has supplied by the commiter
if [ -z "$LOGMSG" ]; then
echo "Your commit was blocked because it have no comments." 1>&2
exit 1
fi

#check minimum size of text
if [ ${#LOGMSG} -lt 20 ]; then
echo "Your Commit was blocked because the comments does not meet minimum length requirements (20 letters)." 1>&2
exit 1
fi

# get jira ID by regex
JIRAID=$(expr "$LOGMSG" : '^\([A-Z]*-[0-9]*\)[: ].*')

# Check if jira id was found. 
if [ -z "$JIRAID" ]; then
echo "No JIRA id found in log message \"$LOGMSG\"" 1>&2
echo "Please use log message of the form JIRA-ID: My message" 1>&2
exit 1
fi

# check if JIRA issue exists on the server
JSON_RETURN=$(${CURL} -s ${JIRAURL}/${JIRAID} -u username:password | grep -o "errorMessage")

if [ -n "$JSON_RETURN" ]; then
        echo "The specified Jira Issue \"$JIRAID\" was not found in Jira server." 1>&2
        echo "Please, verify the specified issue ID and try again!" 1>&2
        exit 1
fi

@Dumk0
Copy link

Dumk0 commented Sep 16, 2016

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