mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	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:
		@@ -98,9 +98,10 @@ def check_send_webhook_message(
 | 
				
			|||||||
            pass
 | 
					            pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def validate_extract_webhook_http_header(request: HttpRequest, header: str,
 | 
					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)
 | 
					    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(
 | 
					        message_body = MISSING_EVENT_HEADER_MESSAGE.format(
 | 
				
			||||||
            bot_name=request.user.full_name,
 | 
					            bot_name=request.user.full_name,
 | 
				
			||||||
            request_path=request.path,
 | 
					            request_path=request.path,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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.
 | 
					        # Note that we only need the HTTP header to determine pullrequest events.
 | 
				
			||||||
        # We rely on the payload itself to determine the other ones.
 | 
					        # We rely on the payload itself to determine the other ones.
 | 
				
			||||||
        event_key = validate_extract_webhook_http_header(request, "X_EVENT_KEY", "BitBucket")
 | 
					        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)
 | 
					        action = re.match('pullrequest:(?P<action>.*)$', event_key)
 | 
				
			||||||
        if action:
 | 
					        if action:
 | 
				
			||||||
            action_group = action.group('action')
 | 
					            action_group = action.group('action')
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -168,6 +168,7 @@ def api_reviewboard_webhook(
 | 
				
			|||||||
) -> HttpResponse:
 | 
					) -> HttpResponse:
 | 
				
			||||||
    event_type = validate_extract_webhook_http_header(
 | 
					    event_type = validate_extract_webhook_http_header(
 | 
				
			||||||
        request, 'X_REVIEWBOARD_EVENT', 'ReviewBoard')
 | 
					        request, 'X_REVIEWBOARD_EVENT', 'ReviewBoard')
 | 
				
			||||||
 | 
					    assert event_type is not None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    body_function = RB_MESSAGE_FUNCTIONS.get(event_type)
 | 
					    body_function = RB_MESSAGE_FUNCTIONS.get(event_type)
 | 
				
			||||||
    if body_function is not None:
 | 
					    if body_function is not None:
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user