Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
webhooks: Fix incorrect None checks for WildValue.
Anders Kaseorgcab30af51ba0
diff --git a/zerver/webhooks/gosquared/view.py b/zerver/webhooks/gosquared/view.pyindex d94bd01763e..c42a30869fc 100644--- a/zerver/webhooks/gosquared/view.py+++ b/zerver/webhooks/gosquared/view.py@@ -47,7 +47,7 @@ def api_gosquared_webhook(check_send_webhook_message(request, user_profile, topic_name, body, "traffic_spike")# Live chat message event- elif payload.get("message") is not None and payload.get("person") is not None:+ elif "message" in payload and "person" in payload:# Only support non-direct messagesif not payload["message"]["private"].tame(check_bool):session_title = payload["message"]["session"]["title"].tame(check_string)diff --git a/zerver/webhooks/grafana/view.py b/zerver/webhooks/grafana/view.pyindex 6862c8541f8..206621ad2e1 100644--- a/zerver/webhooks/grafana/view.py+++ b/zerver/webhooks/grafana/view.py@@ -185,7 +185,7 @@ def api_grafana_webhook(topic_name = OLD_TOPIC_TEMPLATE.format(alert_title=legacy_alert.title)eval_matches_text = ""- if "evalMatches" in payload and payload["evalMatches"] is not None:+ if "evalMatches" in payload:for match in payload["evalMatches"]:eval_matches_text += "**{}:** {}\n".format(match["metric"].tame(check_string),diff --git a/zerver/webhooks/pagerduty/view.py b/zerver/webhooks/pagerduty/view.pyindex 444361e85e9..83aad63fcaf 100644--- a/zerver/webhooks/pagerduty/view.py+++ b/zerver/webhooks/pagerduty/view.py@@ -86,12 +86,11 @@ def build_pagerduty_formatdict_v2(message: WildValue) -> FormatDictType:else:format_dict["assignee_info"] = "nobody"- last_status_change_by = message["incident"].get("last_status_change_by")- if last_status_change_by is not None:- format_dict["agent_info"] = AGENT_TEMPLATE.format(- username=last_status_change_by["summary"].tame(check_string),- url=last_status_change_by["html_url"].tame(check_string),- )+ last_status_change_by = message["incident"]["last_status_change_by"]+ format_dict["agent_info"] = AGENT_TEMPLATE.format(+ username=last_status_change_by["summary"].tame(check_string),+ url=last_status_change_by["html_url"].tame(check_string),+ )return format_dict@@ -120,8 +119,8 @@ def build_pagerduty_formatdict_v3(event: WildValue) -> FormatDictType:else:format_dict["assignee_info"] = "nobody"- agent = event.get("agent")- if agent is not None:+ agent = event["agent"]+ if agent.value is not None:format_dict["agent_info"] = AGENT_TEMPLATE.format(username=agent["summary"].tame(check_string),url=agent["html_url"].tame(check_string),diff --git a/zerver/webhooks/raygun/view.py b/zerver/webhooks/raygun/view.pyindex 2cd2854e71f..2afc6d577ac 100644--- a/zerver/webhooks/raygun/view.py+++ b/zerver/webhooks/raygun/view.py@@ -7,7 +7,14 @@ from zerver.lib.exceptions import UnsupportedWebhookEventTypeErrorfrom zerver.lib.response import json_successfrom zerver.lib.timestamp import datetime_to_global_timefrom zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint-from zerver.lib.validator import WildValue, check_anything, check_int, check_list, check_string+from zerver.lib.validator import (+ WildValue,+ check_dict,+ check_int,+ check_list,+ check_none_or,+ check_string,+)from zerver.lib.webhooks.common import check_send_webhook_messagefrom zerver.models import UserProfile@@ -175,7 +182,7 @@ def notification_message_error_occurred(payload: WildValue) -> str:# Extract each of the keys and values in error_instance for easier handle# Contains list of tags for the error. Can be empty (null)- tags = error_instance["tags"]+ tags = error_instance["tags"].tame(check_none_or(check_list(check_string)))# Contains the identity of affected user at the moment this error# happened. This surprisingly can be null. Somehow.@@ -183,12 +190,12 @@ def notification_message_error_occurred(payload: WildValue) -> str:# Contains custom data for this particular error (if supplied). Can be# null.- custom_data = error_instance["customData"]+ custom_data = error_instance["customData"].tame(check_none_or(check_dict()))if tags is not None:- message += "* **Tags**: {}\n".format(", ".join(tags.tame(check_list(check_string))))+ message += "* **Tags**: {}\n".format(", ".join(tags))- if affected_user is not None:+ if affected_user.value is not None:user_uuid = affected_user["UUID"].tame(check_string)message += f"* **Affected user**: {user_uuid[:6]}...{user_uuid[-5:]}\n"@@ -196,7 +203,7 @@ def notification_message_error_occurred(payload: WildValue) -> str:# We don't know what the keys and values beforehand, so we are forced# to iterate.for key in sorted(custom_data.keys()):- message += f"* **{key}**: {custom_data[key].tame(check_anything)}\n"+ message += f"* **{key}**: {custom_data[key]}\n"message += make_app_info_chunk(payload["application"])diff --git a/zerver/webhooks/reviewboard/view.py b/zerver/webhooks/reviewboard/view.pyindex cfe0180bcb0..765d33df9af 100644--- a/zerver/webhooks/reviewboard/view.py+++ b/zerver/webhooks/reviewboard/view.py@@ -121,7 +121,7 @@ def get_review_request_published_body(payload: WildValue) -> str:message = REVIEW_REQUEST_PUBLISHED + REVIEW_REQUEST_DETAILSbranch = payload["review_request"].get("branch").tame(check_none_or(check_string))- if branch and branch is not None:+ if branch:branch_info = BRANCH_TEMPLATE.format(branch_name=branch)kwargs["extra_info"] = branch_info@@ -142,7 +142,7 @@ def get_review_request_reopened_body(payload: WildValue) -> str:message = REVIEW_REQUEST_REOPENED + REVIEW_REQUEST_DETAILSbranch = payload["review_request"].get("branch").tame(check_none_or(check_string))- if branch and branch is not None:+ if branch:branch_info = BRANCH_TEMPLATE.format(branch_name=branch)kwargs["extra_info"] = branch_info@@ -163,7 +163,7 @@ def get_review_request_closed_body(payload: WildValue) -> str:message = REVIEW_REQUEST_CLOSED + REVIEW_REQUEST_DETAILSbranch = payload["review_request"].get("branch").tame(check_none_or(check_string))- if branch and branch is not None:+ if branch:branch_info = BRANCH_TEMPLATE.format(branch_name=branch)kwargs["extra_info"] = "{}\n{}".format(kwargs["extra_info"], branch_info)