webhooks/common: Allow suppressing exceptions for missing headers.

This is useful in cases where an event type HTTP header isn't
crucial for inferring the payload's type.
This commit is contained in:
Eeshan Garg
2019-01-31 22:05:10 -03:30
parent 5779320874
commit f9ee393e4f
3 changed files with 5 additions and 2 deletions

View File

@@ -98,9 +98,10 @@ def check_send_webhook_message(
pass
def validate_extract_webhook_http_header(request: HttpRequest, header: str,
integration_name: str) -> str:
integration_name: str,
fatal: Optional[bool]=True) -> Optional[str]:
extracted_header = request.META.get(DJANGO_HTTP_PREFIX + header)
if extracted_header is None:
if extracted_header is None and fatal:
message_body = MISSING_EVENT_HEADER_MESSAGE.format(
bot_name=request.user.full_name,
request_path=request.path,

View File

@@ -142,6 +142,7 @@ def get_type(request: HttpRequest, payload: Dict[str, Any]) -> str:
# Note that we only need the HTTP header to determine pullrequest events.
# We rely on the payload itself to determine the other ones.
event_key = validate_extract_webhook_http_header(request, "X_EVENT_KEY", "BitBucket")
assert event_key is not None
action = re.match('pullrequest:(?P<action>.*)$', event_key)
if action:
action_group = action.group('action')

View File

@@ -168,6 +168,7 @@ def api_reviewboard_webhook(
) -> HttpResponse:
event_type = validate_extract_webhook_http_header(
request, 'X_REVIEWBOARD_EVENT', 'ReviewBoard')
assert event_type is not None
body_function = RB_MESSAGE_FUNCTIONS.get(event_type)
if body_function is not None: