How To Integrate Subversion and Mantis

September 26th, 2008 | by Daniel |

I’ve been using Subversion for years now (and it’s a great app), but just recently I’ve started using Mantis to keep track of tasks/bugs for some of my projects. Having your issue tracker reflect related changes in the source is very helpful and lately I’ve been thinking of setting this up for myself.

Word of warning: While Subversion’s documentation is well organized and detailed, the same cannot be said for Mantis’s manual. I’ve tried making this howto as detailed as possible, so hopefully you won’t run into any problems. If I’ve missed anything, let me know and I’ll clarify.

My primary goal is to automatically add a note to an issue in Mantis when an associated commit is made to Subversion. This note should include some basic information about the commit as well as the log message itself. As a secondary goal, it’d be nice to automatically resolve/close issues via this mechanism as well (I myself prefer to do this manually however, but you may not).

A quick overview of what we’ll need to accomplish this:

  • Working installations of Mantis and Subversion - This is beyond the scope of this howto, but you can refer to the documentation links provided above to get you started
  • A user account in Mantis with permissions to add notes to issues
  • A script to do the actual issue updates in Mantis - Thankfully this is provided as part of the Mantis distribution
  • Tweaking of a few Mantis configuration variables - We need to tell the above script what to look for in a commit log in order to match it up with the associated issue
  • An SVN post-commit hook script - This will kick off the process when a new commit comes in

Now that we’ve got the general idea of what we’ll be doing, let’s get down to it.

Step 1 - The easy stuff

The first thing you’ll need to do is create a Mantis user which has permissions to add notes to any issue. I created one called ’svn’. You may want to revise your e-mail notification settings in Mantis so that this user is not e-mailed for changes to issues.

Step 2 - Mantis Configuration

Now we need to add a few config variables to our Mantis config_inc.php:

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

Line 1 is pretty self-explanatory - it tells Mantis which account to use for these updates, so we’ll set this to the name of the user that we created in step 1. Line 2 provides a regular expression which is used to match commit logs to their associated issues. In this case, the regexp will match “bug #1234″, “issue #1234″ or “task #1234″.

If you want to get fancy and have Mantis automatically resolve or close issues for you, you’ll also need to add the following config:

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

Line 1 tells Mantis what to look for in a log message before applying status changes to the issue. In this case, we match the words “fix”, “fixed” or “fixes”, along with a corresponding “bug #1234″, “issue #1234″ or “task #1234″ (see above).

Lines 2 and 3 tell Mantis what new status and resolution to apply to the issue. The values are Mantis constants, “RESOLVED” is used in this example but you could also set it to “CLOSED”, or (technically) any other status you want.

Now that we’ve configured Mantis, we can use the script <mantis_dir>/core/checkin.php. This script attempts to match the regular expressions we’ve set up against input from STDIN and acts accordingly. In other words, if it finds an issue id in the input (e.g. “bug #1234″), it will dump its input into a note attached to the appropriate issue.

To try it out, execute something like the following:
<path to php>/php <mantis_dir>/core/checkin.php <<< "Testing issue #1234"

Assuming issue #1234 exists and you completed all the above steps properly, then viewing issue #1234 in Mantis should show the note that you added via checkin.php.

Step 3 - The post-commit hook

Now that we’ve got Mantis doing its part, we need to set Subversion up so that it runs checkin.php when a commit is made. We need to create a Subversion post-commit hook which will gather the required information about the commit and submit that data to checkin.php. I won’t cover the details of the hook system here, but the SVN book’s hook section should give you all the information you need.

I wrote my script in PHP, but you could use any scripting language you have available.

<?php
define('SVNLOOK', '/usr/local/bin/svnlook');
define('CHECKIN', '/usr/local/bin/php /sites/mantis/core/checkin.php');
 
$repo_path = $argv[1];
$rev = $argv[2];
 
$changed = array();
$date = trim(shell_exec(SVNLOOK . " date $repo_path -r$rev"));
$author = trim(shell_exec(SVNLOOK . " author $repo_path -r$rev"));
$log = trim(shell_exec(SVNLOOK . " log $repo_path -r$rev"));
exec(SVNLOOK . " changed $repo_path -r$rev", $changed);
 
$change_list = array();
foreach($changed as $change) {
        $content_change = substr($change, 0, 1);
        $prop_change = substr($change, 1, 1);
 
        $line = substr($change, 3);
        if( $prop_change == 'U' ) {
                $line .= ' (props changed)';
        }
 
        switch($content_change) {
                case 'A':
                        $change_list['added'][] = $line;
                        break;
 
                case '_':
                case 'U':
                        $change_list['modified'][] = $line;
                        break;
 
                case 'D':
                        $change_list['deleted'][] = $line;
                        break;
 
                default:
        }
}
 
$message = '';
$message .= "Revision: $rev ($date)\n";
$message .= "Author: $author\n";
$message .= "\nLog:\n$log\n";
 
if( isset($change_list['added']) ) {
        $message .= "\nAdded:\n";
        foreach($change_list['added'] as $line) {
                $message .= "    $line\n";
        }
        $message .= "\n";
}
 
if( isset($change_list['modified']) ) {
        $message .= "Modified:\n";
        foreach($change_list['modified'] as $line) {
                $message .= "    $line\n";
        }
        $message .= "\n";
}
 
if( isset($change_list['deleted']) ) {
        $message .= "Deleted:\n";
        foreach($change_list['added'] as $line) {
                $message .= "    $line\n";
        }
}
 
exec(CHECKIN . " <<< \"$message\"");
?>

The output of that script should look something like this:

Revision: 1783 (2008-07-30 22:49:19 -0400 (Wed, 30 Jul 2008))
Author: joe
 
Log:
Blah blah blah
 
Added:
     foo/bar/baz.h
 
Modified:
     foo/bar/ (props changed)
     foo/bar/baz.c

Now set Subversion up to use your hook script and you’re done!

  • Digg
  • Reddit
  • del.icio.us
  • Facebook
  • Technorati
  • E-mail this story to a friend!
  1. 2 Responses to “How To Integrate Subversion and Mantis”

  2. By TyWen on Nov 22, 2008 | Reply

    Thanks for the great blog! I have a question. I’m not a php person so I hope you can help me here. How do you get the php script to post to a mantis server when the SVN and the MANTIS systems are on two separate servers? Our SVN is on one server and our MANTIS is running on another server. What needs to be done to get the SVN server to run the post-hook and contact the MANTIS system?

  3. By Daniel on Nov 22, 2008 | Reply

    TyWen,

    I haven’t really thought about this too much but I think one way to do this would be to have your SVN hook script run checkin.php on the Mantis server via ssh. I think you’d be looking at something like ’ssh user@host command’. You can use public key authorization so that a password isn’t required.

Post a Comment