August 28, 2007

Subversion Post Commit Hook To Update TestTrack

Helix ALM

Works with TestTrack 7.6 and later

Works with Subversion 1.4.5 & 1.5.1

This article outlines how to create a post-commit hook in Subversion, which calls a script that updates TestTrack. This was all developed and tested against TT 7.6 and Subversion 1.4.5 in Windows. Minor changes will have to be made to make this work on a different platform. As for Python, I used Python 2.4 and also used the PyXML libraries for Python 2.4. I did try Python 2.5 but it would not load some of the XML objects. Since 2.4 worked, I just went with that version. You'll need:
  • The TestTrack SOAP SDK installed.
  • The path to your TestTrack SDK installation. If you're familiar with how web servers work and your IT environment, you can probably guess this. If not, ask the TestTrack administrator or IT staff for help.
  • Subversion and the necessary permissions to commit files.
You'll get:
  • A Subversion commit hook that automatically associates committed changes with one or more defects in TestTrack.
What we'll do:
  • Review the script we're going to use to integrate with TestTrack.
  • Create a batch file to call the script.
  • Go through an example of using the new trigger.

Subversion Integration Script

The script we'll be using is written in Python. Python is good because it's easily installed and used on a variety of platforms. I'm not going to review the actual source, but it's not overly complex if you understand a little bit of Python and XML. If you need to enhance this script, I would not recommend using any of the Python SOAP libraries. I tried several of them in writing this script, and none of them worked as advertised. The common issue I ran into was lack of support for complex objects. Download Python Script The script requires 4 input parameters, which are passed by the batch file. If you have Subversion in a Linux environment, you will have to use a shell script instead. This should be easy to create since all of the examples provided with Subversion are shell scripts. The parameters are:
  • Subversion Repository - The repository where the changes are committed to. This value is passed by the commit action.
  • Revision Number - The revision number of the commit. This value is passed by the commit action.
  • Username - Plaintext TestTrack username.
  • Password - Plaintext TestTrack password.
Once called by the batch file, the script does the following:
  • Uses the svnlook client to fetch the comments and list of files changed by the commit.
  • Parses out the comment.
  • Looks for the following token in the comment: <ttp:project name,defect#,{defect#>}. An example would be:
<ttp:Sampe Project,12,14,15,25>
  • If the token is found, parses out the files submitted with the changelist and links them to the SCC tab of each referenced defect.
In the example above, the script will attach all of the submitted files to defects 12, 14, 15 and 25 along with the revision of the commit. Other items of interest:
  • The path to the TestTrack SDK cgi is hard-coded in the script.
  • The call to svnlook is also hard-coded and will have to be changed if used in a Linux environment.
  • The Python script could be modified so the TestTrack username and password are included in the commit comments. This would mean that this username and password would be passed in clear text over the network. Having the username and password in text file on the server might be safer, since you can restrict who has access to that server.

Create the Subversion Hook

Now that we understand how to call the script, we can set up the hook in Subversion.
  1. Browse to the repository directory.
  2. Locate the Hooks directory
  3. Create a batch file called 'post-commit.bat'. This file has to have 'post-commit' in the name. As outlined in the Subversion documentation, this does not have to be a batch file. Please review the documentation for more information.
We're going to create a post-commit hook that calls our script to update TestTrack. The basic syntax here is: <path to script/script name> repo rev# ttuser ttpassword Here is the contents of a sample batch file:
set TTUSER="soapuser"
set TTPASS="password"

set REPOS="%1%"
Notice that I had to specify the full path of the script file eventhough it existed in the same directory as the batch file. After a successful commit, our script should be called. We're connecting to the sample project as user "soapuser", with password of "password. %REPOS% and %REV% are simply the two variables that Subversion will replace with the actual corresponding values, on every post commit action.

Using the Trigger

Now we can actually use the trigger!
  1. Check out a file from a Subversion repository.
  2. Edit the file and save change.
  3. Commit the change.
  4. Enter <ttp:Sample Project, 1> This is my first attempt at using the trigger that will integrate to Subversion!! as the comment for the commit.
Now you should have a file linked to defect #1 in your TestTrack sample project, on the SCC tab. If you've configured Subversion integration within TestTrack, you should be able to perform Subversion commands on that file from within TestTrack as well. Note: Seapine does not provide support for sample triggers.