2015-05-02 21:54:48 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from flask import Blueprint, request, redirect, url_for
|
2016-09-29 00:17:14 +00:00
|
|
|
from flask_login import current_user
|
|
|
|
|
|
|
|
import features
|
2015-05-02 21:54:48 +00:00
|
|
|
|
|
|
|
from app import app, gitlab_trigger
|
|
|
|
from auth.permissions import AdministerRepositoryPermission
|
2016-09-29 19:24:57 +00:00
|
|
|
from auth.process import require_session_login
|
2015-05-02 21:54:48 +00:00
|
|
|
from data import model
|
|
|
|
from endpoints.common import route_show_if
|
|
|
|
from util.http import abort
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
client = app.config['HTTPCLIENT']
|
|
|
|
gitlabtrigger = Blueprint('gitlab', __name__)
|
|
|
|
|
2016-09-29 00:17:14 +00:00
|
|
|
|
2015-05-02 21:54:48 +00:00
|
|
|
@gitlabtrigger.route('/gitlab/callback/trigger', methods=['GET'])
|
|
|
|
@route_show_if(features.GITLAB_BUILD)
|
|
|
|
@require_session_login
|
|
|
|
def attach_gitlab_build_trigger():
|
|
|
|
state = request.args.get('state', None)
|
|
|
|
if not state:
|
|
|
|
abort(400)
|
|
|
|
state = state[len('repo:'):]
|
|
|
|
try:
|
|
|
|
[namespace, repository] = state.split('/')
|
|
|
|
except ValueError:
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
|
|
|
code = request.args.get('code')
|
2015-05-03 16:58:48 +00:00
|
|
|
token = gitlab_trigger.exchange_code_for_token(app.config, client, code,
|
|
|
|
redirect_suffix='/trigger')
|
|
|
|
if not token:
|
|
|
|
msg = 'Could not exchange token. It may have expired.'
|
|
|
|
abort(404, message=msg)
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
repo = model.repository.get_repository(namespace, repository)
|
2015-05-02 21:54:48 +00:00
|
|
|
if not repo:
|
|
|
|
msg = 'Invalid repository: %s/%s' % (namespace, repository)
|
|
|
|
abort(404, message=msg)
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
trigger = model.build.create_build_trigger(repo, 'gitlab', token, current_user.db_user())
|
2015-05-02 21:54:48 +00:00
|
|
|
repo_path = '%s/%s' % (namespace, repository)
|
2016-09-27 14:52:34 +00:00
|
|
|
full_url = url_for('web.buildtrigger', path=repo_path, trigger=trigger.uuid)
|
2015-05-02 21:54:48 +00:00
|
|
|
|
|
|
|
logger.debug('Redirecting to full url: %s', full_url)
|
|
|
|
return redirect(full_url)
|
|
|
|
|
|
|
|
abort(403)
|