2022-05-11 08:29:06 +02:00
|
|
|
from typing import Callable, Optional
|
2017-11-16 00:43:10 +01:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2017-11-16 00:43:10 +01:00
|
|
|
from zerver.lib.response import json_success
|
2022-04-02 11:48:58 +02:00
|
|
|
from zerver.lib.validator import WildValue, check_float, check_int, check_string, to_wild_value
|
2018-03-16 22:53:50 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2017-10-09 16:54:32 +02:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
GCI_MESSAGE_TEMPLATE = "**{actor}** {action} the task [{task_name}]({task_url})."
|
|
|
|
GCI_TOPIC_TEMPLATE = "{student_name}"
|
2017-10-09 16:54:32 +02:00
|
|
|
|
|
|
|
|
2022-04-02 11:48:58 +02:00
|
|
|
def build_instance_url(instance_id: int) -> str:
|
2020-06-09 00:25:09 +02:00
|
|
|
return f"https://codein.withgoogle.com/dashboard/task-instances/{instance_id}/"
|
2017-11-28 22:38:11 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-10-09 16:54:32 +02:00
|
|
|
class UnknownEventType(Exception):
|
|
|
|
pass
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-02 11:48:58 +02:00
|
|
|
def get_abandon_event_body(payload: WildValue) -> str:
|
2017-10-09 16:54:32 +02:00
|
|
|
return GCI_MESSAGE_TEMPLATE.format(
|
2022-04-02 11:48:58 +02:00
|
|
|
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)),
|
2017-10-09 16:54:32 +02:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-02 11:48:58 +02:00
|
|
|
def get_submit_event_body(payload: WildValue) -> str:
|
2017-10-16 21:44:14 +02:00
|
|
|
return GCI_MESSAGE_TEMPLATE.format(
|
2022-04-02 11:48:58 +02:00
|
|
|
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)),
|
2017-10-16 21:44:14 +02:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-02 11:48:58 +02:00
|
|
|
def get_comment_event_body(payload: WildValue) -> str:
|
2017-10-16 21:44:14 +02:00
|
|
|
return GCI_MESSAGE_TEMPLATE.format(
|
2022-04-02 11:48:58 +02:00
|
|
|
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)),
|
2017-10-16 21:44:14 +02:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-02 11:48:58 +02:00
|
|
|
def get_claim_event_body(payload: WildValue) -> str:
|
2017-10-16 21:44:14 +02:00
|
|
|
return GCI_MESSAGE_TEMPLATE.format(
|
2022-04-02 11:48:58 +02:00
|
|
|
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)),
|
2017-10-16 21:44:14 +02:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-02 11:48:58 +02:00
|
|
|
def get_approve_event_body(payload: WildValue) -> str:
|
2017-10-16 21:44:14 +02:00
|
|
|
return GCI_MESSAGE_TEMPLATE.format(
|
2022-04-02 11:48:58 +02:00
|
|
|
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)),
|
2017-10-16 21:44:14 +02:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-02 11:48:58 +02:00
|
|
|
def get_approve_pending_pc_event_body(payload: WildValue) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
template = "{} (pending parental consent).".format(GCI_MESSAGE_TEMPLATE.rstrip("."))
|
2017-12-17 23:56:29 +01:00
|
|
|
return template.format(
|
2022-04-02 11:48:58 +02:00
|
|
|
actor=payload["author"].tame(check_string),
|
2021-02-12 08:20:45 +01:00
|
|
|
action="approved",
|
2022-04-02 11:48:58 +02:00
|
|
|
task_name=payload["task_definition_name"].tame(check_string),
|
|
|
|
task_url=build_instance_url(payload["task_instance"].tame(check_int)),
|
2017-12-17 23:56:29 +01:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-02 11:48:58 +02:00
|
|
|
def get_needswork_event_body(payload: WildValue) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
template = "{} for more work.".format(GCI_MESSAGE_TEMPLATE.rstrip("."))
|
2017-11-02 20:16:11 +01:00
|
|
|
return template.format(
|
2022-04-02 11:48:58 +02:00
|
|
|
actor=payload["author"].tame(check_string),
|
2021-02-12 08:20:45 +01:00
|
|
|
action="submitted",
|
2022-04-02 11:48:58 +02:00
|
|
|
task_name=payload["task_definition_name"].tame(check_string),
|
|
|
|
task_url=build_instance_url(payload["task_instance"].tame(check_int)),
|
2017-11-02 20:16:11 +01:00
|
|
|
)
|
2017-10-09 16:54:32 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-02 11:48:58 +02:00
|
|
|
def get_extend_event_body(payload: WildValue) -> str:
|
2021-02-12 08:19:30 +01:00
|
|
|
template = "{} by {days} day(s).".format(
|
2022-04-02 11:48:58 +02:00
|
|
|
GCI_MESSAGE_TEMPLATE.rstrip("."), days=payload["extension_days"].tame(check_float)
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2018-01-02 22:07:42 +01:00
|
|
|
return template.format(
|
2022-04-02 11:48:58 +02:00
|
|
|
actor=payload["author"].tame(check_string),
|
2021-02-12 08:20:45 +01:00
|
|
|
action="extended the deadline for",
|
2022-04-02 11:48:58 +02:00
|
|
|
task_name=payload["task_definition_name"].tame(check_string),
|
|
|
|
task_url=build_instance_url(payload["task_instance"].tame(check_int)),
|
2018-01-02 22:07:42 +01:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-02 11:48:58 +02:00
|
|
|
def get_unassign_event_body(payload: WildValue) -> str:
|
2018-01-02 22:19:34 +01:00
|
|
|
return GCI_MESSAGE_TEMPLATE.format(
|
2022-04-02 11:48:58 +02:00
|
|
|
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)),
|
2018-01-02 22:19:34 +01:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-02 11:48:58 +02:00
|
|
|
def get_outoftime_event_body(payload: WildValue) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
return "The deadline for the task [{task_name}]({task_url}) has passed.".format(
|
2022-04-02 11:48:58 +02:00
|
|
|
task_name=payload["task_definition_name"].tame(check_string),
|
|
|
|
task_url=build_instance_url(payload["task_instance"].tame(check_int)),
|
2018-01-17 00:28:14 +01:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
EVENTS_FUNCTION_MAPPER = {
|
|
|
|
"abandon": get_abandon_event_body,
|
|
|
|
"approve": get_approve_event_body,
|
|
|
|
"approve-pending-pc": get_approve_pending_pc_event_body,
|
|
|
|
"claim": get_claim_event_body,
|
|
|
|
"comment": get_comment_event_body,
|
|
|
|
"extend": get_extend_event_body,
|
|
|
|
"needswork": get_needswork_event_body,
|
|
|
|
"outoftime": get_outoftime_event_body,
|
|
|
|
"submit": get_submit_event_body,
|
|
|
|
"unassign": get_unassign_event_body,
|
|
|
|
}
|
|
|
|
|
|
|
|
ALL_EVENT_TYPES = list(EVENTS_FUNCTION_MAPPER.keys())
|
|
|
|
|
|
|
|
|
|
|
|
@webhook_view("GoogleCodeIn", all_event_types=ALL_EVENT_TYPES)
|
2017-10-09 16:54:32 +02:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_gci_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2022-04-02 11:48:58 +02:00
|
|
|
payload: WildValue = REQ(argument_type="body", converter=to_wild_value),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2017-10-09 16:54:32 +02:00
|
|
|
event = get_event(payload)
|
|
|
|
if event is not None:
|
|
|
|
body = get_body_based_on_event(event)(payload)
|
2018-11-09 21:02:59 +01:00
|
|
|
subject = GCI_TOPIC_TEMPLATE.format(
|
2022-04-02 11:48:58 +02:00
|
|
|
student_name=payload["task_claimed_by"].tame(check_string),
|
2017-10-09 16:54:32 +02:00
|
|
|
)
|
2021-07-16 11:40:46 +02:00
|
|
|
check_send_webhook_message(request, user_profile, subject, body, event)
|
2017-10-09 16:54:32 +02:00
|
|
|
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2017-10-09 16:54:32 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-02 11:48:58 +02:00
|
|
|
def get_event(payload: WildValue) -> Optional[str]:
|
|
|
|
event = payload["event_type"].tame(check_string)
|
2017-10-09 16:54:32 +02:00
|
|
|
if event in EVENTS_FUNCTION_MAPPER:
|
|
|
|
return event
|
|
|
|
|
2020-06-09 00:25:09 +02:00
|
|
|
raise UnknownEventType(f"Event '{event}' is unknown and cannot be handled") # nocoverage
|
2017-10-09 16:54:32 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-05-11 08:29:06 +02:00
|
|
|
def get_body_based_on_event(event: str) -> Callable[[WildValue], str]:
|
2017-10-09 16:54:32 +02:00
|
|
|
return EVENTS_FUNCTION_MAPPER[event]
|