/** * Helper service for defining the various kinds of build triggers and retrieving information * about them. */ angular.module('quay').factory('TriggerService', ['UtilService', '$sanitize', 'KeyService', 'Features', 'CookieService', 'Config', function(UtilService, $sanitize, KeyService, Features, CookieService, Config) { var triggerService = {}; var triggerTypes = { 'github': { 'description': function(config) { var source = UtilService.textToSafeHtml(config['build_source']); var desc = ' Push to Github Repository '; desc += '' + source + ''; desc += '
Dockerfile folder: //' + UtilService.textToSafeHtml(config['subdir']); return desc; }, 'run_parameters': [ { 'title': 'Branch', 'type': 'option', 'name': 'branch_name' } ], 'get_redirect_url': function(namespace, repository) { var redirect_uri = KeyService['githubRedirectUri'] + '/trigger/' + namespace + '/' + repository; // TODO(jschorr): Remove once the new layout is in place. if (CookieService.get('quay.exp-new-layout') == 'true') { redirect_uri += '/__new'; } var authorize_url = KeyService['githubTriggerAuthorizeUrl']; var client_id = KeyService['githubTriggerClientId']; return authorize_url + 'client_id=' + client_id + '&scope=repo,user:email&redirect_uri=' + redirect_uri; }, 'is_external': true, 'is_enabled': function() { return Features.GITHUB_BUILD; }, 'icon': 'fa-github', 'title': function() { var isEnterprise = KeyService.isEnterprise('github-trigger'); if (isEnterprise) { return 'GitHub Enterprise Repository Push'; } return 'GitHub Repository Push'; }, 'supports_full_directory_listing': true, 'templates': { 'credentials': '/static/directives/trigger/githost/credentials.html', 'trigger-description': '/static/directives/trigger/github/trigger-description.html' } }, 'bitbucket': { 'description': function(config) { var source = UtilService.textToSafeHtml(config['build_source']); var desc = ' Push to Bitbucket Repository '; desc += '' + source + ''; desc += '
Dockerfile folder: //' + UtilService.textToSafeHtml(config['subdir']); return desc; }, 'run_parameters': [ { 'title': 'Branch', 'type': 'option', 'name': 'branch_name' } ], 'get_redirect_url': function(namespace, repository) { return Config.getUrl('/bitbucket/setup/' + namespace + '/' + repository); }, 'is_external': false, 'is_enabled': function() { return Features.BITBUCKET_BUILD; }, 'icon': 'fa-bitbucket', 'title': function() { return 'Bitbucket Repository Push'; }, 'supports_full_directory_listing': false, 'templates': { 'credentials': '/static/directives/trigger/githost/credentials.html', 'trigger-description': '/static/directives/trigger/bitbucket/trigger-description.html' } }, 'gitlab': { 'description': function(config) { var source = UtilService.textToSafeHtml(config['build_source']); var desc = ' Push to GitLab Repository '; desc += '' + source + ''; desc += '
Dockerfile folder: //' + UtilService.textToSafeHtml(config['subdir']); return desc; }, 'run_parameters': [ { 'title': 'Branch', 'type': 'option', 'name': 'branch_name' } ], 'get_redirect_url': function(namespace, repository) { var redirect_uri = KeyService['gitlabRedirectUri'] + '/trigger'; var authorize_url = KeyService['gitlabTriggerAuthorizeUrl']; var client_id = KeyService['gitlabTriggerClientId']; return authorize_url + '?client_id=' + client_id + '&redirect_uri=' + redirect_uri + '&response_type=code&state=repo:' + namespace + '/' + repository; }, 'is_external': false, 'is_enabled': function() { return Features.GITLAB_BUILD; }, 'icon': 'fa-git-square', 'title': function() { return 'GitLab Repository Push'; }, 'supports_full_directory_listing': false, 'templates': { 'credentials': '/static/directives/trigger/githost/credentials.html', 'trigger-description': '/static/directives/trigger/gitlab/trigger-description.html' } }, 'custom-git': { 'description': function(config) { var source = UtilService.textToSafeHtml(config['build_source']); var desc = ' Push to Custom Git Repository ' + source; desc += '
Dockerfile folder: //' + UtilService.textToSafeHtml(config['subdir']); return desc; }, 'run_parameters': [ { 'title': 'Commit', 'type': 'regex', 'name': 'commit_sha', 'regex': '^([A-Fa-f0-9]{7})$', 'placeholder': '1c002dd' } ], 'get_redirect_url': function(namespace, repository) { return Config.getUrl('/customtrigger/setup/' + namespace + '/' + repository); }, 'is_external': false, 'is_enabled': function() { return true; }, 'icon': 'fa-git', 'title': function() { return 'Custom Git Repository Push'; }, 'templates': { 'credentials': '/static/directives/trigger/custom-git/credentials.html', 'trigger-description': '/static/directives/trigger/custom-git/trigger-description.html' } } }; triggerService.populateTemplate = function(scope, name) { scope.$watch('trigger', function(trigger) { if (!trigger) { return; } scope.triggerTemplate = triggerService.getTemplate(trigger.service, name); }); }; triggerService.supportsFullListing = function(name) { var type = triggerTypes[name]; if (!type) { return false; } return !!type['supports_full_directory_listing']; }; triggerService.getTypes = function() { var types = []; for (var key in triggerTypes) { if (!triggerTypes.hasOwnProperty(key)) { continue; } types.push(key); } return types; }; triggerService.getTemplate = function(name, template) { var type = triggerTypes[name]; if (!type) { return ''; } return type['templates'][template]; }; triggerService.getRedirectUrl = function(name, namespace, repository) { var type = triggerTypes[name]; if (!type) { return ''; } return type['get_redirect_url'](namespace, repository); }; triggerService.getDockerfileLocation = function(trigger) { if (!trigger.config.subdir) { return '//Dockerfile'; } return '//' + trigger.config.subdir.replace(new RegExp('(^\/+|\/+$)'), '') + '/Dockerfile'; }; triggerService.isEnabled = function(name) { var type = triggerTypes[name]; if (!type) { return false; } return type['is_enabled'](); }; triggerService.getTitle = function(name) { var type = triggerTypes[name]; if (!type) { return 'Unknown'; } return type['title'](); }; triggerService.getDescription = function(name, config) { var type = triggerTypes[name]; if (!type) { return 'Unknown'; } return type['description'](config); }; triggerService.getMetadata = function(name) { return triggerTypes[name]; }; triggerService.getRunParameters = function(name, config) { var type = triggerTypes[name]; if (!type) { return []; } return type['run_parameters']; } return triggerService; }]);