任何人都有提交通知挂钩脚本,将在提交代码时发送电子邮件?

时间:2020-03-06 14:51:56  来源:igfitidea点击:

可以与我分享任何此脚本吗?

解决方案

默认的一个名为commit-email.pl,在安装Subversion时会包含它。但是这是红宝石中的一种:

#!/usr/bin/ruby -w

# A Subversion post-commit hook. Edit the configurable stuff below, and
# copy into your repository's hooks/ directory as "post-commit". Don't
# forget to "chmod a+x post-commit".

# ------------------------------------------------------------------------

# You *will* need to change these.

address="FOO@SOME_DOMAIN.com"
sendmail="/usr/sbin/sendmail"
svnlook="/usr/bin/svnlook"

# ------------------------------------------------------------------------

require 'cgi'

# 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 = "<p><b>#{author}</b> #{date}</p>"
body << "<p>"
comment.each { |line|  body << "#{CGI.escapeHTML(line)}<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 header.
header = ""
header << "To: #{address}\n"
header << "From: #{address}\n"
header << "Subject: [SVN] #{repo} revision #{rev}\n"
header << "Reply-to: #{address}\n"
header << "MIME-Version: 1.0\n"
header << "Content-Type: text/html; charset=UTF-8\n"
header << "Content-Transfer-Encoding: 8bit\n"
header << "\n"

# Send the mail.
begin
    fd = open("|#{sendmail} #{address}", "w")
    fd.print(header)
    fd.print(body)
rescue
    exit(1)
end
fd.close

# We're done.
exit(0)

在svn信息库的hooks目录中,我们将找到一个post-commit.tmpl脚本。复制它以命名为" post-commit",然后对其进行编辑以适合需要。通常,它将运行Subversion附带的commit-email.pl脚本。这还需要进行编辑以设置所需的内容。

由于某些原因,ruby脚本和默认的钩子脚本对我不起作用。这可能是由于我们的邮件服务器有些奇怪,但是无论如何,我还是将其中的重要部分包括在内:

#!/bin/sh

REPOS=""
REV=""

svnnotify --repos-path "$REPOS" --revision "$REV" --with-diff --to [email protected] --smtp mailserver.domain --from [email protected] -VVVVVVVVV -P "[repository_name]"

如果要在脚本外部测试命令,-VVVVVVVV部分将显示非常详细的消息。应该在实际脚本中将其删除。

当然,要使其正常工作,我们需要安装svnnotify。我们可以先安装cpan,然后再安装perl来安装它。然后,我们需要启动cpan并安装SVN :: Notify库。

$ cpan
cpan> install SVN::Notify

请注意," $"和" cpan>"部分仅是提示,我们无需键入它们。

该解决方案对我来说更具吸引力,因为它提供了详细的错误消息,有助于解决我提到的邮件服务器的那些问题。我们还有多个存储库,因此将整个程序/脚本复制到每个目录中将是多余的。你的旅费可能会改变。

顶部代码块中的文本应放置在名为" post-commit"的文本文件中。该文件应位于/ path / to / svn / repos / repository_name / hooks,并标记为可执行文件。