Tuesday, November 22, 2011

How to use git hooks to enforce relating commits to issues on github

Recently my team has been trying to take advantage of some of the issue and planning features github offers.  We've been using github's issue tracking for a while and we really like how easy it is to tie commits to issues.  We thought we could provide more visibility to the rest of our company and have smaller, more direct commits if we were more disciplined about tying commits to issues at commit time.

This is really easy to do using git hooks.  Simply create a file named commit-msg in repo\.git\hooks and paste in the following code:

#!/bin/sh
if git-rev-parse --verify HEAD >/dev/null 2>&1; then
    against=HEAD
else
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
if grep -i 'gh-[0-9]\+' $1 > /dev/null 2>&1; then
  exit 0
else
  echo COMMIT REJECTED BY "commit-msg" HOOK:
  echo Commit log must close or mention a github issue \(use "closes GH-9999" or "mentions gh-9999"\)
  exit 1
fi

That will just make sure that gh-### appears in the commit message somewhere.  If you need to commit something and it doesn't make sense to tie it to an issue, just add the --no-verify flag to the commit line.  It's been working really well for us so far.

Wednesday, April 22, 2009

Integrating Subversion with Axosoft OnTime

I've been meaning to sit down and get our ticket / bug tracker (Axosoft OnTime) integrated with our SCM tool (Subversion) for a while now. Today I finally ran out of excuses to keep putting it off. Having had a pleasant prior experience integrating Trac with Subversion, I wasn't expecting it to take too long. The real problem is the lack of information on the Internet about this subject. So, without further delay, here are the steps I went through to get our installation of OnTime and Subversion talking with each other.

Configure Source Control Management

The first thing you'll want to do is configure Source Control Management in OnTime. Right click on any of your projects (although I would suggest you do this from the root project) and select Manage Source Control. OnTime will then present you with the following screen:


  1. Set Subversion as your source control plug-in.
  2. Command Line Client -- TortoiseSVN will not cut it. If you don't have a command line client installed (VisualSVN is fine), any of the ones here will work. I have VisualSVN but I verified that the CollabNet one works as well. Enter the path to svn.exe.
  3. Server URL -- This is the same value you use for your source repository. It's usually something like svn://host/repositoryName.
  4. Username -- Your username in Subversion.
  5. Password -- Your password for Subversion
  6. Working Copy Location -- Location of the repository on your hard drive.
  7. Timeout Value -- This one is important. It defaults to 15 seconds. If you have a slow Internet connection or you're working with a large repository you will likely need more time. If you get a null reference exception that isn't handled, increasing this value may resolve your problem.
Configure Subversion Properties

Go to the subversion properties on the root folder in your repository and enter the following values:


bugtraq:number false is actually important for the way I'm getting around a specific design choice with OnTime. All of the primary keys for tasks, defects, incidents, features seem to be shared across ticket types. In order to properly navigate to the ticket, we're going to adopt a naming convention to tell TortoiseLinker.asp what type of issue we're dealing with so it will know where to send us.

When you enter the bug id on a code checkin, prefix the issue with a d for defect, t for task, and an f for feature.


Create TortoiseLinker.asp


Now that we've got our subversion variables setup, the only thing left is placing a script in your OnTime web app that will direct the user to the appropriate ticket.

  1. Create a file called TortoiseLinker.asp and put it in the root directory of your OnTime web application.
My file has the following contents, it's quite possible you will need to modify this for your needs, but this should give you the general idea of what's going on here.
<%
Dim urlid
Dim ticketType


'Set both of these values = to the bug id coming across
ticketType = request("id")
urlid = request("id")

'Grab the first character. (d = defect, t = task, f = feature)
ticketType = left(ticketType, 1)

Dim ticketTypeAsciiNumber

'Get the ascii value of the first character
ticketTypeAsciiNumber = Asc(UCase(ticketType))

'See if the ascii value of the first character equals d, f, or t
if(ticketTypeAsciiNumber = 68 or ticketTypeAsciiNumber = 70 or ticketTypeAsciiNumber = 84) Then
'Set the url to the actual bug number, ripping off the first character that indicates the issue type
urlid = right(request("id"), len(request("id"))- 1)

'defect
if(ticketTypeAsciiNumber = 68) then
response.redirect("/OnTime2008Web/Defects/ViewDefect.aspx?DefectId=" + urlid)
'feature
elseif(ticketTypeAsciiNumber = 70) then
response.redirect("/OnTime2008Web/Features/ViewFeature.aspx?FeatureId=" + urlid)
'task
elseif(ticketTypeAsciiNumber = 84) then
response.redirect("/OnTime2008Web/Tasks/ViewTask.aspx?TaskId=" + urlid)
'should never really get here
else
response.redirect("/OnTime2008Web/Defects/ViewDefect.aspx?DefectId=" + urlid)
end if
'the first number is something we don't support, just send them on to the defect page and hope for the best.
else
response.redirect("/OnTime2008Web/Defects/ViewDefect.aspx?DefectId=" + urlid)
end if

response.end
%>

Done! Now you can manually link files in OnTime to issues and you'll get links in the log messages from subversion that will take you directly to the issue in the web app.

One final note: I had to enable asp page processing on our server to get this to work. Directions on how to do that can be found here.