Use Template::Toolkit to generate email messages

Also, make the messages much shorter by not including build details.
This commit is contained in:
Eelco Dolstra 2013-05-09 14:23:15 +02:00
parent b378d94087
commit de89c566f8

View file

@ -9,17 +9,17 @@ use Email::Sender::Transport::SMTP;
use Email::Simple; use Email::Simple;
use Email::Simple::Creator; use Email::Simple::Creator;
use Sys::Hostname::Long; use Sys::Hostname::Long;
use Text::Table;
use File::Slurp; use File::Slurp;
use Template;
use Hydra::Helper::Nix; use Hydra::Helper::Nix;
use Hydra::Helper::CatalystUtils; use Hydra::Helper::CatalystUtils;
sub statusDescription { sub showStatus {
my ($buildstatus) = @_; my ($build) = @_;
my $status = "Failed"; my $status = "Failed";
given ($buildstatus) { given ($build->buildstatus) {
when (0) { $status = "Success"; } when (0) { $status = "Success"; }
when (1) { $status = "Failed with non-zero exit code"; } when (1) { $status = "Failed with non-zero exit code"; }
when (2) { $status = "Dependency failed"; } when (2) { $status = "Dependency failed"; }
@ -30,6 +30,12 @@ sub statusDescription {
} }
sub showJobName {
my ($build) = @_;
return $build->project->name . ":" . $build->jobset->name . ":" . $build->job->name;
}
sub getPrevBuild { sub getPrevBuild {
my ($self, $build) = @_; my ($self, $build) = @_;
return $self->{db}->resultset('Builds')->search( return $self->{db}->resultset('Builds')->search(
@ -45,10 +51,31 @@ sub getPrevBuild {
} }
sub showJobName { my $template = <<EOF;
my ($build) = @_; Hi,
return $build->project->name . ":" . $build->jobset->name . ":" . $build->job->name;
} The status of Hydra job [% showJobName(build) %] (on [% build.system %]) [% IF prevBuild && build.buildstatus != prevBuild.buildstatus %]has changed from "[% showStatus(prevBuild) %]" to "[% showStatus(build) %]"[% ELSE %]is "[% showStatus(build) %]"[% END %]. For details, see
[% baseurl %]/build/[% build.id %]
[% IF dependents.size > 0 -%]
The following dependent jobs also failed:
[% FOREACH b IN dependents -%]
* [% showJobName(b) %] ([% baseurl %]/build/[% b.id %])
[% END -%]
[% END -%]
[% IF build.buildstatus == 0 -%]
Yay!
[% ELSE -%]
Go forth and fix [% IF dependents.size == 0 -%]it[% ELSE %]them[% END %].
[% END -%]
Regards,
The Hydra build daemon.
EOF
sub buildFinished { sub buildFinished {
@ -91,97 +118,37 @@ sub buildFinished {
print STDERR "sending mail notification to ", $to, "\n"; print STDERR "sending mail notification to ", $to, "\n";
my @builds = @{$addresses{$to}->{builds}}; my @builds = @{$addresses{$to}->{builds}};
my $jobName = showJobName $build; my $tt = Template->new({});
my $status = statusDescription($build->buildstatus); my $vars =
{ build => $build, prevBuild => getPrevBuild($self, $build)
, dependents => [grep { $_->id != $build->id } @builds]
, baseurl => $self->{config}->{'base_uri'} || "http://localhost:3000"
, showJobName => \&showJobName, showStatus => \&showStatus
};
my $baseurl = hostname_long; my $body;
my $sender = $self->{config}->{'notification_sender'} || $tt->process(\$template, $vars, \$body)
(($ENV{'USER'} || "hydra") . "@" . $baseurl); or die "failed to generate email from template";
my $selfURI = $self->{config}->{'base_uri'} || "http://localhost:3000";
sub showTime { my ($x) = @_; return strftime('%Y-%m-%d %H:%M:%S', localtime($x)); }
my $infoTable = Text::Table->new({ align => "left" }, \ " | ", { align => "left" });
my @lines = (
[ "Build ID:", $build->id ],
[ "Nix name:", $build->nixname ],
[ "Short description:", $build->description || '(not given)' ],
[ "Maintainer(s):", $build->maintainers ],
[ "System:", $build->system ],
[ "Derivation store path:", $build->drvpath ],
[ "Output store path:", join(", ", map { $_->path } $build->buildoutputs) ],
[ "Time added:", showTime $build->timestamp ],
);
push @lines, (
[ "Build started:", showTime $build->starttime ],
[ "Build finished:", showTime $build->stoptime ],
[ "Duration:", $build->stoptime - $build->starttime . "s" ],
) if $build->starttime;
$infoTable->load(@lines);
my $inputsTable = Text::Table->new(
{ title => "Name", align => "left" }, \ " | ",
{ title => "Type", align => "left" }, \ " | ",
{ title => "Value", align => "left" });
@lines = ();
foreach my $input ($build->inputs) {
my $type = $input->type;
push @lines,
[ $input->name
, $input->type
, ( $input->type eq "build" || $input->type eq "sysbuild")
? $input->dependency->id
: ($input->type eq "string" || $input->type eq "boolean")
? $input->value : ($input->uri . ':' . $input->revision)
];
}
$inputsTable->load(@lines);
my $loglines = 50;
my $logtext = logContents($build->drvpath, $loglines);
$logtext = removeAsciiEscapes($logtext);
my $prevBuild = getPrevBuild($self, $build);
my $foo = "\nIn addition, the following jobs failed:\n";
foreach my $b (@builds) {
$foo .= " " . showJobName($b) . " ($selfURI/build/" . $b->id . ")\n";
}
my $body = "Hi,\n"
. "\n"
. "This is to let you know that Hydra build " . $build->id
. " of job " . $jobName . " " . (defined $prevBuild ? "has changed from '" . statusDescription($prevBuild->buildstatus) . "' to '$status'" : "is '$status'" ) .".\n"
. $foo
. "\n"
. "Complete build information can be found on this page: "
. "$selfURI/build/" . $build->id . "\n"
. ($build->buildstatus != 0 ? "\nThe last $loglines lines of the build log are shown at the bottom of this email.\n" : "")
. "\n"
. "A summary of the build information follows:\n"
. "\n"
. $infoTable->body
. "\n"
. "The build inputs were:\n"
. "\n"
. $inputsTable->title
. $inputsTable->rule('-', '+')
. $inputsTable->body
. "\n"
. "Regards,\n\nThe Hydra build daemon.\n"
. ($build->buildstatus != 0 ? "\n---\n$logtext" : "");
# stripping trailing spaces from lines # stripping trailing spaces from lines
$body =~ s/[\ ]+$//gm; $body =~ s/[\ ]+$//gm;
print "$body\n";
my $sender = $self->{config}->{'notification_sender'} ||
(($ENV{'USER'} || "hydra") . "@" . hostname_long);
#my $loglines = 50;
#my $logtext = logContents($build->drvpath, $loglines);
#$logtext = removeAsciiEscapes($logtext);
my $email = Email::Simple->create( my $email = Email::Simple->create(
header => [ header => [
To => $to, To => $to,
From => "Hydra Build Daemon <$sender>", From => "Hydra Build Daemon <$sender>",
Subject => "$status: Hydra job $jobName on " . $build->system . ", build " . $build->id, Subject => showStatus($build) . ": Hydra job " . showJobName($build) . " on " . $build->system,
'X-Hydra-Instance' => $baseurl, 'X-Hydra-Instance' => $vars->{baseurl},
'X-Hydra-Project' => $build->project->name, 'X-Hydra-Project' => $build->project->name,
'X-Hydra-Jobset' => $build->jobset->name, 'X-Hydra-Jobset' => $build->jobset->name,
'X-Hydra-Job' => $build->job->name, 'X-Hydra-Job' => $build->job->name,