Eliminate duplicate getPreviousBuild functions

And move some stuff into CatalystUtils for use by other plugins.
This commit is contained in:
Eelco Dolstra 2013-06-27 18:08:00 +02:00
parent c65f40e146
commit 698d6eac89
3 changed files with 33 additions and 51 deletions

View file

@ -24,7 +24,7 @@ sub build : Chained('/') PathPart CaptureArgs(1) {
notFound($c, "Build with ID $id doesn't exist.") notFound($c, "Build with ID $id doesn't exist.")
if !defined $c->stash->{build}; if !defined $c->stash->{build};
$c->stash->{prevBuild} = getPreviousBuild($c, $c->stash->{build}); $c->stash->{prevBuild} = getPreviousBuild($c->stash->{build});
$c->stash->{prevSuccessfulBuild} = getPreviousSuccessfulBuild($c, $c->stash->{build}); $c->stash->{prevSuccessfulBuild} = getPreviousSuccessfulBuild($c, $c->stash->{build});
$c->stash->{firstBrokenBuild} = getNextBuild($c, $c->stash->{prevSuccessfulBuild}); $c->stash->{firstBrokenBuild} = getNextBuild($c, $c->stash->{prevSuccessfulBuild});

View file

@ -10,6 +10,7 @@ use Email::Sender::Transport::SMTP;
use Sys::Hostname::Long; use Sys::Hostname::Long;
use Nix::Store; use Nix::Store;
use Hydra::Helper::Nix; use Hydra::Helper::Nix;
use feature qw/switch/;
our @ISA = qw(Exporter); our @ISA = qw(Exporter);
our @EXPORT = qw( our @EXPORT = qw(
@ -18,12 +19,14 @@ our @EXPORT = qw(
requireLogin requireProjectOwner requireAdmin requirePost isAdmin isProjectOwner requireLogin requireProjectOwner requireAdmin requirePost isAdmin isProjectOwner
trim trim
getLatestFinishedEval getLatestFinishedEval
parseJobsetName
sendEmail sendEmail
paramToList paramToList
backToReferer backToReferer
$pathCompRE $relPathRE $relNameRE $projectNameRE $jobsetNameRE $jobNameRE $systemRE $userNameRE $pathCompRE $relPathRE $relNameRE $projectNameRE $jobsetNameRE $jobNameRE $systemRE $userNameRE
@buildListColumns @buildListColumns
parseJobsetName
showJobName
showStatus
); );
@ -39,19 +42,14 @@ sub getBuild {
sub getPreviousBuild { sub getPreviousBuild {
my ($c, $build) = @_; my ($build) = @_;
return undef if !defined $build; return undef if !defined $build;
return $build->job->builds->search(
(my $prevBuild) = $c->model('DB::Builds')->search(
{ finished => 1 { finished => 1
, system => $build->system , system => $build->system
, project => $build->project->name
, jobset => $build->jobset->name
, job => $build->job->name
, 'me.id' => { '<' => $build->id } , 'me.id' => { '<' => $build->id }
}, {rows => 1, order_by => "me.id DESC"}); , -not => { buildstatus => { -in => [4, 3]} }
}, { rows => 1, order_by => "me.id DESC" })->single;
return $prevBuild;
} }
@ -230,4 +228,26 @@ sub parseJobsetName {
} }
sub showJobName {
my ($build) = @_;
return $build->project->name . ":" . $build->jobset->name . ":" . $build->job->name;
}
sub showStatus {
my ($build) = @_;
my $status = "Failed";
given ($build->buildstatus) {
when (0) { $status = "Success"; }
when (1) { $status = "Failed"; }
when (2) { $status = "Dependency failed"; }
when (4) { $status = "Cancelled"; }
when (6) { $status = "Failed with output"; }
}
return $status;
}
1; 1;

View file

@ -2,7 +2,6 @@ package Hydra::Plugin::EmailNotification;
use strict; use strict;
use parent 'Hydra::Plugin'; use parent 'Hydra::Plugin';
use feature qw/switch/;
use POSIX qw(strftime); use POSIX qw(strftime);
use Email::Sender::Simple qw(sendmail); use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP; use Email::Sender::Transport::SMTP;
@ -15,43 +14,6 @@ use Hydra::Helper::Nix;
use Hydra::Helper::CatalystUtils; use Hydra::Helper::CatalystUtils;
sub showStatus {
my ($build) = @_;
my $status = "Failed";
given ($build->buildstatus) {
when (0) { $status = "Success"; }
when (1) { $status = "Failed"; }
when (2) { $status = "Dependency failed"; }
when (4) { $status = "Cancelled"; }
when (6) { $status = "Failed with output"; }
}
return $status;
}
sub showJobName {
my ($build) = @_;
return $build->project->name . ":" . $build->jobset->name . ":" . $build->job->name;
}
sub getPrevBuild {
my ($self, $build) = @_;
return $self->{db}->resultset('Builds')->search(
{ project => $build->project->name
, jobset => $build->jobset->name
, job => $build->job->name
, system => $build->system
, finished => 1
, id => { '<', $build->id }
, -not => { buildstatus => { -in => [4, 3]} }
}, { order_by => ["id DESC"], rows => 1 }
)->single;
}
my $template = <<EOF; my $template = <<EOF;
Hi, Hi,
@ -89,7 +51,7 @@ sub buildFinished {
# relevant builds for that address. # relevant builds for that address.
my %addresses; my %addresses;
foreach my $b ($build, @{$dependents}) { foreach my $b ($build, @{$dependents}) {
my $prevBuild = getPrevBuild($self, $b); my $prevBuild = getPreviousBuild($b);
my $to = $b->jobset->emailoverride ne "" ? $b->jobset->emailoverride : $b->maintainers; my $to = $b->jobset->emailoverride ne "" ? $b->jobset->emailoverride : $b->maintainers;
foreach my $address (split ",", $to) { foreach my $address (split ",", $to) {
@ -122,7 +84,7 @@ sub buildFinished {
my $tt = Template->new({}); my $tt = Template->new({});
my $vars = my $vars =
{ build => $build, prevBuild => getPrevBuild($self, $build) { build => $build, prevBuild => getPreviousBuild($build)
, dependents => [grep { $_->id != $build->id } @builds] , dependents => [grep { $_->id != $build->id } @builds]
, baseurl => $self->{config}->{'base_uri'} || "http://localhost:3000" , baseurl => $self->{config}->{'base_uri'} || "http://localhost:3000"
, showJobName => \&showJobName, showStatus => \&showStatus , showJobName => \&showJobName, showStatus => \&showStatus