2016-05-09 19:59:33 +02:00
|
|
|
# Webhooks for external integrations.
|
|
|
|
from __future__ import absolute_import
|
|
|
|
from zerver.lib.actions import check_send_message
|
|
|
|
from zerver.lib.response import json_success, json_error
|
|
|
|
from zerver.decorator import REQ, has_request_variables, api_key_only_webhook_view
|
|
|
|
|
|
|
|
import ujson
|
|
|
|
|
|
|
|
|
|
|
|
CIRCLECI_SUBJECT_TEMPLATE = '{repository_name}'
|
|
|
|
CIRCLECI_MESSAGE_TEMPLATE = '[Build]({build_url}) triggered by {username} on {branch} branch {status}.'
|
|
|
|
|
|
|
|
FAILED_STATUS = 'failed'
|
|
|
|
|
2016-05-12 22:49:36 +02:00
|
|
|
@api_key_only_webhook_view('CircleCI')
|
2016-05-09 19:59:33 +02:00
|
|
|
@has_request_variables
|
2016-07-17 19:49:17 +02:00
|
|
|
def api_circleci_webhook(request, user_profile, client, payload=REQ(argument_type='body'),
|
|
|
|
stream=REQ(default='circleci')):
|
2016-05-09 19:59:33 +02:00
|
|
|
payload = payload['payload']
|
|
|
|
subject = get_subject(payload)
|
|
|
|
body = get_body(payload)
|
|
|
|
|
2016-05-12 22:49:36 +02:00
|
|
|
check_send_message(user_profile, client, 'stream', [stream], subject, body)
|
2016-05-09 19:59:33 +02:00
|
|
|
return json_success()
|
|
|
|
|
|
|
|
def get_subject(payload):
|
|
|
|
return CIRCLECI_SUBJECT_TEMPLATE.format(repository_name=payload['reponame'])
|
|
|
|
|
|
|
|
def get_body(payload):
|
|
|
|
data = {
|
|
|
|
'build_url': payload['build_url'],
|
|
|
|
'username': payload['username'],
|
|
|
|
'branch': payload['branch'],
|
|
|
|
'status': get_status(payload)
|
|
|
|
}
|
|
|
|
return CIRCLECI_MESSAGE_TEMPLATE.format(**data)
|
|
|
|
|
|
|
|
def get_status(payload):
|
|
|
|
status = payload['status']
|
|
|
|
if payload['previous']['status'] == FAILED_STATUS and status == FAILED_STATUS:
|
|
|
|
return 'is still failing'
|
|
|
|
if status == 'success':
|
|
|
|
return 'succeeded'
|
|
|
|
return status
|