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-09-29 00:17:14 +00:00
|
|
|
from app import app
|
2017-03-16 20:50:09 +00:00
|
|
|
from auth.decorators import require_session_login
|
2015-09-11 21:40:32 +00:00
|
|
|
from buildtrigger.basehandler import BuildTriggerHandler
|
|
|
|
from buildtrigger.bitbuckethandler import BitbucketBuildTrigger
|
2015-04-24 19:13:08 +00:00
|
|
|
from data import model
|
2016-09-29 00:17:14 +00:00
|
|
|
from endpoints.common import route_show_if
|
2015-04-24 19:13:08 +00:00
|
|
|
from util.http import abort
|
|
|
|
|
|
|
|
import features
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
client = app.config['HTTPCLIENT']
|
|
|
|
bitbuckettrigger = Blueprint('bitbuckettrigger', __name__)
|
|
|
|
|
|
|
|
|
|
|
|
@bitbuckettrigger.route('/bitbucket/callback/trigger/<trigger_uuid>', methods=['GET'])
|
|
|
|
@route_show_if(features.BITBUCKET_BUILD)
|
|
|
|
@require_session_login
|
|
|
|
def attach_bitbucket_build_trigger(trigger_uuid):
|
2015-07-15 21:25:41 +00:00
|
|
|
trigger = model.build.get_build_trigger(trigger_uuid)
|
2015-04-24 19:13:08 +00:00
|
|
|
if not trigger or trigger.service.name != BitbucketBuildTrigger.service_name():
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
if trigger.connected_user != current_user.db_user():
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
verifier = request.args.get('oauth_verifier')
|
2015-04-24 22:36:48 +00:00
|
|
|
handler = BuildTriggerHandler.get_handler(trigger)
|
|
|
|
result = handler.exchange_verifier(verifier)
|
|
|
|
if not result:
|
|
|
|
trigger.delete_instance()
|
|
|
|
return 'Token has expired'
|
|
|
|
|
|
|
|
namespace = trigger.repository.namespace_user.username
|
|
|
|
repository = trigger.repository.name
|
2015-04-24 19:13:08 +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-04-24 19:13:08 +00:00
|
|
|
|
|
|
|
logger.debug('Redirecting to full url: %s', full_url)
|
2016-09-29 00:17:14 +00:00
|
|
|
return redirect(full_url)
|