2013-11-15 19:42:31 +00:00
|
|
|
import logging
|
|
|
|
import stripe
|
2014-02-11 18:53:44 +00:00
|
|
|
import urlparse
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
from util.email import send_invoice_email
|
2014-02-11 18:53:44 +00:00
|
|
|
from util.names import parse_repository_name
|
|
|
|
from util.http import abort
|
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
|
|
|
|
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)
|
|
|
|
|
|
|
|
event_type = request_data['type'] if 'type' in request_data else None
|
|
|
|
if event_type == 'charge.succeeded':
|
|
|
|
data = request_data['data'] if 'data' in request_data else {}
|
|
|
|
obj = data['object'] if 'object' in data else {}
|
|
|
|
invoice_id = obj['invoice'] if 'invoice' in obj else None
|
|
|
|
customer_id = obj['customer'] if 'customer' in obj else None
|
|
|
|
|
|
|
|
if invoice_id and customer_id:
|
|
|
|
# Find the user associated with the customer ID.
|
|
|
|
user = model.get_user_or_org_by_customer_id(customer_id)
|
|
|
|
if user and user.invoice_email:
|
2013-12-19 22:06:04 +00:00
|
|
|
# Lookup the invoice.
|
|
|
|
invoice = stripe.Invoice.retrieve(invoice_id)
|
|
|
|
if invoice:
|
|
|
|
invoice_html = renderInvoiceToHtml(invoice, user)
|
|
|
|
send_invoice_email(user.email, invoice_html)
|
2013-11-15 19:42:31 +00:00
|
|
|
|
|
|
|
return make_response('Okay')
|
2014-02-11 18:53:44 +00:00
|
|
|
|
|
|
|
@webhooks.route('/github/push/repository/<path:repository>', methods=['POST'])
|
|
|
|
@process_auth
|
|
|
|
@parse_repository_name
|
|
|
|
def github_push_webhook(namespace, repository):
|
|
|
|
# data = request.get_json()
|
|
|
|
permission = ModifyRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
|
|
|
payload = json.loads(request.form['payload'])
|
|
|
|
ref = payload['ref']
|
|
|
|
|
|
|
|
repo = model.get_repository(namespace, repository)
|
|
|
|
token = model.create_access_token(repo, 'write')
|
|
|
|
|
|
|
|
host = urlparse.urlparse(request.url).netloc
|
|
|
|
tag = '%s/%s/%s:latest' % (host, repo.namespace, repo.name)
|
|
|
|
|
|
|
|
model.create_repository_build(repo, token, build_spec, tag)
|
|
|
|
|
|
|
|
abort(403)
|