Name
SVN::Notify - Subversion activity notification
Synopsis
Use svnnotify in post-commit:
svnnotify --repos-path "$1" --revision "$2" \
--to developers@example.com [options]
svnnotify --repos-path "$1" --revision "$2" \
--to-cx-regex i10n@example.com=I10N [options]
Use the class in a custom script:
use SVN::Notify;
my $notifier = SVN::Notify->new(%params);
$notifier->prepare;
$notifier->send;
Description
This program may be used for sending email messages for Subversion repository activity. There are a number of different modes supported. A list of all the files affected by the commit will be assembled and listed in the single message. An additional option allows for diffs to be calculated for the recent changes, and either appended to the message or added as an attachment. See the with_diff
and attach_diff
options below.
Prerequisites
- Getopt::Long
-
This script requires Getopt::Long, which is included with Perl.
Corequisites
- Pod::Usage
-
For calling svnnotify with the
--help
or--man
options, or when it fails to process the command-line options, usage output will be triggered by Pod::Usage, which has been included with Perl since 5.6.0. - HTML::Entities
-
For sending HTML formatted email, this script requires HTML::Entities, which is available from CPAN.
Usage
To use SVN::Notify, simply add a call to svnnotify to your Subversion repository's post-commit script. This script lives at the root of the repository directory; consult the documentation in post-commit.tmpl for details. Make sure that you specify the complete path to svnnotify, as well as to svnlook and sendmail in the options passed to svnnotify so that everything executes properly.
Class Interface
Constructor
new
my $notifier = SVN::Notify->new(%params);
Constructs and returns a new SVN::Notify object. This object is a handle on the whole process of collecting metadata and content for the commit email and then sending it. As such, it takes a number of parameters to affect that process.
Each of these parameters has a corresponding command-line option that can be passed to svnnotify. The options have the same names as these parameters, but any underscores you see here should be replaced with dashes when passed to svnnotify. Each also has a corresponding single-character option. All of these are documented here.
Supported parameters:
- repos_path
-
svnnotify --repos-path "$PATH" svnnotify -p "$PATH"
The path to the Subversion repository. The path is passed as the first argument when Subversion executes post-commit. So you can simply pass
$1
to this parameter if you like. See the documentation in post-commit for details. Required. - revision
-
svnnotify --revision "$REV" svnnotify -r "$REV"
The revision number for the current commit. The revision number is passed as the second argument when Subversion executes post-commit. So you can simply pass
$2
to this parameter if you like. See the documentation in post-commit for details. Required. - to
-
svnnotify --to commiters@example.com svnnotify -t commiters@example.com
The address or addresses to which to send the notification email. To specify multiple addresses, simply put them all into a comma-delimited list suitable for an SMTP "From" header. This parameter is required unless
to_regex_map
is specified. - to_regex_map
-
svnnotify --to-regex-map translate@example.com=L18N \ -x legal@example.com=License
This parameter specifies a hash reference of email addresses to regular expression strings. SVN::Notify will compile the regular expression strings into regular expression objects, and then send notification messages if and only if the name of one or more of the directories affected by a commit matches the regular expression. This is a good way to have a notification email sent to a particular mail address (or comma-delimited list of addresses) only for certain parts of the subversion tree. This parameter is required unless
to
is specified.The command-line options,
--to-regex_map
and-x
, can be specified any number of times, once for each entry in the hash to be passed tonew()
. The value passed to the option must be in the form of the key and the value separated by an equal sign. Consult the Getopt::Long documentation for more information. - from
-
svnnotify --from somewhere@example.com svnnotify -f elsewhere@example.com
The email address to use in the "From" line of the email. If not specified, SVN::Notify will use the username from the commit, as returned by
svnlook info
. - user_domain
-
svnnotify --user-domain example.com svnnotify -D example.net
A domain name to append to the username for the "From" header of the email. During a Subversion commit, the username returned by
svnlook info
is usually something like a Unix login name. SVN::Notify will use this username in the email "From" header unless thefrom
parameter is specified. If you wish to have the username take the form of a real email address, specify a domain name and SVN::Notify will append\@$domain_name
to the username in order to create a real email address. This can be useful if all of your committers have an email address that corresponds to their username at the domain specified by theuser_domain
parameter. - svnlook
-
svnnotify --svnlook /path/to/svnlook svnnotify --l /path/to/svnlook
The location of the svnlook executable. The default is /usr/local/bin/svnlook. Specify a different path or set the
$SVNLOOK
environment variable if this is not the location of svnlook on your box. It's important to provide a complete path to svnlook because the environment during the execution of post-commit is anemic, with nary a$PATH
environment variable to be found. So if svnnotify appears not to be working at all (and Subversion seems loathe to log when it dies!), make sure that you have specified the complete path to a working svnlook executable. - sendmail
-
svnnotify --sendmail /path/to/sendmail svnnotify --s /path/to/sendmail
The location of the sendmail executable. The default is /usr/sbin/sendmail. Specify a different path or set the
$SENDMAIL
environment variable if this is not the location of sendmail on your box. The same caveats as applied to the location of the svnlook executable apply here. - charset
-
svnnotify --charset UTF-8 svnnotify -c Big5
The character set typically used on the repository for log messages, file names, and file contents. Used to specify the character set in the email Content-Type headers. Defaults to UTF-8.
- with_diff
-
svnnotify --with-diff svnnotify -d
A boolean value specifying whether or not to include the output of
svnlook diff
in the notification email. The diff will be inline at the end of the email unless theattach_diff
parameter specifies a true value. - attach_diff
-
svnnotify --attach-diff svnnotify -a
A boolean value specifying whether or not to attach the output of
svnlook diff
to the notification email. Rather than being inline in the body of the email, this parameter causes SVN::Notify to attach the diff as a separate file, named for the user who triggered the commit and the date and time UTC at which the commit took place. Specifying this parameter to a true value implicitly sets thewith_diff
parameter to a true value. - reply_to
-
svnnotify --reply-to devlist@example.com svnnotify --R developers@example.net
The email address to use in the "Reply-To" header of the notification email. No "Reply-To" header will be added to the email if no value is specified for the
reply_to
parameter. - subject_prefix
-
svnnotify --subject-prefix [Devlist] svnnotify -P (Our-Developers)
An optional string to prepend to the beginning of the subject line of the notification email.
- subject_cx
-
svnnotify --subject-cx svnnotify -C
A boolean value indicating whether or not to include a the context of the commit in the subject line of the email. In a commit that affects multiple files, the context will be the name of the shortest directory affected by the commit. This should indicate up to how high up the Subversion repository tree the commit had an effect. If the commit affects a single file, then the context will simply be the name of that file.
- max_sub_length
-
svnnotify --max-sub-length 72 svnnotify -i 76
The maximum length of the notification email subject line. SVN::Notify includes the first line of the commit log message, or the first sentence of the message (defined as any text up to the string ". "), whichever is shorter. This could potentially be quite long. To prevent the subject from being over a certain number of characters, specify a maximum length here, and SVN::Notify will truncate the subject to the last word under that length.
- format
-
svnnotify --format text svnnotify -A html
The format of the notification email. Two formats are supported, "text" and "html". If "html" is specified, HTML::Entities must be installed to ensure that the log message, file names, and diff are properly escaped.
- viewcvs_url
-
svnnotify --viewcvs-url http://svn.example.com/viewcvs/ svnnotify -U http://viewcvs.example.net/
If a URL is specified for this parameter, then it will be used to create links to the files affected by the commit on the relevant Website. The URL is assumed to be the base URL for a ViewCVS script; the file name of the affected file will simply be appended to it. For modified files, the link will be to the ViewCVS diff output for the commit on that file, although in HTML mode both links will be specified.
- verbose
-
svnnotify --verbose -V
A value between 0 and 3 specifying how verbose SVN::Notify should be. The default is 0, meaning that SVN::Notify will be silent. A value of 1 causes SVN::Notify to output some information about what it's doing, while 2 and 3 each cause greater verbosity. To set the verbosity on the command line, simply pass the
--verbose
or-V
option once for each level of verbosity, up to three times.
Instance Interface
Instance Methods
prepare
$notifier->prepare;
Prepares the SVN::Notify object, collecting all the data it needs in preparation for sending the notification email. Really it's just a shortcut for:
$self->prepare_recipients;
$self->prepare_contents;
$self->prepare_files;
$self->prepare_subject;
Only it returns after the call to prepare_recipients()
if there are no recipients (that is, as when recipients are specified solely by the to_regex_map
parameter and none of the regular expressions match any of the affected directories).
prepare_recipients
$notifier->prepare_recipients;
Collects and prepares a list of the notification recipients. The recipients are a combination of the value passed to the to
parameter as well as any email addresses specified as keys in the hash reference passed to_regex_map
parameter, where the regular expressions stored in the values match one or more of the names of the directories affected by the commit.
If the subject_cx parameter to new()
has a true value, prepare_recipients()
also determines the directory name to use for the context.
prepare_contents
$notifier->prepare_contents;
Prepares the contents of the commit message, including the name of the user who triggered the commit (and therefore the contents of the "From" header to be used in the email) and the log message.
prepare_files
$notifier->prepare_files;
Prepares the lists of files affected by the commit, sorting them into their categories: modified files, added files, and deleted files. It also compiles a list of files wherein a property was set, which might have some overlap with the list of modified files (if a single commit both modified a file and set a property on it).
If the subject_cx
parameter was specified and a single file was affected by the commit, then prepare_files()
will also specify that file name as the context to be used in the subject line of the commit email.
prepare_subject
$notifier->prepare_subject;
Prepares the subject line for the notification email. This method must be called after prepare_recipients()
and prepare_files()
, since each of those methods potentially sets up the context for use in the the subject line. The subject may have a prefix defined by the subject_prefix
parameter to new()
, it has the revision number, it might have the context if the subject_cx
specified a true value, and it will have the first sentence or line of the commit, whichever is shorter. The subject may then be truncated to the maximum length specified by the max_sub_length
parameter.
send
$notifier->send;
Sends the notification message. This involves opening a connection to sendmail, sending the appropriate headers, calling the appropriate formatting method (See "output_as_text" and "output_as_html" below), and then closing the connection so that sendmail can send the email.
output_as_text
$notifier->output_as_text($file_handle);
Called internally by send
to output a text version of the commit email. The file handle passed is a piped connection to sendmail, so that output_as_text()
can print directly to sendmail. output_as_text()
also appends or attaches a diff if the with_diff
or attach_diff
parameter to new()
specified a true value.
output_as_html
$notifier->output_as_html($file_handle);
Called internally by send
to output an HTML version of the commit email. The file handle passed is a piped connection to sendmail, so that output_as_html()
can print directly to sendmail. output_as_html()
also appends or attaches a diff if the with_diff
or attach_diff
parameter to new()
specified a true value.
output_diff
$notifier->output_diff($file_handle);
Called internally by output_as_text()
and output_as_html()
to send the output of svnlook diff
to the specified file handle. That file handle is an open pipe to sendmail.
output_diff()
is also aware of whether the format of the notification email is text or HTML, so that it can properly escape the diff for HTML email when the attach_diff
parameter to new()
has a false value.
Bugs
Report all bugs via the CPAN Request Tracker at http://rt.cpan.org/NoAuth/Bugs.html?Dist=SVN-Notify.
To Do
Port to Win32. I think it just needs to use Win32::Process to manage communication with svnlook and sendmail, and maybe some file name conversions with the
viewcvs_url
parameter. See comments in the source code.
Author
David Wheeler <david@kineticode.com>
Copyright and License
Copyright (c) 2004 Kineticode, Inc. All Rights Reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.