From 698d6eac8904b154dd1054dca2a90293c1e4a1f2 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 27 Jun 2013 18:08:00 +0200 Subject: [PATCH] Eliminate duplicate getPreviousBuild functions And move some stuff into CatalystUtils for use by other plugins. --- src/lib/Hydra/Controller/Build.pm | 2 +- src/lib/Hydra/Helper/CatalystUtils.pm | 40 +++++++++++++++------ src/lib/Hydra/Plugin/EmailNotification.pm | 42 ++--------------------- 3 files changed, 33 insertions(+), 51 deletions(-) diff --git a/src/lib/Hydra/Controller/Build.pm b/src/lib/Hydra/Controller/Build.pm index 03c18fcc..dcb17a2c 100644 --- a/src/lib/Hydra/Controller/Build.pm +++ b/src/lib/Hydra/Controller/Build.pm @@ -24,7 +24,7 @@ sub build : Chained('/') PathPart CaptureArgs(1) { notFound($c, "Build with ID $id doesn't exist.") 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->{firstBrokenBuild} = getNextBuild($c, $c->stash->{prevSuccessfulBuild}); diff --git a/src/lib/Hydra/Helper/CatalystUtils.pm b/src/lib/Hydra/Helper/CatalystUtils.pm index 07c593e3..06719a97 100644 --- a/src/lib/Hydra/Helper/CatalystUtils.pm +++ b/src/lib/Hydra/Helper/CatalystUtils.pm @@ -10,6 +10,7 @@ use Email::Sender::Transport::SMTP; use Sys::Hostname::Long; use Nix::Store; use Hydra::Helper::Nix; +use feature qw/switch/; our @ISA = qw(Exporter); our @EXPORT = qw( @@ -18,12 +19,14 @@ our @EXPORT = qw( requireLogin requireProjectOwner requireAdmin requirePost isAdmin isProjectOwner trim getLatestFinishedEval - parseJobsetName sendEmail paramToList backToReferer $pathCompRE $relPathRE $relNameRE $projectNameRE $jobsetNameRE $jobNameRE $systemRE $userNameRE @buildListColumns + parseJobsetName + showJobName + showStatus ); @@ -39,19 +42,14 @@ sub getBuild { sub getPreviousBuild { - my ($c, $build) = @_; + my ($build) = @_; return undef if !defined $build; - - (my $prevBuild) = $c->model('DB::Builds')->search( + return $build->job->builds->search( { finished => 1 , system => $build->system - , project => $build->project->name - , jobset => $build->jobset->name - , job => $build->job->name , 'me.id' => { '<' => $build->id } - }, {rows => 1, order_by => "me.id DESC"}); - - return $prevBuild; + , -not => { buildstatus => { -in => [4, 3]} } + }, { rows => 1, order_by => "me.id DESC" })->single; } @@ -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; diff --git a/src/lib/Hydra/Plugin/EmailNotification.pm b/src/lib/Hydra/Plugin/EmailNotification.pm index 8db67d3c..5a043b8e 100644 --- a/src/lib/Hydra/Plugin/EmailNotification.pm +++ b/src/lib/Hydra/Plugin/EmailNotification.pm @@ -2,7 +2,6 @@ package Hydra::Plugin::EmailNotification; use strict; use parent 'Hydra::Plugin'; -use feature qw/switch/; use POSIX qw(strftime); use Email::Sender::Simple qw(sendmail); use Email::Sender::Transport::SMTP; @@ -15,43 +14,6 @@ use Hydra::Helper::Nix; 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 = <jobset->emailoverride ne "" ? $b->jobset->emailoverride : $b->maintainers; foreach my $address (split ",", $to) { @@ -122,7 +84,7 @@ sub buildFinished { my $tt = Template->new({}); my $vars = - { build => $build, prevBuild => getPrevBuild($self, $build) + { build => $build, prevBuild => getPreviousBuild($build) , dependents => [grep { $_->id != $build->id } @builds] , baseurl => $self->{config}->{'base_uri'} || "http://localhost:3000" , showJobName => \&showJobName, showStatus => \&showStatus