From 50e368c24a01e1793ff607c3bba6f5cd9d190347 Mon Sep 17 00:00:00 2001 From: Eichin David Date: Tue, 28 Oct 2014 14:13:23 +0100 Subject: [PATCH] refactoring commit controller fixed bug where include of pr template raised angularjs error --- server/app/index.html | 1 + server/app/scripts/app.js | 90 ---------------------- server/app/scripts/controllers/commit.js | 95 ++++++++++++++++++++++++ server/app/scripts/services/stdout.js | 34 +++++---- server/app/views/commit.html | 4 +- 5 files changed, 116 insertions(+), 108 deletions(-) create mode 100644 server/app/scripts/controllers/commit.js diff --git a/server/app/index.html b/server/app/index.html index fe60da6b..3b359e5d 100644 --- a/server/app/index.html +++ b/server/app/index.html @@ -37,6 +37,7 @@ + diff --git a/server/app/scripts/app.js b/server/app/scripts/app.js index 263821c2..322dd510 100644 --- a/server/app/scripts/app.js +++ b/server/app/scripts/app.js @@ -224,93 +224,3 @@ app.controller("AccountReposController", function($scope, $http, user) { return $scope.remote == "" || $scope.remote == entry.remote; }; }); - - -app.controller("CommitController", function($scope, $http, $route, $routeParams, stdout, feed) { - - var remote = $routeParams.remote; - var owner = $routeParams.owner; - var name = $routeParams.name; - var branch = $routeParams.branch; - var commit = $routeParams.commit; - $scope.console=''; - - var handleOutput = function(id, clearConsole) { - var lineFormatter = new Drone.LineFormatter(); - var el = document.querySelector('#output'); - if(clearConsole === true) { - el.innerHTML = ''; - } - stdout.subscribe(id, function(out){ - angular.element(el).append(lineFormatter.format(out)); - if ($scope.following) { - window.scrollTo(0, document.body.scrollHeight); - } - }); - } - - feed.subscribe(function(item) { - if (item.commit.sha == commit && - item.commit.branch == branch) { - if(item.commit.status == "Started") { - handleOutput(item.commit.id, true); - } - $scope.commit = item.commit; - $scope.$apply(); - - } else { - // we trigger an toast notification so the - // user is aware another build started - - } - }); - - // load the repo meta-data - $http({method: 'GET', url: '/api/repos/'+remote+'/'+owner+"/"+name}). - success(function(data, status, headers, config) { - $scope.repo = data; - }). - error(function(data, status, headers, config) { - console.log(data); - }); - - // load the repo commit data - $http({method: 'GET', url: '/api/repos/'+remote+'/'+owner+"/"+name+"/branches/"+branch+"/commits/"+commit}). - success(function(data, status, headers, config) { - $scope.commit = data; - - if (data.status!='Started' && data.status!='Pending') { - $http({method: 'GET', url: '/api/repos/'+remote+'/'+owner+"/"+name+"/branches/"+branch+"/commits/"+commit+"/console"}). - success(function(data, status, headers, config) { - var lineFormatter = new Drone.LineFormatter(); - var el = document.querySelector('#output'); - angular.element(el).append(lineFormatter.format(data)); - }). - error(function(data, status, headers, config) { - console.log(data); - }); - return; - } - - handleOutput(data.id, false); - - }). - error(function(data, status, headers, config) { - console.log(data); - }); - - $scope.following = false; - $scope.follow = function() { - $scope.following = true; - window.scrollTo(0, document.body.scrollHeight); - } - $scope.unfollow = function() { - $scope.following = false; - } - - $scope.rebuildCommit = function() { - $http({method: 'POST', url: '/api/repos/'+remote+'/'+owner+'/'+name+'/'+'branches/'+branch+'/'+'commits/'+commit+'?action=rebuild' }); - } - - -}); diff --git a/server/app/scripts/controllers/commit.js b/server/app/scripts/controllers/commit.js new file mode 100644 index 00000000..1db3594a --- /dev/null +++ b/server/app/scripts/controllers/commit.js @@ -0,0 +1,95 @@ +/*global angular, Drone, console */ +angular.module('app').controller("CommitController", function ($scope, $http, $route, $routeParams, stdout, feed) { + 'use strict'; + + var remote = $routeParams.remote, + owner = $routeParams.owner, + name = $routeParams.name, + branch = $routeParams.branch, + commit = $routeParams.commit, + // Create lineFormatter and outputElement since we need them anyway. + lineFormatter = new Drone.LineFormatter(), + outputElement = angular.element(document.querySelector('#output')); + + var connectRemoteConsole = function (id) { + // Clear console output if connecting to new remote console (rebuild) + if (!outputElement.html() !== 0) { + outputElement.empty(); + } + // Subscribe to stdout of the remote build + stdout.subscribe(id, function (out) { + // Append new output to console + outputElement.append(lineFormatter.format(out)); + // Scroll if following + if ($scope.following) { + window.scrollTo(0, document.body.scrollHeight); + } + }); + }; + + // Subscribe to feed so we can update gui if changes to the commit happen. (Build finished, Rebuild triggered, change from Pending to Started) + feed.subscribe(function (item) { + // If event is part of the active commit currently showing. + if (item.commit.sha === commit && + item.commit.branch === branch) { + // If new status is Started, connect to remote console to get live output + if (item.commit.status === "Started") { + connectRemoteConsole(item.commit.id); + } + $scope.commit = item.commit; + $scope.$apply(); + + } else { + // we trigger an toast notification so the + // user is aware another build started + + } + }); + + // Load the repo meta-data + $http({method: 'GET', url: '/api/repos/' + remote + '/' + owner + "/" + name}). + success(function (data, status, headers, config) { + $scope.repo = data; + }). + error(function (data, status, headers, config) { + console.log(data); + }); + + // Load the repo commit data + $http({method: 'GET', url: '/api/repos/' + remote + '/' + owner + "/" + name + "/branches/" + branch + "/commits/" + commit}). + success(function (data, status, headers, config) { + $scope.commit = data; + + // If build has already finished, load console output from database + if (data.status !== 'Started' && data.status !== 'Pending') { + $http({method: 'GET', url: '/api/repos/' + remote + '/' + owner + "/" + name + "/branches/" + branch + "/commits/" + commit + "/console"}). + success(function (data, status, headers, config) { + outputElement.append(lineFormatter.format(data)); + }). + error(function (data, status, headers, config) { + console.log(data); + }); + return; + // If build is currently running, connect to remote console; + } else if (data.status === 'Started') { + connectRemoteConsole(data.id); + } + + }). + error(function (data, status, headers, config) { + console.log(data); + }); + + $scope.following = false; + $scope.follow = function () { + $scope.following = true; + window.scrollTo(0, document.body.scrollHeight); + }; + $scope.unfollow = function () { + $scope.following = false; + }; + + $scope.rebuildCommit = function () { + $http({method: 'POST', url: '/api/repos/' + remote + '/' + owner + '/' + name + '/branches/' + branch + '/commits/' + commit + '?action=rebuild' }); + }; +}); diff --git a/server/app/scripts/services/stdout.js b/server/app/scripts/services/stdout.js index 894e2616..3c9d0f32 100644 --- a/server/app/scripts/services/stdout.js +++ b/server/app/scripts/services/stdout.js @@ -1,32 +1,34 @@ -'use strict'; +/*global angular, WebSocket, localStorage, console */ +angular.module('app').service('stdout', ['$window', function ($window) { + 'use strict'; + + var callback, + websocket, + token = localStorage.getItem('access_token'); -angular.module('app').service('stdout', ['$window', function($window) { - var callback = undefined; - var websocket = undefined; - var token = localStorage.getItem('access_token'); - - this.subscribe = function(path, _callback) { + this.subscribe = function (path, _callback) { callback = _callback; - var proto = ($window.location.protocol == 'https:' ? 'wss' : 'ws'); - var route = [proto, "://", $window.location.host, '/api/stream/stdout/', path, '?access_token=', token].join(''); + var proto = ($window.location.protocol === 'https:' ? 'wss' : 'ws'), + route = [proto, "://", $window.location.host, '/api/stream/stdout/', path, '?access_token=', token].join(''); websocket = new WebSocket(route); - websocket.onmessage = function(event) { - if (callback != undefined) { + websocket.onmessage = function (event) { + if (callback !== undefined) { callback(event.data); } }; - websocket.onclose = function(event) { - console.log('websocket closed at '+path); + websocket.onclose = function (event) { + console.log('websocket closed at ' + path); }; }; - this.unsubscribe = function() { + this.unsubscribe = function () { callback = undefined; - if (websocket != undefined) { - console.log('unsubscribing websocket at '+websocket.url); + if (websocket !== undefined) { + console.log('unsubscribing websocket at ' + websocket.url); websocket.close(); + websocket = undefined; } }; }]); diff --git a/server/app/views/commit.html b/server/app/views/commit.html index 1f398e6c..2cfdfd80 100644 --- a/server/app/views/commit.html +++ b/server/app/views/commit.html @@ -11,8 +11,8 @@

{{ commit.duration | toDuration}}

-
-
+
+
{{ commit.finished_at | fromNow }}
Started {{ commit.started_at | fromNow }}
Created {{ commit.created_at}}