File Coverage

blib/lib/PkgForge/BuildCommand/Reporter/Email.pm
Criterion Covered Total %
statement 36 68 52.9
branch 0 10 0.0
condition n/a
subroutine 12 19 63.1
pod 0 3 0.0
total 48 100 48.0


line stmt bran cond sub pod time code
1             package PkgForge::BuildCommand::Reporter::Email; # -*-perl-*-
2 1     1   2450 use strict;
  1         3  
  1         44  
3 1     1   10 use warnings;
  1         3  
  1         59  
4              
5             # $Id: Email.pm.in 17916 2011-08-05 08:51:50Z squinney@INF.ED.AC.UK $
6             # $Source:$
7             # $Revision: 17916 $
8             # $HeadURL: https://svn.lcfg.org/svn/source/tags/PkgForge-Server/PkgForge_Server_1_1_10/lib/PkgForge/BuildCommand/Reporter/Email.pm.in $
9             # $Date: 2011-08-05 09:51:50 +0100 (Fri, 05 Aug 2011) $
10              
11             our $VERSION = '1.1.10';
12              
13 1     1   6 use English qw(-no_match_vars);
  1         3  
  1         10  
14 1     1   717 use File::Spec ();
  1         3  
  1         19  
15 1     1   1203 use MIME::Lite::TT ();
  1         72646  
  1         23  
16 1     1   11 use Sys::Hostname ();
  1         2  
  1         14  
17 1     1   5 use Template ();
  1         2  
  1         14  
18 1     1   5 use Try::Tiny;
  1         2  
  1         114  
19              
20 1     1   7 use overload q{""} => sub { shift->stringify };
  1     0   2  
  1         28  
  0         0  
21              
22 1     1   64 use Moose;
  1         2  
  1         13  
23 1     1   8716 use MooseX::Types::Moose qw(Str HashRef);
  1         3  
  1         14  
24              
25             with 'PkgForge::BuildCommand::Reporter';
26              
27             has 'report_from' => (
28             is => 'ro',
29             isa => Str,
30             required => 1,
31             default => sub {
32             my $hostname = Sys::Hostname::hostname();
33             return 'pkgforge@' . $hostname;
34             },
35             documentation => 'The From address for the email report',
36             );
37              
38             has 'template' => (
39             is => 'ro',
40             isa => 'Str|ScalarRef[Str]',
41             required => 1,
42             builder => 'build_template',
43             documentation => 'The template for the email report',
44             );
45              
46             has 'subject' => (
47             is => 'ro',
48             isa => Str,
49             required => 1,
50             builder => 'build_subject',
51             documentation => 'The subject for the email report',
52             );
53              
54             has 'options' => (
55             is => 'ro',
56             isa => HashRef,
57             default => sub { {} },
58             documentation => 'A hash of extra options to pass into the template',
59             );
60              
61 1     1   7175 no Moose;
  1         4  
  1         8  
62             __PACKAGE__->meta->make_immutable;
63              
64             sub build_subject {
65 0     0 0   my ($self) = @_;
66              
67 0           my $subject = 'PkgForge [% IF buildinfo.completed %][OK][% ELSE %][FAIL][% END %] - [% buildinfo.platform %]/[% buildinfo.architecture %] - [% job.id %]';
68              
69 0           return $subject;
70             }
71              
72             sub build_template {
73 0     0 0   my ($self) = @_;
74              
75 0           my $template = \<<'EOT';
76             [%- USE date -%]
77             Package Forge Build Report for [% job.id %] on [% buildinfo.platform %]/[% buildinfo.architecture %]
78              
79             [% IF buildinfo.completed -%]
80             Task build and submission successful. The following packages were generated:
81             [% ELSE -%]
82             [% IF buildinfo.built_successfully -%]
83             Task built successfully but submission failed. The following packages were generated:
84             [% ELSE -%]
85             Build failure. The following packages failed to build:
86             [% END -%]
87             [% END -%]
88              
89             [% IF buildinfo.built_successfully -%]
90             [% FOREACH pkg IN buildinfo.products -%]
91             [% pkg | file_basename %]
92             [% END -%]
93             [% ELSE -%]
94             [% FOREACH pkg IN buildinfo.failures -%]
95             [% pkg | file_basename %]
96             [% END -%]
97             [% END -%]
98              
99             Build Information
100             =================
101              
102             Build Daemon: [% buildinfo.builder %]
103             Hostname: [% buildinfo.hostname %]
104             Platform: [% buildinfo.platform %]
105             Architecture: [% buildinfo.architecture %]
106             Submitter: [% job.submitter %]
107             Submission Time: [% date.format( job.subtime, '%A %d %B %Y, %H:%M:%S' ) %]
108              
109             --
110              
111             You were sent this job status report from the Package Forge build
112             service, running on [% buildinfo.hostname %], because it was requested by you
113             (or someone else on your behalf).
114              
115             EOT
116              
117 0           return $template;
118             }
119              
120             sub run {
121 0     0 0   my ( $self, $job, $buildinfo, $buildlog ) = @_;
122              
123 0           my $logger = $buildlog->logger;
124              
125 0 0         if ( !$job->report_required ) {
126 0           return;
127             }
128              
129 0           my ( $to, @cc ) = $job->report_list;
130 0           my $cc = join q{, }, @cc;
131              
132 0           my %params = (
133             buildinfo => $buildinfo,
134             job => $job,
135             );
136              
137 0           my $tt_options = $self->options;
138             $tt_options->{FILTERS}{file_basename} =
139             sub {
140 0     0     my ($path) = @_;
141 0           my $basename = ( File::Spec->splitpath($path) )[2];
142 0           return $basename;
143 0           };
144              
145             # Pass the message subject through Template Toolkit processor
146              
147 0           my $subject = $self->subject;
148             try {
149 0     0     my $new_subject;
150              
151 0 0         my $tt = Template->new($tt_options) or die $Template::ERROR . "\n";
152 0 0         $tt->process( \$subject, \%params, \$new_subject )
153             or die $tt->error() . "\n";
154 0           $subject = $new_subject;
155             } catch {
156 0     0     $logger->error("Failed to process the message subject: $_");
157 0           $subject = "PkgForge Report - $job";
158 0           };
159              
160 0 0         my $msg = MIME::Lite::TT->new(
161             From => $self->report_from,
162             To => $to,
163             Cc => $cc,
164             References => "pkgforge-$job",
165             Subject => $subject,
166             Template => $self->template,
167             TmplParams => \%params,
168             TmplOptions => $tt_options,
169             TmplUpgrade => 1,
170             'X-PkgForge-Status' => ( $buildinfo->completed ? 'success' : 'fail' ),
171             'X-PkgForge-Builder' => $buildinfo->builder,
172             'X-PkgForge-ID' => $job->id,
173             );
174              
175 0           my $send_ok = $msg->send();
176 0 0         if ( !$send_ok ) {
177 0           $logger->error("Failed to send report email for '$subject'");
178             }
179              
180 0           return $send_ok;
181             }
182              
183             1;
184             __END__