Restricting Subversion commits if the Jira Issue key is Not in the commit message

commit, jira, svn

Solution

It's not difficult to check that the issue exists in JIRA also, using the JIRA ReST API.

In our case I used the `pre-commit.tmpl` file and added the following after the opening comments section:

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

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

# Make sure that the log message contains some text.
LOGMSG=$($SVNLOOK log -t "$TXN" "$REPOS")
echo ${LOGMSG} | grep "[a-zA-Z0-9]" > /dev/null || exit 1

# check that log message starts with a JIRA ticket
# should have format 'FOO-123: my commit message' or 'FOO-123 my commit message'
JIRAID=$(expr "${LOGMSG}" : '^\([A-Z]*-[0-9]*\)[: ].*')
if [[ "$JIRAID" == "" ]]
then
  echo "No JIRA id found in log message \"${LOGMSG}\"" >&2
  echo "Please use log message of the form \"JIRA-ID: My message\"" >&2
  exit 1
fi

# check if JIRA issue exists
JIRAISSUE=$(${CURL} ${JIRAURL}/${JIRAID})
if [[ "${JIRAISSUE}" =~ "Issue Does Not Exist" ]]
then
  echo "The JIRA id ${JIRAID} was not found" >&2
  echo "Please use log message of the form \"JIRA-ID: My message\"" >&2
  exit 1
fi

This requires the the message to be of the form "JIRA-id: text" or "JIRA-id test". You could make the regular expression more general to allow a JIRA id anywhere in the text. You could also add checks on the `${JIRAISSUE}` to ensure that the issue is open if desired, but this seems sufficient for our purposes.

Problem

I am using SVN-1.7.4 for revision control and atlassian JIRA as the issue tracker for my LAMP website. I want to restrict SVN commit if any of my team member commits without mentioning the Jira Issue key for the same. I am using JIRA standalone and have installed it on my server. Google search gave me Subversion Jira Plugin (https://studio.plugins.atlassian.com/wiki/display/SVN/Subversion+JIRA+plugin) but it can only help me out in tracking the commits that had a JIRA key, not in restricting them. Please let me know, if I should post any more specifics about the issue.

Original source