2016-03-13 15:14:29 +01:00
|
|
|
# Webhooks for external integrations.
|
2017-11-16 00:43:10 +01:00
|
|
|
|
2016-06-05 23:42:30 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2023-08-14 23:54:59 +02:00
|
|
|
from pydantic import BaseModel, Json
|
|
|
|
from typing_extensions import Annotated
|
2016-06-05 23:42:30 +02:00
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2017-11-16 00:43:10 +01:00
|
|
|
from zerver.lib.response import json_success
|
2023-08-14 23:54:59 +02:00
|
|
|
from zerver.lib.typed_endpoint import ApiParamConfig, typed_endpoint
|
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
|
|
|
|
2023-08-14 23:54:59 +02:00
|
|
|
class TravisPayload(BaseModel):
|
|
|
|
author_name: str
|
|
|
|
status_message: str
|
|
|
|
compare_url: str
|
|
|
|
build_url: str
|
|
|
|
type: str
|
|
|
|
|
|
|
|
|
2021-06-26 10:07:54 +02:00
|
|
|
@webhook_view("Travis", all_event_types=ALL_EVENT_TYPES)
|
2023-08-14 23:54:59 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_travis_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-14 23:54:59 +02:00
|
|
|
*,
|
|
|
|
message: Annotated[Json[TravisPayload], ApiParamConfig("payload")],
|
|
|
|
ignore_pull_requests: Json[bool] = True,
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2023-08-14 23:54:59 +02:00
|
|
|
event = message.type
|
|
|
|
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(
|
2023-08-14 23:54:59 +02:00
|
|
|
message.author_name,
|
2017-03-06 05:56:36 +01:00
|
|
|
message_status,
|
|
|
|
emoji,
|
2023-08-14 23:54:59 +02:00
|
|
|
message.compare_url,
|
|
|
|
message.build_url,
|
2017-03-06 05:56:36 +01:00
|
|
|
)
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = "builds"
|
2016-03-13 15:14:29 +01:00
|
|
|
|
2024-01-17 15:53:30 +01:00
|
|
|
check_send_webhook_message(request, user_profile, topic_name, body, event)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|