From 1329284848637432610721bbc9d1506e3aa1407d Mon Sep 17 00:00:00 2001 From: Zixuan James Li Date: Sat, 12 Aug 2023 03:35:14 -0400 Subject: [PATCH] webhooks: Migrate webhooks with str parameters to use @typed_endpoint. These webhooks have some URL query params that do not need additional validation or parsing from JSON. So WebhookPaylaod is not applicable to these webhooks. --- zerver/webhooks/heroku/view.py | 15 ++++++++------- zerver/webhooks/slack/view.py | 16 +++++++++------- zerver/webhooks/wordpress/view.py | 19 ++++++++++--------- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/zerver/webhooks/heroku/view.py b/zerver/webhooks/heroku/view.py index 19bd29894e..092213784b 100644 --- a/zerver/webhooks/heroku/view.py +++ b/zerver/webhooks/heroku/view.py @@ -2,8 +2,8 @@ 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.typed_endpoint import typed_endpoint from zerver.lib.webhooks.common import check_send_webhook_message from zerver.models import UserProfile @@ -17,15 +17,16 @@ TEMPLATE = """ @webhook_view("Heroku", notify_bot_owner_on_invalid_json=False) -@has_request_variables +@typed_endpoint def api_heroku_webhook( request: HttpRequest, user_profile: UserProfile, - head: str = REQ(), - app: str = REQ(), - user: str = REQ(), - url: str = REQ(), - git_log: str = REQ(), + *, + head: str, + app: str, + user: str, + url: str, + git_log: str, ) -> HttpResponse: content = TEMPLATE.format(user=user, head=head, app=app, url=url, git_log=git_log) diff --git a/zerver/webhooks/slack/view.py b/zerver/webhooks/slack/view.py index 6751c2b485..6128af02b0 100644 --- a/zerver/webhooks/slack/view.py +++ b/zerver/webhooks/slack/view.py @@ -5,8 +5,9 @@ from django.utils.translation import gettext as _ from zerver.actions.message_send import check_send_stream_message from zerver.decorator import webhook_view from zerver.lib.exceptions import JsonableError -from zerver.lib.request import REQ, RequestNotes, has_request_variables +from zerver.lib.request import RequestNotes from zerver.lib.response import json_success +from zerver.lib.typed_endpoint import typed_endpoint from zerver.models import UserProfile ZULIP_MESSAGE_TEMPLATE = "**{message_sender}**: {text}" @@ -14,15 +15,16 @@ VALID_OPTIONS = {"SHOULD_NOT_BE_MAPPED": "0", "SHOULD_BE_MAPPED": "1"} @webhook_view("Slack", notify_bot_owner_on_invalid_json=False) -@has_request_variables +@typed_endpoint def api_slack_webhook( request: HttpRequest, user_profile: UserProfile, - user_name: str = REQ(), - text: str = REQ(), - channel_name: str = REQ(), - stream: str = REQ(default="slack"), - channels_map_to_topics: str = REQ(default="1"), + *, + user_name: str, + text: str, + channel_name: str, + stream: str = "slack", + channels_map_to_topics: str = "1", ) -> HttpResponse: if channels_map_to_topics not in list(VALID_OPTIONS.values()): raise JsonableError(_("Error: channels_map_to_topics parameter other than 0 or 1")) diff --git a/zerver/webhooks/wordpress/view.py b/zerver/webhooks/wordpress/view.py index 96a0555398..2c0390eccc 100644 --- a/zerver/webhooks/wordpress/view.py +++ b/zerver/webhooks/wordpress/view.py @@ -4,8 +4,8 @@ from django.utils.translation import gettext as _ from zerver.decorator import webhook_view from zerver.lib.exceptions import JsonableError -from zerver.lib.request import REQ, has_request_variables from zerver.lib.response import json_success +from zerver.lib.typed_endpoint import typed_endpoint from zerver.lib.webhooks.common import check_send_webhook_message from zerver.models import UserProfile @@ -28,17 +28,18 @@ ALL_EVENT_TYPES = [ @webhook_view("WordPress", notify_bot_owner_on_invalid_json=False, all_event_types=ALL_EVENT_TYPES) -@has_request_variables +@typed_endpoint def api_wordpress_webhook( request: HttpRequest, user_profile: UserProfile, - hook: str = REQ(default="WordPress action"), - post_title: str = REQ(default="New WordPress post"), - post_type: str = REQ(default="post"), - post_url: str = REQ(default="WordPress post URL"), - display_name: str = REQ(default="New user name"), - user_email: str = REQ(default="New user email"), - user_login: str = REQ(default="Logged in user"), + *, + hook: str = "WordPress action", + post_title: str = "New WordPress post", + post_type: str = "post", + post_url: str = "WordPress post URL", + display_name: str = "New user name", + user_email: str = "New user email", + user_login: str = "Logged in user", ) -> HttpResponse: # remove trailing whitespace (issue for some test fixtures) hook = hook.rstrip()