Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

freshping: Strengthen types using WildValue.

Christopher Chongf349da218c75
diff --git a/zerver/webhooks/freshping/view.py b/zerver/webhooks/freshping/view.py
index cbe595478e0..476a72eaab8 100644
--- a/zerver/webhooks/freshping/view.py
+++ b/zerver/webhooks/freshping/view.py
@@ -1,10 +1,9 @@
-from typing import Any, Dict
-
from django.http import HttpRequest, HttpResponse
from zerver.decorator import webhook_view
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success
+from zerver.lib.validator import WildValue, check_int, check_string, to_wild_value
from zerver.lib.webhooks.common import check_send_webhook_message, get_setup_webhook_message
from zerver.models import UserProfile
@@ -24,36 +23,46 @@ ALL_EVENT_TYPES = ["Reporting Error", "Available"]
def api_freshping_webhook(
request: HttpRequest,
user_profile: UserProfile,
- payload: Dict[str, Any] = REQ(argument_type="body"),
+ payload: WildValue = REQ(argument_type="body", converter=to_wild_value),
) -> HttpResponse:
body = get_body_for_http_request(payload)
subject = get_subject_for_http_request(payload)
check_send_webhook_message(
- request, user_profile, subject, body, payload["webhook_event_data"]["check_state_name"]
+ request,
+ user_profile,
+ subject,
+ body,
+ payload["webhook_event_data"]["check_state_name"].tame(check_string),
)
return json_success(request)
-def get_subject_for_http_request(payload: Dict[str, Any]) -> str:
+def get_subject_for_http_request(payload: WildValue) -> str:
webhook_event_data = payload["webhook_event_data"]
- if webhook_event_data["application_name"] == "Webhook test":
+ if webhook_event_data["application_name"].tame(check_string) == "Webhook test":
subject = FRESHPING_TOPIC_TEMPLATE_TEST
else:
- subject = FRESHPING_TOPIC_TEMPLATE.format(check_name=webhook_event_data["check_name"])
-
+ subject = FRESHPING_TOPIC_TEMPLATE.format(
+ check_name=webhook_event_data["check_name"].tame(check_string)
+ )
return subject
-def get_body_for_http_request(payload: Dict[str, Any]) -> str:
+def get_body_for_http_request(payload: WildValue) -> str:
webhook_event_data = payload["webhook_event_data"]
- if webhook_event_data["check_state_name"] == "Reporting Error":
- body = FRESHPING_MESSAGE_TEMPLATE_UNREACHABLE.format(**webhook_event_data)
- elif webhook_event_data["check_state_name"] == "Available":
- if webhook_event_data["application_name"] == "Webhook test":
+ if webhook_event_data["check_state_name"].tame(check_string) == "Reporting Error":
+ body = FRESHPING_MESSAGE_TEMPLATE_UNREACHABLE.format(
+ request_url=webhook_event_data["request_url"].tame(check_string),
+ http_status_code=webhook_event_data["http_status_code"].tame(check_int),
+ )
+ elif webhook_event_data["check_state_name"].tame(check_string) == "Available":
+ if webhook_event_data["application_name"].tame(check_string) == "Webhook test":
body = get_setup_webhook_message("Freshping")
else:
- body = FRESHPING_MESSAGE_TEMPLATE_UP.format(**webhook_event_data)
+ body = FRESHPING_MESSAGE_TEMPLATE_UP.format(
+ request_url=webhook_event_data["request_url"].tame(check_string)
+ )
return body