2016-05-09 19:59:33 +02:00
|
|
|
# Webhooks for external integrations.
|
2016-07-30 00:41:28 +02:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
from typing import Any, Dict
|
2016-07-30 00:41:28 +02:00
|
|
|
|
2017-11-16 00:43:10 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.decorator import api_key_only_webhook_view
|
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.lib.response import json_success
|
2018-03-13 23:43:02 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2017-05-02 01:00:50 +02:00
|
|
|
from zerver.models import UserProfile
|
2016-05-09 19:59:33 +02:00
|
|
|
|
2018-11-09 21:02:59 +01:00
|
|
|
CIRCLECI_TOPIC_TEMPLATE = u'{repository_name}'
|
2016-09-11 23:57:44 +02:00
|
|
|
CIRCLECI_MESSAGE_TEMPLATE = u'[Build]({build_url}) triggered by {username} on {branch} branch {status}.'
|
2016-05-09 19:59:33 +02:00
|
|
|
|
|
|
|
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
|
2017-12-14 10:31:47 +01:00
|
|
|
def api_circleci_webhook(request: HttpRequest, user_profile: UserProfile,
|
2018-03-13 23:43:02 +01:00
|
|
|
payload: Dict[str, Any]=REQ(argument_type='body')) -> HttpResponse:
|
2016-05-09 19:59:33 +02:00
|
|
|
payload = payload['payload']
|
|
|
|
subject = get_subject(payload)
|
|
|
|
body = get_body(payload)
|
|
|
|
|
2018-03-13 23:43:02 +01:00
|
|
|
check_send_webhook_message(request, user_profile, subject, body)
|
2016-05-09 19:59:33 +02:00
|
|
|
return json_success()
|
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_subject(payload: Dict[str, Any]) -> str:
|
2018-11-09 21:02:59 +01:00
|
|
|
return CIRCLECI_TOPIC_TEMPLATE.format(repository_name=payload['reponame'])
|
2016-05-09 19:59:33 +02:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_body(payload: Dict[str, Any]) -> str:
|
2016-05-09 19:59:33 +02:00
|
|
|
data = {
|
|
|
|
'build_url': payload['build_url'],
|
|
|
|
'username': payload['username'],
|
|
|
|
'branch': payload['branch'],
|
|
|
|
'status': get_status(payload)
|
|
|
|
}
|
|
|
|
return CIRCLECI_MESSAGE_TEMPLATE.format(**data)
|
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_status(payload: Dict[str, Any]) -> str:
|
2016-05-09 19:59:33 +02:00
|
|
|
status = payload['status']
|
2017-06-23 01:06:50 +02:00
|
|
|
if payload['previous'] and payload['previous']['status'] == FAILED_STATUS and status == FAILED_STATUS:
|
2016-09-11 23:57:44 +02:00
|
|
|
return u'is still failing'
|
2016-05-09 19:59:33 +02:00
|
|
|
if status == 'success':
|
2016-09-11 23:57:44 +02:00
|
|
|
return u'succeeded'
|
2016-05-09 19:59:33 +02:00
|
|
|
return status
|