Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
webhooks: Remove get_event_handler ritual and Any casts.
Anders Kaseorg3117969f0364
diff --git a/zerver/webhooks/bitbucket3/view.py b/zerver/webhooks/bitbucket3/view.pyindex 7b7c56ab269..7e0c5b8ce13 100644--- a/zerver/webhooks/bitbucket3/view.py+++ b/zerver/webhooks/bitbucket3/view.py@@ -358,7 +358,7 @@ def pr_comment_handler(return [{"subject": subject, "body": body}]-EVENT_HANDLER_MAP = {+EVENT_HANDLER_MAP: Dict[str, Optional[Callable[..., List[Dict[str, str]]]]] = {"diagnostics:ping": ping_handler,"repo:comment:added": partial(repo_comment_handler, action="commented"),"repo:comment:edited": partial(repo_comment_handler, action="edited their comment"),@@ -378,15 +378,7 @@ EVENT_HANDLER_MAP = {"pr:reviewer:needs_work": partial(pr_handler, action="needs_work"),"pr:reviewer:updated": partial(pr_handler, action="reviewers_updated"),"pr:reviewer:unapproved": partial(pr_handler, action="unapproved"),-} # type Dict[str, Optional[Callable[..., List[Dict[str, str]]]]]---def get_event_handler(eventkey: str) -> Callable[..., List[Dict[str, str]]]:- # The main reason for this function existence is because of mypy- handler: Any = EVENT_HANDLER_MAP.get(eventkey)- if handler is None:- raise UnsupportedWebhookEventType(eventkey)- return handler+}@webhook_view("Bitbucket3")@@ -402,7 +394,9 @@ def api_bitbucket3_webhook(eventkey = payload["eventKey"]except KeyError:eventkey = request.META["HTTP_X_EVENT_KEY"]- handler = get_event_handler(eventkey)+ handler = EVENT_HANDLER_MAP.get(eventkey)+ if handler is None:+ raise UnsupportedWebhookEventType(eventkey)if "branches" in signature(handler).parameters:data = handler(payload, branches)diff --git a/zerver/webhooks/clubhouse/view.py b/zerver/webhooks/clubhouse/view.pyindex c6c0cd7d301..356b9a26877 100644--- a/zerver/webhooks/clubhouse/view.py+++ b/zerver/webhooks/clubhouse/view.py@@ -1,5 +1,5 @@from functools import partial-from typing import Any, Dict, Optional+from typing import Any, Callable, Dict, Optionalfrom django.http import HttpRequest, HttpResponse@@ -511,7 +511,7 @@ def get_name_template(entity: str) -> str:return EPIC_NAME_TEMPLATE-EVENT_BODY_FUNCTION_MAPPER = {+EVENT_BODY_FUNCTION_MAPPER: Dict[str, Callable[[Dict[str, Any]], Optional[str]]] = {"story_update_archived": partial(get_update_archived_body, entity="story"),"epic_update_archived": partial(get_update_archived_body, entity="epic"),"story_create": get_story_create_body,@@ -574,7 +574,7 @@ def api_clubhouse_webhook(if event is None:return json_success()- body_func: Any = EVENT_BODY_FUNCTION_MAPPER.get(event)+ body_func = EVENT_BODY_FUNCTION_MAPPER.get(event)topic_func = get_topic_function_based_on_type(payload)if body_func is None or topic_func is None:raise UnsupportedWebhookEventType(event)diff --git a/zerver/webhooks/groove/view.py b/zerver/webhooks/groove/view.pyindex b7dcc57d5ac..26b133228b5 100644--- a/zerver/webhooks/groove/view.py+++ b/zerver/webhooks/groove/view.py@@ -79,14 +79,6 @@ def replied_body(payload: Dict[str, Any], actor: str, action: str) -> str:return body-def get_event_handler(event: str) -> Callable[..., str]:- # The main reason for this function existence is because of mypy- handler: Any = EVENTS_FUNCTION_MAPPER.get(event)- if handler is None:- raise UnsupportedWebhookEventType(event)- return handler--@webhook_view("Groove")@has_request_variablesdef api_groove_webhook(@@ -96,7 +88,9 @@ def api_groove_webhook() -> HttpResponse:event = validate_extract_webhook_http_header(request, "X_GROOVE_EVENT", "Groove")assert event is not None- handler = get_event_handler(event)+ handler = EVENTS_FUNCTION_MAPPER.get(event)+ if handler is None:+ raise UnsupportedWebhookEventType(event)body = handler(payload)topic = "notifications"@@ -107,7 +101,7 @@ def api_groove_webhook(return json_success()-EVENTS_FUNCTION_MAPPER = {+EVENTS_FUNCTION_MAPPER: Dict[str, Callable[[Dict[str, Any]], Optional[str]]] = {"ticket_started": ticket_started_body,"ticket_assigned": ticket_assigned_body,"agent_replied": partial(replied_body, actor="agent", action="replied to"),diff --git a/zerver/webhooks/intercom/view.py b/zerver/webhooks/intercom/view.pyindex 41643408461..0a0906eccaf 100644--- a/zerver/webhooks/intercom/view.py+++ b/zerver/webhooks/intercom/view.py@@ -268,7 +268,7 @@ def get_user_unsubscribed_message(payload: Dict[str, Any]) -> Tuple[str, str]:return (topic, body)-EVENT_TO_FUNCTION_MAPPER = {+EVENT_TO_FUNCTION_MAPPER: Dict[str, Callable[[Dict[str, Any]], Tuple[str, str]]] = {"company.created": get_company_created_message,"contact.added_email": get_contact_added_email_message,"contact.created": get_contact_created_message,@@ -302,13 +302,6 @@ EVENT_TO_FUNCTION_MAPPER = {}-def get_event_handler(event_type: str) -> Callable[..., Tuple[str, str]]:- handler: Any = EVENT_TO_FUNCTION_MAPPER.get(event_type)- if handler is None:- raise UnsupportedWebhookEventType(event_type)- return handler--@webhook_view("Intercom")@has_request_variablesdef api_intercom_webhook(@@ -320,7 +313,10 @@ def api_intercom_webhook(if event_type == "ping":return json_success()- topic, body = get_event_handler(event_type)(payload)+ handler = EVENT_TO_FUNCTION_MAPPER.get(event_type)+ if handler is None:+ raise UnsupportedWebhookEventType(event_type)+ topic, body = handler(payload)check_send_webhook_message(request, user_profile, topic, body)return json_success()diff --git a/zerver/webhooks/jira/view.py b/zerver/webhooks/jira/view.pyindex a9a209056c5..eb6c0b4460d 100644--- a/zerver/webhooks/jira/view.py+++ b/zerver/webhooks/jira/view.py@@ -325,7 +325,9 @@ def handle_comment_deleted_event(payload: Dict[str, Any], user_profile: UserProf)-JIRA_CONTENT_FUNCTION_MAPPER = {+JIRA_CONTENT_FUNCTION_MAPPER: Dict[+ Optional[str], Optional[Callable[[Dict[str, Any], UserProfile], str]]+] = {"jira:issue_created": handle_created_issue_event,"jira:issue_deleted": handle_deleted_issue_event,"jira:issue_updated": handle_updated_issue_event,@@ -335,13 +337,6 @@ JIRA_CONTENT_FUNCTION_MAPPER = {}-def get_event_handler(event: Optional[str]) -> Optional[Callable[..., str]]:- if event is None:- return None-- return JIRA_CONTENT_FUNCTION_MAPPER.get(event)--@webhook_view("JIRA")@has_request_variablesdef api_jira_webhook(@@ -354,7 +349,7 @@ def api_jira_webhook(if event in IGNORED_EVENTS:return json_success()- content_func = get_event_handler(event)+ content_func = JIRA_CONTENT_FUNCTION_MAPPER.get(event)if content_func is None:raise UnsupportedWebhookEventType(event)