From 03323f6ef19e187c37da3a4fd2db6803474d6e80 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Mon, 22 Nov 2021 13:14:01 -0500 Subject: [PATCH] TT: add helpers for linking to jobs, jobsets, and projects, and for generating colon separated names. --- src/lib/Hydra/View/TT.pm | 262 ++++++++++++++++++++++++++++++++++++++- t/View/TT.t | 77 ++++++++++++ 2 files changed, 338 insertions(+), 1 deletion(-) create mode 100644 t/View/TT.t diff --git a/src/lib/Hydra/View/TT.pm b/src/lib/Hydra/View/TT.pm index c6176231..84fcf3e9 100644 --- a/src/lib/Hydra/View/TT.pm +++ b/src/lib/Hydra/View/TT.pm @@ -3,6 +3,7 @@ package Hydra::View::TT; use strict; use warnings; use base 'Catalyst::View::TT'; +use Template::Plugin::HTML; use Hydra::Helper::Nix; use Time::Seconds; @@ -11,7 +12,20 @@ __PACKAGE__->config( ENCODING => 'utf-8', PRE_CHOMP => 1, POST_CHOMP => 1, - expose_methods => [qw/buildLogExists buildStepLogExists jobExists relativeDuration stripSSHUser/]); + expose_methods => [qw/ + buildLogExists + buildStepLogExists + jobExists + linkToJob + linkToJobset + linkToProject + makeNameLinksForJob + makeNameLinksForJobset + makeNameTextForJob + makeNameTextForJobset + relativeDuration + stripSSHUser + /]); sub buildLogExists { my ($self, $c, $build) = @_; @@ -64,4 +78,250 @@ sub jobExists { return defined $jobset->builds->search({ job => $jobName, iscurrent => 1 })->single; } +=head2 linkToProject + +Given a L, return a link to the project. + +Arguments: + +=over 3 + +=item C<$self> +=back + +=item C<$c> +Catalyst Context +=back + +=item C<$project> + +The L to link to. + +=back + +=cut +sub linkToProject { + my ($self, $c, $project) = @_; + + my $html = Template::Plugin::HTML->new(); + + my $projectName = $project->name; + my $escapedProjectName = $html->escape($projectName); + + return '' . $escapedProjectName . ''; +} + +=head2 linkToJobset + +Given a L, return a link to the jobset +and its project in project:jobset notation. + +Arguments: + +=over 3 + +=item C<$self> +=back + +=item C<$c> +Catalyst Context +=back + +=item C<$jobset> + +The L to link to. + +=back + +=cut +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) . + ':' . $escapedJobsetName . ''; +} + +=head2 linkToJobset + +Given a L and L Job name, return +a link to the job, jobset, and project in project:jobset:job notation. + +Arguments: + +=over 4 + +=item C<$self> +=back + +=item C<$c> +Catalyst Context +=back + +=item C<$jobset> + +The L to link to. +=back + +=item C<$jobName> + +The L job name to link to. + +=back + +=cut +sub linkToJob { + my ($self, $c, $jobset, $jobName) = @_; + + my $html = Template::Plugin::HTML->new(); + + my $escapedJobName = $html->escape($jobName); + + return linkToJobset($self, $c, $jobset) . + ':' . $escapedJobName . ''; +} + +=head2 makeNameLinksForJobset + +Given a L, return a link to the jobset's +project and a non-link to the jobset in project:jobset notation. + +Arguments: + +=over 3 + +=item C<$self> +=back + +=item C<$c> +Catalyst Context +=back + +=item C<$jobset> + +The L to link to. + +=back + +=cut +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; +} + +=head2 makeNameLinksForJob + +Given a L and L Job name, return +a link to the jobset and project, and a non-link to the job in +project:jobset:job notation. + +Arguments: + +=over 4 + +=item C<$self> +=back + +=item C<$c> +Catalyst Context +=back + +=item C<$jobset> + +The L to link to. + +=back + + +=item C<$jobName> + +The L job name to link to. + +=back + +=cut +sub makeNameLinksForJob { + my ($self, $c, $jobset, $jobName) = @_; + + my $html = Template::Plugin::HTML->new(); + + my $escapedJobName = $html->escape($jobName); + + return linkToJobset($self, $c, $jobset) . ':' . $escapedJobName; +} + +=head2 makeNameTextForJobset + +Given a L, return the project and +jobset in project:jobset notation. + +Arguments: + +=over 3 + +=item C<$self> +=back + +=item C<$c> +Catalyst Context +=back + +=item C<$jobset> + +The L to link to. + +=back + +=cut +sub makeNameTextForJobset { + my ($self, $c, $jobset) = @_; + + return $jobset->project->name . ":" . $jobset->name; +} + +=head2 makeNameTextForJob + +Given a L and L Job name, return +the job, jobset, and project in project:jobset:job notation. + +Arguments: + +=over 4 + +=item C<$self> +=back + +=item C<$c> +Catalyst Context +=back + +=item C<$jobset> + +The L to link to. + +=back + + +=item C<$jobName> + +The L job name to link to. + +=back + +=cut +sub makeNameTextForJob { + my ($self, $c, $jobset, $jobName) = @_; + + return $jobset->project->name . ":" . $jobset->name . ":" . $jobName; +} + 1; diff --git a/t/View/TT.t b/t/View/TT.t new file mode 100644 index 00000000..346879a1 --- /dev/null +++ b/t/View/TT.t @@ -0,0 +1,77 @@ +use feature 'unicode_strings'; +use strict; +use warnings; +use Setup; + +my %ctx = test_init(); + +require Hydra::Schema; +require Hydra::Model::DB; + +use Test2::V0; + +require Hydra; # calls setup() + + +my $db = Hydra::Model::DB->new; +hydra_setup($db); + +require Hydra::View::TT; + +# The following lines are a cheap and hacky trick to get $c, +# there is no other reason to call /. +require Catalyst::Test; +Catalyst::Test->import('Hydra'); +my($_, $c) = ctx_request('/'); + + +my $project = $db->resultset('Projects')->create({name => "tests", displayname => "", owner => "root"}); +my $jobset = createBaseJobset("example", "bogus.nix", $ctx{jobsdir}); +my $job = "myjob"; + + +is( + Hydra::View::TT::linkToProject(undef, $c, $project), + 'tests', + "linkToProject" +); +is( + Hydra::View::TT::linkToJobset(undef, $c, $jobset), + 'tests:' + . 'example', + "linkToJobset" +); +is( + Hydra::View::TT::linkToJob(undef, $c, $jobset, $job), + 'tests:' + . 'example:' + . 'myjob', + "linkToJob" +); + +is( + Hydra::View::TT::makeNameLinksForJobset(undef, $c, $jobset), + 'tests' + . ':example', + "makeNameLinksForJobset" +); +is( + Hydra::View::TT::makeNameLinksForJob(undef, $c, $jobset, $job), + 'tests:' + . 'example' + . ':myjob', + "makeNameLinksForJob" +); + +is( + Hydra::View::TT::makeNameTextForJobset(undef, $c, $jobset), + 'tests:example', + "makeNameTextForJobset" +); +is( + Hydra::View::TT::makeNameTextForJob(undef, $c, $jobset, $job), + 'tests:example:myjob', + "makeNameTextForJob" +); + +done_testing;