From dad2f31099f24173095c4753187afc5b98df1cff Mon Sep 17 00:00:00 2001
From: Eelco Dolstra <e.dolstra@tudelft.nl>
Date: Wed, 4 Mar 2009 13:08:09 +0000
Subject: [PATCH] * Provide a channel for each project containing all the
 latest   succesful builds for each job in the project (under  
 http://server/project/<name>/channel/latest).

---
 src/Hydra/lib/Hydra/Controller/Project.pm   | 12 +++++++++++
 src/Hydra/lib/Hydra/Controller/Root.pm      | 19 ++++--------------
 src/Hydra/lib/Hydra/Helper/CatalystUtils.pm | 22 ++++++++++++++++++++-
 3 files changed, 37 insertions(+), 16 deletions(-)

diff --git a/src/Hydra/lib/Hydra/Controller/Project.pm b/src/Hydra/lib/Hydra/Controller/Project.pm
index 84178b28..c0a16587 100644
--- a/src/Hydra/lib/Hydra/Controller/Project.pm
+++ b/src/Hydra/lib/Hydra/Controller/Project.pm
@@ -3,6 +3,7 @@ package Hydra::Controller::Project;
 use strict;
 use warnings;
 use base 'Hydra::Base::Controller::ListBuilds';
+use base 'Hydra::Base::Controller::NixChannel';
 use Hydra::Helper::Nix;
 use Hydra::Helper::CatalystUtils;
 
@@ -224,6 +225,17 @@ sub updateProject {
 }
 
 
+# Hydra::Base::Controller::NixChannel needs this.
+sub nix : Chained('project') PathPart('channel/latest') CaptureArgs(0) {
+    my ($self, $c) = @_;
+    eval {
+        $c->stash->{channelName} = $c->stash->{curProject}->name . "-latest";
+        getChannelData($c, scalar $c->stash->{curProject}->builds);
+    };
+    error($c, $@) if $@;
+}
+
+
 # Hydra::Base::Controller::ListBuilds needs this.
 sub get_builds : Chained('project') PathPart('') CaptureArgs(0) {
     my ($self, $c) = @_;
diff --git a/src/Hydra/lib/Hydra/Controller/Root.pm b/src/Hydra/lib/Hydra/Controller/Root.pm
index 9842a3de..fa2e41f7 100644
--- a/src/Hydra/lib/Hydra/Controller/Root.pm
+++ b/src/Hydra/lib/Hydra/Controller/Root.pm
@@ -237,22 +237,11 @@ sub job :Local {
 
 sub nix : Chained('/') PathPart('channel/latest') CaptureArgs(0) {
     my ($self, $c) = @_;
-
-    $c->stash->{channelName} = "hydra-all-latest";
-
-    my @builds = @{getLatestBuilds($c, $c->model('DB::Builds'), {buildStatus => 0})};
-
-    my @storePaths = ();
-    foreach my $build (@builds) {
-        # !!! better do this in getLatestBuilds with a join.
-        next unless $build->buildproducts->find({type => "nix-build"});
-        next unless isValidPath($build->outpath);
-        push @storePaths, $build->outpath;
-        my $pkgName = $build->nixname . "-" . $build->system . "-" . $build->id . ".nixpkg";
-        $c->stash->{nixPkgs}->{$pkgName} = $build;
+    eval {
+        $c->stash->{channelName} = "hydra-all-latest";
+        getChannelData($c, $c->model('DB::Builds'));
     };
-
-    $c->stash->{storePaths} = [@storePaths];
+    error($c, $@) if $@;
 }
 
 
diff --git a/src/Hydra/lib/Hydra/Helper/CatalystUtils.pm b/src/Hydra/lib/Hydra/Helper/CatalystUtils.pm
index 456a2bb8..7abfdee0 100644
--- a/src/Hydra/lib/Hydra/Helper/CatalystUtils.pm
+++ b/src/Hydra/lib/Hydra/Helper/CatalystUtils.pm
@@ -3,10 +3,11 @@ package Hydra::Helper::CatalystUtils;
 use strict;
 use Exporter;
 use Readonly;
+use Hydra::Helper::Nix;
 
 our @ISA = qw(Exporter);
 our @EXPORT = qw(
-    getBuild getBuildStats getLatestBuilds
+    getBuild getBuildStats getLatestBuilds getChannelData
     error notFound
     requireLogin requireProjectOwner requireAdmin
     trim
@@ -66,6 +67,25 @@ sub getLatestBuilds {
 }
 
 
+sub getChannelData {
+    my ($c, $builds) = @_;
+    
+    my @builds = @{getLatestBuilds($c, $builds, {buildStatus => 0})};
+
+    my @storePaths = ();
+    foreach my $build (@builds) {
+        # !!! better do this in getLatestBuilds with a join.
+        next unless $build->buildproducts->find({type => "nix-build"});
+        next unless isValidPath($build->outpath);
+        push @storePaths, $build->outpath;
+        my $pkgName = $build->nixname . "-" . $build->system . "-" . $build->id . ".nixpkg";
+        $c->stash->{nixPkgs}->{$pkgName} = $build;
+    };
+
+    $c->stash->{storePaths} = [@storePaths];
+}
+
+
 sub error {
     my ($c, $msg) = @_;
     $c->error($msg);