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.
This commit is contained in:
Zixuan James Li 2023-08-12 03:35:14 -04:00 committed by Tim Abbott
parent 9377080f1f
commit 1329284848
3 changed files with 27 additions and 23 deletions

View File

@ -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)

View File

@ -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"))

View File

@ -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()