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
|
|
|
},
|
2016-11-18 17:38:08 +00:00
|
|
|
controller: function($scope, $element, ApiService, $routeParams, Config, $rootScope, TableService) {
|
2015-02-19 21:21:54 +00:00
|
|
|
$scope.robots = null;
|
|
|
|
$scope.loading = false;
|
2015-04-01 17:56:30 +00:00
|
|
|
$scope.Config = Config;
|
2016-11-18 17:38:08 +00:00
|
|
|
$scope.TableService = TableService;
|
2015-07-13 12:34:46 +00:00
|
|
|
$scope.feedback = null;
|
2016-05-12 21:59:49 +00:00
|
|
|
|
2016-04-29 23:37:35 +00:00
|
|
|
$scope.robotDisplayInfo = null;
|
2016-05-12 21:59:49 +00:00
|
|
|
$scope.createRobotInfo = null;
|
2015-02-19 21:21:54 +00:00
|
|
|
|
2016-11-18 17:38:08 +00:00
|
|
|
$scope.options = {
|
|
|
|
'filter': null,
|
|
|
|
'predicate': 'name',
|
|
|
|
'reverse': false,
|
|
|
|
};
|
2015-04-23 18:40:01 +00:00
|
|
|
|
2016-11-18 17:38:08 +00:00
|
|
|
var buildOrderedRobots = function() {
|
|
|
|
if (!$scope.robots) {
|
2015-04-23 18:40:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-18 17:38:08 +00:00
|
|
|
var robots = $scope.robots;
|
|
|
|
robots.forEach(function(robot) {
|
|
|
|
robot['teams_string'] = robot.teams.map(function(team) {
|
|
|
|
return team['name'] || '';
|
|
|
|
}).join(',');
|
2018-03-09 18:55:19 +00:00
|
|
|
|
|
|
|
robot['created_datetime'] = robot.created ? TableService.getReversedTimestamp(robot.created) : null;
|
2018-03-13 00:30:19 +00:00
|
|
|
robot['last_accessed_datetime'] = robot.last_accessed ? TableService.getReversedTimestamp(robot.last_accessed) : null;
|
2016-11-18 17:38:08 +00:00
|
|
|
});
|
2015-04-23 18:40:01 +00:00
|
|
|
|
2016-11-18 17:38:08 +00:00
|
|
|
$scope.orderedRobots = TableService.buildOrderedItems(robots, $scope.options,
|
|
|
|
['name', 'teams_string'], []);
|
2015-04-23 18:40:01 +00:00
|
|
|
};
|
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
$scope.showRobot = function(info) {
|
2016-04-29 23:37:35 +00:00
|
|
|
$scope.robotDisplayInfo = {
|
|
|
|
'name': info.name
|
|
|
|
};
|
2015-02-19 21:21:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$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);
|
|
|
|
};
|
|
|
|
|
2016-05-12 21:59:49 +00:00
|
|
|
$scope.askCreateRobot = function() {
|
|
|
|
$scope.createRobotInfo = {
|
|
|
|
'namespace': $scope.organization ? $scope.organization.name : $scope.user.username
|
|
|
|
};
|
2015-02-19 21:21:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$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);
|
2015-07-13 12:34:46 +00:00
|
|
|
$scope.feedback = {
|
2015-07-14 09:22:18 +00:00
|
|
|
'kind': 'success',
|
|
|
|
'message': 'Robot account {robot} was deleted',
|
|
|
|
'data': {
|
|
|
|
'robot': info.name
|
|
|
|
}
|
2015-07-13 12:34:46 +00:00
|
|
|
};
|
2016-11-18 17:38:08 +00:00
|
|
|
buildOrderedRobots();
|
2015-02-19 21:21:54 +00:00
|
|
|
}
|
|
|
|
}, 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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-08-22 18:42:35 +00:00
|
|
|
$scope.setPermissions = function(info) {
|
|
|
|
var namespace = $scope.organization ? $scope.organization.name : $scope.user.username;
|
|
|
|
$scope.setRepoPermissionsInfo = {
|
|
|
|
'namespace': namespace,
|
|
|
|
'entityName': info.name,
|
|
|
|
'entityKind': 'robot',
|
|
|
|
'entityIcon': 'ci-robot'
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.handlePermissionsSet = function(info, repositories) {
|
|
|
|
var index = $scope.findRobotIndexByName(info.entityName);
|
|
|
|
$scope.robots[index]['repositories'] = repositories;
|
|
|
|
};
|
|
|
|
|
2016-05-12 21:59:49 +00:00
|
|
|
$scope.robotCreated = function() {
|
|
|
|
update();
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2015-05-08 20:43:07 +00:00
|
|
|
var params = {
|
|
|
|
'permissions': true
|
|
|
|
};
|
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
$scope.loading = true;
|
2015-05-08 20:43:07 +00:00
|
|
|
ApiService.getRobots($scope.organization, null, params).then(function(resp) {
|
2015-02-19 21:21:54 +00:00
|
|
|
$scope.robots = resp.robots;
|
2016-11-18 17:38:08 +00:00
|
|
|
buildOrderedRobots();
|
2015-02-19 21:21:54 +00:00
|
|
|
$scope.loading = false;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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);
|
2016-11-18 17:38:08 +00:00
|
|
|
$scope.$watch('options.filter', buildOrderedRobots);
|
|
|
|
$scope.$watch('options.predicate', buildOrderedRobots);
|
|
|
|
$scope.$watch('options.reverse', buildOrderedRobots);
|
2015-02-19 21:21:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
return directiveDefinitionObject;
|
|
|
|
});
|