This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/endpoints/gitlabtrigger.py

56 lines
1.8 KiB
Python
Raw Normal View History

2015-05-02 21:54:48 +00:00
import logging
from flask import Blueprint, request, redirect, url_for
from flask.ext.login import current_user
from app import app, gitlab_trigger
from auth.auth import require_session_login
from auth.permissions import AdministerRepositoryPermission
from data import model
from endpoints.common import route_show_if
from util.http import abort
from util.names import parse_repository_name
import features
logger = logging.getLogger(__name__)
client = app.config['HTTPCLIENT']
gitlabtrigger = Blueprint('gitlab', __name__)
@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')
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-05-02 21:54:48 +00:00
repo = model.get_repository(namespace, repository)
if not repo:
msg = 'Invalid repository: %s/%s' % (namespace, repository)
abort(404, message=msg)
trigger = model.create_build_trigger(repo, 'gitlab', token, current_user.db_user())
repo_path = '%s/%s' % (namespace, repository)
full_url = '%s%s%s' % (url_for('web.repository', path=repo_path), '?tab=builds&newtrigger=', trigger.uuid)
logger.debug('Redirecting to full url: %s', full_url)
return redirect(full_url)
abort(403)