Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

commit

svn

jira

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.

like image 969
minamata Avatar asked May 08 '12 12:05

minamata


5 Answers

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.

like image 119
akosky Avatar answered Nov 02 '22 10:11

akosky


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:

  • NONE: I fixed an issue that had no Jira ticket number
  • FDS-1231: I fixed a single Jira ticket
  • FDS-1231, FDS-3232: I fixed several Jira tickets

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.

like image 30
David W. Avatar answered Nov 02 '22 09:11

David W.


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.

like image 4
gareth_bowles Avatar answered Nov 02 '22 10:11

gareth_bowles


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
like image 3
Tevo Avatar answered Nov 02 '22 11:11

Tevo


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:

  • in-progress user stories in the current sprint
  • unresolved bugs or tasks targeting the next product version
  • high-priority tasks assigned to team-leads (in a code freeze period)

Other than checking the mentioned issues, it can also verify:

  • the committer's identity (is he in JIRA? is he in a JIRA group?)
  • the changed files (only images are submitted? is there any, .class,.obj,*.tmp file? all *.PNG are in the /images directory?)
  • the commit message (is this 10+ characters long without whitespace? does it start with a JIRA key?)

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.

like image 1
Ferenc Kiss Avatar answered Nov 02 '22 09:11

Ferenc Kiss