UI and code improvements to make working with the multiple SCMs easier
This commit is contained in:
parent
f091aaa07e
commit
d07f9f04e9
10 changed files with 70 additions and 114 deletions
|
@ -120,12 +120,6 @@ class BuildTriggerHandler(object):
|
|||
""" Returns the auth token for the trigger. """
|
||||
return self.trigger.auth_token
|
||||
|
||||
def dockerfile_url(self):
|
||||
"""
|
||||
Returns the URL at which the Dockerfile for the trigger is found or None if none/not applicable.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def load_dockerfile_contents(self):
|
||||
"""
|
||||
Loads the Dockerfile found for the trigger's config and returns them or None if none could
|
||||
|
@ -188,6 +182,11 @@ class BuildTriggerHandler(object):
|
|||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def get_repository_url(self):
|
||||
""" Returns the URL of the current trigger's repository. Note that this operation
|
||||
can be called in a loop, so it should be as fast as possible. """
|
||||
raise NotImplementedError
|
||||
|
||||
@classmethod
|
||||
def service_name(cls):
|
||||
"""
|
||||
|
@ -212,6 +211,18 @@ class BuildTriggerHandler(object):
|
|||
""" Sets the auth token for the trigger, saving it to the DB. """
|
||||
model.update_build_trigger(self.trigger, self.config, auth_token=auth_token)
|
||||
|
||||
def get_dockerfile_path(self):
|
||||
""" Returns the normalized path to the Dockerfile found in the subdirectory
|
||||
in the config. """
|
||||
subdirectory = self.config.get('subdir', '')
|
||||
if subdirectory == '/':
|
||||
subdirectory = ''
|
||||
else:
|
||||
if not subdirectory.endswith('/'):
|
||||
subdirectory = subdirectory + '/'
|
||||
|
||||
return subdirectory + 'Dockerfile'
|
||||
|
||||
|
||||
class BitbucketBuildTrigger(BuildTriggerHandler):
|
||||
"""
|
||||
|
@ -371,24 +382,9 @@ class BitbucketBuildTrigger(BuildTriggerHandler):
|
|||
|
||||
return []
|
||||
|
||||
def dockerfile_url(self):
|
||||
repository = self._get_repository_client()
|
||||
subdirectory = self.config.get('subdir', '')
|
||||
path = subdirectory + '/Dockerfile' if subdirectory else 'Dockerfile'
|
||||
|
||||
master_branch = 'master'
|
||||
(result, data, _) = repository.get_main_branch()
|
||||
if result:
|
||||
master_branch = data['name']
|
||||
|
||||
return 'https://bitbucket.org/%s/%s/src/%s/%s' % (repository.namespace,
|
||||
repository.repository_name,
|
||||
master_branch, path)
|
||||
|
||||
def load_dockerfile_contents(self):
|
||||
repository = self._get_repository_client()
|
||||
subdirectory = self.config.get('subdir', '/')[1:]
|
||||
path = subdirectory + '/Dockerfile' if subdirectory else 'Dockerfile'
|
||||
path = self.get_dockerfile_path()
|
||||
|
||||
(result, data, err_msg) = repository.get_raw_path_contents(path, revision='master')
|
||||
if not result:
|
||||
|
@ -539,6 +535,11 @@ class BitbucketBuildTrigger(BuildTriggerHandler):
|
|||
|
||||
return self._prepare_build(commit_sha, ref, True)
|
||||
|
||||
def get_repository_url(self):
|
||||
source = self.config['build_source']
|
||||
(namespace, name) = source.split('/')
|
||||
return 'https://bitbucket.org/%s/%s' % (namespace, name)
|
||||
|
||||
|
||||
class GithubBuildTrigger(BuildTriggerHandler):
|
||||
"""
|
||||
|
@ -696,30 +697,13 @@ class GithubBuildTrigger(BuildTriggerHandler):
|
|||
|
||||
raise RepositoryReadException(message)
|
||||
|
||||
def dockerfile_url(self):
|
||||
config = self.config
|
||||
|
||||
source = config['build_source']
|
||||
subdirectory = config.get('subdir', '')
|
||||
path = subdirectory + '/Dockerfile' if subdirectory else 'Dockerfile'
|
||||
gh_client = self._get_client()
|
||||
|
||||
try:
|
||||
repo = gh_client.get_repo(source)
|
||||
master_branch = repo.default_branch or 'master'
|
||||
return 'https://github.com/%s/blob/%s/%s' % (source, master_branch, path)
|
||||
except GithubException:
|
||||
logger.exception('Could not load repository for Dockerfile.')
|
||||
return None
|
||||
|
||||
def load_dockerfile_contents(self):
|
||||
config = self.config
|
||||
gh_client = self._get_client()
|
||||
|
||||
source = config['build_source']
|
||||
subdirectory = config.get('subdir', '')
|
||||
path = subdirectory + '/Dockerfile' if subdirectory else 'Dockerfile'
|
||||
|
||||
path = self.get_dockerfile_path()
|
||||
try:
|
||||
repo = gh_client.get_repo(source)
|
||||
file_info = repo.get_file_contents(path)
|
||||
|
@ -922,6 +906,12 @@ class GithubBuildTrigger(BuildTriggerHandler):
|
|||
|
||||
return None
|
||||
|
||||
def get_repository_url(self):
|
||||
from app import github_trigger
|
||||
source = self.config['build_source']
|
||||
return github_trigger.get_public_url(source)
|
||||
|
||||
|
||||
class CustomBuildTrigger(BuildTriggerHandler):
|
||||
payload_schema = {
|
||||
'type': 'object',
|
||||
|
@ -1084,6 +1074,9 @@ class CustomBuildTrigger(BuildTriggerHandler):
|
|||
self.config = config
|
||||
return config
|
||||
|
||||
def get_repository_url(self):
|
||||
return None
|
||||
|
||||
|
||||
class GitLabBuildTrigger(BuildTriggerHandler):
|
||||
"""
|
||||
|
@ -1221,35 +1214,9 @@ class GitLabBuildTrigger(BuildTriggerHandler):
|
|||
|
||||
return []
|
||||
|
||||
def dockerfile_url(self):
|
||||
gl_client = self._get_authorized_client()
|
||||
subdir = self.config.get('subdir', '')
|
||||
path = subdir + '/Dockerfile' if subdir else 'Dockerfile'
|
||||
|
||||
repository = gl_client.getproject(self.config['build_source'])
|
||||
if repository is False:
|
||||
return None
|
||||
|
||||
branches = self.list_field_values('branch_name')
|
||||
branches = find_matching_branches(self.config, branches)
|
||||
if branches == []:
|
||||
return None
|
||||
branch_name = branches[0]
|
||||
if repository['default_branch'] in branches:
|
||||
branch_name = repository['default_branch']
|
||||
|
||||
return '%s/%s/blob/%s/%s' % (gl_client.host,
|
||||
repository['path_with_namespace'],
|
||||
branch_name,
|
||||
path)
|
||||
|
||||
def load_dockerfile_contents(self):
|
||||
gl_client = self._get_authorized_client()
|
||||
subdir = self.config.get('subdir', '')
|
||||
if subdir == '/':
|
||||
subdir = ''
|
||||
|
||||
path = subdir + 'Dockerfile' if subdir else 'Dockerfile'
|
||||
path = self.get_dockerfile_path()
|
||||
|
||||
repository = gl_client.getproject(self.config['build_source'])
|
||||
if repository is False:
|
||||
|
@ -1394,3 +1361,12 @@ class GitLabBuildTrigger(BuildTriggerHandler):
|
|||
ref = 'refs/heads/%s' % branch_name
|
||||
|
||||
return self._prepare_build(commit, ref, True)
|
||||
|
||||
def get_repository_url(self):
|
||||
gl_client = self._get_authorized_client()
|
||||
repository = gl_client.getproject(self.config['build_source'])
|
||||
if repository is False:
|
||||
return None
|
||||
|
||||
return '%s/%s' % (gl_client.host, repository['path_with_namespace'])
|
||||
|
||||
|
|
Reference in a new issue