2022-07-31 09:13:58 +02:00
|
|
|
from typing import Tuple
|
2018-08-18 18:46:01 +02:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2022-11-17 09:30:48 +01:00
|
|
|
from zerver.lib.exceptions import UnsupportedWebhookEventTypeError
|
2018-08-18 18:46:01 +02:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.lib.response import json_success
|
2022-07-31 09:13:58 +02:00
|
|
|
from zerver.lib.validator import WildValue, check_string, to_wild_value
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.webhooks.common import (
|
|
|
|
check_send_webhook_message,
|
|
|
|
get_http_headers_from_filename,
|
|
|
|
validate_extract_webhook_http_header,
|
|
|
|
)
|
2018-08-18 18:46:01 +02:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
ALL_EVENT_TYPES = [
|
|
|
|
"deploy_failed",
|
|
|
|
"deploy_locked",
|
|
|
|
"deploy_unlocked",
|
|
|
|
"deploy_building",
|
|
|
|
"deploy_created",
|
|
|
|
]
|
2018-08-18 18:46:01 +02:00
|
|
|
|
2019-07-04 21:00:56 +02:00
|
|
|
fixture_to_headers = get_http_headers_from_filename("HTTP_X_NETLIFY_EVENT")
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
@webhook_view("Netlify", all_event_types=ALL_EVENT_TYPES)
|
2018-08-18 18:46:01 +02:00
|
|
|
@has_request_variables
|
|
|
|
def api_netlify_webhook(
|
2021-02-12 08:19:30 +01:00
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2022-07-31 09:13:58 +02:00
|
|
|
payload: WildValue = REQ(argument_type="body", converter=to_wild_value),
|
2018-08-18 18:46:01 +02:00
|
|
|
) -> HttpResponse:
|
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
message_template, event = get_template(request, payload)
|
2018-08-18 18:46:01 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
body = message_template.format(
|
2022-07-31 09:13:58 +02:00
|
|
|
build_name=payload["name"].tame(check_string),
|
|
|
|
build_url=payload["url"].tame(check_string),
|
|
|
|
branch_name=payload["branch"].tame(check_string),
|
|
|
|
state=payload["state"].tame(check_string),
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2018-08-18 18:46:01 +02:00
|
|
|
|
2022-07-31 09:13:58 +02:00
|
|
|
topic = "{topic}".format(topic=payload["branch"].tame(check_string))
|
2018-08-18 18:46:01 +02:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
check_send_webhook_message(request, user_profile, topic, body, event)
|
2018-08-18 18:46:01 +02:00
|
|
|
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2018-08-18 18:46:01 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-07-31 09:13:58 +02:00
|
|
|
def get_template(request: HttpRequest, payload: WildValue) -> Tuple[str, str]:
|
2018-08-18 18:46:01 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
message_template = "The build [{build_name}]({build_url}) on branch {branch_name} "
|
2022-05-12 06:54:12 +02:00
|
|
|
event = validate_extract_webhook_http_header(request, "X-Netlify-Event", "Netlify")
|
2018-08-18 18:46:01 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if event == "deploy_failed":
|
2022-07-31 09:13:58 +02:00
|
|
|
message_template += payload["error_message"].tame(check_string)
|
2021-02-12 08:20:45 +01:00
|
|
|
elif event == "deploy_locked":
|
2021-07-16 11:40:46 +02:00
|
|
|
message_template += "is now locked."
|
2021-02-12 08:20:45 +01:00
|
|
|
elif event == "deploy_unlocked":
|
2021-07-16 11:40:46 +02:00
|
|
|
message_template += "is now unlocked."
|
|
|
|
elif event in ALL_EVENT_TYPES:
|
2022-07-31 09:13:58 +02:00
|
|
|
message_template += "is now {state}.".format(state=payload["state"].tame(check_string))
|
2018-08-18 18:46:01 +02:00
|
|
|
else:
|
2022-11-17 09:30:48 +01:00
|
|
|
raise UnsupportedWebhookEventTypeError(event)
|
2021-07-16 11:40:46 +02:00
|
|
|
|
|
|
|
return message_template, event
|