From bef263c9306699c5c317ced28373bfebf053c520 Mon Sep 17 00:00:00 2001
From: Eelco Dolstra <eelco.dolstra@logicblox.com>
Date: Mon, 12 Aug 2013 22:17:04 +0200
Subject: [PATCH] =?UTF-8?q?Add=20a=20=E2=80=98latest-finished=E2=80=99=20a?=
 =?UTF-8?q?ction?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

It redirects to the latest successful build from a finished
evaluation.  This is mostly useful for the Nixpkgs/NixOS mirroring
script, which need the latest finished evaluation in which some
aggregate job (such as ‘tested’ in NixOS) succeeded.
---
 src/lib/Hydra/Base/Controller/ListBuilds.pm | 18 ++++++++++++++++++
 src/lib/Hydra/Controller/Root.pm            |  2 +-
 src/root/job.tt                             |  1 +
 3 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/src/lib/Hydra/Base/Controller/ListBuilds.pm b/src/lib/Hydra/Base/Controller/ListBuilds.pm
index 59163e77..f3c69263 100644
--- a/src/lib/Hydra/Base/Controller/ListBuilds.pm
+++ b/src/lib/Hydra/Base/Controller/ListBuilds.pm
@@ -119,4 +119,22 @@ sub latest_for : Chained('get_builds') PathPart('latest-for') {
 }
 
 
+# Redirect to the latest successful build in a finished evaluation
+# (i.e. an evaluation that has no unfinished builds).
+sub latest_finished : Chained('get_builds') PathPart('latest-finished') {
+    my ($self, $c, @rest) = @_;
+
+    my $latest = $c->stash->{allBuilds}->find(
+        { finished => 1, buildstatus => 0 },
+        { order_by => ["id DESC"], rows => 1, join => ["jobsetevalmembers"]
+        , where => \
+            "not exists (select 1 from jobsetevalmembers m2 join builds b2 on jobsetevalmembers.eval = m2.eval and m2.build = b2.id and b2.finished = 0)"
+        });
+
+    notFound($c, "There is no successful build to redirect to.") unless defined $latest;
+
+    $c->res->redirect($c->uri_for($c->controller('Build')->action_for("build"), [$latest->id], @rest));
+}
+
+
 1;
diff --git a/src/lib/Hydra/Controller/Root.pm b/src/lib/Hydra/Controller/Root.pm
index 1ddd6a0c..8b7cf4ee 100644
--- a/src/lib/Hydra/Controller/Root.pm
+++ b/src/lib/Hydra/Controller/Root.pm
@@ -283,7 +283,7 @@ sub narinfo :LocalRegex('^([a-z0-9]+).narinfo$') :Args(0) {
     my $path = queryPathFromHashPart($hash);
 
     if (!$path) {
-	$c->response->status(404);
+        $c->response->status(404);
         $c->response->content_type('text/plain');
         $c->stash->{plain}->{data} = "does not exist\n";
         $c->forward('Hydra::View::Plain');
diff --git a/src/root/job.tt b/src/root/job.tt
index 5ccef23f..1361fccd 100644
--- a/src/root/job.tt
+++ b/src/root/job.tt
@@ -24,6 +24,7 @@
   <div id="tabs-links" class="tab-pane">
     <ul>
       <li><a href="[% c.uri_for('/job' project.name jobset.name job.name 'latest') %]">Latest successful build</a></li>
+      <li><a href="[% c.uri_for('/job' project.name jobset.name job.name 'latest-finished') %]">Latest successful build from a finished evaluation</a></li>
       [% FOREACH system IN systems %]
         <li><a href="[% c.uri_for('/job' project.name jobset.name job.name 'latest-for' system.system) %]">Latest successful build for <tt>[% system.system %]</tt></a></li>
       [% END %]