github refactor: Use early-exit idiom.

We also comment a bit more explicitly about the
None case.
This commit is contained in:
Steve Howell 2020-09-01 15:18:28 +00:00 committed by Tim Abbott
parent 5c916135c9
commit 0d9b1817f9
1 changed files with 19 additions and 11 deletions

View File

@ -554,17 +554,25 @@ def api_github_webhook(
branches: Optional[str]=REQ(default=None),
user_specified_topic: Optional[str]=REQ("topic", default=None)) -> HttpResponse:
event = get_event(request, payload, branches)
if event is not None:
subject = get_subject_based_on_type(payload, event)
body_function = get_body_function_based_on_type(event)
if 'include_title' in signature(body_function).parameters:
body = body_function(
payload,
include_title=user_specified_topic is not None,
)
else:
body = body_function(payload)
check_send_webhook_message(request, user_profile, subject, body)
if event is None:
# This is nothing to worry about--get_event() returns None
# for events that are valid but not yet handled by us.
# See IGNORED_EVENTS, for example.
return json_success()
subject = get_subject_based_on_type(payload, event)
body_function = get_body_function_based_on_type(event)
if 'include_title' in signature(body_function).parameters:
body = body_function(
payload,
include_title=user_specified_topic is not None,
)
else:
body = body_function(payload)
check_send_webhook_message(request, user_profile, subject, body)
return json_success()
def get_event(request: HttpRequest, payload: Dict[str, Any], branches: Optional[str]) -> Optional[str]: