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 django.http import HttpRequest, HttpResponse
from zerver.decorator import webhook_view 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.response import json_success
from zerver.lib.typed_endpoint import typed_endpoint
from zerver.lib.webhooks.common import check_send_webhook_message from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.models import UserProfile from zerver.models import UserProfile
@ -17,15 +17,16 @@ TEMPLATE = """
@webhook_view("Heroku", notify_bot_owner_on_invalid_json=False) @webhook_view("Heroku", notify_bot_owner_on_invalid_json=False)
@has_request_variables @typed_endpoint
def api_heroku_webhook( def api_heroku_webhook(
request: HttpRequest, request: HttpRequest,
user_profile: UserProfile, user_profile: UserProfile,
head: str = REQ(), *,
app: str = REQ(), head: str,
user: str = REQ(), app: str,
url: str = REQ(), user: str,
git_log: str = REQ(), url: str,
git_log: str,
) -> HttpResponse: ) -> HttpResponse:
content = TEMPLATE.format(user=user, head=head, app=app, url=url, git_log=git_log) 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.actions.message_send import check_send_stream_message
from zerver.decorator import webhook_view from zerver.decorator import webhook_view
from zerver.lib.exceptions import JsonableError 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.response import json_success
from zerver.lib.typed_endpoint import typed_endpoint
from zerver.models import UserProfile from zerver.models import UserProfile
ZULIP_MESSAGE_TEMPLATE = "**{message_sender}**: {text}" 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) @webhook_view("Slack", notify_bot_owner_on_invalid_json=False)
@has_request_variables @typed_endpoint
def api_slack_webhook( def api_slack_webhook(
request: HttpRequest, request: HttpRequest,
user_profile: UserProfile, user_profile: UserProfile,
user_name: str = REQ(), *,
text: str = REQ(), user_name: str,
channel_name: str = REQ(), text: str,
stream: str = REQ(default="slack"), channel_name: str,
channels_map_to_topics: str = REQ(default="1"), stream: str = "slack",
channels_map_to_topics: str = "1",
) -> HttpResponse: ) -> HttpResponse:
if channels_map_to_topics not in list(VALID_OPTIONS.values()): if channels_map_to_topics not in list(VALID_OPTIONS.values()):
raise JsonableError(_("Error: channels_map_to_topics parameter other than 0 or 1")) 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.decorator import webhook_view
from zerver.lib.exceptions import JsonableError 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.response import json_success
from zerver.lib.typed_endpoint import typed_endpoint
from zerver.lib.webhooks.common import check_send_webhook_message from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.models import UserProfile 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) @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( def api_wordpress_webhook(
request: HttpRequest, request: HttpRequest,
user_profile: UserProfile, user_profile: UserProfile,
hook: str = REQ(default="WordPress action"), *,
post_title: str = REQ(default="New WordPress post"), hook: str = "WordPress action",
post_type: str = REQ(default="post"), post_title: str = "New WordPress post",
post_url: str = REQ(default="WordPress post URL"), post_type: str = "post",
display_name: str = REQ(default="New user name"), post_url: str = "WordPress post URL",
user_email: str = REQ(default="New user email"), display_name: str = "New user name",
user_login: str = REQ(default="Logged in user"), user_email: str = "New user email",
user_login: str = "Logged in user",
) -> HttpResponse: ) -> HttpResponse:
# remove trailing whitespace (issue for some test fixtures) # remove trailing whitespace (issue for some test fixtures)
hook = hook.rstrip() hook = hook.rstrip()