mirror of
https://github.com/zulip/zulip.git
synced 2025-10-29 11:03:54 +00:00
webhooks/jira: Use dictionary to map events to functions.
This commit is contained in:
committed by
Eeshan Garg
parent
ee2af16dc9
commit
a47edd4fc8
@@ -13,7 +13,6 @@ from zerver.lib.webhooks.common import check_send_webhook_message, \
|
|||||||
from zerver.models import Realm, UserProfile, get_user_by_delivery_email
|
from zerver.models import Realm, UserProfile, get_user_by_delivery_email
|
||||||
|
|
||||||
IGNORED_EVENTS = [
|
IGNORED_EVENTS = [
|
||||||
'comment_created', # we handle issue_update event instead
|
|
||||||
'comment_deleted', # we handle issue_update event instead
|
'comment_deleted', # we handle issue_update event instead
|
||||||
'issuelink_created',
|
'issuelink_created',
|
||||||
'comment_updated',
|
'comment_updated',
|
||||||
@@ -215,7 +214,7 @@ def handle_updated_issue_event(payload: Dict[str, Any], user_profile: UserProfil
|
|||||||
|
|
||||||
return content
|
return content
|
||||||
|
|
||||||
def handle_created_issue_event(payload: Dict[str, Any]) -> str:
|
def handle_created_issue_event(payload: Dict[str, Any], user_profile: UserProfile) -> str:
|
||||||
return u"{} **created** {} priority {}, assigned to **{}**:\n\n> {}".format(
|
return u"{} **created** {} priority {}, assigned to **{}**:\n\n> {}".format(
|
||||||
get_issue_author(payload),
|
get_issue_author(payload),
|
||||||
get_issue_string(payload),
|
get_issue_string(payload),
|
||||||
@@ -224,32 +223,35 @@ def handle_created_issue_event(payload: Dict[str, Any]) -> str:
|
|||||||
get_issue_title(payload)
|
get_issue_title(payload)
|
||||||
)
|
)
|
||||||
|
|
||||||
def handle_deleted_issue_event(payload: Dict[str, Any]) -> str:
|
def handle_deleted_issue_event(payload: Dict[str, Any], user_profile: UserProfile) -> str:
|
||||||
return u"{} **deleted** {}!".format(get_issue_author(payload), get_issue_string(payload))
|
return u"{} **deleted** {}!".format(get_issue_author(payload), get_issue_string(payload))
|
||||||
|
|
||||||
|
JIRA_CONTENT_FUNCTION_MAPPER = {
|
||||||
|
"jira:issue_created": handle_created_issue_event,
|
||||||
|
"jira:issue_deleted": handle_deleted_issue_event,
|
||||||
|
"jira:issue_updated": handle_updated_issue_event,
|
||||||
|
"comment_created": handle_updated_issue_event
|
||||||
|
}
|
||||||
|
|
||||||
@api_key_only_webhook_view("JIRA")
|
@api_key_only_webhook_view("JIRA")
|
||||||
@has_request_variables
|
@has_request_variables
|
||||||
def api_jira_webhook(request: HttpRequest, user_profile: UserProfile,
|
def api_jira_webhook(request: HttpRequest, user_profile: UserProfile,
|
||||||
payload: Dict[str, Any]=REQ(argument_type='body')) -> HttpResponse:
|
payload: Dict[str, Any]=REQ(argument_type='body')) -> HttpResponse:
|
||||||
|
|
||||||
event = get_event_type(payload)
|
event = get_event_type(payload)
|
||||||
if event == 'jira:issue_created':
|
subject = get_issue_subject(payload)
|
||||||
subject = get_issue_subject(payload)
|
|
||||||
content = handle_created_issue_event(payload)
|
if event in IGNORED_EVENTS:
|
||||||
elif event == 'jira:issue_deleted':
|
|
||||||
subject = get_issue_subject(payload)
|
|
||||||
content = handle_deleted_issue_event(payload)
|
|
||||||
elif event == 'jira:issue_updated':
|
|
||||||
subject = get_issue_subject(payload)
|
|
||||||
content = handle_updated_issue_event(payload, user_profile)
|
|
||||||
elif event == 'comment_created':
|
|
||||||
subject = get_issue_subject(payload)
|
|
||||||
content = handle_updated_issue_event(payload, user_profile)
|
|
||||||
elif event in IGNORED_EVENTS:
|
|
||||||
return json_success()
|
return json_success()
|
||||||
else:
|
|
||||||
|
if event is not None:
|
||||||
|
content_func = JIRA_CONTENT_FUNCTION_MAPPER.get(event) # type: Any
|
||||||
|
|
||||||
|
if subject is None or content_func is None:
|
||||||
raise UnexpectedWebhookEventType('Jira', event)
|
raise UnexpectedWebhookEventType('Jira', event)
|
||||||
|
|
||||||
|
content = content_func(payload, user_profile) # type: str
|
||||||
|
|
||||||
check_send_webhook_message(request, user_profile,
|
check_send_webhook_message(request, user_profile,
|
||||||
subject, content,
|
subject, content,
|
||||||
unquote_url_parameters=True)
|
unquote_url_parameters=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user