SVN与ReviewBoard联动(3)

前面做测试,生成了很多错误的review request。
但没有找到可以批量删除的工具,于是自己写了一个。

deleterequest.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Created on 2016-11-24
@author: Hansen
获取指定svn文件夹下,指定svn版本的注释
'''

import os
import sys

#获取指定svn版本的注释
def del_rb_request(rid1,rid2):
    for rid in range(rid1,rid2):
        cmd='curl -X DELETE --header "Authorization: token API_TOKEN" http://127.0.0.1/api/review-requests/'+str(rid)+'/'
        print(cmd)
        logs = os.popen(cmd.encode()).readlines()
        for log in logs:
            print(log)

#start here
del_rb_request(0,86)

SVN与ReviewBoard联动(2)

接上一篇。

1、脚本写了没多久,但郁闷的是测通上面的脚本我花了很久,后来发现主要是字符集的问题。

我用的是Debian默认字符集为UTF8,python2.7默认字符集为ASCII。
后面,设置了python的字符集默认为UTF8,第一篇的脚本才测通了。
/usr/lib/python2.7/sitecustomize.py

# install the apport exception handler if available
import sys

sys.setdefaultencoding('UTF8')

try:
    import apport_python_hook
except ImportError:
    pass
else:
    apport_python_hook.install()

2、然后发现RBT貌似不支持UTF8的中文参数,反正我没有测通,好吧,自己写了一个脚本将SVN上的中文注释更新到mysql中。
说明一下,RBT的这个问题,我已经向社区提交Bug了,亲测补丁有效,大家等待下一版本就好了。

#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Created on 2016-11-24
@author: Hansen
用于更新rb的说明
'''

import sys
import datetime
import MySQLdb
import nsvnutils

#记录日志
def nlog(logpath,msg):
    fo = open(logpath,'a+')
    try:
        fo.write(msg+'\n')
    finally:
        fo.close()

#更新rb的reviewrequest说明
def update_rb_comment():
    #获取连接
    try:
        connection=MySQLdb.connect(user='user',passwd="passwd",host='127.0.0.1',port=3306,db='db',charset='utf8')
        subconnection=MySQLdb.connect(user='user',passwd="passwd",host='127.0.0.1',port=3306,db='db',charset='utf8')
        #print('mysql connection ok')
        nlog('/mnt/rb/sql.log','mysql connection ok')
    except Exception as ex:
        #print(str(ex))
        nlog('/mnt/rb/sql.log',str(ex))
        return

    #获取需要处理的数据
    try:
        cursor=connection.cursor()
	subcursor=subconnection.cursor()
        sql="select id,summary from reviews_reviewrequest r where r.description='' or r.description is null"
        cursor.execute(sql)
        #print('mysql select ok')
        nlog('/mnt/rb/sql.log','mysql select ok')
        for row in cursor.fetchall():
            rid=str(row[0])
            ss=row[1].split('_Rev')
	    if len(ss)==2:
                nsvnutils.get_repo_name(ss[0])
                comment=nsvnutils.get_svn_log(ss[1])
		commiter=nsvnutils.get_svn_commiter(ss[1])
                subsql="update reviews_reviewrequest r set r.description='"+commiter+"_"+comment+"' where r.id="+rid
	        #print(subsql)
                nlog('/mnt/rb/sql.log',subsql)
		subcursor.execute(subsql)
            else:
                #print('skiping id='+rid+' summary='+row[1])
                nlog('/mnt/rb/sql.log','skiping id='+rid+' summary='+row[1])
        subconnection.commit()
    except Exception as ex:
        #print(str(ex))
        nlog('/mnt/rb/sql.log',str(ex))
	return
    finally:
        cursor.close()
	subcursor.close()

    connection.close()
    subconnection.close()

#start here
nlog("/mnt/rb/sql.log", 'Starting job on '+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
update_rb_comment()
nlog("/mnt/rb/sql.log", 'Ending job on '+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

3、建立定时任务,执行脚本

crontab -l

#10分钟运行一次
*/10 * * * * /home/neohope/scripts/updatedb.py

SVN与ReviewBoard联动(1)

公司的SVN服务器是在Windows上的,ReviewBoard是在Linux虚拟服务器上的。
好吧,主要是历史原因了哦。。。

解决思路是,利用SVN的post-commit钩子,在提交后,将版本号存到一个文件中。
在Linux服务器中,定时去扫描这些文件,有变动的话,则利用RBTools提交请求。

1、Windows中用post-commit钩子获取版本号
post-commit.bat

@echo off
SET REPOS=%1%
SET REV=%2%
SET RPATH=PATH_TO_STORE_FILE

CALL :getfilename %REPOS%

echo %REV% >> %RPATH%\%FNAME%.txt

:success
exit 0

:getfilename
set FNAME=%~nx1

2、Linux中用python脚本扫描文件变动
runme.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Created on 2016-11-24
@author: Hansen
遍历监视文件夹下所有的txt文件
当文件多余两行时,对于每一行,调用rbt生成review请求
最后,只保留最后一行
'''

import os
import sys
import datetime
import nsvnutils

#记录日志
def nlog(logpath,msg):
    fo = open(logpath,'a+')
    try:
        fo.write(msg)
    finally:
        fo.close()

#创建rbt命令,并调用命令
def create_rbt_command(repo, revision1, revision2, logpath):
    title = repo+"_Rev"+revision1
    comment = nsvnutils.get_svn_log(revision1)
    commiter = nsvnutils.get_svn_commiter(revision1)
    #rbt好像不支持utf8参数,因此用更新db的方式添加注释
    cmd = 'rbt post -p --repository ' +repo+" --repository-type svn --server http://127.0.0.1 --api-token API_TOKEN --summary "+title+' '+revision1 
    fo = open(logpath,'a+')
    try:
        fo.write('adding rbt job on '+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+'\n')
	fo.write('PWD is '+os.getcwd()+'\n')
	fo.write('CMD is '+cmd+'\n')
	logs = os.popen(cmd.encode('UTF8')).readlines()
        for log in logs:
	   fo.write(log+'\n')
	fo.write('\n')
    finally:
        fo.close()

#写回最后一行
def write_last_line(txtpath,line):
    fo = open(txtpath,'w+')
    try:
	fo.write(line)
    finally:
        fo.close()

#遍历行
#多于两行则自动生成post review
def enum_line(txtpath,logpath,repo):
    nlog('/mnt/rb/rb.log','Trying repositor '+repo+'\n')
    fi = open(txtpath)
    try:
	lines=fi.readlines()
	linesize=len(lines)
	if linesize <= 1:
            nlog('/mnt/rb/rb.log','Skiping repositor '+repo+'\n')
            return

        for i in range(0, linesize):
            if i+1<linesize:
		rbRepo = nsvnutils.get_repo_name(repo)
                if len(rbRepo)==0 :
                    nlog('/mnt/rb/rb.log','Skiping repositor '+repo+'\n')
                    return

                nlog('/mnt/rb/rb.log','Parsing repository '+rbRepo+'\n')
		create_rbt_command(rbRepo,lines[i].replace('\n','').replace('\r',''),lines[i+1].replace('\n','').replace('\r',''),logpath)
    except Exception as ex:
        nlog('/mnt/rb/rb.log',str(ex))
    finally:
        fi.close()

    write_last_line(txtpath,lines[linesize-1])

#递归遍历文件
def enum_file(targetdir,sufix,logpath):
    for root, dirs, files in os.walk(targetdir, True):
        for fname in files:
            fsufix = os.path.splitext(fname)[1][1:]
	    repo = os.path.splitext(fname)[0]
            if sufix == fsufix:
                txtpath = root+"/"+fname
                #txtpath = root+"\\"+fname
		enum_line(txtpath,logpath,repo)


#start here
nlog("/mnt/rb/rb.log", 'Starting job on '+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+'\n')
enum_file("/mnt/rb","txt", "/mnt/rb/rb.log")
#enum_file("D:/MyProducts/Python/ReviewBoard/rb","txt", "D:/MyProducts/Python/ReviewBoard/rb/rb.log")
nlog("/mnt/rb/rb.log", 'Ending job on '+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+'\n')

nsvnutils.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Created on 2016-11-24
@author: Hansen
获取指定svn文件夹下,指定svn版本的注释
'''

import os
import sys

#切换路径,并获取repository名称
def get_repo_name(repo):
    mydict = {'REPO1':'REPO1','REPO2':'REPO2'}
    if not mydict.get(repo):
        return '' 
    os.chdir('/home/neohope/repository/'+mydict.get(repo))
    #os.chdir('D:/MyProducts/Python/ReviewBoard/repo/'+mydict(repo))
    return mydict[repo]

#获取指定svn版本的注释
def get_svn_log(revision):
    cmd = 'svn log -r '+revision
    logs = os.popen(cmd).readlines()
    #for log in logs:
    #    print(log)
    if len(logs)>3:
        print(logs[3].replace('\r','').replace('\n',''))
        return logs[3].replace('\r','').replace('\n','')
    else:
        print('no svn comment') 
        return 'no svn comment'

#获取指定svn版本的提交者
def get_svn_commiter(revision):
    cmd = 'svn log -r '+revision
    logs = os.popen(cmd).readlines()
    if len(logs)>2:
        ss=logs[1].split('|')
        if len(ss)>2:
            print(ss[1].strip().replace('\r','').replace('\n',''))
            return ss[1].strip().replace('\r','').replace('\n','')
        else:
            #print('no svn commiter') 
            return 'no svn commiter'
    else:
        #print('no svn commiter') 
        return 'no svn commiter'

#start here
#get_svn_log('193')
#get_svn_commiter('193')

3、建立定时任务,执行脚本

crontab -l

#半小时运行一次
*/30 * * * * /home/neohope/scripts/runme.py

比较麻烦的是,仍需要在Linux下面,先把SVN的repository检出才可以。
同时要注意,要做好SVN及ReviewBoard之间名称的映射。。。

SVN禁止提交时不写注释

最常用的方法,就是使用SVN提供的pre-commit钩子。
具体做法是,在对应的repository下的hooks文件夹下中,新建对于的pre-commit脚本,来禁止提交。

1.windows下pre-commit.bat

@echo off
setlocal

set REPOS=%1
set TXN=%2

rem 保证输入8个字符
svnlook log %REPOS% -t %TXN% | findstr "........" > nul
if %errorlevel% gtr 0 goto :err_action

rem 过滤空格字符
svnlook log %REPOS% -t %TXN% | findstr /ic:"        " > nul
if %errorlevel% gtr 0 goto :success

:err_action
echo 备注信息验证失败。                >&2
echo 提交代码时,必须填写备注信息。    >&2
echo 备注信息不少于8个字符(或4个汉字)。>&2
goto :err_exit


:err_exit
exit 1

:success
exit 0

2.linux下pre-commit.sh

#!/bin/sh

REPOS="$1"
TXN="$2"

SVNLOOK=/usr/local/bin/svnlook
LOGMSG=`$SVNLOOK log -t "$TXN" "$REPOS" | grep "[a-zA-Z0-9]" | wc -c`

#要求注释不能少于8个字符,您可自定义
if ["$LOGMSG" -lt 8];
then
  echo -e "\n备注信息验证失败。\n提交代码时,必须填写备注信息。\n备注信息不少于8个字符(或4个汉字)。" 1>&2
  exit 1
fi 

exit 0