Pass trigger information on build status. Set up a trigger for the sample building repository. Allow to list the builds started from a trigger. Protect the callback with the proper auth for creating a trigger on a repo.
This commit is contained in:
parent
f60f9eb62a
commit
9e426816a5
7 changed files with 94 additions and 43 deletions
|
@ -1110,7 +1110,7 @@ def get_repo(namespace, repository):
|
|||
'can_write': can_write,
|
||||
'can_admin': can_admin,
|
||||
'is_public': is_public,
|
||||
'is_building': len(active_builds) > 0,
|
||||
'is_building': len(list(active_builds)) > 0,
|
||||
'is_organization': bool(organization)
|
||||
})
|
||||
|
||||
|
@ -1118,6 +1118,18 @@ def get_repo(namespace, repository):
|
|||
abort(403) # Permission denied
|
||||
|
||||
|
||||
def trigger_view(trigger):
|
||||
if trigger and trigger.uuid:
|
||||
return {
|
||||
'service': trigger.service.name,
|
||||
'config': json.loads(trigger.config),
|
||||
'id': trigger.uuid,
|
||||
'connected_user': trigger.connected_user.username,
|
||||
}
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def build_status_view(build_obj, can_write=False):
|
||||
status = build_logs.get_status(build_obj.uuid)
|
||||
return {
|
||||
|
@ -1127,7 +1139,8 @@ def build_status_view(build_obj, can_write=False):
|
|||
'display_name': build_obj.display_name,
|
||||
'status': status,
|
||||
'resource_key': build_obj.resource_key if can_write else None,
|
||||
'is_writer': can_write
|
||||
'is_writer': can_write,
|
||||
'trigger': trigger_view(build_obj.trigger),
|
||||
}
|
||||
|
||||
|
||||
|
@ -1328,15 +1341,6 @@ def delete_webhook(namespace, repository, public_id):
|
|||
abort(403) # Permission denied
|
||||
|
||||
|
||||
def trigger_view(trigger):
|
||||
return {
|
||||
'service': trigger.service.name,
|
||||
'config': json.loads(trigger.config),
|
||||
'id': trigger.uuid,
|
||||
'connected_user': trigger.connected_user.username,
|
||||
}
|
||||
|
||||
|
||||
@api.route('/repository/<path:repository>/trigger/<trigger_uuid>',
|
||||
methods=['GET'])
|
||||
@api_login_required
|
||||
|
@ -1354,6 +1358,22 @@ def get_build_trigger(namespace, repository, trigger_uuid):
|
|||
abort(403) # Permission denied
|
||||
|
||||
|
||||
@api.route('/repository/<path:repository>/trigger/<trigger_uuid>/builds',
|
||||
methods=['GET'])
|
||||
@api_login_required
|
||||
@parse_repository_name
|
||||
def list_trigger_recent_builds(namespace, repository, trigger_uuid):
|
||||
permission = AdministerRepositoryPermission(namespace, repository)
|
||||
if permission.can():
|
||||
limit = request.args.get('limit', 5)
|
||||
builds = model.list_trigger_builds(namespace, repository, trigger_uuid,
|
||||
limit)
|
||||
return jsonify({
|
||||
'builds': [build_status_view(build, True) for build in builds]
|
||||
})
|
||||
|
||||
abort(403) # Permission denied
|
||||
|
||||
@api.route('/repository/<path:repository>/trigger/', methods=['GET'])
|
||||
@api_login_required
|
||||
@parse_repository_name
|
||||
|
|
|
@ -7,6 +7,8 @@ from endpoints.common import render_page_template, common_login
|
|||
from app import app, mixpanel
|
||||
from data import model
|
||||
from util.names import parse_repository_name
|
||||
from util.http import abort
|
||||
from auth.permissions import AdministerRepositoryPermission
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
@ -113,10 +115,18 @@ def github_oauth_attach():
|
|||
@login_required
|
||||
@parse_repository_name
|
||||
def attach_github_build_trigger(namespace, repository):
|
||||
token = exchange_github_code_for_token(request.args.get('code'))
|
||||
model.create_build_trigger(namespace, repository, 'github', token,
|
||||
current_user.db_user())
|
||||
admin_path = '%s/%s/%s' % (namespace, repository, 'admin')
|
||||
full_url = url_for('web.repository', path=admin_path) + '?tab=trigger'
|
||||
logger.debug('Redirecting to full url: %s' % full_url)
|
||||
return redirect(full_url)
|
||||
permission = AdministerRepositoryPermission(namespace, repository)
|
||||
if permission.can():
|
||||
token = exchange_github_code_for_token(request.args.get('code'))
|
||||
repo = model.get_repository(namespace, repository)
|
||||
if not repo:
|
||||
msg = 'Invalid repository: %s/%s' % (namespace, repository)
|
||||
abort(404, message=msg)
|
||||
|
||||
model.create_build_trigger(repo, 'github', token, current_user.db_user())
|
||||
admin_path = '%s/%s/%s' % (namespace, repository, 'admin')
|
||||
full_url = url_for('web.repository', path=admin_path) + '?tab=trigger'
|
||||
logger.debug('Redirecting to full url: %s' % full_url)
|
||||
return redirect(full_url)
|
||||
|
||||
abort(403)
|
|
@ -30,10 +30,10 @@ class BuildTrigger(object):
|
|||
def __init__(self):
|
||||
pass
|
||||
|
||||
def list_repositories(self, auth_token):
|
||||
def list_build_sources(self, auth_token):
|
||||
"""
|
||||
Take the auth information for the specific trigger type and load the
|
||||
list of repositories.
|
||||
list of build sources(repositories).
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
|
@ -73,7 +73,7 @@ class GithubBuildTrigger(BuildTrigger):
|
|||
def service_name(cls):
|
||||
return 'github'
|
||||
|
||||
def list_repositories(self, auth_token):
|
||||
def list_build_sources(self, auth_token):
|
||||
gh_client = self._get_client(auth_token)
|
||||
usr = gh_client.get_user()
|
||||
|
||||
|
|
Reference in a new issue