Created
April 17, 2011 16:14
-
-
Save yukioc/924175 to your computer and use it in GitHub Desktop.
svn commit notification mail sender
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Encode; | |
use Net::SMTP; | |
my $svn_rev=shift or die; | |
my $svn_dir=shift or die; | |
my $smtp_url='xxx.yyy.zzz'; | |
my $smtp_user='aaaa'; | |
my $smtp_pass='bbbb'; | |
my $sender='your@sender.address'; | |
my $to='ccc@ddd.eee'; | |
my $subject_prefix='[yourprj]'; | |
my $rev_url="http://path/to/trac/yourprj/changeset/%s"; | |
my $bts_url="http://path/to/trac/yourprj/ticket/%s"; | |
my $mode='commit'; | |
sub svnlook{ | |
my $cmd=shift; | |
my $s=decode('utf8',`/usr/bin/svnlook $cmd -r $svn_rev $svn_dir`); | |
chomp($s); | |
return $s; | |
} | |
sub related_tids{ | |
my $log=shift; | |
my @t; | |
while($log=~/\(refs ([\d\s,#]*)\)/g){ | |
push(@t,map{my $a=$_;$a=~s/#//;$a}split(',',$1)) | |
} | |
return @t; | |
} | |
sub send_mail{ | |
my ($from, $to, $subject, $body) = @_; | |
$subject=Encode::encode('MIME-Header-ISO_2022_JP',$subject); | |
$body= Encode::encode('iso-2022-jp',$body); | |
print <<"END"; | |
From: $from | |
To: $to | |
Subject: $subject | |
$body | |
END | |
if ($to && length($to)){ | |
my $smtp=Net::SMTP->new($smtp_url,Timeout=>60); | |
$smtp->mail($from); | |
$smtp->to($to); | |
$smtp->auth($smtp_user,$smtp_pass) if($smtp_pass); | |
$smtp->data(); | |
$smtp->datasend("From: $from\n"); | |
$smtp->datasend("To: $to\n"); | |
$smtp->datasend("Content-Type: text/plain; charset=\"iso-2022-jp\"\n"); | |
$smtp->datasend("Content-Transfer-Encoding: 7bit\n"); | |
$smtp->datasend("Subject: $subject\n\n"); | |
$smtp->datasend($body); | |
$smtp->dataend(); | |
$smtp->quit; | |
} | |
} | |
my ($log)=map{s/\n+$//;$_}(svnlook('log')); | |
my $author=svnlook('author'); | |
my $from=sprintf('"%s" <%s>',svnlook('author'),$sender); | |
my $subject=$subject_prefix."r$svn_rev - ".(split(/\n/,$log))[0]; | |
my $date=svnlook('date'); | |
my $rev="r$svn_rev"; | |
$rev.=" ".sprintf($rev_url,$svn_rev) if ($rev_url); | |
my @tids=related_tids($log); | |
my $tickets='none'; | |
if ($#tids!= -1){ | |
$tickets=join("\n"." "x9, | |
map {my $a="#$_";$a.=" ".sprintf($bts_url,$_) if($bts_url); $a} @tids); | |
} | |
my ($changed)=map{s/\n/\n /g;$_} (svnlook('changed')); | |
my $body=<<"ENDBODY"; | |
$log | |
Author: $author | |
Date: $date | |
Rev: $rev | |
Tickets: $tickets | |
Changed: | |
$changed | |
ENDBODY | |
send_mail($from, $to, $subject, $body); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment