SVN Hook + Python实现commit后自动发送邮件

版权声明:本文为博主(微博@Gary的影响力)原创文章,未经博主允许不得转载。博客地址:http://garyelephant.me https://blog.csdn.net/gaoyingju/article/details/8884276

       svn 的hook机制方便了用户根据svn动作自定义一些操作,post-commithook是当用户执行commit后执行的用户自定义操作。如用户commit后,自动调用post-commit hook script群发邮件到项目组。实现过程是Commit后post-commithook 自动调用,发送邮件。

         我自己写了一个功能简单的sendmail.py来辅助发送邮件。

         现在关注一下主要的实现:post-commit, sendmail.py

        

post-commit的内容如下:

# REPOS-PATH (the path to this repository)
REPOS="$1"
# REV (the number of the revision just committed)
REV="$2"

LANG=zh_CN.GB18030
#没有这句不起作用
export LANG

HOOK_DIR=/root/gaoyj/test_repo/hooks
SVNLOOK_BIN=/usr/local/subversion/bin/svnlook
PYTHON_BIN=/usr/bin/python
${PYTHON_BIN} ${HOOK_DIR}/sendmail.py ${REPOS} ${REV}

有以下注意事项:

(1)svn调用post-commit时,传递了两个参数: REPOS-PATH(Repository路径)和REV版本号

(1)      如果收到邮件中,无法显示中文,应在post-commit中设置环境变量LANG并且export

(2)      post-commit执行时,无法获得PATH变量的值,所以script中应使用绝对路径

(3)      将Repository的hook目录下post-commit.tmpl改为post-commit(windows下改为post-commit.bat),并赋予执行权限。


sendmail.py的原理是通过调用svnlook获得提交信息(版本号、提交时间、提交者、commitlog,发生变更的文件),内容如下:

#coding: gb2312
import sys
import os
import smtplib
from email.mime.text import MIMEText

mail_host = 'smtp.example.com' #发送邮件的smtp地址
mail_user = 'example' # 发送通知邮件的用户名
mail_pass = 'example_passwd' # 用户的密码
me = 'Example' + '<' + 'example' + '@' + 'example.com' + '>' #发送邮件人的地址标识
to_list = ['person_a@example.com', 'person_b@example.com', 'person_c@example.com'] # 收件人

html_template = """
<html>
        <h2 style="color:#FFFFFF; background: #008040;">基本信息</h2>
        <div> <b>版本库:</b>
                <a href="svn:%s">%s</a>
        </div>
        <div> <b>版本号:</b>%s
        </div>
        <div>
                <b>提交者:</b>%s
        </div>
        <div>
                <b>提交时间:</b>%s
        </div>
        <h2 style="color:#FFFFFF; background: #4682B4;">提交说明</h2> <font size="4" color="#BF6000"><xmp>%s</xmp></font>
        <h2 style="color:#FFFFFF; background: #5353A8;">文件清单</h2>
        <xmp>%s</xmp>
        <hr>
        <center>
                ☆ Powered by
                <a href="http://garyelephant.me">Gary</a>
        </center>
        <center>
                ☆ Inspired by
                <a href="http://crearo-sw.blogspot.com">CREARO-SW</a>
        </center>
</html>
"""

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, file_list
        return content

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

def get_author(repo, rev):
        """svnlook author -r REV REPOS 获得提交者
        """
        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 REV REPOS 获得提交时间
        """
        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 REV REPOS 获得提交日志
        """
        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 -r REV 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, e:
                print str(e)
                return False

def write_mail(sender, to_list, sub, content):
        msg = MIMEText(content, _subtype = 'html', _charset='gb2312')
        msg['Subject'] = sub
        msg['From'] = sender
        msg['To'] = ';'.join(to_list)
        return msg

global svnlook_bin_path
if __name__ == '__main__':
        svnlook_bin_path = '/usr/local/subversion/bin/svnlook'

        subject = 'SVN Commit Notification'
        content = write_mail_content(sys.argv[1], sys.argv[2])
        msg = write_mail(me, to_list, subject, content)
        send_mail(msg, me, to_list)

有以下注意事项:

(1)发送通知邮件用户的密码是明文保存的,存在安全隐患

(2)用os.popen调用svnlook需要使用svnlook的绝对路径

 

源码托管在Github上:https://github.com/garyelephant/SVN-Hooks


References:

[1] SVN服务器与客户端Hooks脚本的使用http://scmbob.org/subversion-hooks-windows.html
[2] O'Reilly Media 出版的Subversion与版本控制,http://svnbook.red-bean.com/

[3] Subversion Hooks, http://www.worldhello.net/doc/svn_hooks/svn_hooks.mm.htm

[4] A SubversionPre-Commit Hook

  http://wordaligned.org/articles/a-subversion-pre-commit-hook

[5] Svn hookencoding problem

  http://stackoverflow.com/questions/2213670/svn-hook-encoding-problem


转载本文请注明作者和出处[Gary的影响力]http://garyelephant.me,请勿用于任何商业用途!

Author: Gary Gao( garygaowork[at]gmail.com) 关注互联网、分布式、高性能、NoSQL、自动化、软件团队

支持我的工作:  https://me.alipay.com/garygao




阅读更多
想对作者说点什么?

博主推荐

换一批

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