gci: Strengthen types using WildValue.

This commit is contained in:
Christopher Chong 2022-04-02 09:48:58 +00:00 committed by Tim Abbott
parent 087d4dff9d
commit 83178a474f
1 changed files with 55 additions and 52 deletions

View File

@ -1,10 +1,11 @@
from typing import Any, Dict, Optional
from typing import Any, Optional
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_float, check_int, check_string, to_wild_value
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.models import UserProfile
@ -12,7 +13,7 @@ GCI_MESSAGE_TEMPLATE = "**{actor}** {action} the task [{task_name}]({task_url}).
GCI_TOPIC_TEMPLATE = "{student_name}"
def build_instance_url(instance_id: str) -> str:
def build_instance_url(instance_id: int) -> str:
return f"https://codein.withgoogle.com/dashboard/task-instances/{instance_id}/"
@ -20,96 +21,98 @@ class UnknownEventType(Exception):
pass
def get_abandon_event_body(payload: Dict[str, Any]) -> str:
def get_abandon_event_body(payload: WildValue) -> str:
return GCI_MESSAGE_TEMPLATE.format(
actor=payload["task_claimed_by"],
action="{}ed".format(payload["event_type"]),
task_name=payload["task_definition_name"],
task_url=build_instance_url(payload["task_instance"]),
actor=payload["task_claimed_by"].tame(check_string),
action="{}ed".format(payload["event_type"].tame(check_string)),
task_name=payload["task_definition_name"].tame(check_string),
task_url=build_instance_url(payload["task_instance"].tame(check_int)),
)
def get_submit_event_body(payload: Dict[str, Any]) -> str:
def get_submit_event_body(payload: WildValue) -> str:
return GCI_MESSAGE_TEMPLATE.format(
actor=payload["task_claimed_by"],
action="{}ted".format(payload["event_type"]),
task_name=payload["task_definition_name"],
task_url=build_instance_url(payload["task_instance"]),
actor=payload["task_claimed_by"].tame(check_string),
action="{}ted".format(payload["event_type"].tame(check_string)),
task_name=payload["task_definition_name"].tame(check_string),
task_url=build_instance_url(payload["task_instance"].tame(check_int)),
)
def get_comment_event_body(payload: Dict[str, Any]) -> str:
def get_comment_event_body(payload: WildValue) -> str:
return GCI_MESSAGE_TEMPLATE.format(
actor=payload["author"],
action="{}ed on".format(payload["event_type"]),
task_name=payload["task_definition_name"],
task_url=build_instance_url(payload["task_instance"]),
actor=payload["author"].tame(check_string),
action="{}ed on".format(payload["event_type"].tame(check_string)),
task_name=payload["task_definition_name"].tame(check_string),
task_url=build_instance_url(payload["task_instance"].tame(check_int)),
)
def get_claim_event_body(payload: Dict[str, Any]) -> str:
def get_claim_event_body(payload: WildValue) -> str:
return GCI_MESSAGE_TEMPLATE.format(
actor=payload["task_claimed_by"],
action="{}ed".format(payload["event_type"]),
task_name=payload["task_definition_name"],
task_url=build_instance_url(payload["task_instance"]),
actor=payload["task_claimed_by"].tame(check_string),
action="{}ed".format(payload["event_type"].tame(check_string)),
task_name=payload["task_definition_name"].tame(check_string),
task_url=build_instance_url(payload["task_instance"].tame(check_int)),
)
def get_approve_event_body(payload: Dict[str, Any]) -> str:
def get_approve_event_body(payload: WildValue) -> str:
return GCI_MESSAGE_TEMPLATE.format(
actor=payload["author"],
action="{}d".format(payload["event_type"]),
task_name=payload["task_definition_name"],
task_url=build_instance_url(payload["task_instance"]),
actor=payload["author"].tame(check_string),
action="{}d".format(payload["event_type"].tame(check_string)),
task_name=payload["task_definition_name"].tame(check_string),
task_url=build_instance_url(payload["task_instance"].tame(check_int)),
)
def get_approve_pending_pc_event_body(payload: Dict[str, Any]) -> str:
def get_approve_pending_pc_event_body(payload: WildValue) -> str:
template = "{} (pending parental consent).".format(GCI_MESSAGE_TEMPLATE.rstrip("."))
return template.format(
actor=payload["author"],
actor=payload["author"].tame(check_string),
action="approved",
task_name=payload["task_definition_name"],
task_url=build_instance_url(payload["task_instance"]),
task_name=payload["task_definition_name"].tame(check_string),
task_url=build_instance_url(payload["task_instance"].tame(check_int)),
)
def get_needswork_event_body(payload: Dict[str, Any]) -> str:
def get_needswork_event_body(payload: WildValue) -> str:
template = "{} for more work.".format(GCI_MESSAGE_TEMPLATE.rstrip("."))
return template.format(
actor=payload["author"],
actor=payload["author"].tame(check_string),
action="submitted",
task_name=payload["task_definition_name"],
task_url=build_instance_url(payload["task_instance"]),
task_name=payload["task_definition_name"].tame(check_string),
task_url=build_instance_url(payload["task_instance"].tame(check_int)),
)
def get_extend_event_body(payload: Dict[str, Any]) -> str:
def get_extend_event_body(payload: WildValue) -> str:
template = "{} by {days} day(s).".format(
GCI_MESSAGE_TEMPLATE.rstrip("."), days=payload["extension_days"]
GCI_MESSAGE_TEMPLATE.rstrip("."), days=payload["extension_days"].tame(check_float)
)
return template.format(
actor=payload["author"],
actor=payload["author"].tame(check_string),
action="extended the deadline for",
task_name=payload["task_definition_name"],
task_url=build_instance_url(payload["task_instance"]),
task_name=payload["task_definition_name"].tame(check_string),
task_url=build_instance_url(payload["task_instance"].tame(check_int)),
)
def get_unassign_event_body(payload: Dict[str, Any]) -> str:
def get_unassign_event_body(payload: WildValue) -> str:
return GCI_MESSAGE_TEMPLATE.format(
actor=payload["author"],
action="unassigned **{student}** from".format(student=payload["task_claimed_by"]),
task_name=payload["task_definition_name"],
task_url=build_instance_url(payload["task_instance"]),
actor=payload["author"].tame(check_string),
action="unassigned **{student}** from".format(
student=payload["task_claimed_by"].tame(check_string)
),
task_name=payload["task_definition_name"].tame(check_string),
task_url=build_instance_url(payload["task_instance"].tame(check_int)),
)
def get_outoftime_event_body(payload: Dict[str, Any]) -> str:
def get_outoftime_event_body(payload: WildValue) -> str:
return "The deadline for the task [{task_name}]({task_url}) has passed.".format(
task_name=payload["task_definition_name"],
task_url=build_instance_url(payload["task_instance"]),
task_name=payload["task_definition_name"].tame(check_string),
task_url=build_instance_url(payload["task_instance"].tame(check_int)),
)
@ -134,21 +137,21 @@ ALL_EVENT_TYPES = list(EVENTS_FUNCTION_MAPPER.keys())
def api_gci_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 = get_event(payload)
if event is not None:
body = get_body_based_on_event(event)(payload)
subject = GCI_TOPIC_TEMPLATE.format(
student_name=payload["task_claimed_by"],
student_name=payload["task_claimed_by"].tame(check_string),
)
check_send_webhook_message(request, user_profile, subject, body, event)
return json_success(request)
def get_event(payload: Dict[str, Any]) -> Optional[str]:
event = payload["event_type"]
def get_event(payload: WildValue) -> Optional[str]:
event = payload["event_type"].tame(check_string)
if event in EVENTS_FUNCTION_MAPPER:
return event