2016-03-13 15:14:29 +01:00
|
|
|
# Webhooks for external integrations.
|
2017-11-16 00:43:10 +01:00
|
|
|
from typing import Dict
|
|
|
|
|
2016-06-05 23:42:30 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2017-11-16 00:43:10 +01:00
|
|
|
from zerver.lib.response import json_success
|
|
|
|
from zerver.lib.validator import check_bool, check_dict, check_string
|
2020-01-14 22:06:24 +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-03-13 15:14:29 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
GOOD_STATUSES = ["Passed", "Fixed"]
|
|
|
|
BAD_STATUSES = ["Failed", "Broken", "Still Failing", "Errored", "Canceled"]
|
|
|
|
PENDING_STATUSES = ["Pending"]
|
2021-06-26 10:07:54 +02:00
|
|
|
ALL_EVENT_TYPES = [
|
|
|
|
"push",
|
|
|
|
"pull_request",
|
|
|
|
]
|
2017-03-06 05:56:36 +01:00
|
|
|
|
2021-02-12 03:52:14 +01:00
|
|
|
MESSAGE_TEMPLATE = """\
|
|
|
|
Author: {}
|
|
|
|
Build status: {} {}
|
|
|
|
Details: [changes]({}), [build log]({})"""
|
2016-03-13 15:14:29 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-06-26 10:07:54 +02:00
|
|
|
@webhook_view("Travis", all_event_types=ALL_EVENT_TYPES)
|
2016-03-13 15:14:29 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_travis_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2021-04-07 22:00:44 +02:00
|
|
|
ignore_pull_requests: bool = REQ(json_validator=check_bool, default=True),
|
2021-02-12 08:19:30 +01:00
|
|
|
message: Dict[str, object] = REQ(
|
2021-02-12 08:20:45 +01:00
|
|
|
"payload",
|
2021-04-07 22:00:44 +02:00
|
|
|
json_validator=check_dict(
|
2021-02-12 08:19:30 +01:00
|
|
|
[
|
2021-02-12 08:20:45 +01:00
|
|
|
("author_name", check_string),
|
|
|
|
("status_message", check_string),
|
|
|
|
("compare_url", check_string),
|
2021-02-12 08:19:30 +01:00
|
|
|
]
|
|
|
|
),
|
|
|
|
),
|
|
|
|
) -> HttpResponse:
|
2021-05-17 20:26:48 +02:00
|
|
|
event = str(message["type"])
|
2021-02-12 08:20:45 +01:00
|
|
|
message_status = message["status_message"]
|
|
|
|
if ignore_pull_requests and message["type"] == "pull_request":
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2017-03-06 05:56:36 +01:00
|
|
|
|
|
|
|
if message_status in GOOD_STATUSES:
|
2021-02-12 08:20:45 +01:00
|
|
|
emoji = ":thumbs_up:"
|
2017-03-06 05:56:36 +01:00
|
|
|
elif message_status in BAD_STATUSES:
|
2021-02-12 08:20:45 +01:00
|
|
|
emoji = ":thumbs_down:"
|
2020-05-20 10:43:07 +02:00
|
|
|
elif message_status in PENDING_STATUSES:
|
2021-02-12 08:20:45 +01:00
|
|
|
emoji = ":counterclockwise:"
|
2016-03-13 15:14:29 +01:00
|
|
|
else:
|
2020-06-09 00:25:09 +02:00
|
|
|
emoji = f"(No emoji specified for status '{message_status}'.)"
|
2017-03-06 05:56:36 +01:00
|
|
|
|
|
|
|
body = MESSAGE_TEMPLATE.format(
|
2021-02-12 08:20:45 +01:00
|
|
|
message["author_name"],
|
2017-03-06 05:56:36 +01:00
|
|
|
message_status,
|
|
|
|
emoji,
|
2021-02-12 08:20:45 +01:00
|
|
|
message["compare_url"],
|
|
|
|
message["build_url"],
|
2017-03-06 05:56:36 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
topic = "builds"
|
2016-03-13 15:14:29 +01:00
|
|
|
|
2021-05-17 20:26:48 +02:00
|
|
|
check_send_webhook_message(request, user_profile, topic, body, event)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|