endpoints.trigger: address gitlab PR comments

This commit is contained in:
Jimmy Zelinskie 2015-05-01 23:35:54 -04:00
parent eb3d88bd92
commit d21fbb1204

View file

@ -1120,11 +1120,12 @@ class GitLabBuildTrigger(BuildTriggerHandler):
'value': public_key, 'value': public_key,
}, },
] ]
success = gl_client.adddeploykey(repository['id'], '%s Builder' % app.config['REGISTRY_TITLE'], key = gl_client.adddeploykey(repository['id'], '%s Builder' % app.config['REGISTRY_TITLE'],
public_key) public_key)
if success is False: if key is False:
msg = 'Unable to add deploy key to repository: %s' % new_build_source msg = 'Unable to add deploy key to repository: %s' % new_build_source
raise TriggerActivationException(msg) raise TriggerActivationException(msg)
config['key_id'] = key['id']
# Add the webhook to the GitLab repository. # Add the webhook to the GitLab repository.
hook = gl_client.addprojecthook(repository['id'], standard_webhook_url, push=True) hook = gl_client.addprojecthook(repository['id'], standard_webhook_url, push=True)
@ -1151,8 +1152,15 @@ class GitLabBuildTrigger(BuildTriggerHandler):
if success is False: if success is False:
msg = 'Unable to remove hook: %s' % config['hook_id'] msg = 'Unable to remove hook: %s' % config['hook_id']
raise TriggerDeactivationException(msg) raise TriggerDeactivationException(msg)
config.pop('hook_id', None) config.pop('hook_id', None)
# Remove the key
success = gl_client.deletedeploykey(repository['id'], config['key_id'])
if success is False:
msg = 'Unable to remove deploy key: %s' % config['key_id']
raise TriggerDeactivationException(msg)
config.pop('key_id', None)
self.config = config self.config = config
return config return config
@ -1219,11 +1227,18 @@ class GitLabBuildTrigger(BuildTriggerHandler):
repository = gl_client.getproject(self.config['build_source']) repository = gl_client.getproject(self.config['build_source'])
if repository is False: if repository is False:
return None return None
branch = repository['default_branch']
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, return '%s/%s/blob/%s/%s' % (gl_client.host,
repository['path_with_namespace'], repository['path_with_namespace'],
branch, branch_name,
path) path)
def load_dockerfile_contents(self): def load_dockerfile_contents(self):
@ -1235,7 +1250,15 @@ class GitLabBuildTrigger(BuildTriggerHandler):
if repository is False: if repository is False:
return None return None
contents = gl_client.getrawfile(repository['id'], repository['default_branch'], path) 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']
contents = gl_client.getrawfile(repository['id'], branch_name, path)
if contents is False: if contents is False:
return None return None
return contents return contents
@ -1257,13 +1280,13 @@ class GitLabBuildTrigger(BuildTriggerHandler):
tags = gl_client.getall(gl_client.getrepositorytags(repo['id'])) tags = gl_client.getall(gl_client.getrepositorytags(repo['id']))
if tags is False: if tags is False:
return [] return []
return [tag.name for tag in tags] return [tag['name'] for tag in tags]
if field_name == 'branch_name': if field_name == 'branch_name':
branches = gl_client.getbranches(repo['id']) branches = gl_client.getbranches(repo['id'])
if branches is False: if branches is False:
return [] return []
return [branch.name for branch in branches] return [branch['name'] for branch in branches]
return None return None
@ -1291,7 +1314,7 @@ class GitLabBuildTrigger(BuildTriggerHandler):
'default_branch': repo['default_branch'], 'default_branch': repo['default_branch'],
'git_url': repo['ssh_url_to_repo'], 'git_url': repo['ssh_url_to_repo'],
'commit_info': { 'commit_info': {
'url': '', 'url': gl_client.host + '/' + repo['path_with_namespace'] + '/commit/' + commit['id'],
'message': commit['message'], 'message': commit['message'],
'date': commit['committed_date'], 'date': commit['committed_date'],
}, },
@ -1327,7 +1350,7 @@ class GitLabBuildTrigger(BuildTriggerHandler):
logger.debug('GitLab trigger payload %s', payload) logger.debug('GitLab trigger payload %s', payload)
if not payload.gets('commits'): if not payload.get('commits'):
raise SkipRequestException() raise SkipRequestException()
commit = payload['commits'][0] commit = payload['commits'][0]
@ -1356,7 +1379,7 @@ class GitLabBuildTrigger(BuildTriggerHandler):
commit = None commit = None
for branch in branches: for branch in branches:
if branch['name'] is branch_name: if branch['name'] == branch_name:
commit = branch['commit'] commit = branch['commit']
if commit is None: if commit is None:
raise TriggerStartException('Could not find commit') raise TriggerStartException('Could not find commit')