SVN hook + Python automatically sends emails after commit is implemented

Source: Internet
Author: User

The SVN hook mechanism allows you to customize some operations based on SVN actions. Post-commithook is a user-defined operation performed after the user executes commit. For example, after the user commit, the user automatically calls the post-commit hook script group to send an email to the project team. The implementation process is to automatically call post-commithook after commit to send emails.

I wrote a simple sendmail. py function to help send emails.

Now let's take a look at the main implementation: Post-commit, sendmail. py

The content of post-commit is as follows:

# REPOS-PATH (the path to this repository) repos = "$1" # Rev (the number of the revision just committed) REV = "$2" lang = zh_cn.gb18030 # This sentence does not work. Export langhook_dir =/root/gaoyj/test_repo/logs =/usr/local/subversion/bin/svnlookpython_bin =/usr /bin/Python $ {python_bin }$ {hook_dir}/sendmail. PY $ {repos }$ {rev}

Note the following:

(1) SVN calls post-commit, passed two parameters: REPOS-PATH (repository path) and Rev version

(1) If Chinese characters cannot be displayed in the email, set the environment variable Lang and export in post-commit.

(2) The value of the PATH variable cannot be obtained during post-commit execution. Therefore, the absolute path should be used in the script.

(3) change the post-commit.tmpl under the hook directory of repository to post-commit (change to post-commit.bat under Windows) and grant execution permissions.

The principle of sendmail. py is to call svnlook to obtain the submission information (version number, submission time, Submitter, commitlog, and files with changes). The content is as follows:

# Coding: gb2312import sysimport osimport smtplibfrom email. mime. text import mimetextmail_host = 'smtp .example.com '# SMTP address for sending emails mail_user = 'example' # username for sending notification emails mail_pass = 'example _ passwd '# user password me = 'example '+' <'+ 'example' + '@' + 'example. com '+'> '# the sender's address ID to_list = ['person _ a@example.com', 'person _ B @example.com ', 'person _ c@example.com '] # recipient html_template = "<HTML> <H2 style =" color: # ffffff; Background: #008040; "> Basic Information </H2> <div> <B> Version Library: </B> <a href =" SVN: % s "> % S </a> </div> <B> Version: </B> % S </div> <B> submitted: </B> % S </div> <B> submission time: </B> % S </div> <H2 style = "color: # ffffff; background: #4682b4; "> submission instructions </H2> <font size =" 4 "color =" # bf6000 "> <XMP> % S </XMP> </font> <H2 style =" color: # ffffff; Background: #5366a8; "> file list </H2> <XMP> % S </XMP> <HR> <center> ☆powered by <a href =" http://garyelephant.me "> Gary </a> </center> <center> ☆too red by <a href = "http://crearo-sw.blogspot.com"> CREARO-SW </a> </center> 

Note the following:

(1) the password of the user sending the notification email is saved in plain text, and there are security risks.

(2) To call svnlook using OS. popen, use the absolute path of svnlook.

 

Source code hosted on GitHub: https://github.com/garyelephant/SVN-Hooks

References:

[1] SVN server and client hooks script use http://scmbob.org/subversion-hooks-windows.html
[2] o'reilly media published by subversion with version control, 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

Repost this article please indicate the author and the source [Gary's influence] http://garyelephant.me, do not for any commercial purposes!

Author: Gary Gao (garygaowork [at] gmail.com) focuses on the internet, distributed, high-performance, nosql, automation, and software teams


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.