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

霸气de小男生 提交于 2019-12-07 06:08:57

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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



回答5:


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.




回答6:


Following script has added regex to find any pattern like abc-123 and check if it is present in jira. Also any similar pattern could also be present , anywhere in the comment but presence of one successful pattern will allow the commit -

#!/usr/bin/perl -w

$STATUS="1";
$REPOS=$ARGV[0];
$TXN=$ARGV[1];

$SVNLOOK="/usr/bin/svnlook";
$CURL="/usr/bin/curl";

$JIRAURL="http://x.x.x.x/jira/rest/api/2/issue";

$LOGMSG = `$SVNLOOK log -t $TXN $REPOS`;

chomp ($LOGMSG);
print "--$LOGMSG--\n";

if ($LOGMSG !~ /[A-Za-z][A-Za-z]*-[0-9][0-9]*/ ) {
print STDERR "NO JIRA case Found in comment - $LOGMSG";
exit (1);
}
@ARRAY= split (' ',$LOGMSG);

foreach (@ARRAY){

next if ! /[A-Za-z][A-Za-z]*-[0-9][0-9]*/ ;
chop();

$JIRAISSUE = `$CURL $JIRAURL/$_`;
chop ($JIRAISSUE);
if ($JIRAISSUE =~ /Issue Does Not Exist/){
print STDERR "The JIRA id $_ was not found\n";
}else { exit (0) };
}
exit ($STATUS);



回答7:


I have seen this done before using a simple regex in a pre-commit hook.

On the plus side it didn't need to check Jira at all, but it also meant you could just put something in that just looked like a valid issue key.



来源:https://stackoverflow.com/questions/10499098/restricting-subversion-commits-if-the-jira-issue-key-is-not-in-the-commit-messag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!