You are here:Home»KB»Programming»General»Subversion (SVN)»Integrate Subversion with Mantis
Saturday, 10 January 2015 11:10

Integrate Subversion with Mantis

Written by

These are my indepth instructions on integrating Mantis with subversion. This came about because i had a website but no subversion server and the easiest way of getting one of those is to install it on my local PC. I then wanted to integrate Subversion with Mantis so when i closed a job it did it automatically. I thought ther would be an easy way of doing this, there is if both the Subversion and Mantis are installed on the same physical machine. I coveer all the integration types here and hopefully fill in a few blanks. I did find the documentation for this whole process lacking. I have scanned the web, categorised these links and figured out the integration for both remote and local integration.

There are a few ways of integrating Matis with a Subversion server and i will outline them below. My instructions will more than likely concentrate on integrating with CollabNet Subversion Edge but i will add some notes on VisualSVN aswell.

  1. checking.php - Mantis Native
  2. Source (Source Control Integration) with SourceSVN plugin
  3. Source with SourceWebSVN
  4. Custom Direct Integrations

For those who want it working and are not to bothered how, if you click this link it will take you straight to my instructions on what i did, no fluff, just 1.2.3...

General Links

Hook Links

Text Notes / Questions

  • what are the 'Enabled Features' in sourceSVN setting?
  • when you submit to a sVN their are hooks that are fired and can run code.
    • in particular there is one called 'post-commit' this code can read the submission notes and act on it if it finds for instance a regexex match
    • curl can then be fired to report this to a bug tracker system which in turn can then read it, and sign of the bug if needed. (CURL might not be used any more)
  • hook folders are in the individual repositoriesand these do not appear until you create the repository/project in SVN admin.
  • check out all regexs for the best one as there a lot of different ones
    • $g_source_control_fixed_regexp = '/\b(?:fixed|fixes|resolved)\s+(?:bug|issue|error)?\s*[#](\d+)\b/i';
    • ?:bug|issue|error|task)(?:feature|bug|issue)
  • can use WebSVn for browsing and integration - but this software is old and might not last long
  • to fully integrate mantis it needs to run on the same server/PC? - check this
  • visualsvn has templates for scripts see this article
  • issues numbers are not linear per project, they are linear over the whole of mantis.
  • remote checkin.php might be possible by SSH
  • The latest version of the hooks are always available in a freshly created repository. These files always have a complete list of what variables are available.

 

 


1 - checkin.php - Mantis Native (Local / Remote)

This is the native basic inbuilt script in Mantis for integrating a Subversion server on the same machine. However with some modification can be used for closing issues from a remote SVN Server.

How Does it work?

The script /mantis/scripts/checkin.php is native to Mantis and 'Provides source control (SCM) integration for closing bugs upon commits'.

This script is used to post infromation about a commit to a remote Subversion Repository. The basic mechanisim is outlined below and assumes the integration is configured correctely.

  1. a user submits a commit to a SVN Repository. With in the commit notes are key notificatsion

    add comment examples here
  2. once the files are commited, the post-commit hook/script of the SVN is triggered and posts the comment to mantis/scripts/checkin.php
    NB: Hooks are specific to individual repositories and not the SVN server
  3. the post is then processed by Mantis with regex rules that have been configured.
  4. if there are any matches Mantis then uses the information to perform actions such as 'close' job (possibly with commit references) as appropriate

This method requires the SVN post-commit hook to have a script on it to run when it is triggered to POST the commit notes to Mantis. Also in mantis you need to add certain settings to the config_inc.php, these settings includes the REGEX settings.

Now a legacy!!

This is now a legacy method of connection your svn server to mantis because it is:

  • old and hard configure with not many options.
  • It requires your Mantis install and Subversion to be on the same server.
  • You must access the script by the command line (cli) because this is how it is written.
  • checkin.php is getting phased out for the plugin SourceSVN which is easier tocnfigure and has more options.
  • SourceSVN, the replacement, can also be used from remote servers as is. SourceSVN also utilises plugins

It should be noted that are some work arounds to allow you to use a remote subversion server with checkin.php and i will make a note of them here to complete my research so you guys dont have to go the hard way to figure all of this out.

Old Read These

Solutions

As mention above there are a couple of ways to get checkin.php to work so i will outline these here.

  1. Standard Method (Local) - Installing the Subversion Server and Mantis on the same server. The following instructions are mainly for linux/unix, but the hook scripts can be easily altered to work in windows and interactions between Xampp and another apache stack is possible

The standard code of Mantis does not allow for web access of the checking script for one reason or another. I have come across 2 unique and clever solutions

  1. PHP Gateway Wrapper (Remote)
  2. Alter Mantis Code (Remote) - as mentioned already mantis checkin.php can only be accessed from the command line, this however is easy to overcome by rewriting the file and once you know this it is a simple procedure.

Research Links:

Generic Subversion

VisualSVN / Windows

 

 


2 - Source Control Integration Plugin (Local / Remote)

The Source Plugin provides a flexible framework for fully integrating any sort of Version Control System (VCS) with Mantis, including support for multiple repositories, changesets or revisions, and file path tracking. It has many features already, such as importing repository history and optional support for remote check-ins or imports, and utilizes a class-based API to access objects in the database.

Source require you to install the Source Core and then an appropriate plugin. There are several plugins and they are available from here... and support several plaforms. Only WebSVN currently has support toa loow to run your own server remotely, the rest are either online services or require Mantis and your Subversion Server to be on the same machine. Beacuse this is a plugin all of the configuration options are handled within the Mantis Plugin Panel, so no code is required to be altered.

The Process Overview

All plugins require a post-commit hook to be configured in the Subversion repository to post information to Mantis.

The checkin / issue close process is simliar to the checkin.php but all the regex settings are configured in the plugin installed in Mantis, the POST URL is different, and the POST only informs Mantis Source that a changes has been made and where to find it, which it then does to aquire all the information whereas the checkin.php method sends all the information in one go to Mantis.

  1. make a change to a file or files
  2. use TortoiseSVN or other client to submit your changes to your Subversion Repository
  3. You start a commit and add your comments including fixes #0000001 and what ever else you want to add to your comment (#0000001 is a phrase that is searched for by manits)
  4. you submit your commit
  5. the post-commit hook is triggered and this hook POSTs that a Revision has occured to Mantis including the REV number and location
  6. Mantis then locally reads the related Subversion Revision
  7. Mantis then pastes the comments in to a note on the related issue.
  8. if the trigger code was a close code ie Fixes #0000001 it will then close the issue, if it was just a reference link ie issue #0000001 it will just post the comment . In both these case, Souce, will do some internal link in Mantis to all the changesets and Diff link - Check this

Different Post Locations for Source

These are some alternative SourceSVN POST URLs that do slightly different things but are not well documented.

http://<your mantis server>/mantis/plugin.php?page=Source/import&id=all&api_key=<your api key>
http://<your mantis server>/mantis/plugin.php?page=Source/import&id=all
http://<your mantis server>/mantis/plugin.php?page=Source/repo_import_latest&id=XX
http://<your mantis server>/mantis/plugin.php?page=Source/index
http://<your mantis server>/mantis/plugin.php?page=Source/checkin

Some of these should have variables with them, via POST or GET (is possibly valid)

Install Source Control Integration Plugin Core

Files can be download here (download the files by clicking 'download zip' on the right). You will also find some simple instructions for installing the plugins.

To get Source to work you need to install its core and then the additional plugin for the platform for you choice. But for now we will just deal with the Source Core

Official Instructions Here (scroll down)

Installation Notes

  • Mantis is required to be on the same machine as the Subversion Server. If like me you are running CollabNet Subversion Edge on your windows PC (if not i recommend it) you will need to run your Mantis install on your PC. The quickest way to do this is to install XAMPP and run it as a service. Make sure you also run it on another port, ie. not 80 or 8080 you will need these for running XAMPP for website development. Using these ports might cause issues with other installed software on your computer.
  • Generate API Key
    • where it says "run openssl rand -hex 12" for us running on windows the binary/exe is here: D:\Documents\Developer\SubversionEdge\bin\openssl.exe
    • open a command prompt and run D:\Documents\Developer\SubversionEdge\bin\openssl.exe rand -hex 12 and copy the results (right click on cmd window, press enter, paste contents into notepad)

Configuration

Most of the options are preconfigured and dont need touching but i will describe what the options are becaus ei have not found descriptions elsewhere. If you need to set any they will be listed at the end.

  • View Threshold -
  • Update Threshold -
  • Manage Threshold -
  • Set Username Threshold -
  • Main Menu Links - show thes link in the Mantis Main Menu
  • Enabled Features -
  • Bug Link Regex Pass 1 - the regex that scans for issue number comments (i.e. issue #0000001)
  • Bug Link Regex Pass 2 - the follow up regex that extracts the actual issue number (i.e. 0000001)
  • Bug Fixed Regex Pass 1 - the regex that scans for fixed issue commnets (i.e. fixed #0000001)
  • Bug Fixed Regex Pass 2 - the follow up regex that extracts the actual issue number (i.e. 0000001)
  • Bug Fixed Status - what status to set when a comment says fixed
  • Bug Fixed Resolution - what outcome to state when issue is fixed
  • Bug Fixed Message Template -
  • Bug Fixed Message View State - should the comment be set as Private or Public
  • Bug Fixed Assign To Committer - the SVN Commiter will be registered as the author of the comment/Issue close
  • API Key - the secret key required for POSTing informatioin to Mantis. It adds a bit of security
  • Allow Remote Check-In (Deprecated) -
  • Allowed Addresses - Allowed addresses for Allow Remote Check-In
  • Allow Remote Imports (Deprecated)
  • Allowed Addresses - Allowed addresses for Allowed Addresses
  • SVN: Path to binary - the physical path to your Subversion Binary. Both windows(c:\csvn\) and Linux paths (/usr/path/bin/) are suitable here.
  • SVN: Command arguments -
  • SVN: Trust All SSL Certs (Requires Subversion 1.6 or newer) - allow to use self-signed certificates and certificates that dont match but are presented.
  • SVN: Use Windows `start` (Requires a configured path to binary) - Is required when using windows

Source Control Integration with SourceSVN Plugin (Local)

This plugin is used to allow direct integration with a local Subversion server (ie on same PC / Machine). It must have direct access to the binaries but can run on either windows or Linux.

Installation

  • From your downloaded package from GitHub, upload the plugin /SourceSVN/ to your {your Mantis}plugins/ folder
  • No Configuration required

Subversion Server Hook

You need to configure a batch file to be run when a commit is made on the Subversion Server. This trigger is called a hook. When you have created your repository there will be a folder inside called Hooks. Each repository gets its own Hooks folder so this process will need to be repeated for each repository.

My instructions are based on me running a CollabNet Subversion Edge Locally on my PC.

  1. navigate to your repository hooks folder (D:\Documents\Developer\SubversionEdge\data\repositories\Test\Hooks\)
  2. create a file called post-commit.bat
  3. paste the contents below from the 'Windows post-commit.bat'
  4. edit the details as needed
  5. done

Linux / Unix post-commit.tmpl

This is an example post-commit script that is found in plugin, /SourceSVN/post-commit.tmpl . It is clearly written for Linux, so will not run on windows.

#!/bin/sh

# Copyright (c) 2012 John Reese
# Licensed under the MIT license

REV="$2"

URL="http://localhost/mantisbt/plugin.php?page=Source/checkin"
PROJECT="Repository Name"
API_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxx"

LOG_FILE=`mktemp /tmp/svn_${PROJECT}_${REV}_log.XXX`

CURL=/usr/bin/curl

${CURL} -d "repo_name=${PROJECT}" -d "data=${REV}" -d "api_key=${API_KEY}" ${URL} >> ${LOG_FILE}

Windows post-commit.bat

@ECHO OFF

SET REV=%2

SET URL="http://localhost/mantisbt/plugin.php?page=Source/checkin"
SET PROJECT="Repository Name - exactly the same as set in mantis ?"
SET API_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxx"

LOG_FILE="%PROJECT%_%REV%_log.XXX"

D:\Documents\Developer\SubversionEdge\curl.exe -d "repo_name=%PROJECT%" -d "data=%REV%}" -d "api_key=%API_KEY%" %URL% >> %LOG_FILE%

rem this is better. logging is optional
rem D:\Documents\Developer\SubversionEdge\curl.exe -d "repo_name=%PROJECT%" -d "data=%REV%}" -d "api_key=%API_KEY%" %URL% >> %PROJECT%_%REV%.log

You will need to download the correct version of CURL to allow POSTs from the command line. You can also use wget via Cygwin.

Research Links

These links will include all the basic POST information as well as setup information becasue they are all required for further plugins relating to Subversion on a Local PC.

Generic Subversion

VisualSVN / Windows

 

 


3 - Source Control Integration with SourceSVN Plugin extended by SourceWebSVN Plugin (Local)

WebSVN allows you to show File / Revision differences by using a web browser and this plugin integrates WebSVN. This is an extension to Source Integration and requires the SourceSVN plugin to be installed.

You should install Source Integration Core and SourceSVN and SourceWebSVN. These should already be done from the steps above.

As WebSVN doesnt seem to be developed anymore, there is a replacement Web Based Fifference View called ViewVC. This come preconfigured in CollabNet Subversion Edge. Perhaps the developr could look at making this a plugin and while he is at it consider making it possibly to remotely ccess a Subversion server and which case CollabNet Subversion Edge will be perfect as it has a REST API just for this sort of thing.

Installation Instuctions

  • Install WebSVN and link it to your Subversion Server
  • Install the WebSVN plugin by uploading it to {mantis}/plugins/ folder
  • configure the WebSVN plugin. Use the links below for a more indepth tutorial

Research Links

Generic Subversion

VisualSVN / Windows

 

 


4 - Custom Direct Integrations

This section for for custom integrations such as direct database accessing

Research Links

Generic Subversion

VisualSVN / Windows

  • A nameless soldier...: Integrate Mantis+Subversion on Windows - this tutorial goes through setting up each aspect of the stack using indivdual stack packages and then the guy has used a custom python script to interegate the Mantis SQL database directely. This might be useful on setting up individual stack items in windows

 

 


What I Did

This are the real bare bone instructions of what i did to get the particular methods to work. litterately just what you should to do to get things working. If you want more you should read the rest of the article but i do know people either just want a quick fix or find it easier to process things when it is working.

I have covered the 2 distinct methods of Remote checkin.php via Gateway PHP script (Not fully working) and Remote checkin.php via Altered checkin.php to accept $_POST

 

 

 

Method 1 - Remote checkin.php via Gateway PHP script (Not fully working)

NB: all of this code where appropriate, has logging enabled to help you get this setup, but it is not required for operation

Basics

  • install Mantis on your Web Server
  • install CollabNet Subversion Edge on your windows PC locally. This assumes you have setup the server as per my CollabNet Subversion Server Setup Instructions

Create the Gateway Script

  • Create this script, /mantis/scripts/do_checkin.php
<?php
// For testing if your data is getting POSTed
echo "\r\n";
echo "This is the POSTed data at checkin.php";
echo "\r\n";
echo 'Secret:  ';
echo $_POST['secret'];
echo "\r\n";
echo "\r\n";
echo 'Message:';
echo "\r\n";
echo $_POST['message'];
echo "\r\n";
//exit;

// For diagnostics only
echo 'Connection Made'; echo "\r\n";

// Very cheap security measure, simply so that not just *anybody* can post issues.
if ($_POST['secret'] !== 'somePasswordHere') {
	echo 'You are not Authorised'; echo "\r\n"; // for diagnostics only
	exit;
}

// For diagnostics only
echo 'Authorised, I will now process the Data'; echo "\r\n";

$cmd = 'php checkin.php <<< ' . escapeshellarg($_POST['message']);
system($cmd);

// For diagnostics only
echo 'Data POSTed'; echo "\r\n";

// To prevent infinite looping
exit;

Notes

  • system($cmd); in do_checkin.php i had to put an exit; command after it as it seems to keep re-running do_checkin.php. the POST variables seem not to be passed back from checkin.php. Perhaps using exec() which does not return a result but just executes the command. Not adding the exit; command causes the script to keep re-running and kills my shared server.
  • This needs testing on my local server because you can murder a webserver. It is the looping that does it. try tommorow with exec and see if theyou are not authorised is caused. ie does exec() not loop where as system() loops because it is sending stuff back to the do_plugin.php. either way it is the looping that kills the server and the authorization if statements stops the looping.
  • <<< means it is passing a single string into the file via STDIN/STDOUT , page 404 of the 'The Linux Command Line' PDF/Book

Enable CLi (Command Line) on Web Server

some webservers will has the command line (CLi) disabled. to fix this we need to enabled system(); This is done by the following:

  1. you need to do a dump of the systems php.ini (i assume you can do this) and put it in the php.ini /mantis/scripts/ folder
  2. now in /mantis/scripts/, you need to create a .htaccess file and add the following command in to it:

    SetEnv PHPRC /home/myaccount/public_html/sp/mantis/scripts/php.ini

    NB: you should alter the above location to match your own physical path
    NB: PHPRC does not apply php.ini recusively

  3. now edit the php.ini file
    1. search for disable_functions.
    2. remove system from the disabled functions
    3. save changes

You have now enable CLi for this folder only. if this is a little insecure for you because people will know where to look. You can create a completely random folder any where on your website with a whacky URL and put the do_checkin.php file in there and just alter the file locations appropriately. Then this will also hide this gateway to the regular internet.

Create Subversion post-commit Hook

This hook methods relies on a post-commit.bat being triggered by Subversion which in turn triggers a powershell script which allows us to grab more information about the revision/commit to send this to mantis. Such as real author, time and comments.

post-commit.bat

  • create a post-commit.bat in D:\Documents\Developer\SubversionEdge\data\repositories\Test\hooks\ . You should use the script below and edit as needed
@ECHO OFF

rem For live Subversion Server
rem SET REPOS=%1
rem SET REV=%2

rem For local testing by directly setting variables to use with directely running post-commit.bat
rem saves having to make a change for each submit and the process with that
SET REV="1"
SET REPOS="D:\Documents\Developer\SubversionEdge\data\repositories\Test"

CD %REPOS%\hooks
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy ByPass -File %REPOS%\hooks\post-commit.ps1 "%REPOS%" "%REV%"

This file is what Subversion executes and is where you can change it from live to test. And by Live and Test i mean where you load the variables from. To use in the live mode while testing can be a pain because you have to change a file and submit it with TortoiseSVN everytime you want to use it. Instead i have added the Test mode where you can set the variables REPOS & REV with some arbitrary data allowing you to manually execute post-commit.bat from the command line on your window PC allowing for rapid testing and deployment.

The file above is already configured to be in Test modes, to change it to use the real variables from the Subversion Server (assumes you use a real SVN commit) you need too:

Rem out these Lines

  1. SET REV="1"
  2. SET REPOS="D:\Documents\Developer\SubversionEdge\data\repositories\Test"

UnRem these Lines

  1. rem SET REPOS=%1
  2. rem SET REV=%2

Done, you have now changed the mode. Both these methods will communicate with your website so just bear this in mind.

Powershell Script - post-commit.ps1

  • create a post-commit.ps1 in D:\Documents\Developer\SubversionEdge\data\repositories\Test\hooks\ (one not L). You should use the script below and edit as needed
[System.Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null

# output this to send a newline operation to the log file
$newline = "`r`n"

## --------------------------------------------------
## get environmental variables set by Subversion
## --------------------------------------------------

$REPOS = $args[0]
$REV = $args[1]

"$REPOS $REV"

## --------------------------------------------------
## Manually load information from Subversion
## Commit/Rev Author, Date, Time, Comment, Changed Files(s)
## --------------------------------------------------

$svnlook = 'D:\Documents\Developer\SubversionEdge\bin\svnlook.exe'

$auth = & $svnlook author -r $REV $REPOS
$dt = & $svnlook date -r $REV $REPOS
$changed = & $svnlook changed -r $REV $REPOS
$changed = $changed -join "`n"
$log = & $svnlook log -r $REV $REPOS
$log = $log -join "`n"
$data = "Changeset [${REV}] by $auth, $dt
$log
$changed"

## --------------------------------------------------
## POSTing information to checkin.php
## --------------------------------------------------

$postData = "secret=somePasswordHere&message="
$postData += [System.Web.HttpUtility]::UrlEncode($data)

# outputs the post to the logfile (appends)(for diagnostics only)
Write-Output "This is the data POSTed to checkin.php" >> checkin-response.log
$postData >> checkin-response.log

# POSTing using wget and cywin (creates/replaces log with response)
#D:\cygwin64\bin\wget.exe -O checkin-response.log --post-data=$postData http://exampleserver.com/sp/mantis/scripts/do_checkin.php

# POSTing with CURL (exactly the same as wget above)(creates/replaces log with response)(seems slightly slower than wget)
#D:\Documents\Developer\SubversionEdge\curl.exe -d $postData http://exampleserver.com/sp/mantis/scripts/do_checkin.php > checkin-response.log

# POSTing with CURL (same as wget above, but appends log with response)(seems slightly slower than wget)
D:\Documents\Developer\SubversionEdge\curl.exe -d $postData http://exampleserver.com/sp/mantis/scripts/do_checkin.php >> checkin-response.log

## --------------------------------------------------
## POSTing with CURL (alternate syntax)
## --------------------------------------------------

# you must disable $postData and other POSTing method above befor using this
#$secret = 'somePasswordHere'
#$postData = [System.Web.HttpUtility]::UrlEncode($data)

# my prefered CURL call (append output to log file)
#D:\Documents\Developer\SubversionEdge\curl.exe -d secret=$secret -d message=$postData http://exampleserver.com/sp/mantis/scripts/do_checkin.php >> checking-response.log

## CURL Syntax
# Posting variables: 			-d secret=$secret, -d "secret=$secret", -d secret=somePasswordHere, -d $postData,  -d "$postData", -d "message=$postData", -d message=$postData
# Append to log (add at end): 	>> checkin-response.log, >> D:\checkin-response.log
# Replace log (add at end): 	| Out-File checkin-response.log, > checkin-response.log

## --------------------------------------------------

# start a newline in the log file - useful when appending to log file
$newline >> checkin-response.log

# Delete checkin-response.log
#rm checkin-response.log

In this file you will see a lot of code that is not used. I have left this code in there so people, including me, can check syntaxes should they ever wish to make changes because unless you have come across these commands before you could spend quite a lot of time just reseaching the basics.

This file is a Windows Poweshell Script and until i did this project i have never used Powershell. It allows us, via the command line, to use svnlook and get extra information formt he Subversion server that is not passed in the variables we have to work with. We will use this data to populate the close message in the Mantis issue with some more data other than closed. It makes the integration a little tighter.

Errors You might get when running this powershell script (seen best when you manually run post-commit.bat from the command line)

Whilst trying to get the script to work i got these error message, which means more than likely you guys will at some point. So i ahve pasted the errors and their solutions. Simple as that.

D:\Documents\Developer\SubversionEdge\data\repositories\Test\hooks>post-commit.bat
The term 'D:\Documents\Developer\SubversionEdge\data\repositories\Test\hooks\post-commit.ps1 D:\
Documents\edgeSVN\data\repositories\Test ' is not recognized as the name of a c
mdlet, function, script file, or operable program. Check the spelling of the na
me, or if a path was included, verify that the path is correct and try again.
At line:1 char:115
+ D:\Documents\Developer\SubversionEdge\data\repositories\Test"\hooks\post-commit.ps1 "D:\Docume
nts\edgeSVN\data\repositories\Test" " <<<<
    + CategoryInfo          : ObjectNotFound: (D:\Documents\ed...ositories\Tes
   t :String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

most likely the file path or name is wrong

D:\Documents\Developer\SubversionEdge\data\repositories\Test\hooks>post-commit.bat
File D:\Documents\Developer\SubversionEdge\data\repositories\Test\hooks\post-commit.ps1 cannot b
e loaded because the execution of scripts is disabled on this system. Please se
e "get-help about_signing" for more details.
At line:1 char:66
+ D:\Documents\Developer\SubversionEdge\data\repositories\Test\hooks\post-commit.ps1 <<<<  D:\Do
cuments\edgeSVN\data\repositories\Test
    + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
    + FullyQualifiedErrorId : RuntimeException

because i am on windows x64, by default it will not run unsigned code so i added -ExecutionPolicy ByPass -File into the command for powershell

Mantis

These are all of the Mantis related changes you need to do.

Edit Mantis Config File

For some unknown reason you need to add these variables into the config file. They are put there because they become globally available, but i think they should of been there in the first place. These variables are used to match issue number in your comments so it know which issue to close. These can be altered if you know regex to match prefered words ie fixed issue #500.

  • edit config_inc.php and add this code at the bottom.
# Array of allowed SVN hosts that can close issues via curlcheckin.php
$g_source_control_server = '127.0.0.1';


# Account to be used by the source control script.  The account must be enabled
# and must have the appropriate access level to add notes to all issues even
# private ones (DEVELOPER access recommended).
$g_source_control_account = 'svn';

# Checks for 'Issue' references in the comment - used so the comment can be assigned to the correct issue
# Regular expression used to detect issue ids within checkin comments.
# see preg_match_all() documentation at (detect bug or issue in comment)
$g_source_control_regexp = '/(?:bugs?|issues?|reports?)+\s*:?\s+(?:#(?:\d+)[,\.\s]*)+/i';

# Check for 'Fixed Issue' references in the comment - used to id issues that need closing
# Regular expression used to detect the fact that an issue is fixed and extracts
# its issue id.  If there is a match to this regular expression, then the issue
# will be marked as resolved and the resolution will be set to fixed.
$g_source_control_fixed_regexp = '/(?:fixe?d?s?|resolved?s?)+\s*:?\s+(?:#(?:\d+)[,\.\s]*)+/i';


# For open source projects it is expected that the notes be public VS_PUBLIC, however,
# for non-open source it will probably be VS_PRIVATE.
$g_source_control_notes_view_status = VS_PRIVATE;


# If set to a status, then after a checkin with a log message that matches the regular expression in
# $g_source_control_fixed_regexp, the issue status is set to the specified status.  If set to OFF, the
# issue status is not changed. Use CLOSED if you prefer.
$g_source_control_set_status_to = RESOLVED;


# Whenever an issue status is set to $g_source_control_set_status_to, the issue resolution is set to
# the value specified for this configuration.
$g_source_control_set_resolution_to = FIXED;
  • You should note this code is not the same as the code in the articles as the regex was wrong and i made the order more logical. I took the regex code from Source Plugin. There is also furthe regex in the checkincurl.php to take the issue number out of the matches.
  • $g_source_control_server - this feature allows you to set which ip address are allowed to connect. i have this disabled in my curlcheckin.php script because it will only work with dedicated address.
  • $g_source_control_notes_view_status - this requires further codeing (see here) and allows you to set the status of the comment added to the related issue whether you want it private or public.

Configure Mantis

There are some basic settings and obvious stuff to be done in matis.

  • create a user called 'SVN' or whatever you want with 'Developer Access' . This will be the account that is used to post issue closes.
  • create your project. for purposes of this we will call it 'Test'
  • give 'SVN' user permissions to the 'Test' repository if it does not already have

Install CURL / WGET

Obviously you need to install one of these at least to be able to perfomr the POSt from the command line. You dont need both unless you want to play.

  • CURL (easier)
    • download the non SSL version of the package for your system here
    • extract CURL.exe to D:\Documents\Developer\SubversionEdge\
    • when everything is working i would recommend making it SSL. I think all you have to do is swap the binary for one that is SSL (not done it so cant say)
  • wget
    • download the appropriate installer from here
    • install the package to D:\cygwin64\
    • when it mentions about a place to put the downlaoded files it just means where can it create a local download repository because not all packages are installed
    • during the install it will ask you what packages you want
      • search for wget
      • select wget
      • click next
    • done

Make You First Commit

Now this is where things get tricky. I have not been able to make a successful issue close on mantis yet and i believe that is because of the securoty on my web server. If any one manages to get this working i would like to here how.

What i have managed to do is get the Subversion post-commit to POST all the required data to my do_checkin.php successfully.

Below are some responses i get in the log files:

Test Mode set in post-commit.bat

This is the data POSTed to checkin.php
secret=somePasswordHere&message=Changeset+%5b1%5d+by+SYSTEM%2c+2015-01-09+18%3a01%3a24+%2b0000+(Fri%2c+09+Jan+2015)%0d%0aCreating_initial_branch_structure%0d%0aA+++branches%2f%0aA+++tags%2f%0aA+++trunk%2f

This is the POSTed data at checkin.php
Secret:  somePasswordHere

Message:
Changeset [1] by SYSTEM, 2015-01-09 18:01:24 +0000 (Fri, 09 Jan 2015)
Creating_initial_branch_structure
A   branches/
A   tags/
A   trunk/
Connection Made
Authorised, I will now process the Data
Content-type: text/html


This is the POSTed data at checkin.php
Secret:  

Message:

Connection Made
You are not Authorised
Data POSTed

How To Run

  1. Just open cmd
  2. navigate to your post-commit.bat
  3. and run it

see the A lines, this is because no information matches a Revision and so it gets an invalid lookup

Live Mode set in post-commit.bat

This is the data POSTed to checkin.php
secret=somePasswordHere&message=Changeset+%5b54%5d+by+admin%2c+2015-01-17+17%3a14%3a04+%2b0000+(Sat%2c+17+Jan+2015)%0d%0aWe+have+fixed+issue+%230000001+and+solved+some+things+etc.+This+is+a+test+commit%0d%0aU+++trunk%2fchangelog.txt

This is the POSTed data at checkin.php
Secret:  somePasswordHere

Message:
Changeset [54] by admin, 2015-01-17 17:14:04 +0000 (Sat, 17 Jan 2015)
We have fixed issue #0000001 and solved some things etc. This is a test commit
U   trunk/changelog.txt
Connection Made
Authorised, I will now process the Data
Content-type: text/html


This is the POSTed data at checkin.php
Secret:  

Message:

Connection Made
You are not Authorised
Data POSTed

How To Run

  1. Configure TortoiseSVN and checkout your 'Test' repository. Search for my article if i have written it. If not, figure it out.
  2. make a change to a file in your checkedout repositroy, or create a file
  3. right click on the local folder you have used to check out files from you SVN Repository select 'SVN Commit'
  4. TortoiseSVN will open and you should enter a commit message, for now i would recommend

    We have fixed issue #0000001 and solved some things etc. This is a test commit
  5. click submit

Final Thoughts

So this is where i am at. I (with help from alt-tag, GraFX, LinLog) have got all the information to the Mantis checkin.php but cannot get that part of the process to work. All is not lost because with the work i have do here someone else might be able to finish.

If you have a private server or even a server on a local network (seperate mantis and subversion servers) this method might work as is.

 

 

 

 

 

Method 2 - Remote checkin.php via Altered checkin.php to accept $_POST

In this method i have rewritten the checkin.php to accept information via $_POST and will not require CLI access which eludes me, i have also renamed it curlcheckin.php so no core code is hacked.

For further information read the appropriate sections from Remote checkin.php via Gateway PHP script - What i did (Not fully working), this prevents me from duplicating the instructions and having to maintain the text twice.

NB: some of this code where appropriate, has logging enabled to help you get this setup, but it is not required for operation

Basics

  • install Mantis on your Web Server
  • install CollabNet Subversion Edge on your windows PC locally. This assumes you have setup the server as per my CollabNet Subversion Server Setup Instructions

Create Subversion post-commit Hook

use the instructions above for extra information

  • create a post-commit.bat in D:\Documents\Developer\SubversionEdge\data\repositories\Test\hooks\ . You should use the script below and edit as needed - see notes above
  • create a post-commit.ps1 in D:\Documents\Developer\SubversionEdge\data\repositories\Test\hooks\ (one not L). You should use the script below and edit as needed - see notes above but change do_checkin to curlcheckin.php as we will not be using a gateway file.

Create a new checkin.php to accept $_POST

  • create the following file {mantis}/scripts/curlcheckin.php using this code
<?php
# MantisBT - a php based bugtracking system
# Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
# Copyright (C) 2002 - 2014  MantisBT Team - mantisbt-dev@lists.sourceforge.net
# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT.  If not, see <http://www.gnu.org/licenses/>.
# See the README and LICENSE files for details

	# --------------------------------------------------------
	# $Id: checkin.php,v 1.5.2.1 2007-10-13 22:35:16 giallu Exp $
	# from https://www.mantisbt.org/bugs/view.php?id=8847
	# Updated 17-01-2015 shoulders @ exampleserver.com
	# --------------------------------------------------------

global $g_bypass_headers;
$g_bypass_headers = 1;
require_once( dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'core.php' );

// Very cheap security measure, simply so that not just *anybody* can post issues.
if ($_POST['secret'] !== 'somePasswordHere') {
    echo 'You are not Authorised'; echo "\r\n"; // for diagnostics only
    exit;
}

/*# check if we are called from the right IP ( @todo might wanna use a array here )
# Only works for dedicated IP addresses
$t_source_control_server = config_get( 'source_control_server' );
if($_SERVER['REMOTE_ADDR'] != $t_source_control_server )
{
	echo "Not allowed from this IP !!\n";
	exit(0);
}*/

# Check that the username is set and exists
$t_username = config_get( 'source_control_account' );
if( is_blank( $t_username ) || ( user_get_id_by_name( $t_username ) === false ) ) {
	echo "Invalid source control account ('$t_username').\n";
	exit( 1 );
}

# Detect references to issues + concat all lines to have the comment log.
$t_commit_regexp = config_get( 'source_control_regexp' );
$t_commit_fixed_regexp = config_get( 'source_control_fixed_regexp' );

# make sure both the regex scanner and final output gets the full message
$t_line = $_POST['message'];
$t_comment = $_POST['message'];
echo $message;

# Regex Scanning of Comment
$t_issues = array();
$t_fixed_issues = array();

if( preg_match_all( $t_commit_regexp, $t_line, $t_matches ) ) {
	$t_count = count( $t_matches[0] );
	for( $i = 0;$i < $t_count;++$i ) {
		$t_issues[] = $t_matches[0][$i];
	}
}

if( preg_match_all( $t_commit_fixed_regexp, $t_line, $t_matches ) ) {
	$t_count = count( $t_matches[0] );
	for( $i = 0;$i < $t_count;++$i ) {
		$t_fixed_issues[] = $t_matches[0][$i];
	}
}

# If no issues found, then no work to do.
if ( ( count( $t_issues ) == 0 ) && ( count( $t_fixed_issues ) == 0 ) ) {
	echo "Comment does not reference any issues.\n";
	exit(0);
}
		
# If no issues found, then no work to do.
if(( count( $t_issues ) == 0 ) && ( count( $t_fixed_issues ) == 0 ) ) {
	echo "Comment does not reference any issues.\n";
	exit( 0 );
}

# Login as source control user
if( !auth_attempt_script_login( $t_username ) ) {
	echo "Unable to login\n";
	exit( 1 );
}

/*
// not implemented needs additonal coding and $_POST['user'] configuring in the post-commit.bat
# first we try to figure out if we can login with the source control/Subversion username.
# Usernames on Subversion and Mantis should match
$temp_username = user_get_name_by_source_control_id( $_POST['user'] );
if( !auth_attempt_script_login( $temp_username ) ) {
	# Login as source control user
	if ( !auth_attempt_script_login( $t_username ) ) {
		echo "Unable to login\n";
		exit( 1 );
	}	
}
*/

# history parameters are reserved for future use.
$t_history_old_value = '';
$t_history_new_value = '';

# add note to each bug only once
$t_issues = array_unique( $t_issues );
$t_fixed_issues = array_unique( $t_fixed_issues );

# Call the custom function to register the checkin on each issue.
foreach( $t_issues as $t_issue_id ) {
	if( !in_array( $t_issue_id, $t_fixed_issues ) ) {		
		$matches = Null;		
		preg_match('/#?(\\d+)/', $t_issue_id, $matches);
		$t_issue_id  = $matches[1];
		helper_call_custom_function( 'checkin', array( $t_issue_id, $t_comment, $t_history_old_value, $t_history_new_value, false ) );
	}
}
foreach( $t_fixed_issues as $t_issue_id ) {		
		$matches = Null;		
		preg_match('/#?(\\d+)/', $t_issue_id, $matches);
		$t_issue_id  = $matches[1];
		helper_call_custom_function( 'checkin', array( $t_issue_id, $t_comment, $t_history_old_value, $t_history_new_value, true ) );
}

exit( 0 );
  • This file is a rewrite of checkin.php but i have made it accept $_POST and then corrected the regex accordingly.
  • There are lots of different refgex out there but i have used the defaults from Source Integration Plugin because they work.
  • SVN post-commit with windows - Mantis - In source integration you cannot solve muliple jobs, this i a regex fix, also some very good Regex for bug detection. I used this to fix multiple issues not getting processed in checkin.php

Mantis

Follow the instructions above.

Install CURL / WGET

Follow the instructions above.

Make You First Commit

Follow the instructions above.

Final Thoughts

This method is fully working with extended comments and to be honest should of been like from the beginning also native to Mantis. It appears that in the next version of mantis that this methods has been removed. I am sure it will be very easy to add back in and from what i can see it only utilises an inbuilt fuction helper_call_custom_function() to post the comments to the issue and process the issue status.

Although Mantis is moving towards using Source Integration Plugin as its primary integrator, which is good, for those of us who do not have their Subversion Server and mantis on the same server this removal will cause problems.

One way around this is for Mantis / John Reese to add Remote Subversion support by utilising CollabNet Subversion Edge's REST API whilst also adding support for ViewVC which is an excellent Web based Difference viewer that comes preconfigured in this package. It should be noted that ViewVC is availabe as a seperate package.

Most techies that i know use PCs and some have websites. This is probably a large group of people that have PCs and basic websites where they could host Mantis but not a Subversion Server and that is where CollabNet Subversion Edge comes in handy as this setup would be the first suitable arrangement they come across, Mantis on website, Subversion on their own PC.

Read 4116 times Last modified on Wednesday, 11 February 2015 10:28