From 2dab5cbd032f019f14b687747829c13e65daefe9 Mon Sep 17 00:00:00 2001 From: Hari Prashant Bhimaraju Date: Thu, 23 Jun 2022 21:09:38 +0530 Subject: [PATCH] solano: Strengthen types using WildValue. This commit strengthens types by typing the Solano webhook's incoming payload as WildValue, which eradicates the use of Any within the incoming webhook integration. The KeyError exception has been replaced to catch a ValidationError instead now, since the incoming payload's keys will be tamed before usage and the non-existence of the key is raised as a ValidationError in the taming function. --- zerver/webhooks/solano/view.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/zerver/webhooks/solano/view.py b/zerver/webhooks/solano/view.py index f11b5a77f9..eb521208c8 100644 --- a/zerver/webhooks/solano/view.py +++ b/zerver/webhooks/solano/view.py @@ -1,11 +1,11 @@ # Webhooks for external integrations. -from typing import Any, Dict - +from django.core.exceptions import ValidationError 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.validator import WildValue, check_string, to_wild_value from zerver.lib.webhooks.common import check_send_webhook_message from zerver.models import UserProfile @@ -24,20 +24,20 @@ ALL_EVENT_TYPES = ["first-fail", "stop", "received"] def api_solano_webhook( request: HttpRequest, user_profile: UserProfile, - payload: Dict[str, Any] = REQ(argument_type="body"), + payload: WildValue = REQ(argument_type="body", converter=to_wild_value), ) -> HttpResponse: - event = payload.get("event") + event = payload["event"].tame(check_string) topic = "build update" if event == "test": return handle_test_event(request, user_profile, topic) try: - author = payload["committers"][0] - except KeyError: + author = payload["committers"][0].tame(check_string) + except ValidationError: author = "Unknown" - status = payload["status"] - build_log = payload["url"] - repository = payload["repository"]["url"] - commit_id = payload["commit_id"] + status = payload["status"].tame(check_string) + build_log = payload["url"].tame(check_string) + repository = payload["repository"]["url"].tame(check_string) + commit_id = payload["commit_id"].tame(check_string) good_status = ["passed"] bad_status = ["failed", "error"]