/** * An element which displays the setup and management workflow for a normal SCM git trigger. */ angular.module('quay').directive('manageTriggerGithost', function () { var directiveDefinitionObject = { priority: 0, templateUrl: '/static/directives/manage-trigger-githost.html', replace: false, transclude: true, restrict: 'C', scope: { 'repository': '=repository', 'trigger': '=trigger', 'activateTrigger': '&activateTrigger' }, controller: function($scope, $element, ApiService, TableService, TriggerService, RolesService) { $scope.TableService = TableService; $scope.config = {}; $scope.local = {}; $scope.currentState = null; $scope.namespacesPerPage = 10; $scope.repositoriesPerPage = 10; $scope.robotsPerPage = 10; $scope.local.namespaceOptions = { 'filter': '', 'predicate': 'score', 'reverse': false, 'page': 0 }; $scope.local.repositoryOptions = { 'filter': '', 'predicate': 'last_updated', 'reverse': false, 'page': 0, 'hideStale': true }; $scope.local.robotOptions = { 'filter': '', 'predicate': 'can_read', 'reverse': false, 'page': 0 }; $scope.getTriggerIcon = function() { return TriggerService.getIcon($scope.trigger.service); }; $scope.createTrigger = function() { var config = { 'build_source': $scope.local.selectedRepository.full_name, 'subdir': $scope.local.dockerfilePath.substr(1) // Remove starting / }; if ($scope.local.triggerOptions.hasBranchTagFilter && $scope.local.triggerOptions.branchTagFilter) { config['branchtag_regex'] = $scope.local.triggerOptions.branchTagFilter; } var activate = function() { $scope.activateTrigger({'config': config, 'pull_robot': $scope.local.robotAccount}); }; if ($scope.local.robotAccount) { if ($scope.local.robotAccount.can_read) { activate(); } else { // Add read permission onto the base repository for the robot and then activate the // trigger. var robot_name = $scope.local.robotAccount.name; RolesService.setRepositoryRole($scope.repository, 'read', 'robot', robot_name, activate); } } else { activate(); } }; var buildOrderedNamespaces = function() { if (!$scope.local.namespaces) { return; } var namespaces = $scope.local.namespaces || []; $scope.local.orderedNamespaces = TableService.buildOrderedItems(namespaces, $scope.local.namespaceOptions, ['id'], ['score']) $scope.local.maxScore = 0; namespaces.forEach(function(namespace) { $scope.local.maxScore = Math.max(namespace.score, $scope.local.maxScore); }); }; var loadNamespaces = function() { $scope.local.namespaces = null; $scope.local.selectedNamespace = null; $scope.local.orderedNamespaces = null; $scope.local.selectedRepository = null; $scope.local.orderedRepositories = null; var params = { 'repository': $scope.repository.namespace + '/' + $scope.repository.name, 'trigger_uuid': $scope.trigger.id }; ApiService.listTriggerBuildSourceNamespaces(null, params).then(function(resp) { $scope.local.namespaces = resp['namespaces']; $scope.local.repositories = null; buildOrderedNamespaces(); }, ApiService.errorDisplay('Could not retrieve the list of ' + $scope.namespaceTitle)) }; var buildOrderedRepositories = function() { if (!$scope.local.repositories) { return; } var repositories = $scope.local.repositories || []; repositories.forEach(function(repository) { repository['last_updated_datetime'] = new Date(repository['last_updated'] * 1000); }); if ($scope.local.repositoryOptions.hideStale) { var existingRepositories = repositories; repositories = repositories.filter(function(repository) { var older_date = moment(repository['last_updated_datetime']).add(1, 'months'); return !moment().isAfter(older_date); }); if (existingRepositories.length > 0 && repositories.length == 0) { repositories = existingRepositories; } } $scope.local.orderedRepositories = TableService.buildOrderedItems(repositories, $scope.local.repositoryOptions, ['name', 'description'], []); }; var loadRepositories = function(namespace) { $scope.local.repositories = null; $scope.local.selectedRepository = null; $scope.local.repositoryRefs = null; $scope.local.triggerOptions = { 'hasBranchTagFilter': false }; $scope.local.orderedRepositories = null; var params = { 'repository': $scope.repository.namespace + '/' + $scope.repository.name, 'trigger_uuid': $scope.trigger.id }; var data = { 'namespace': namespace.id }; ApiService.listTriggerBuildSources(data, params).then(function(resp) { if (namespace == $scope.local.selectedNamespace) { $scope.local.repositories = resp['sources']; buildOrderedRepositories(); } }, ApiService.errorDisplay('Could not retrieve repositories')); }; var loadRepositoryRefs = function(repository) { $scope.local.repositoryRefs = null; $scope.local.triggerOptions = { 'hasBranchTagFilter': false }; var params = { 'repository': $scope.repository.namespace + '/' + $scope.repository.name, 'trigger_uuid': $scope.trigger.id, 'field_name': 'refs' }; var config = { 'build_source': repository.full_name }; ApiService.listTriggerFieldValues(config, params).then(function(resp) { if (repository == $scope.local.selectedRepository) { $scope.local.repositoryRefs = resp['values']; $scope.local.repositoryFullRefs = resp['values'].map(function(ref) { var kind = ref.kind == 'branch' ? 'heads' : 'tags'; var icon = ref.kind == 'branch' ? 'fa-code-fork' : 'fa-tag'; return { 'value': kind + '/' + ref.name, 'icon': icon, 'title': ref.name }; }); } }, ApiService.errorDisplay('Could not retrieve repository refs')); }; var loadDockerfileLocations = function(repository) { $scope.local.dockerfilePath = null; var params = { 'repository': $scope.repository.namespace + '/' + $scope.repository.name, 'trigger_uuid': $scope.trigger.id }; var config = { 'build_source': repository.full_name }; ApiService.listBuildTriggerSubdirs(config, params).then(function(resp) { if (repository == $scope.local.selectedRepository) { $scope.local.dockerfileLocations = resp; } }, ApiService.errorDisplay('Could not retrieve Dockerfile locations')); }; var buildOrderedRobotAccounts = function() { if (!$scope.local.triggerAnalysis || !$scope.local.triggerAnalysis.robots) { return; } var robots = $scope.local.triggerAnalysis.robots; $scope.local.orderedRobotAccounts = TableService.buildOrderedItems(robots, $scope.local.robotOptions, ['name'], []); }; var checkDockerfilePath = function(repository, path) { $scope.local.triggerAnalysis = null; $scope.local.robotAccount = null; var params = { 'repository': $scope.repository.namespace + '/' + $scope.repository.name, 'trigger_uuid': $scope.trigger.id }; var config = { 'build_source': repository.full_name, 'subdir': path.substr(1) }; var data = { 'config': config }; ApiService.analyzeBuildTrigger(data, params).then(function(resp) { $scope.local.triggerAnalysis = resp; buildOrderedRobotAccounts(); }, ApiService.errorDisplay('Could not analyze trigger')); }; $scope.$watch('trigger', function(trigger) { if (trigger && $scope.repository) { $scope.config = trigger['config'] || {}; $scope.namespaceTitle = 'organization'; $scope.local.selectedNamespace = null; loadNamespaces(); } }); $scope.$watch('local.selectedNamespace', function(namespace) { if (namespace) { loadRepositories(namespace); } }); $scope.$watch('local.selectedRepository', function(repository) { if (repository) { loadRepositoryRefs(repository); loadDockerfileLocations(repository); } }); $scope.$watch('local.dockerfilePath', function(path) { if (path && $scope.local.selectedRepository) { checkDockerfilePath($scope.local.selectedRepository, path); } }); $scope.$watch('local.namespaceOptions.predicate', buildOrderedNamespaces); $scope.$watch('local.namespaceOptions.reverse', buildOrderedNamespaces); $scope.$watch('local.namespaceOptions.filter', buildOrderedNamespaces); $scope.$watch('local.repositoryOptions.predicate', buildOrderedRepositories); $scope.$watch('local.repositoryOptions.reverse', buildOrderedRepositories); $scope.$watch('local.repositoryOptions.filter', buildOrderedRepositories); $scope.$watch('local.repositoryOptions.hideStale', buildOrderedRepositories); $scope.$watch('local.robotOptions.predicate', buildOrderedRobotAccounts); $scope.$watch('local.robotOptions.reverse', buildOrderedRobotAccounts); $scope.$watch('local.robotOptions.filter', buildOrderedRobotAccounts); } }; return directiveDefinitionObject; });