package Hydra::View::TT;
use strict;
use warnings;
use base 'Catalyst::View::TT';
use Template::Plugin::HTML;
use Hydra::Helper::Nix;
use Time::Seconds;
__PACKAGE__->config(
TEMPLATE_EXTENSION => '.tt',
ENCODING => 'utf-8',
PRE_CHOMP => 1,
POST_CHOMP => 1,
expose_methods => [qw/
buildLogExists
buildStepLogExists
jobExists
linkToJob
linkToJobset
linkToProject
makeNameLinksForJob
makeNameLinksForJobset
makeNameTextForJob
makeNameTextForJobset
relativeDuration
stripSSHUser
/]);
sub buildLogExists {
my ($self, $c, $build) = @_;
return 1 if defined $c->config->{log_prefix};
my @outPaths = map { $_->path } $build->buildoutputs->all;
return defined findLog($c, $build->drvpath, @outPaths);
}
sub buildStepLogExists {
my ($self, $c, $step) = @_;
return 1 if defined $c->config->{log_prefix};
my @outPaths = map { $_->path } $step->buildstepoutputs->all;
return defined findLog($c, $step->drvpath, @outPaths);
}
sub relativeDuration {
my ($self, $c, $seconds) = @_;
return Time::Seconds->new($seconds)->pretty();
}
sub stripSSHUser {
my ($self, $c, $name) = @_;
if ($name =~ /^.*@(.*)$/) {
return $1;
} else {
return $name;
}
}
sub jobExists {
my ($self, $c, $jobset, $jobName) = @_;
return defined $jobset->builds->search({ job => $jobName, iscurrent => 1 })->single;
}
sub linkToProject {
my ($self, $c, $project) = @_;
my $html = Template::Plugin::HTML->new();
my $projectName = $project->name;
my $escapedProjectName = $html->escape($projectName);
return '<a href="' . $c->uri_for('/project', $projectName) . '">' . $escapedProjectName . '</a>';
}
sub linkToJobset {
my ($self, $c, $jobset) = @_;
my $html = Template::Plugin::HTML->new();
my $jobsetName = $jobset->name;
my $escapedJobsetName = $html->escape($jobsetName);
return linkToProject($self, $c, $jobset->project) .
':<a href="' . $c->uri_for('/jobset', $jobset->project->name, $jobsetName) . '">' . $escapedJobsetName . '</a>';
}
sub linkToJob {
my ($self, $c, $jobset, $jobName) = @_;
my $html = Template::Plugin::HTML->new();
my $escapedJobName = $html->escape($jobName);
return linkToJobset($self, $c, $jobset) .
':<a href="' . $c->uri_for('/job', $jobset->project->name, $jobset->name, $jobName) . '">' . $escapedJobName . '</a>';
}
sub makeNameLinksForJobset {
my ($self, $c, $jobset) = @_;
my $html = Template::Plugin::HTML->new();
my $escapedJobsetName = $html->escape($jobset->name);
return linkToProject($self, $c, $jobset->project) . ':' . $escapedJobsetName;
}
sub makeNameLinksForJob {
my ($self, $c, $jobset, $jobName) = @_;
my $html = Template::Plugin::HTML->new();
my $escapedJobName = $html->escape($jobName);
return linkToJobset($self, $c, $jobset) . ':' . $escapedJobName;
}
sub makeNameTextForJobset {
my ($self, $c, $jobset) = @_;
return $jobset->project->name . ":" . $jobset->name;
}
sub makeNameTextForJob {
my ($self, $c, $jobset, $jobName) = @_;
return $jobset->project->name . ":" . $jobset->name . ":" . $jobName;
}
1;