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.
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.
I happen to have a pre-commit hook that covers this (as well as many other things).
The hook is available via Git-Hub. It doesn't verify that the Jira ID exists, but it can verify that a Jira like ID does exist in the commit message. This is usually good enough to ensure developers are adding in Jira ticket numbers into their commit message. There's an example control.ini
file that shows you how to check for a Jira like ticket number in your commit message. Valid commit messages would be formatted as follows:
However, a better way than a pre-commit hook is to change the culture of the workplace, so that developers will naturally put the Jira ticket number in commit messages and will automatically give more detailed commit messages. I found that using a continuous build server like Jenkins will do this.
Jenkins will automatically build your code with each check in. Each build shows you the changes, and the commit comment. Jenkins integrates to Jira, so with one click, you can see the Jira information. Jenkins will also attach the commit message and build # onto the Jira ticket, so a QA person can look at a particular Jira ticket, and see which build fixed this ticket.
Suddenly, the commit message information becomes more visible. Developers and QA start to rely on it. Developers who don't add the Jira ticket, are hounded not by the guy who does the builds, but by their boss and their fellow developers. Putting good commit messages now becomes the culture of the place. And, that's a way better enforcer than any pre-commit hook.
Atlassian provides a trigger script that does this for most common VCS systems including SVN, plus a JIRA plugin that lets you define what to look for in the commit description. See the JIRA Commit Acceptance plugin page.
Well, i did it creating a simple shell script (pre-commit) hook that verify if jira issue key was supplied by a regex and call a REST API to guarantee the issue exists on the server
#!/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
Commit Policy Plugin is a fairly new JIRA add-on to enforce this.
Unlike the other solutions suggested here, it does not only verify if there is an issue key like pattern in the commit message (like "FOO-123"), but even matches that against a configure JQL query in JIRA!
For instance, this allows to check if the mentioned issue(s) are:
Other than checking the mentioned issues, it can also verify:
Make sure you check out the documentation and try it.
Disclaimer: I'm a developer working on this add-on. Nevertheless this is the best solution available to this problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With