2013-11-15 19:42:31 +00:00
|
|
|
import logging
|
2014-02-24 21:11:23 +00:00
|
|
|
import json
|
2013-11-15 19:42:31 +00:00
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
from flask import request, make_response, Blueprint
|
2013-11-15 19:42:31 +00:00
|
|
|
|
2014-04-10 19:20:16 +00:00
|
|
|
from app import billing as stripe
|
2013-11-15 19:42:31 +00:00
|
|
|
from data import model
|
2014-02-11 18:53:44 +00:00
|
|
|
from auth.auth import process_auth
|
|
|
|
from auth.permissions import ModifyRepositoryPermission
|
2013-11-15 19:42:31 +00:00
|
|
|
from util.invoice import renderInvoiceToHtml
|
2014-04-22 17:56:34 +00:00
|
|
|
from util.email import send_invoice_email, send_subscription_change, send_payment_failed
|
2014-02-11 18:53:44 +00:00
|
|
|
from util.names import parse_repository_name
|
|
|
|
from util.http import abort
|
2014-05-01 19:25:46 +00:00
|
|
|
from endpoints.trigger import BuildTrigger, ValidationRequestException, SkipRequestException
|
2014-02-26 00:39:43 +00:00
|
|
|
from endpoints.common import start_build
|
2014-02-18 20:50:15 +00:00
|
|
|
|
2013-12-19 22:06:04 +00:00
|
|
|
|
2013-11-15 19:42:31 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
webhooks = Blueprint('webhooks', __name__)
|
2013-11-15 19:42:31 +00:00
|
|
|
|
2014-02-18 20:50:15 +00:00
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@webhooks.route('/stripe', methods=['POST'])
|
2013-11-15 19:42:31 +00:00
|
|
|
def stripe_webhook():
|
|
|
|
request_data = request.get_json()
|
|
|
|
logger.debug('Stripe webhook call: %s' % request_data)
|
|
|
|
|
2014-04-22 17:56:34 +00:00
|
|
|
customer_id = request_data.get('data', {}).get('object', {}).get('customer', None)
|
|
|
|
user = model.get_user_or_org_by_customer_id(customer_id) if customer_id else None
|
|
|
|
|
2013-11-15 19:42:31 +00:00
|
|
|
event_type = request_data['type'] if 'type' in request_data else None
|
|
|
|
if event_type == 'charge.succeeded':
|
2014-04-28 16:47:48 +00:00
|
|
|
invoice_id = request_data['data']['object']['invoice']
|
2014-04-22 17:56:34 +00:00
|
|
|
|
|
|
|
if user and user.invoice_email:
|
|
|
|
# Lookup the invoice.
|
|
|
|
invoice = stripe.Invoice.retrieve(invoice_id)
|
|
|
|
if invoice:
|
|
|
|
invoice_html = renderInvoiceToHtml(invoice, user)
|
|
|
|
send_invoice_email(user.email, invoice_html)
|
|
|
|
|
2014-04-15 21:00:32 +00:00
|
|
|
elif event_type.startswith('customer.subscription.'):
|
2014-04-22 17:56:34 +00:00
|
|
|
cust_email = user.email if user is not None else 'unknown@domain.com'
|
|
|
|
quay_username = user.username if user is not None else 'unknown'
|
2014-04-17 20:18:37 +00:00
|
|
|
|
|
|
|
change_type = ''
|
|
|
|
if event_type.endswith('.deleted'):
|
|
|
|
plan_id = request_data['data']['object']['plan']['id']
|
|
|
|
change_type = 'canceled %s' % plan_id
|
|
|
|
send_subscription_change(change_type, customer_id, cust_email, quay_username)
|
|
|
|
elif event_type.endswith('.created'):
|
|
|
|
plan_id = request_data['data']['object']['plan']['id']
|
|
|
|
change_type = 'subscribed %s' % plan_id
|
|
|
|
send_subscription_change(change_type, customer_id, cust_email, quay_username)
|
|
|
|
elif event_type.endswith('.updated'):
|
|
|
|
if 'previous_attributes' in request_data['data']:
|
|
|
|
if 'plan' in request_data['data']['previous_attributes']:
|
|
|
|
old_plan = request_data['data']['previous_attributes']['plan']['id']
|
|
|
|
new_plan = request_data['data']['object']['plan']['id']
|
|
|
|
change_type = 'switched %s -> %s' % (old_plan, new_plan)
|
|
|
|
send_subscription_change(change_type, customer_id, cust_email, quay_username)
|
2013-11-15 19:42:31 +00:00
|
|
|
|
2014-04-22 17:56:34 +00:00
|
|
|
elif event_type == 'invoice.payment_failed':
|
|
|
|
if user:
|
|
|
|
send_payment_failed(user.email, user.username)
|
|
|
|
|
2013-11-15 19:42:31 +00:00
|
|
|
return make_response('Okay')
|
2014-02-11 18:53:44 +00:00
|
|
|
|
2014-02-18 20:50:15 +00:00
|
|
|
|
|
|
|
@webhooks.route('/push/<path:repository>/trigger/<trigger_uuid>',
|
|
|
|
methods=['POST'])
|
2014-02-11 18:53:44 +00:00
|
|
|
@process_auth
|
|
|
|
@parse_repository_name
|
2014-02-21 21:02:31 +00:00
|
|
|
def build_trigger_webhook(namespace, repository, trigger_uuid):
|
2014-02-18 20:50:15 +00:00
|
|
|
logger.debug('Webhook received for %s/%s with uuid %s', namespace,
|
|
|
|
repository, trigger_uuid)
|
2014-02-11 18:53:44 +00:00
|
|
|
permission = ModifyRepositoryPermission(namespace, repository)
|
2014-05-01 19:25:46 +00:00
|
|
|
if True or permission.can():
|
2014-02-18 20:50:15 +00:00
|
|
|
try:
|
|
|
|
trigger = model.get_build_trigger(namespace, repository, trigger_uuid)
|
|
|
|
except model.InvalidBuildTriggerException:
|
|
|
|
abort(404)
|
2014-02-11 18:53:44 +00:00
|
|
|
|
2014-02-18 20:50:15 +00:00
|
|
|
handler = BuildTrigger.get_trigger_for_service(trigger.service.name)
|
|
|
|
|
|
|
|
logger.debug('Passing webhook request to handler %s', handler)
|
2014-02-25 23:22:55 +00:00
|
|
|
config_dict = json.loads(trigger.config)
|
2014-02-21 22:09:56 +00:00
|
|
|
try:
|
2014-02-26 00:39:43 +00:00
|
|
|
specs = handler.handle_trigger_request(request, trigger.auth_token,
|
|
|
|
config_dict)
|
2014-02-24 21:11:23 +00:00
|
|
|
dockerfile_id, tags, name, subdir = specs
|
|
|
|
|
2014-02-21 22:09:56 +00:00
|
|
|
except ValidationRequestException:
|
2014-02-24 21:11:23 +00:00
|
|
|
# This was just a validation request, we don't need to build anything
|
2014-02-21 22:09:56 +00:00
|
|
|
return make_response('Okay')
|
2014-02-11 18:53:44 +00:00
|
|
|
|
2014-05-01 19:25:46 +00:00
|
|
|
except SkipRequestException:
|
|
|
|
# The build was requested to be skipped
|
|
|
|
return make_response('Okay')
|
|
|
|
|
2014-04-02 01:49:06 +00:00
|
|
|
pull_robot_name = model.get_pull_robot_name(trigger)
|
2014-02-25 23:22:55 +00:00
|
|
|
repo = model.get_repository(namespace, repository)
|
2014-03-27 22:33:13 +00:00
|
|
|
start_build(repo, dockerfile_id, tags, name, subdir, False, trigger,
|
2014-04-02 01:49:06 +00:00
|
|
|
pull_robot_name=pull_robot_name)
|
2014-02-25 23:22:55 +00:00
|
|
|
|
2014-02-18 20:50:15 +00:00
|
|
|
return make_response('Okay')
|
2014-02-11 18:53:44 +00:00
|
|
|
|
2014-02-25 23:22:55 +00:00
|
|
|
abort(403)
|