hydra/src/lib/Hydra/Helper/CatalystUtils.pm

193 lines
4.8 KiB
Perl
Raw Normal View History

package Hydra::Helper::CatalystUtils;
use strict;
use Exporter;
2009-03-02 10:23:40 +00:00
use Readonly;
2011-11-30 14:25:28 +00:00
use Nix::Store;
use Hydra::Helper::Nix;
our @ISA = qw(Exporter);
2009-03-02 10:23:40 +00:00
our @EXPORT = qw(
getBuild getPreviousBuild getNextBuild getPreviousSuccessfulBuild getBuildStats getChannelData
error notFound
2010-06-04 14:43:28 +00:00
requireLogin requireProjectOwner requireAdmin requirePost isAdmin isProjectOwner
trim
2012-04-03 09:28:59 +00:00
getLatestFinishedEval
$pathCompRE $relPathRE $relNameRE $jobNameRE $systemRE
@buildListColumns
2009-03-02 10:23:40 +00:00
);
# Columns from the Builds table needed to render build lists.
Readonly our @buildListColumns => ('id', 'finished', 'timestamp', 'project', 'jobset', 'job', 'nixname', 'system', 'priority', 'busy', 'buildstatus', 'releasename');
sub getBuild {
my ($c, $id) = @_;
my $build = $c->model('DB::Builds')->find($id);
return $build;
}
sub getPreviousBuild {
my ($c, $build) = @_;
2010-07-27 11:21:21 +00:00
return undef if !defined $build;
(my $prevBuild) = $c->model('DB::Builds')->search(
{ finished => 1
, system => $build->system
, project => $build->project->name
, jobset => $build->jobset->name
, job => $build->job->name
, 'me.id' => { '<' => $build->id }
2011-03-14 14:05:32 +00:00
}, {rows => 1, order_by => "me.id DESC"});
return $prevBuild;
}
sub getNextBuild {
my ($c, $build) = @_;
2010-07-27 11:21:21 +00:00
return undef if !defined $build;
(my $nextBuild) = $c->model('DB::Builds')->search(
{ finished => 1
, system => $build->system
, project => $build->project->name
, jobset => $build->jobset->name
, job => $build->job->name
, 'me.id' => { '>' => $build->id }
2011-03-14 14:05:32 +00:00
}, {rows => 1, order_by => "me.id ASC"});
return $nextBuild;
}
sub getPreviousSuccessfulBuild {
my ($c, $build) = @_;
2010-07-27 11:21:21 +00:00
return undef if !defined $build;
(my $prevBuild) = $c->model('DB::Builds')->search(
{ finished => 1
, system => $build->system
, project => $build->project->name
, jobset => $build->jobset->name
, job => $build->job->name
, buildstatus => 0
, 'me.id' => { '<' => $build->id }
2011-03-14 14:05:32 +00:00
}, {rows => 1, order_by => "me.id DESC"});
return $prevBuild;
}
sub getBuildStats {
my ($c, $builds) = @_;
$c->stash->{finishedBuilds} = $builds->search({finished => 1}) || 0;
$c->stash->{succeededBuilds} = $builds->search({finished => 1, buildStatus => 0}) || 0;
$c->stash->{scheduledBuilds} = $builds->search({finished => 0}) || 0;
$c->stash->{busyBuilds} = $builds->search({finished => 0, busy => 1}) || 0;
2009-11-18 12:59:58 +00:00
my $res;
$res = $builds->search({}, {select => {sum => 'stoptime - starttime'}, as => ['sum']})->first;
2009-11-18 12:59:58 +00:00
$c->stash->{totalBuildTime} = defined ($res) ? $res->get_column('sum') : 0 ;
}
sub error {
my ($c, $msg) = @_;
$c->error($msg);
$c->detach; # doesn't return
}
2009-02-25 14:34:29 +00:00
sub notFound {
my ($c, $msg) = @_;
$c->response->status(404);
error($c, $msg);
}
sub requireLogin {
my ($c) = @_;
$c->flash->{afterLogin} = $c->request->uri;
$c->response->redirect($c->uri_for('/login'));
$c->detach; # doesn't return
}
2010-06-04 14:43:28 +00:00
sub isProjectOwner {
my ($c, $project) = @_;
return $c->user_exists && ($c->check_user_roles('admin') || $c->user->username eq $project->owner->username || defined $c->model('DB::ProjectMembers')->find({ project => $project, userName => $c->user->username }));
}
sub requireProjectOwner {
my ($c, $project) = @_;
requireLogin($c) if !$c->user_exists;
error($c, "Only the project members or administrators can perform this operation.")
2010-06-04 14:43:28 +00:00
unless isProjectOwner($c, $project);
}
2010-06-04 14:43:28 +00:00
sub isAdmin {
my ($c) = @_;
return $c->user_exists && $c->check_user_roles('admin');
}
sub requireAdmin {
my ($c) = @_;
requireLogin($c) if !$c->user_exists;
error($c, "Only administrators can perform this operation.")
2010-06-04 14:43:28 +00:00
unless isAdmin($c);
}
sub requirePost {
my ($c) = @_;
error($c, "Request must be POSTed.") if $c->request->method ne "POST";
}
sub trim {
my $s = shift;
$s =~ s/^\s+|\s+$//g;
return $s;
}
2012-04-03 09:28:59 +00:00
sub getLatestFinishedEval {
my ($c, $jobset) = @_;
my ($eval) = $jobset->jobsetevals->search(
{ hasnewbuilds => 1 },
{ order_by => "id DESC", rows => 1
, where => \ "not exists (select 1 from JobsetEvalMembers m join Builds b on m.build = b.id where m.eval = me.id and b.finished = 0)"
});
return $eval;
}
2009-03-02 10:23:40 +00:00
# Security checking of filenames.
Readonly our $pathCompRE => "(?:[A-Za-z0-9-\+\._][A-Za-z0-9-\+\._]*)";
Readonly our $relPathRE => "(?:$pathCompRE(?:/$pathCompRE)*)";
Readonly our $relNameRE => "(?:[A-Za-z0-9-][A-Za-z0-9-\.]*)";
Readonly our $attrNameRE => "(?:[A-Za-z_][A-Za-z0-9_]*)";
Readonly our $jobNameRE => "(?:$attrNameRE(?:\\.$attrNameRE)*)";
Readonly our $systemRE => "(?:[a-z0-9_]+-[a-z0-9_]+)";
2009-03-02 10:23:40 +00:00
1;