| 1 | package PkgForge::BuildCommand::Reporter::Email; # -*-perl-*- | ||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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 | 1 | our $VERSION = '1.1.10'; | |||||||||
| 12 | |||||||||||
| 13 | 1 | ▶ | use English qw(-no_match_vars); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use File::Spec (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | ▶ | use MIME::Lite::TT (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 1 | ▶ | use Sys::Hostname (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | ▶ | use Template (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 1 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | 0 | ▶ | use overload q{""} => sub { shift->stringify }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
Subroutine
__ANON__: not called CC=1 SCAR=0.2
| |||||||||||
| 21 | |||||||||||
| 22 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 1 | ▶ | use MooseX::Types::Moose qw(Str HashRef); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | |||||||||||
| 25 | 1 | with 'PkgForge::BuildCommand::Reporter'; | |||||||||
| 26 | |||||||||||
| 27 | has 'report_from' => ( | ||||||||||
| 28 | is => 'ro', | ||||||||||
| 29 | isa => Str, | ||||||||||
| 30 | required => 1, | ||||||||||
| 31 | 0 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 32 | 0 | my $hostname = Sys::Hostname::hostname(); | |||||||||
| 33 | 0 | return 'pkgforge@' . $hostname; | |||||||||
| 34 | }, | ||||||||||
| 35 | 1 | documentation => 'The From address for the email report', | |||||||||
| 36 | ); | ||||||||||
| 37 | |||||||||||
| 38 | 1 | 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 | 1 | 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 | 0 | ▶ | default => sub { {} }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 58 | 1 | documentation => 'A hash of extra options to pass into the template', | |||||||||
| 59 | ); | ||||||||||
| 60 | |||||||||||
| 61 | 1 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 62 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||
| 63 | |||||||||||
| 64 | 0 | ▶ | sub build_subject { | ||||||||
|
Subroutine
build_subject: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 65 | 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 | 0 | ▶ | sub build_template { | ||||||||
|
Subroutine
build_template: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 73 | 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 | 0 | ▶ | sub run { | ||||||||
|
Subroutine
run: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 121 | 0 | my ( $self, $job, $buildinfo, $buildlog ) = @_; | |||||||||
| 122 | |||||||||||
| 123 | 0 | my $logger = $buildlog->logger; | |||||||||
| 124 | |||||||||||
| 125 | 0 | ▶ | if ( !$job->report_required ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 0 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 140 | 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 | 0 | ▶ | try { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||
| 149 | 0 | my $new_subject; | |||||||||
| 150 | |||||||||||
| 151 | 0 | ▶ | my $tt = Template->new($tt_options) or die $Template::ERROR . "\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 152 | 0 | ▶ | $tt->process( \$subject, \%params, \$new_subject ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 153 | or die $tt->error() . "\n"; | ||||||||||
| 154 | 0 | $subject = $new_subject; | |||||||||
| 155 | 0 | ▶ | } catch { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 156 | 0 | $logger->error("Failed to process the message subject: $_"); | |||||||||
| 157 | 0 | $subject = "PkgForge Report - $job"; | |||||||||
| 158 | 0 | }; | |||||||||
| 159 | |||||||||||
| 160 | 0 | ▶ | my $msg = MIME::Lite::TT->new( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | if ( !$send_ok ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 177 | 0 | $logger->error("Failed to send report email for '$subject'"); | |||||||||
| 178 | } | ||||||||||
| 179 | |||||||||||
| 180 | 0 | return $send_ok; | |||||||||
| 181 | } | ||||||||||
| 182 | |||||||||||
| 183 | 1 | 1; | |||||||||
| 184 | __END__ | ||||||||||