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.

System:
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 http://www.php.net/
* 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 www.python.org, 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 www.mysql.com
* 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 www.apache.org
* 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 http://windows.fyicenter.com/71_Apache_PHP_Tutorials_Installing_and_Running_Apache_Server_and.html

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 http://www.mantisbt.org/
* 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.

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

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

#----------------------------------------#
# FILE UPLOADING #
#----------------------------------------#
# 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';

#----------------------------------------#
# EMAIL #
#----------------------------------------#
#$g_administrator_email = 'ramon.li@mpos.net';
#$g_webmaster_email = 'ramon.li@mpos.net';
# the sender email, part of 'From: ' header in emails
#$g_from_email = 'mantis@mpos.net';
# 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 = 'mantis@mpos.net';
# 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 = 'mail.mpos.net';
$g_smtp_port = 25;
$g_smtp_connection_mode = '';
$g_smtp_username = 'ramon.li@mpos.net';
$g_smtp_password = 'ramonramon';

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

6. Config MysqlDB for python
* download binary package of mysql-python from http://www.technicalbard.com/files/MySQL-python-1.2.2.win32-py2.6.exe
* install mysql-python
NOTE: I ever tried to compile source package from http://mysql-python.sourceforge.net/, 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 http://www.visualsvn.com/
* 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"
setlocal
set REPOS=%1
set TXN=%2

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

if %errorlevel% gtr 0 goto err
exit 0
:err
rem echo Empty log message not allowed. Commit aborted! 1>&2
exit 1
* implement python script mantis_integration.py.
"""
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'
logging.basicConfig(filename=LOG_FILENAME,level=logging.ERROR)

# database setting
host = '192.168.2.9'
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.
"""
try:
# 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
conn.set_character_set('GBK')
if is_issue_exist(conn, issue_number):
update_mantis(conn, issue_number, author, log, changed)
except Exception as e:
logging.error(e)
sys.exit(1)

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

" + changed
query = "insert into mantis_bugnote_text_table(note) values('" + note + "')";
conn.query(query)
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 + ")"
conn.query(query)

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
conn.query(query)
r = conn.store_result()
if r.num_rows() <= 0:
msg = "No issue(id=" + issue_number + ") exist in mantis"
sys.stderr.write(msg)
raise Exception(msg)
return True

def get_issue_number(log):
log_pattern = r'^[#]{1}(\d+)[\s]+(.+)'
result = re.search(log_pattern, log)
if result == None:
msg = 'the log message must follow: #issue_number log_message'
sys.stderr.write(msg)
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)
return output.read()

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.");
sys.exit(1);

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