webhooks: Migrate to UnexpectedWebhookEventType.

This improves test coverage for a lot of our webhooks that relied
on ad-hoc methods to handle unexpected event types.

Note that I have deliberately skipped github_legacy, it isn't
advertised and is officially deprecated.

Also, I have refrained from making further changes to Trello, I
believe further improvements to test coverage should be covered
in separate per-webhook commits/PRs.
This commit is contained in:
Eeshan Garg
2018-05-22 12:16:45 -02:30
committed by Tim Abbott
parent 3ed20589f2
commit e0ef831993
15 changed files with 40 additions and 70 deletions

View File

@@ -10,7 +10,7 @@ from zerver.decorator import api_key_only_webhook_view
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_error, json_success
from zerver.lib.webhooks.common import check_send_webhook_message, \
validate_extract_webhook_http_header
validate_extract_webhook_http_header, UnexpectedWebhookEventType
from zerver.lib.webhooks.git import SUBJECT_WITH_BRANCH_TEMPLATE, \
SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE, \
get_commits_comment_action_message, get_force_push_commits_event_message, \
@@ -41,10 +41,6 @@ PULL_REQUEST_SUPPORTED_ACTIONS = [
'comment_deleted',
]
class UnknownTriggerType(Exception):
pass
@api_key_only_webhook_view('Bitbucket2')
@has_request_variables
def api_bitbucket2_webhook(request: HttpRequest, user_profile: UserProfile,
@@ -142,7 +138,7 @@ def get_type(request: HttpRequest, payload: Dict[str, Any]) -> str:
if event_key == 'repo:updated':
return event_key
raise UnknownTriggerType("We don't support {} event type".format(event_key))
raise UnexpectedWebhookEventType('BitBucket2', event_key)
def get_body_based_on_type(type: str) -> Callable[[Dict[str, Any]], str]:
fn = GET_SINGLE_MESSAGE_BODY_DEPENDING_ON_TYPE_MAPPER.get(type)