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.
This commit is contained in:
Hari Prashant Bhimaraju 2022-06-23 21:09:38 +05:30 committed by Tim Abbott
parent 31c7f8266d
commit 2dab5cbd03
1 changed files with 10 additions and 10 deletions

View File

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