Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
opbeat: Strengthen types using WildValue.
Hari Prashant Bhimaraju8ab47bbcc435
diff --git a/zerver/webhooks/opbeat/tests.py b/zerver/webhooks/opbeat/tests.pyindex 9ce9b49ccd6..2d12efbd3f8 100644--- a/zerver/webhooks/opbeat/tests.py+++ b/zerver/webhooks/opbeat/tests.py@@ -1,4 +1,5 @@from zerver.lib.test_classes import WebhookTestCase+from zerver.lib.validator import wrap_wild_valuefrom zerver.webhooks.opbeat.view import get_value@@ -37,7 +38,8 @@ App foo created)def test_get_empty_value(self) -> None:- self.assertEqual(get_value({"key": "value"}, "foo"), "")+ test_dictionary = wrap_wild_value("test_dictionary", {"key": "value"})+ self.assertEqual(get_value(test_dictionary, "foo"), "")def test_no_subject_type(self) -> None:expected_topic = "test title"diff --git a/zerver/webhooks/opbeat/view.py b/zerver/webhooks/opbeat/view.pyindex c5620196965..a9b837c9a31 100644--- a/zerver/webhooks/opbeat/view.py+++ b/zerver/webhooks/opbeat/view.py@@ -1,11 +1,19 @@# Webhooks for external integrations.-from typing import Any, Dict, List+from typing import Dict, Listfrom django.http import HttpRequest, HttpResponsefrom zerver.decorator import webhook_viewfrom zerver.lib.request import REQ, has_request_variablesfrom zerver.lib.response import json_success+from zerver.lib.validator import (+ WildValue,+ check_int,+ check_none_or,+ check_string,+ check_union,+ to_wild_value,+)from zerver.lib.webhooks.common import check_send_webhook_messagefrom zerver.models import UserProfile@@ -40,17 +48,17 @@ subject_types: Dict[str, List[List[str]]] = {}-def get_value(_obj: Dict[str, Any], key: str) -> str:+def get_value(_obj: WildValue, key: str) -> str:for _key in key.lstrip("!").split("/"):if _key in _obj.keys():_obj = _obj[_key]else:return ""- return str(_obj)+ return str(_obj.tame(check_union([check_string, check_int])))def format_object(- obj: Dict[str, Any],+ obj: WildValue,subject_type: str,message: str,) -> str:@@ -63,9 +71,10 @@ def format_object(if len(title) > 1:title_str = title[0].format(get_value(obj, title[1]))else:- title_str = obj[title[0]]- if obj["html_url"] is not None:- url: str = obj["html_url"]+ title_str = obj[title[0]].tame(check_string)++ url = obj["html_url"].tame(check_none_or(check_string))+ if url is not None:if "opbeat.com" not in url:url = "https://opbeat.com/" + url.lstrip("/")message += f"\n**[{title_str}]({url})**"@@ -89,7 +98,9 @@ def format_object(else:message += f"\n>{key}: {value}"if key == "subject":- message = format_object(obj["subject"], obj["subject_type"], message + "\n")+ message = format_object(+ obj["subject"], obj["subject_type"].tame(check_string), message + "\n"+ )if ":" in key:value, value_type = key.split(":")message = format_object(obj[value], value_type, message + "\n")@@ -101,7 +112,7 @@ def format_object(def api_opbeat_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:"""This uses the subject name from opbeat to make the subject,@@ -109,7 +120,7 @@ def api_opbeat_webhook(details about the object mentioned."""- message_subject = payload["title"]+ message_subject = payload["title"].tame(check_string)message = format_object(payload, "base", "")