Integrating Mantis and Subversion

Posted by Tom on November 29th, 2006

Do you want to know how to integrate Mantis and SVN? I did, but could never manage the right Google query to return a page written for someone unfamiliar with customizing Mantis. (I’m still learning SVN too.) This is the documentation I wish I’d had. This process is easier if you know Bash or some other scripting language. This was accomplished using Mantis 1.0.0rc2. An older version may not work with these instructions.

Introducing Subversion (SVN) Hooks

Subversion allows for custom scripts to be called when it performs certain actions on a repository. Browse to your repository. The server-side one, not your working copy. In /path/to/svn/repository_name/hooks you will see a list of sample scripts for the five actions subversion recognizes.

start-commit
Run before the commit transaction is created. May be used to stop the transaction and return error messages to clients.
pre-commit
Run when the transaction is complete, but before it is committed. It can be used to require comments to include a bug tracker ticket number, or enforce fine-grained access to parts of the repository. It can cause the transaction to rollback and return error messages to clients.
post-commit
Run after the transaction is committed, and a new revision is created. Often used to send notification emails or trigger repository backups. Errors from this hook are ignored Subversion comes with some scripts for common actions. Read the manual for more details.
pre-revprop-change
Revision properties such as log messages aren’t versioned. If you were to go back and change a log message, for example, the old one would be lost. This hook, and its post- counterpart, offer a chance to reject or log the change.
post-revprop-change
Run after revision properties are changed. There are some security restrictions built in, be sure to read the manual.

Well, that sounds simple. All we have to do is write a script that will log into our bug tracking system and insert a comment on the appropriate ticket. That sounds easy enough, but let’s not make things too complicated. With a few minor customizations, Mantis will do most of the work for us.

Customizing Mantis

The manual for Mantis has two relevant pages. One, titled “Source Control Integration”, provides a list of customizable settings, but offers no directions on where these settings are most appropriately accessed. The other, “CVS integration”, is not terribly helpful either, but it does provide another piece of our puzzle—it tells us there is a command line PHP script included with Mantis that accepts a version control (e.g. SVN, CVS) log message via stdin, and adds comments to any tickets as appropriate.

First things first. Create a new user in Mantis. This is the account the script will use. I called mine “svn”. Make sure the user has permission to add comments and resolve tickets. Don’t forget to add it to your project(s).

Open up /path/to/mantis/config_inc.php. If it doesn’t exist, create it. (If it doesn’t exist, you didn’t follow the installation directions.) This file is used to override default settings. It’s not distributed as part of Mantis, so when you upgrade, it won’t be overwritten. This is where you should have your database connection information, and where you would override the default colors, status/severity options, or any number of configurable settings.

Add the following lines to /path/to/mantis/config_inc.php:

$g_source_control_account = 'svn';
$g_source_control_regexp = '/\b(?:bug|issue)\s*[#]{0,1}(\d+)\b/i';

What does it all mean? (Some credit is due Digitalpeer for helping me through this. I found it just as I was getting the integration working. It would have saved some time.)

$g_source_control_account
This is the user we created in Mantis.
$g_source_control_regexp
This is the regular expression Mantis will match against the log message. This is an optional setting; the default is /\bissue [#]{0,1}(\d+)\b/i.

Using a custom RegExp

You’ll notice I defined a regular expression different from the default. I wanted to not only match the string “issue #1337″, but to be a bit more liberal, matching the phrase “bug #1337″ too. For those of you savy enough to craft your own expressions, the first capturable group must be the bug number, or the script won’t work. (This is hard-coded into Mantis.) Hence the non-capturing group (?:bug|issue).

Automatically closing tickets

These custom settings lay the groundwork for Mantis to automatically add comments to tickets based on SVN log messages. There are additional settings we can use to get Mantis to resolve tickets automatically too, based on a different regular expression. To enable this behavior, add the following lines to /path/to/mantis/config_inc.php:

$g_source_control_set_status_to = RESOLVED;
$g_source_control_set_resolution_to = FIXED;
$g_source_control_fixed_regexp = '/\bfix(?:ed|es)\s+(?:bug|issue)?\s*[#]{0,1}(\d+)\b/i';

$g_source_control_set_status_to
The status to set the ticket to. Use CLOSED if you prefer.
$g_source_control_set_resolution_to
The resolution attached to the ticket. There are constants defined for each built-in resolution. You may use a different one, but FIXED makes the most sense.
$g_source_control_fixed_regexp
This is an undocumented setting. The default is to be equal to $g_source_control_regexp, but I only wanted certain tickets to be automatically closed, so I used a different regular expression. My expression matches “fixes issue #1337″, “fix bug #1337″, or simply “fixed #1337″. (Plus several iterations on those words.) The ticket number must be the first captured group. Altering the expression to also accept the words “solved” and “solves” is left as an exercise for the reader.

Testing Mantis

Before we go further, let’s make sure we implemented the Mantis settings properly. Run the following from the command line (alter the paths, of course)
%> /path/to/php /path/to/mantis/core/checkin.php <<< “My web monkey fixed issue #1337 by accident.”

If you’ve configured Mantis correctly, there should be a new comment on ticket #1337.

Connecting the pieces

Now that we have Mantis set to accept log messages from our version control system, we need to connect Subversion to it. Remember the hooks from the beginning of this article? We’re going to write one. I used Bash script. (Be nice, I’m still learning Bash.)

Save the following file to /path/to/svn/hooks/post-commit (no file extension):

#!/bin/bash
 
REPOS="$1"
REV="$2"
 
log=$(svnlook log -r $REV $REPOS)
 
/usr/bin/php -q /var/bugtracker/core/checkin.php <<< "$log"

What is svnlook?

The program svnlook is distributed as part of Subversion. Given a revision and repository path as parameters, it will return various properties of the revision, such as the author or which files were changed. In this case we’re asking only for the log message.

Permissions

Give the file permission to executable by your repository. This will be slightly different for each setup, but something similar to the following should do the trick:

%> cd /path/to/svn/hooks
%> chmod 770 post-commit
%> chgrp svn post-commit

More on post-commit

The post-commit hook takes two parameters: the repository path and the revision number. If you needed to run post-commit manually (testing your script!), it would look something like this for revision 746:

%> /path/to/svn/hooks/post-commit /path/to/svn/repository_name 746

SVN and Mantis in perfect harmony

There you have it. That’s how you integrate Subversion with Mantis. If you have questions, feel free to leave a comment.

Special thanks to the guys who spent time writing Version Control with Subversion (For Subversion 1.1): Ben Collins-Sussman, Brian W. Fitzpatrick, and C. Michael Pilato. The world would be a better place if every piece of open source software was documented this well. (By contrast, the Mantis documentation is terribly out of date and rather disorganized.)

A more advanced script

I wasn’t content with having just a log message added to my Mantis tickets. I wanted to know who did it, and what files were changed. Thus, my actual script is a bit more complicated. (Remember, I’m still learning Bash, so be nice.)
#!/bin/bash
 
REPOS="$1"
REV="$2"
 
auth=$(svnlook author -r $REV $REPOS)
dt=$(svnlook date -r $REV $REPOS)
changed=$(svnlook changed -r $REV $REPOS)
log=$(svnlook log -r $REV $REPOS)
n=$'\n'
/usr/bin/php -q /var/bugtracker/core/checkin.php <<< "Changeset [${REV}] by $auth, $dt$n$log$n$changed"

69 Responses to “Integrating Mantis and Subversion”

Gave this a try this morning. Very cool.

Thanks for this. However, when I try and run the test:
/path/to/php /path/to/mantis/core/checkin.php

[…] Integrating Mantis and Subversion I want to get a setup like this running reasonably soon, for tracking a few personal bits of stuff. So here’s a guide. (tags: development) […]

Thank you so very, very much for this. While the process makes lots of sense, the documentation on the mantis manual only serves to INCREDIBLY confuse you. :/

Thanks for this excellent page ! I configure the SVN-Mantis integration fingers in the nose :D

[…] Integrating Mantis and Subversion […]

Thanks so much for this.

Thanks you so much!

I’ve been delaying to integrate SVN with Mantis for weeks now, simply because from some first research I knew it would be quite a hassle.
Today I decided to finally tackle it and luckily I researched again and found your blog entry.
It saved me a lot of time, thank you very, very much for this!

Forgot to mention that I found you via the Mantis manual:
http://manual.mantisbugtracker.com/manual.configuration.source.control.integration.php

Thanks again, you’re my hero! :D

Dear Sir,

Need help…
I have Mantis and SVN installed on my machine (Windows. I want to integrate it. I scratched my head for a long time now.. need your help.. If you could help me in giving simpler steps.. that would be great help.. I have php as well i installed activeperl…
Help
Thanks in advance
Regards
sandeep

@sandeep: I don’t have an SVN server installed on my Windows machine, and don’t have a reason to. A few simple Google searches suggest you’d have to write batch scripts (.bat) or .exe files instead of bash scripts like I’ve done here. One of the Google results returned demonstrated someone who installed Perl on Windows, then wrote a batch script to call a Perl script. Your mileage may vary.

The site looks great ! Thanks for all your help ( past, present and future !)

Hey guys.. Thanks..

This is a very very useful document.. I could manage the windows based as well Linux based Mantis + SVN integrationg working… And it looks fantastic…

Thanks again…

Regards
sandeep

[…] 在 Mantis 的文件網站找到了這個 Link : Integrating Mantis and Subversion […]

thank you very much :-)

[…] 7. 활용    - Mantis 기능으로 DocuWIKI 연결, CVS/Subversion 연결, Eclipse 연결 모듈과 연동할 수 있음.   - Config 설정 : Mantis config에 대한 설명은 http://www.mantisbt.org/manual/manual.configuration.php 참조   - Mantis - Subversion 연동은 http://alt-tag.com/blog/archives/2006/11/integrating-mantis-and-subversion/ 참조   […]

This is many many times better than any other resource on how to do this, got it working in minutes. Many thanks.

[…] 在 Mantis 的文件網站找到了這個 Link : Integrating Mantis and Subversion […]

You are a legend mate! Thanks very much

[…] Integrating Mantis and Subversion […]

Hey ! Seems to be a very nice work, but I can’t use it. Actually, I have neither ticket #1338, nor comments on it. However, I’m using a Mantis administrator account, under RHEL 4, and am working on my localhost ;p. I don’t really understand my mistake. Can somebody help me ?

I run in the command line:
> /path/to/php /path/to/mantis/core/checkin.php “I resolved issue #2″

But I get this error: “Comment does not reference any issues”

I can’t understand why! I have got issue #2 in Mantis.
I also tried #0000002, but didn’t work.

Any suggestion?

Hi first want to thank you for the work you did but i have a problem i come to the step where i work with the bashscript.

its similar to yours just the path is different form mantis. if i execute it i get the following msg. from snv look:

svnlook: Can’t open file ‘testprojekt/format’: No such file or directory

do you maybe know whats the reason for this?

First, thank you for the excellent guide.
Second, notes default to private, otherwise add this to your config.
$g_source_control_notes_view_status = VS_PUBLIC;

[…] Hier gibt es ein detailliertes How-to für die Integration von Subversion und Mantis. Dann können z.B. Tickets in Mantis automatisch durch entsprechende Einträge im Subversion-Log geschlossen werden. […]

[…] Re: Subversion Repository There is quite a nice article on Mantis hooks here, in case its of any interest alt-tag.com Blog Archive Integrating Mantis and Subversion […]

Thanks for that article.
Here is a WINDOWS equivalent.

— POST-COMMIT.BAT ———-
@ECHO off

SET REPOS=%1
SET REV=%2

SET PHP=C:\….\php.exe
SET CHECKIN=C:\….\mantis\core\checkin.php
SET SVNLOOK=C:\….\bin\svnlook.exe

SET LOGFILE=log%REV%.txt
SET AUTHORFILE=author%REV%.txt
SET OUTPUTFILE=output%REV%.txt

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

ECHO SVN %REPOS% Revision %REV% > %OUTPUTFILE%
TYPE %AUTHORFILE% >> %OUTPUTFILE%
TYPE %LOGFILE% >> %OUTPUTFILE%

TYPE %OUTPUTFILE% | %PHP% %CHECKIN%

CALL DEL %LOGFILE%
CALL DEL %AUTHORFILE%
CALL DEL %OUTPUTFILE%

— / POST-COMMIT.BAT ———-

Thank you Tom for the excellent article.

Paul, also thanks you as your post-commit file let me get up and running!

Hello,
I have svn running on Linux, and Mantis is running on another computer, under Windows 2003 OS.
Does someone know how I could make the link between the two systems ?

[…] Ideal Build System Posted on September 28, 2007 by Reiot integrating-mantis-and-subversion을  보고, 이상적인 통합 개발 지원 시스템에 대해서 생각해봤다. 일단 맨티스에서 버그는 물론 new feature (or issue, whatever)를 관리한다고 가정하면, […]

Thanks a lot - worked perfectly for me.

Great work!

Hi - I did the basic config in my mantis (i.e set the 2 variables g_source_contol_account and g_source_control_regexp see below). Then before continuing I did the test - Every time i get “Comment does not reference any issues.”

command is /php /core/checkin.php

Tried this out locally, and it works slick. Our problem is that we’re using Sourceforge’s SVN system for the project I want to track. Has anyone gotten this to work using the SVN hooks that sourceforge makes available?

Best,

Perry Clark

This worked like a charm!
However the regexp for fixing bug/issues didn’t work for me.
A slight modification made it work:
‘/\bfix(?:ed|es)?\s+(?:bug|issue)?\s*[#]{0,1}(\d+)\b/i’;

[…] Also, there are lots of other interpretations of what it means to integrate Mantis and Subversion. One of the more pervasive currents seems to be to automate the interaction between Subversion commits and Mantis issue updates, as elucidated in detail here. This is interesting, but not what I had in mind. I want to view the source code changes that correspond to issues in a simple way. […]

[…] alt-tag.com » Blog Archive » Integrating Mantis and Subversion (tags: mantis subversion svn bugtracking integration development howto programming environment) […]

Perhaps this would be of some additional interest:

http://blog.evaristesys.com/index.php/2007/10/18/integrating-mantis-viewvc-subversion-for-source-code-view-a-minor-hack/

Hi, I´m from Brazil, and I need so much a manual or anything step-by-step about integration Mantis with SVN in Windows…please this is an SOS :S
tks a lot

Great howto, thanks a lot for writing it down!

>Hello,
>I have svn running on Linux, and Mantis is >running on another computer, under Windows >2003 OS.
>Does someone know how I could make the link >between the two systems ?

>Left by Sebastien Roy on September 19th, 2007

I have this same setup working.
I installed freeSSHD on the windows server.
I modified the checkin.php script to read its input from the command line instead of from STDIN.
Then I call it with

ssh svn@mantis.myco.com “c:\\php\\php.exe -q path_to_checkin2.php \”args_from_above_example\”"

in the post-commit script.

In this setup I have public-key login working so no passwords are needed.

Hi.
Good design, who make it?

Hi,

I’m trying to get this working on to different win servers. When I try the checkin.php localy it works. But when I use pauls *.bat file nothing happens on Mantis.

I have on the Subversion server mapped the disc with the folder for php and mantis so a can reach them on from Subversion server.

the bat file looks like this:
@ECHO off

SET REPOS=%1
SET REV=%2

SET PHP=Y:\php.exe
SET CHECKIN=Z:\core\checkin.php
SET SVNLOOK=C:\Program Files\Subversion\bin\svnlook.exe

SET LOGFILE=log%REV%.txt
SET AUTHORFILE=author%REV%.txt
SET OUTPUTFILE=output%REV%.txt

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

ECHO SVN %REPOS% Revision %REV% > %OUTPUTFILE%
TYPE %AUTHORFILE% >> %OUTPUTFILE%
TYPE %LOGFILE% >> %OUTPUTFILE%

TYPE %OUTPUTFILE% | %PHP% %CHECKIN%

CALL DEL %LOGFILE%
CALL DEL %AUTHORFILE%
CALL DEL %OUTPUTFILE%

Can someone please point me in a direction where to start look for errors?

Great stuff!

Is it possible to set these config variables through the new Mantis “Manage Configuration” options screen? Would be nice not to have to go edit .php files.

What if my mantis DB is on an external, password-protected server?

Using mantis and SVN on different machines makes using the checkin.php script hard.

I’ve created a copy of the script checkincurl.php

this checks for a ‘log’ field in a posted form and checks that the originating ip is the SVN machine.

the command to make this work on the SVN is something like this:

curl -k -d “$log”

Hi

I have some problem to integrate svn with mantis

I ‘ve created post-commit.bat. All my data are in my log file but when I want ta put this data in my mantis database nothing appears.

I show you my code

D:\Program Files\wamp\php\php.exe D:\Program Files\wamp\www\mantis1.0.7_GTE\core\checkin.php

Works great, right out of the box so to speak. I did run into a minor glitch running the checkin.php script from the command line: If your mysql.sock isn’t in /tmp, this won’t work. Adding the following line to checkin.php will fix the problem (since this script can’t be called from the browser, this change shouldn’t conflict with browser-based Mantis functionality):

ini_set(”mysql.default_socket”,”/var/lib/mysql/mysql.sock”);

P.S. This tip was shamelessly stolen from a suggestion in the Mantis docs which I can’t seem to find at the moment…

I am trying to get this to work but the test line php checkin.php

I am trying to get this to work but I get a Segmentation Fault from checkin.php if a valid issue number is provided.

If you get the “Comment does not reference any issues” error when testing the settings: check your double-quotes. If you copy-pasted it’s likely you entered the command with curly quotes instead of straight ones, thus the input comment is only parsed up to the first whitespace.

Fantastic article.
Hope there will be an easier way of integrating them…

The notes works perfectly but the status and resolved changes didn´t worked for me. I copied the E.R exactly from the article and tried doing some changes but all whith no success.
Anyone could help me ?

[…] Co dalej? Jeżeli programujemy, prawdopodobnie przyda się nam bugtracker. Na naszej nieurodzajnej ziemi dobrze przyjmie się na przykład Mantis (wymaga PHP i MySQL) - świetny system, który łatwo możemy zintegrować z naszym repozytorium. Szczegółowy opis, jak można coś takiego zrobić, znajduje się na blogu alt-tag. Niezłą alternatywą, zarówno wobec WebSVN i Mantisa, jest np. oparty na Pythonie system Trac. Ma on jednak pewne wymagania, które nie każdy serwer musi spełniać. […]

Humm great !

After some tests, I have noticed some problems with checkin.php : seem only accept UTF8 input.
If the varible $t_comment is not UTF8 encoded, the script crash with “APPLICATION ERROR #401″.

After some string conversion , the integration run really great.

Tests with Mantis 1.1.1 under Win32 and Linux.

Hi, I have not tried to integrate Mantis with Subversion yet, but this article looks as if it will help a lot. Before I start integrating I would like to know if it is possible to populate the version list in the Mantis “Report Issue” screen from the versions available in Subversion? Thanks

Hi. I tried to integrate Mantis with Subversion and I need for help.
I did the initial configuration and I execute the line command:

c:/wamp/php c:/wamp/www/mantis/core/checkin.php

[…] MantisBT & Subversion […]

Comment does not reference any issues

./config_inc.php
$g_source_control_account = ’svn’;
#$g_source_control_regexp = ‘/\b(?:bug|issue)\s*[#]{0,1}(\d+)\b/i’;
$g_source_control_set_status_to = RESOLVED;
$g_source_control_set_resolution_to = FIXED;
#$g_source_control_fixed_regexp = ‘/\bfix(?:ed|es)\s+(?:bug|issue)?\s*[#]{0,1}(\d+)\b/i’;

But when I run

php ./core/checkin.php

I followed all your steps and i do understand the process, im running svn and mantis on CentOS5, they’re both set up fine and working but i cant seem to integrate them together after doing all the steps

1. I get ‘permission denied’ when I run the test:

%> /path/to/php/path/to/mantis/core/checkin.php chgrp svn post-commit does not also work, is says invalid group svn

3. lastly, i can’t seem to test the bash script %> /path/to/svn/hooks/post-commit /path/to/svn/repository_name 746, it just says that the repos name i am pointing to is a directory

thanks in advance! any help would be much appreciated :D

Hi. I tried to integrate Mantis with Subversion and I need for help. I did the initial configuration and I execute the line command:

c:/wamp/php c:/wamp/www/mantis/core/checkin. php

(Continuing…)

My command line is:

c:/wamp/php c:/wamp/www/mantis/core/checkin. php > was unexpected at this time”

Someone can help me?
Thanks.

I’m Sorry, but my messages are being cut.

When I execute this command:
c:/wamp/php c:/wamp/www/mantis/core/checkin. php

Just wanted to say thanks, you helped me out a great deal with this blogpost! I now have Mantis/SVN working in sync! :)

[…] You will need to modify your Mantis’ config_inc.php a bit. I found these lines of code from this guy who last year helped us all integrate a local mantis to a local svn repository. Add the following lines: […]

[…] And I work with several ticketing systems, so I’m not at all intimidated by them. I looked at the Dreamhost wiki to see what other people are using… the packages were all familiar, but there aren’t any that are auto-installed. Mantis of course strikes my fancy. Clean and lean but still robust and intuitive. Software written by people who depend on it is a beautiful thing. And I found some great instructions explaining exactly how to integrate mantis and subversion. Happiness ensues! […]

Thanks - seems to work great. I use Dreamhost for mantis and svn hosting and was able to quickly follow these instructions with no problems so far.

Trying this on the packages that come with fedora 9.

the php checkin.php test in the command line does not work. There is no error message at all. even if I do not feed in any text….

does anybody have an idea what might be wrong?

If your server is Windows and using Tortoise SVN this user note at the Mantis site worked first time for me. Includes the source code changes in the note and more impotantly the revision number so I know what to roll back to if I have to.

http://manual.mantisbt.org/manual.configuration.source.control.integration.php#419

Add Your Comment