51 lines
No EOL
1.9 KiB
Python
51 lines
No EOL
1.9 KiB
Python
import logging
|
|
|
|
from flask import request, redirect, url_for, Blueprint
|
|
from flask.ext.login import current_user
|
|
|
|
from endpoints.common import route_show_if
|
|
from app import app, github_trigger
|
|
from data import model
|
|
from util.names import parse_repository_name
|
|
from util.http import abort
|
|
from auth.permissions import AdministerRepositoryPermission
|
|
from auth.auth import require_session_login
|
|
|
|
import features
|
|
|
|
logger = logging.getLogger(__name__)
|
|
client = app.config['HTTPCLIENT']
|
|
githubtrigger = Blueprint('callback', __name__)
|
|
|
|
@githubtrigger.route('/github/callback/trigger/<path:repository>', methods=['GET'])
|
|
@githubtrigger.route('/github/callback/trigger/<path:repository>/__new', methods=['GET'])
|
|
@route_show_if(features.GITHUB_BUILD)
|
|
@require_session_login
|
|
@parse_repository_name
|
|
def attach_github_build_trigger(namespace, repository):
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
if permission.can():
|
|
code = request.args.get('code')
|
|
token = github_trigger.exchange_code_for_token(app.config, client, code)
|
|
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, 'github', token, current_user.db_user())
|
|
|
|
# TODO(jschorr): Remove once the new layout is in place.
|
|
admin_path = '%s/%s/%s' % (namespace, repository, 'admin')
|
|
full_url = '%s%s%s' % (url_for('web.repository', path=admin_path), '?tab=trigger&new_trigger=',
|
|
trigger.uuid)
|
|
|
|
if '__new' in request.url:
|
|
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) |