2015-04-24 19:13:08 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from flask import request, redirect, url_for, Blueprint
|
2016-09-29 00:17:14 +00:00
|
|
|
from flask_login import current_user
|
2015-04-24 19:13:08 +00:00
|
|
|
|
2016-03-09 21:20:28 +00:00
|
|
|
import features
|
|
|
|
|
2015-04-24 19:13:08 +00:00
|
|
|
from app import app, github_trigger
|
2016-03-09 21:20:28 +00:00
|
|
|
from auth.permissions import AdministerRepositoryPermission
|
2016-09-29 19:24:57 +00:00
|
|
|
from auth.process import require_session_login
|
2015-04-24 19:13:08 +00:00
|
|
|
from data import model
|
2016-03-09 21:20:28 +00:00
|
|
|
from endpoints.common import route_show_if, parse_repository_name
|
2015-04-24 19:13:08 +00:00
|
|
|
from util.http import abort
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
client = app.config['HTTPCLIENT']
|
|
|
|
githubtrigger = Blueprint('callback', __name__)
|
|
|
|
|
2016-03-09 21:20:28 +00:00
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@githubtrigger.route('/github/callback/trigger/<repopath:repository>', methods=['GET'])
|
2015-04-24 19:13:08 +00:00
|
|
|
@route_show_if(features.GITHUB_BUILD)
|
|
|
|
@require_session_login
|
2016-03-09 21:20:28 +00:00
|
|
|
@parse_repository_name()
|
|
|
|
def attach_github_build_trigger(namespace_name, repo_name):
|
|
|
|
permission = AdministerRepositoryPermission(namespace_name, repo_name)
|
2015-04-24 19:13:08 +00:00
|
|
|
if permission.can():
|
|
|
|
code = request.args.get('code')
|
|
|
|
token = github_trigger.exchange_code_for_token(app.config, client, code)
|
2016-03-09 21:20:28 +00:00
|
|
|
repo = model.repository.get_repository(namespace_name, repo_name)
|
2015-04-24 19:13:08 +00:00
|
|
|
if not repo:
|
2016-03-09 21:20:28 +00:00
|
|
|
msg = 'Invalid repository: %s/%s' % (namespace_name, repo_name)
|
2015-04-24 19:13:08 +00:00
|
|
|
abort(404, message=msg)
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
trigger = model.build.create_build_trigger(repo, 'github', token, current_user.db_user())
|
2016-03-09 21:20:28 +00:00
|
|
|
repo_path = '%s/%s' % (namespace_name, repo_name)
|
2016-09-27 14:52:34 +00:00
|
|
|
full_url = url_for('web.buildtrigger', path=repo_path, trigger=trigger.uuid)
|
2015-04-24 19:13:08 +00:00
|
|
|
|
|
|
|
logger.debug('Redirecting to full url: %s', full_url)
|
|
|
|
return redirect(full_url)
|
|
|
|
|
2016-03-09 21:20:28 +00:00
|
|
|
abort(403)
|