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

Snehal Masne August 12, 2014

I am using SVN 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://ecosystem.atlassian.net/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.

Regards,

TechProceed.com

2 answers

1 vote
Midori
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 4, 2015

Use the Commit Policy Plugin.

Learn more about the Subversion-specific parts and use this recipe. If you want to limit which issues you allow to commit against, then also use these recipes.

psaswat April 10, 2019

Hello Team,
I tried placing the script in pre-commit , but it is throwing error in the first line. Could you please help me with the exact workaround where to place the script.

#Check first line contains JiraId
        firstline =lines[0].lstrip()
        if  firstline.find(":")==-1:
            sys.stderr.write(stdErrMsg)
            sys.exit(1)
        prefix= firstline.split(":")[0]
  
        if prefix.strip()=="":
            sys.stderr.write(stdErrMsg)
            sys.exit(1)

        jiraIDpattern = re.compile(r"(\A[A-Z]{2,}-\d+)")
        IDs=prefix.split(",")
        invalidMessage=0
        for rawID in IDs:
            ID=rawID.strip()
            result=jiraIDpattern.match(ID)
            if not result:
                sys.stderr.write("\n '"+ID+"' is not a valid JIRA issue ID")
                invalidMessage=1

        if invalidMessage:
            sys.exit(1)
1 vote
Vijay Khacharia
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 12, 2014

Hi Snehal

You will have to write pre-commit hook in SVN to check if the comment has the JIRA key. You can additionally verify if the JIRA key is valid and is not closed. Here is extract from my pre-commit hook.

#Check first line contains JiraId
        firstline =lines[0].lstrip()
        if  firstline.find(":")==-1:
            sys.stderr.write(stdErrMsg)
            sys.exit(1)
        prefix= firstline.split(":")[0]
		
        if prefix.strip()=="":
            sys.stderr.write(stdErrMsg)
            sys.exit(1)

        jiraIDpattern = re.compile(r"(\A[A-Z]{2,}-\d+)")
        IDs=prefix.split(",")
        invalidMessage=0
        for rawID in IDs:
            ID=rawID.strip()
            result=jiraIDpattern.match(ID)
            if not result:
                sys.stderr.write("\n '"+ID+"' is not a valid JIRA issue ID")
                invalidMessage=1

        if invalidMessage:
            sys.exit(1)

		#Check JIRA is up
        try:
            soap = SOAPpy.WSDL.Proxy(jiraWSDL)
            auth = soap.login(jirauser, jirapasswd)
        except Exception, err:
            # If JIRA is down then except commit message as is.
            sendMail(firstline + " " + author )
            sys.exit(0)

        #Check JIRA issues exist
        invalidJIRAissue=0
        for rawID in IDs:
            issueid=rawID.strip()
            try:
                issue = soap.getIssue(auth, issueid)
            except SOAPpy.faultType,e :
                #issue does not exist
                soapError = issueid+": "+ str(e)
                sys.stderr.write("\n"+ issueid + " does not exist in JIRA")
                invalidJIRAissue=1
            else:
                #Check JIRA status is not closed
                if int(issue.status) == 6:
                    sys.stderr.write("\nYou cannot commit against "+issueid+ " as it is in a resolved state (CLOSED) ! ")
                    invalidJIRAissue=1
        if invalidJIRAissue:
            sys.exit(1)

Here user is required to put the commit message in format "JIRA-1234 : <message goes here>"

Vijay

Jose M.
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 16, 2014
Hi Vijay, thanks for the suggestion and the code! Sounds good and probably better than using a not really supported 3rd-party-plugin.
Midori
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 4, 2015

Let me note that you are relying on the SOAP API in your code, which will be removed in JIRA 7.

Suggest an answer

Log in or Sign up to answer