## Integrating Mantis and Subversion#

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" ## 139 Responses • thanks for manual! i have now made bridge to mantis working :) The only thing: in mine setup mantis have installed on one server, and svn on another. Since checkin.php doesn’t allows to run it via webserver, i have made simple gateway script: and put it in root of mantis (called as checkin-gw-ah345hiugh.php, where ah345hiugh is secret code :)) and in post-commit hook, instead of /usr/bin/php -q /var/bugtracker/core/checkin.php • ugh… script eaten… inside php tag put code: ================ ob_start(); unset($_SERVER['SERVER_PORT']);
define(“STDIN”, fopen(‘php://input’,’r’));
require_once( dirname( __FILE__ ) . DIRECTORY_SEPARATOR . ‘core/checkin.php’ );
================

• And call it via
curl http://mantis-path/checkin-gw.php –data-binary @-

• Excellent article. Helped us a lot.

Thanks

• I test mantis and get this error…I’m using CentOS release 5.2 and mantis-1.1.7

# php /usr/local/apache/htdocs/mantisbt/core/checkin.php

• Fatal error: Call to undefined function mysql_connect() in /usr/local/apache/htdocs/mantisbt/core/adodb/drivers/adodb-mysql.inc.php on line 364

• If you are using Windows you can do the following to test the Mantis configurations:
-creat a text file and name it “arg.txt”
-in command line run: \path\to\php \path\to\mantis\core\checkin.php \path\to\atg\arg.txt

• Oops
I forgot to tell write the following text in the arg.txt file

My web monkey fixed issue #0001 by accident.

• @datacompboy Thanks for your gateway script. You could generate a GUID for a more secure filename, but it’s still risky.

• Mantis now (I saw it in 1.1.8) comes with a script core/checkincurl.php instead of checkin.php, which allows integration if your Subversion and Mantis are on different servers!

• Thanks for your comment Martijn! Can I ask the procedures/steps on how to integrate the SVn and Mantis on different machines (all both running in Windows).

Thanks!

• Good posting.
I jz want to add a little.
Sometimes, the scripts provided up there cant work well.

#!/bin/sh
REPOS=”$1″ REV=”$2″
auth=”$(/opt/CollabNet_Subversion/bin/svnlook author -r$REV $REPOS)” dt=”$(/opt/CollabNet_Subversion/bin/svnlook date -r $REV$REPOS)”
log=”$(/opt/CollabNet_Subversion/bin/svnlook log -r$REV $REPOS)” changed=”$(/opt/CollabNet_Subversion/bin/svnlook changed -r $REV$REPOS)”
border=”==============================================================================”
n=$’\n’ echo “Revision [${REV}] by $auth,$dt$n$log$n$border$changed$border” | /usr/local/apache2/php/bin/php -q /etc/opt/CollabNet_Subversion/default-site/htdocs/mantis/core/checkin.php

Hope it helps some of you…

:D cheers!!!

• [...] Integrating Mantis and Subversion – alt-tag.com (tags: svn mantis) [...]

• Hello,

I think there is a security hole in the post-commit script. Committer can run arbitrary shell commands as the user who is running the svn commit hook (using back tics or $(evil_script.sh)). I would suggest using pipe instead of double quotes when running checkin.php: svnlook log -r$REV $REPOS) | \ /usr/bin/php -q /var/bugtracker/core/checkin.php (I have not tested this). Best regards, Frans • Please remove the trailing parenthesis from the above fix: svnlook log -r$REV $REPOS | \ /usr/bin/php -q /var/bugtracker/core/checkin.php Sorry for noise. • Dear Sir, Need help… I have Mantis installed on 1 and 1 linux and SVN installed on Mac OSX,both are situated at different location. 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 … Help Thanks in advance Regards manish • [...] lot of information can be found on the net about integrating Subversion and Mantis when they both run on the same Linux server. Our situation is a bit different, being that [...] • Hi, First of all, thanks all for the information (also the commenters). I’ve compiled all information and put it into another blog post, more specific to having SVN and Mantis on seperate servers. • Hi, When I execute this command: /path/to/php /path/to/mantis/core/checkin.php <<< "My web monkey fixed issue #1337 by accident." It will show error message like these: APPLICATION ERROR #400APPLICATION ERROR #400Database connection failed. Error received from database was #0: PHP Support for database is not enabled.Please use the “Back” button in your web browser to return to the previous page. There you can correct whatever problems were identified in this error or select another action. You can also click an option from the menu bar to go directly to a new section. Would you tell me how to do this ? Thank you very very much. Best regards, Tony. • [...] der Platte des Servers schlummerte — damit war das in fünf Minuten erledigt. Inspiriert von diesem Blog-Post, der das Verfahren mit SVN beschreibt, habe ich das ganze als [...] • [...] Integrating Mantis and Subversion [...] • Thanks a lot, I was able to add an hook to Subversion 1.6.3 installed on Windows Server 2003 with Mantis 1.2.0rc1. With the help of this site (http://www.codeproject.com/KB/threads/redir.aspx) I was able to code my own post-commit.exe hook in C++. • Workaround for portuguese, spanish, french et. al. accented charecters. I had to add the post-commit like this: —————————————— #!/bin/bash export LANG=”en_US.UTF-8″ 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/www/mantis/scripts/checkin.php <<< "Changes to /rail01/pmi/projects/neasden [rev${REV}] by$auth, $dt$n$log$n$changed" —————————————- otherwise tou get posts on mantis like this ora c?\195?\161 est?\195?\161 o c?\195?\163o d'?\195?\161gua em ora?\195?\167?\195?\163o n?\195?\169? The workaround is to add export LANG="en_US.UTF-8" to the post-commit script my setup: – debian 4.0 etch (all defaults for lamp) – mantis-1.2.1 – svnserve • [...] and Mantis on Windows. I wanted to allow developers to resolve issues via commit comment. I read this great page about it, and used it as a starting point for Windows [...] • Hi how are you. I wanted to know that in my case on commit i need the issue to be set to feedback status and also to assign it to a different user. I have been able to change the status to feedback but I cant assign it to a different guy. please help • [...] Not so hard when you follow easy instructions like from this article on alt-tag.com: http://alt-tag.com/blog/archives/2006/11/integrating-mantis-and-subversion/ [...] • [...] a little Googling, I was able to turn up a few useful resources to help me get this set up. This article gave a nice overview of SVn hooks and Mantis integration, while this one provided a nice [...] • I would like to setup the build environment using Subversion+Mantis+Hudson on solaris. Please shed a light on this. • 您好，我想请问一下，当mantis和SVN服务器不在同一台主机的时候，我不能通过svn客户端提交BUG到mantis服务器，必须得在SVN服务器上直接执行脚本才能提交BUG到mantis服务器。 我知道问题的原因是apache不能免密码登陆mantis服务器，我想知道如何设定apache免密码使用root账户登陆到mantis服务器。 • [...] Integrating Mantis and Subversion [...] • [...] все Источник: http://alt-tag.com/blog/archives/2006/11/integrating-mantis-and-subversion/. Хотя нет. Не все. Если у вас возникли проблемы с [...] • [...] integración entre Mantis y Subversion se encuentra probada y registrada en varios sitios en el Internet, y de ser conseguida resulta de mucha ayuda para poner orden en el desarrollo. De forma sencilla, [...] • [...] #!/bin/sh REPOS="$1" REV="$2" SVNLOOK=/usr/local/bin/svnlook PHP=/usr/bin/php MANTIS=/srv/www/novo/html/mantis/core/checkin.php COMMIT_MSG=$SVNLOOK log -r $REV "$REPOS" $PHP -f$MANTIS <<zzzMantiszzz Revision: $REV:$COMMIT_MSG zzzMantiszzz Also, great details at Alt Tag [...]

• [...] This is my first post on my blog! As a first post, I thought I’d share how I have integrated my private SVN (Subversion) repository and my public Mantis bug tracker. You can automatically add comments to Mantis after you check-in files in your SVN repository, using a post-commit event. For more information, click here. [...]

• Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or something. I think that you can do with a few pics to drive the message home a bit, but other than that, this is wonderful blog. An excellent read. I will definitely be back.

• Thanks for the article. Works great! However, I’ve been struggling with the regexp. My typical Mantis note looks something like

————————-
SVN generated message (Repo: X, Rev: X)
User: USER_NAME
Issue ##
————————-

MESSAGE

The “formatting” is done via the batch script (dashes and newlines etc), but with this setup I can’t get the recognition to fix the issue when simply using e.g. ‘fixed’ in the message. If I write for instance MESSAGE = ‘fixed issue #13′ it works ok, but since I’m already mentioning the issue nr above the actual message I would need somewhat more clever regexp to interpret the issue nr first and then if the message contain e.g. fixed, fixes, fixing etc.

• Hi,
I have an environment where the Mantis BT installation is on a linux server and the SVN installation is on a windows server.

Does anyone have a procedure I could refer?
Kind Regards,
Jason

• [...] Subversion -> Mantis [...]