利用svn钩子hooks/post-commit实现邮件通知功能

svn可以实现在commit提交时,使用hook钩子功能,向指定的邮箱发送提交注释comment/log及内容变化的邮件,以方便对之进行监控。



1、  需要一个能用stmp发邮件的邮箱。


2、  在svn服务器上安装python https://www.python.org/downloads/


3、  接下来在svn服务器,进入svn库的hooks文件夹F:\svn_server\MyProject\MyProject\hooks,创建文件post-commit.bat,内容:

remREPOS-PATH (the path to this repository)

setREPOS=%1

remREV (the number of the revision just committed)

setREV=%2

 

setHOOK_DIR=F:/svn_server/MyProject/MyProject/hooks

setPYTHON_BIN=C:/Python27/python.exe

%PYTHON_BIN% %HOOK_DIR%/sendemail.py %REPOS% %REV%

4、  然后再在hooks目录下,创建文件sendemail.py,内容:


# -*- coding:utf-8 -*-

import sys
import os
import smtplib
from email.mime.text import MIMEText
from email.header import Header

mail_host = 'smtp.exmail.qq.com'  # 发送邮件的smtp地址

mail_user = 'svn@123456.com'  # 发送通知邮件的用户名

mail_pass = '123456'  # 用户的密码

me = 'SVN Service' + '<' + 'svn' + '@' + '123456.com' + '>'  # 发送邮件人的地址标识

to_list = ['123456789@qq.com', '123456@qq.com']  # 收件人

html_template = """

<divstyle="color:#FFFFFF; background: #008040;text-align: center;font:bold18px/28px '微软雅黑';margin:10px 0px;">基本信息</div>

<divstyle="font:normal 14px/28px '微软雅黑';margin:0px;text-indent:10px;"><strong>版本库:</strong><ahref="svn:%s" target="_parent">%s</a></div>

<divstyle="font:normal 14px/28px '微软雅黑';margin:0px;text-indent:10px;"><strong>版本号:</strong>%s</div>

<divstyle="font:normal 14px/28px '微软雅黑';margin:0px; text-indent:10px;"><strong>提交者:</strong>%s</div>

<divstyle="font:normal 14px/28px '微软雅黑';margin:0px;text-indent:10px;"><strong>提交时间:</strong>%s</div>

<divstyle="font:normal 14px/28px '微软雅黑';margin:0px;text-indent:10px;"><strong>提交说明:</strong>%s</div>

<divstyle="color:#FFFFFF; background: #008040;text-align: center;font:bold18px/28px '微软雅黑';margin:10px 0px;">文件清单</div>

<xmpstyle="font:normal 12px/20px '微软雅黑';margin:0px;text-indent:10px;">%s</xmp>

<divstyle="text-align: center; font: bold 15px/50px '微软雅黑';">☆Powered by <ahref="http://www.ladyson.net">Ladyson</a></div>

"""


def get_repo_name(repo):
    return os.path.basename(repo)


def get_author(repo, rev):
    """
    svnlook author -r REVREPOS 获得提交者
    """
    cmd = '%s author -r %s %s' % (svnlook_bin_path, rev, repo)
    output = os.popen(cmd).read()
    return output


def get_date(repo, rev):
    """
    svnlook date -r REVREPOS 获得提交时间
    """
    cmd = '%s date -r %s %s' % (svnlook_bin_path, rev, repo)
    output = os.popen(cmd).read()
    return output


def get_log(repo, rev):

    """
    svnlook log -r REVREPOS 获得提交日志
    """
    cmd = '%s log -r %s %s' % (svnlook_bin_path, rev, repo)
    output = os.popen(cmd).read()
    return output


def get_file_list(repo, rev):

    """
    svnlook changed -rREV REPOS 获得发生变更的文件
    """
    cmd = '%s changed -r %s %s' % (svnlook_bin_path, rev, repo)
    output = os.popen(cmd).read()
    return output


def send_mail(msg, sender, to_list):
    try:
        s = smtplib.SMTP()
        s.connect(mail_host)
        s.login(mail_user, mail_pass)
        s.sendmail(sender, to_list, msg.as_string())
        s.close()
        return True
    except Exception as e:
        print(str(e))
        return False


def write_mail(sender, to_list, sub, content):
    msg = MIMEText(content, _subtype='html', _charset='utf-8')
    msg['Subject'] = sub
    msg['From'] = sender
    msg['To'] = ';'.join(to_list)
    msg["Accept-Language"] = "zh-CN"
    msg["Accept-Charset"] = "ISO-8859-1,utf-8"
    return msg


def write_mail_content(repo, rev):

    """
    repo: repository
    rev: revision
    """
    repo_name = get_repo_name(repo)
    author = get_author(repo, rev)
    date = get_date(repo, rev)
    log = get_log(repo, rev)
    file_list = get_file_list(repo, rev)
    content = html_template % (
    repo, repo_name, rev, author, date, log.decode("GBK").encode('UTF-8'), file_list.decode("GBK").encode('UTF-8'))
    return content


if '__main__' == '__main__':
    svnlook_bin_path = '"F:/svn_server/Subversion/bin/svnlook.exe"'
    subject = 'SVN post-commit'
    content = write_mail_content(sys.argv[1], sys.argv[2])
    msg = write_mail(me, to_list, subject, content)
    send_mail(msg, me, to_list)
5、  进行你可以进行测试了
你可以直接双击post-commit.bat文件,然后看是否可以接受到邮件。如果没有接受到,建议你进行调试。


6、  何时会自动发送邮件通知?

当版本库的内容发生改变时,SVN就会自动运行post-commit.bat这个文件来实现邮件的自动通知。


阅读更多
个人分类: SVN
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭