Thursday, February 17, 2011

Integrate Mantis+Subversion on Windows

Integrate Mantis+Subversion on Windows

3 days ago I got a task to integrate mantis with subversion, so that each commit will be linked with a mantis' issue. I
am a java guy, and also know some about python(anyway I every learned python and adopted it in my project). Mantis is
developed by PHP, as I have some experience with script language, it is not a seemingly hard task for me.

Apache 2.2
Mantis 1.2.4
Mysql 5.0
PHP 5.3.5 (zip archieve)
Python 2.6.6
VisualSVN 2.1.5
Window XP(32bit)

1. Install PHP
* download PHP from
* upzip to d:/php-5.3.5(you can unzip to any your favourite directory)
* configure PHP.
* Check to see if you already have a PHP configuration file or not: d:\php-5.3.5\php.ini.
* If not copy d:\php-5.3.5\php.ini-production to d:\php-5.3.5\php.ini
* Open this file php.ini with "notepad", uncomment '; extension_dir = "ext"'
NOTE: maybe you should set extension_dir to absolute path d:/php-5.3.5/ext, as when I set it to 'ext', a strange
window exception 'memory is only read' or something like that will be thrown out when try to launch apache.
* set upload_max_filesize = 8M, you should make corresponding adjustment in mantis later.
* load mysql module, uncomment this line ';extension=php_mysql.dll'
* set timezone, or mantis will show some warning message 'date.timezone = Asia/Shanghai'

2. Install Python
* download python from, the version must be 2.6.X(I will explain why only 2.6.x later)
* install python to d:/python26 (you can install to you favourite directory)
* add python to window path environment vairable

3. Install Mysql
* download mysql from
* install mysql to d:/mysql/mysql-server-5.0(you can install to you favourite directory). When installation, you'd
better install the development kit also(c headers).
* create 'root' user with password 'root'

4. Install Apache
* download apache from
* install apache to d:/apache2.2(you can install to you favourite directory)
* configuring Apache to Use PHP as a Loaded Module
* Use "notepad" to open d:\Apache2.2\conf\httpd.conf
* Go to the LoadModule section and enter:
LoadModule php5_module "d:/php-5.3.5/php5apache2_2.dll"
PHPIniDir "d:/php-5.3.5"
* Go to mime_module section and define a mime type as:

AddType application/x-httpd-php .php

NOTE: refer to

5. Setup Mantis in Apache
* unzip mantis and copy to d:\apache2.2\htdocs, let's say it is D:\Apache2.2\htdocs\mantisbt
* start apache server, and access 'http://localhost/mantisbt/admin/install.php'
* follow the instruction to configure mantis, more detail pls refer to
* configure email and more. below is my config_inc.php(if no-exist, create one) which can be found in D:\Apache2.2\htdocs\mantisbt.

$g_hostname = 'localhost';
$g_db_type = 'mysql';
$g_database_name = 'mantisbt';
$g_db_username = 'root';
$g_db_password = '111111';

#internationalize...chinese-simple UI
#$g_default_language = 'auto';
$g_default_language = 'english';

# must create a 'upload' folder in mantis, and set the upload folder as 'upload' when create project.
$g_file_upload_method = DISK;
# the max file size must be less than php configuration 8M
$g_max_file_size = 7000000;

# LOG #
# looks like it doesn't work
$g_log_destination = 'file:e:/tmp/mantis.log';

#$g_administrator_email = '';
#$g_webmaster_email = '';
# the sender email, part of 'From: ' header in emails
#$g_from_email = '';
# the sender name, part of 'From: ' header in emails
$g_from_name = 'Mantis Bug Tracker';
# the return address for bounced mail
#$g_return_path_email = '';
# allow email notification
# note that if this is disabled, sign-up and password reset messages will
# not be sent.
$g_enable_email_notification = ON;
$g_phpMailer_method = PHPMAILER_METHOD_SMTP;
$g_smtp_host = '';
$g_smtp_port = 25;
$g_smtp_connection_mode = '';
$g_smtp_username = '';
$g_smtp_password = 'ramonramon';

* create new user of mantis...
* create new project ...

6. Config MysqlDB for python
* download binary package of mysql-python from
* install mysql-python
NOTE: I ever tried to compile source package from, but it is really hard. In
order to setup a gcc environment, I tried cygwin, mingw32, even visualC++, but what I got are all failure.

7. Install VisualSVN
* download VisualSVN server from
* create a new repository at "D:\project\VisualSVN-Server\Repositories\HelloWorld"

8. Integrate Subversion with Mantis
* as on window, create pre-commit.bat at "D:\project\VisualSVN-Server\Repositories\HelloWorld\hooks" first.
@echo off
set path=%path%;"D:\SVN_SERVER\svn-win32-1.5.4\bin"
set REPOS=%1
set TXN=%2

python D:\project\VisualSVN-Server\Repositories\HelloWorld\hooks\ %REPOS% %TXN%

if %errorlevel% gtr 0 goto err
exit 0
rem echo Empty log message not allowed. Commit aborted! 1>&2
exit 1
* implement python script
This script will integrate subversion with mantis, its general purpose is to verify that each commit of subversion must
be linked with a open bug of mantis.
import sys,os,datetime,time
import logging
import re
import _mysql as mysql

# init logging
LOG_FILENAME = 'e:/tmp/mantis_subversion.log'

# database setting
host = ''
username = 'root'
password = 'root'
database = 'mantisbt'

# mantis setting
mantis_user_id = 3 #user:'svn'

def integrate_mantis(rep_path, txn):
update the log message and changed list of subversion to a mantis issue, the general work flow as below:
1. check if the log message follow a spcified pattern, if true, start to check mantis, otherwise rollback the transaction.
2. check if the issuse specified in log message exists in mantis, if false, rollback the transaction.
3. insert log message and changed list to mantis.
# the format of log message must follow '#issuenumber log'
log = svnlook('log', rep_path, txn).strip()
logging.debug('log=' + log)
issue_number = get_issue_number(log)
# extract the log message
log = log[(log.find(' ')+1):].strip()

author = svnlook('author', rep_path, txn).strip()
logging.debug("author=" + author)
changed = svnlook('changed', rep_path, txn).strip()
logging.debug("changed=" + changed)

#check if the issue exists in mantis
conn = mysql.connect(host, username, password, database)
# set charset, python will use UTF-8 as default
if is_issue_exist(conn, issue_number):
update_mantis(conn, issue_number, author, log, changed)
except Exception as e:

def update_mantis(conn, issue_number, author, log, changed):
insert a bug note to specifed issue in mantis.
note = author + "@" + + "
" + log + "

" + changed
query = "insert into mantis_bugnote_text_table(note) values('" + note + "')";
bugnote_text_id = conn.insert_id()

now = str(int(time.time()))
query = "insert into mantis_bugnote_table(bug_id,reporter_id,bugnote_text_id,time_tracking,last_modified,date_submitted) "
query += "values(" + issue_number + "," + str(mantis_user_id) + "," + str(bugnote_text_id) + ",0," + now + "," + now + ")"

def is_issue_exist(conn, issue_number):
does the issue with a specified number exists in mantis??
query = "select * from mantis_bug_table where id=" + issue_number
r = conn.store_result()
if r.num_rows() <= 0:
msg = "No issue(id=" + issue_number + ") exist in mantis"
raise Exception(msg)
return True

def get_issue_number(log):
log_pattern = r'^[#]{1}(\d+)[\s]+(.+)'
result =, log)
if result == None:
msg = 'the log message must follow: #issue_number log_message'
raise Exception(msg)
return result.groups()[0]

def svnlook(subcommand, rep_path, txn):
output = os.popen('svnlook ' + subcommand + ' ' + rep_path + ' -t ' + txn)
#output = os.popen('svnlook ' + subcommand + ' ' + rep_path)

if __name__ == "__main__":
if len(sys.argv) != 3:
sys.stderr.write("[Usage] python " + sys.argv[0] + " $REP_PATH $TXN, pls check if you have provided all need arguments.");

integrate_mantis(sys.argv[1], sys.argv[2]);

1 comment:

Anonymous said...

Ηoωdy tеrгifіс blog!
Does гunning a blog liκe thіѕ taκe a great ԁeаl оf work?
I havе νеry little exрertіse іn coԁing hοwevеr Ӏ wаs
hoping to start my oωn blog sοon.
Anyhow, if you haνe any iԁeаs or techniquеѕ fοr
nеω blog oωnегs pleaѕe shагe.
I undeгstand thіѕ is off topіc
but Ι juѕt wanted to ask. Aρpreciatе it!

my webѕіtе - media temple web hosting