2015-02-19 21:21:54 +00:00
|
|
|
/**
|
|
|
|
* Element for managing the robots owned by an organization or a user.
|
|
|
|
*/
|
|
|
|
angular.module('quay').directive('robotsManager', function () {
|
|
|
|
var directiveDefinitionObject = {
|
|
|
|
priority: 0,
|
|
|
|
templateUrl: '/static/directives/robots-manager.html',
|
|
|
|
replace: false,
|
|
|
|
transclude: false,
|
|
|
|
restrict: 'C',
|
|
|
|
scope: {
|
|
|
|
'organization': '=organization',
|
2015-04-21 20:07:24 +00:00
|
|
|
'user': '=user',
|
|
|
|
'isEnabled': '=isEnabled'
|
2015-02-19 21:21:54 +00:00
|
|
|
},
|
2015-04-23 18:40:01 +00:00
|
|
|
controller: function($scope, $element, ApiService, $routeParams, $location, CreateService,
|
|
|
|
Config, $rootScope) {
|
2015-02-19 21:21:54 +00:00
|
|
|
$scope.ROBOT_PATTERN = ROBOT_PATTERN;
|
2015-03-31 22:50:43 +00:00
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
$scope.robots = null;
|
|
|
|
$scope.loading = false;
|
|
|
|
$scope.shownRobot = null;
|
|
|
|
$scope.showRobotCounter = 0;
|
2015-04-01 17:56:30 +00:00
|
|
|
$scope.Config = Config;
|
2015-02-19 21:21:54 +00:00
|
|
|
|
2015-04-23 18:40:01 +00:00
|
|
|
// Listen for route changes and update the tabs accordingly.
|
|
|
|
var locationListener = $rootScope.$on('$routeUpdate', function(){
|
|
|
|
if ($location.search()['showRobot']) {
|
|
|
|
$scope.filterToRobot($location.search()['showRobot']);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$scope.$on('$destroy', function() {
|
|
|
|
locationListener && locationListener();
|
|
|
|
});
|
|
|
|
|
2015-03-31 22:50:43 +00:00
|
|
|
var loadRobotPermissions = function(info) {
|
|
|
|
var shortName = $scope.getShortenedName(info.name);
|
|
|
|
info.loading_permissions = true;
|
|
|
|
ApiService.getRobotPermissions($scope.organization, null, {'robot_shortname': shortName}).then(function(resp) {
|
|
|
|
info.permissions = resp.permissions;
|
|
|
|
info.loading_permissions = false;
|
|
|
|
}, ApiService.errorDisplay('Could not load robot permissions'));
|
|
|
|
};
|
|
|
|
|
2015-04-23 18:40:01 +00:00
|
|
|
$scope.filterToRobot = function(robotName) {
|
|
|
|
if ($scope.robotFilter == robotName) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var index = $scope.findRobotIndexByName(robotName);
|
|
|
|
if (index < 0) {
|
|
|
|
// Robot doesn't exist. Reload the list to see if we can find it.
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.robotFilter = robotName;
|
|
|
|
};
|
|
|
|
|
2015-03-31 22:50:43 +00:00
|
|
|
$scope.showPermissions = function(robotInfo) {
|
|
|
|
robotInfo.showing_permissions = !robotInfo.showing_permissions;
|
|
|
|
|
|
|
|
if (robotInfo.showing_permissions) {
|
|
|
|
loadRobotPermissions(robotInfo);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
$scope.regenerateToken = function(username) {
|
|
|
|
if (!username) { return; }
|
|
|
|
|
|
|
|
var shortName = $scope.getShortenedName(username);
|
|
|
|
ApiService.regenerateRobotToken($scope.organization, null, {'robot_shortname': shortName}).then(function(updated) {
|
|
|
|
var index = $scope.findRobotIndexByName(username);
|
|
|
|
if (index >= 0) {
|
|
|
|
$scope.robots.splice(index, 1);
|
|
|
|
$scope.robots.push(updated);
|
|
|
|
}
|
|
|
|
$scope.shownRobot = updated;
|
|
|
|
}, ApiService.errorDisplay('Cannot regenerate robot account token'));
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.showRobot = function(info) {
|
|
|
|
$scope.shownRobot = info;
|
|
|
|
$scope.showRobotCounter++;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.findRobotIndexByName = function(name) {
|
2015-05-05 19:19:56 +00:00
|
|
|
if (!$scope.robots) { return -1; }
|
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
for (var i = 0; i < $scope.robots.length; ++i) {
|
|
|
|
if ($scope.robots[i].name == name) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
2015-04-09 19:03:07 +00:00
|
|
|
$scope.getShortenedRobotName = function(info) {
|
|
|
|
return $scope.getShortenedName(info.name);
|
|
|
|
};
|
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
$scope.getShortenedName = function(name) {
|
|
|
|
var plus = name.indexOf('+');
|
|
|
|
return name.substr(plus + 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.getPrefix = function(name) {
|
|
|
|
var plus = name.indexOf('+');
|
|
|
|
return name.substr(0, plus);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.createRobot = function(name) {
|
|
|
|
if (!name) { return; }
|
|
|
|
|
|
|
|
CreateService.createRobotAccount(ApiService, !!$scope.organization, $scope.organization ? $scope.organization.name : '', name,
|
|
|
|
function(created) {
|
2015-04-23 17:56:25 +00:00
|
|
|
created.teams = [];
|
|
|
|
created.repositories = [];
|
2015-02-19 21:21:54 +00:00
|
|
|
$scope.robots.push(created);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.deleteRobot = function(info) {
|
|
|
|
var shortName = $scope.getShortenedName(info.name);
|
|
|
|
ApiService.deleteRobot($scope.organization, null, {'robot_shortname': shortName}).then(function(resp) {
|
|
|
|
var index = $scope.findRobotIndexByName(info.name);
|
|
|
|
if (index >= 0) {
|
|
|
|
$scope.robots.splice(index, 1);
|
|
|
|
}
|
|
|
|
}, ApiService.errorDisplay('Cannot delete robot account'));
|
|
|
|
};
|
|
|
|
|
2015-04-27 18:19:50 +00:00
|
|
|
|
|
|
|
$scope.askDeleteRobot = function(info) {
|
|
|
|
bootbox.confirm('Are you sure you want to delete robot ' + info.name + '?', function(resp) {
|
|
|
|
if (resp) {
|
|
|
|
$scope.deleteRobot(info);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
var update = function() {
|
|
|
|
if (!$scope.user && !$scope.organization) { return; }
|
2015-04-21 20:07:24 +00:00
|
|
|
if ($scope.loading || !$scope.isEnabled) { return; }
|
2015-02-19 21:21:54 +00:00
|
|
|
|
|
|
|
$scope.loading = true;
|
|
|
|
ApiService.getRobots($scope.organization).then(function(resp) {
|
|
|
|
$scope.robots = resp.robots;
|
|
|
|
$scope.loading = false;
|
|
|
|
|
|
|
|
if ($routeParams.showRobot) {
|
2015-04-23 18:40:01 +00:00
|
|
|
$scope.filterToRobot($routeParams.showRobot);
|
2015-02-19 21:21:54 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-04-21 20:07:24 +00:00
|
|
|
$scope.$watch('isEnabled', update);
|
2015-02-19 21:21:54 +00:00
|
|
|
$scope.$watch('organization', update);
|
|
|
|
$scope.$watch('user', update);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return directiveDefinitionObject;
|
|
|
|
});
|