Sending multiple email notifications after a SVN commit
This feature is provided by SVN by default. Go to the /repo/hooks directory
and edit the post-commit with the following Ruby code, then whenever someone
checks in to the SVN, an automatic email will be sent out.
Note: this script requires Ruby and sendmail (and of course, Subversion) to work correctly. The following script is adapted from two posts A better Subversion post-commit hook than commit-email.pl and Pretty SVN commit emails. The difference is that this script sends out emails to a group of members rather than one.
#!/usr/bin/ruby -w
# Send svn checkin email, based on a script by Elliott Hughes,
# Adam Doppelt, arron, and modified by Vincent Wang.
# To install, copy this file into your repository's hooks/
# directory as "post-commit". Don't forget to chmod a+x post-commit
require 'cgi'
# addresses with mime information used by sendmail
address = ""User A" <usera@example.com>, "
address += ""User B" <userb@example.com>, "
address += ""User C" <userc@example.com>, "
# actual addresses again
saddress = ["usera@example.com", "userb@example.com", "userc@example.com"]
sendmail = "/usr/sbin/sendmail"
svnlook = "/usr/bin/svnlook"
# Subversion's commit-email.pl suggests that svnlook might create files.
Dir.chdir("/tmp")
# What revision in what repository?
repo = ARGV.shift()
rev = ARGV.shift()
# Get the overview information.
info=`#{svnlook} info #{repo} -r #{rev}`
info_lines=info.split("n")
author=info_lines.shift
date=info_lines.shift
info_lines.shift
comment=info_lines
# Output the overview.
body = ""
body << "<p><b>#{author}</b> #{date}</p>"
body << "<p>"
comment.each() {
|line|
body << CGI.escapeHTML(line)
body << "<br>n"
}
body << "</p>"
body << "<hr noshade>"
# Get and output the patch.
changes=`#{svnlook} diff #{repo} -r #{rev}`
body << "<pre>"
changes.each do |top_line|
top_line.split("n").each do |line|
color = case
when line =~ /^Modified: / || line =~ /^=+$/ || line =~ /^@@ /: "gray"
when line =~ /^-/: "red"
when line =~ /^+/: "blue"
else "black"
end
body << %Q{<font style="color:#{color}">#{CGI.escapeHTML(line)}</font><br/>n}
end
end
body << "</pre>"
# Write the mail headers
header = ""
header << "To: #{address}n"
header << "From: #{author}n"
header << "Subject: [project-title] [svn] [revision:#{rev}]n"
header << "Reply-to: #{address}n"
header << "MIME-Version: 1.0n"
header << "Content-Type: text/html; charset=UTF-8n"
header << "Content-Transfer-Encoding: 8bitn"
header << "n"
# Send the mails
begin
saddress.each do |email|
fd = open("|#{sendmail} #{email}", "w")
fd.print(header)
fd.print(body)
fd.close
end
rescue
exit 1
end