webhooks: Convert gitlab to use @typed_endpoint.

The GitLab webhook has a mix of different types of parameters each
requring a unique set of configurations.
This commit is contained in:
Zixuan James Li 2023-08-14 17:50:15 -04:00 committed by Tim Abbott
parent b163f2fe4e
commit a33607d8ad
1 changed files with 10 additions and 7 deletions

View File

@ -3,13 +3,15 @@ from functools import partial
from typing import Dict, List, Optional, Protocol, Union from typing import Dict, List, Optional, Protocol, Union
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from pydantic import Json
from zerver.decorator import webhook_view from zerver.decorator import webhook_view
from zerver.lib.exceptions import UnsupportedWebhookEventTypeError from zerver.lib.exceptions import UnsupportedWebhookEventTypeError
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.validator import WildValue, check_bool, check_int, check_string, to_wild_value from zerver.lib.typed_endpoint import WebhookPayload, typed_endpoint
from zerver.lib.validator import WildValue, check_int, check_string
from zerver.lib.webhooks.common import ( from zerver.lib.webhooks.common import (
OptionalUserSpecifiedTopicStr,
check_send_webhook_message, check_send_webhook_message,
validate_extract_webhook_http_header, validate_extract_webhook_http_header,
) )
@ -416,14 +418,15 @@ ALL_EVENT_TYPES = list(EVENT_FUNCTION_MAPPER.keys())
@webhook_view("GitLab", all_event_types=ALL_EVENT_TYPES) @webhook_view("GitLab", all_event_types=ALL_EVENT_TYPES)
@has_request_variables @typed_endpoint
def api_gitlab_webhook( def api_gitlab_webhook(
request: HttpRequest, request: HttpRequest,
user_profile: UserProfile, user_profile: UserProfile,
payload: WildValue = REQ(argument_type="body", converter=to_wild_value), *,
branches: Optional[str] = REQ(default=None), payload: WebhookPayload[WildValue],
use_merge_request_title: bool = REQ(default=True, json_validator=check_bool), branches: Optional[str] = None,
user_specified_topic: Optional[str] = REQ("topic", default=None), use_merge_request_title: Json[bool] = True,
user_specified_topic: OptionalUserSpecifiedTopicStr = None,
) -> HttpResponse: ) -> HttpResponse:
event = get_event(request, payload, branches) event = get_event(request, payload, branches)
if event is not None: if event is not None: