h1

Communicating Mantis and Subversion on Windows

7 enero 2010

Although this blog is mainly written in Spanish, because of the lack of information both in Spanish and English, I’ll try to explain how to communicate Mantis and Subversion on a Windows server. Please be gentle with my English, but feel free to send any correction🙂

Installing Mantis and Subversion (it’s logical)

I think it’s not that hard, otherwise there are lots of tutorials out there in the net😉 In my case, I used the CollabNet svnserve package, which automatically creates a repositories folder in C:\svn_repositories and the corresponding Windows service. We must also create a repository, in my case in C:\svn_repositories\repositorio-1 (originality for the win!).

Creating a Mantis user account

This account (something like “svn”) should be able to add messages to the tickets and (optionally) close them. It is enough to grant it the developer role. Note: Don’t forget to assign it to the projects where this task is going to be automated!

Editing the config_inc.php file

It is placed in the Mantis root folder. The changes to this file include the user account created in the previous point, the regular expressions to match in order to identify a message which references an issue and, if we want the tickets to be automatically marked as resolved, another regexp, the final status and the resolution. It will be something like this:

# User account that connects with Subversion
$g_source_control_account = 'svn';

# Regular expression to be matched in the comments
# Example: issue #1
$g_source_control_regexp = '/\b(?:bug|issue|error)\s*[#]{0,1}(\d+)\b/i';

# Regular expression to be matched to fix an issue
# Example: fixed issue #1
$g_source_control_fixed_regexp = '/\b(?:fixed|fixes|resolved)\s+(?:bug|issue|error)?\s*[#](\d+)\b/i';
# Status after solving the issue
$g_source_control_set_status_to = RESOLVED;
# Resolution after solving the issue
$g_source_control_set_resolution_to = FIXED;

First test

We will need some things in order to perform an initial test:

  1. Creating a dummy issue in Mantis and remember its number🙂
  2. Creating a text file which contains a dummy commit message like “fixed issue #1” (this is why we needed the issue number).
  3. Accessing to a system shell, like using remote desktop (why not native SSH?). Once it is open, since Windows’ standard input sucks and we can’t input the text directly with the <<< operator, we run the command
    C:\path\to\php.exe C:\path\to\mantis\scripts\checkin.php < C:\path\to\text\file
    If the contents of the text file don’t match the regexp, we will get the message “Comment does not reference any issues”, otherwise the status of the Mantis issue should have changed to “resolved”, and a comment with the text should have been appended to the ticket. Don’t forget that some Mantis versions (this should be fixed for 1.2 final) only accept one-line messages; if they have more than one line they won’t be related to any issue.

Connecting the Subversion server

Once we have checked the basics, we’re going to connect Mantis with the Subversion server, for which we will use a BAT file that will run after every commit, which is called a SVN hook (there are also pre-commit, pre/post-update hooks, etc.). This file must be placed in the hooks folder of the repository, i.e. C:\svn_repositories\repositorio-1\hooks, and named post-commit.bat.
Its contents will be as follows, without forgetting to change the PHP, CHECKIN and SVNLOOK variables paths to the correct ones in your system:

@ECHO on

SET REPOS=%1
SET REV=%2

SET PHP="C:\Program Files\PHP\php.exe"
SET CHECKIN="C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\mantis\scripts\checkin.php"
SET SVNLOOK="C:\Program Files\CollabNet\Subversion Server\svnlook.exe"

SET LOGFILE=C:\Temp\log.txt
SET AUTHORFILE=C:\Temp\author.txt
SET OUTPUTFILE=C:\Temp\output.txt

%SVNLOOK% log -r %REV% %REPOS% > %LOGFILE%
%SVNLOOK% author -r %REV% %REPOS% > %AUTHORFILE%

TYPE %LOGFILE% > %OUTPUTFILE%

%PHP% %CHECKIN% < %OUTPUTFILE% >> %LOGFILE%
ECHO "Post-commit for revision %REV% finished" >> %LOGFILE%

@ECHO off

Once the file is placed in the folder, we must restart the SVN server in order to register it (I spent a lot of time with this stupidity).
As result, there should be a text file C:\temp\output.txt with the SVN log for the last revision, and another C:\temp\log.txt with the log, the checkin.php output (empty) and the text string confirming the post-commit execution. Oddly, if I didn’t include this last output the script exited with a 255 error and no output, but the Mantis ticket was correctly modified; so it is there just to avoid confusing the user. If someone knows a possible reason for this one, I would be glad to read comments about it😀

One comment

  1. I can’t thank you enough for your great help.
    Actually I didn’t know why I could not use the the ‘<<<' operator on DOS bash…I lost an afternoon with this.
    Thank you again

    Anisse



Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

A %d blogueros les gusta esto: