2019-02-02 23:53:55 +01:00
|
|
|
from typing import Any, Dict, Mapping, Optional, Tuple
|
2017-11-16 00:43:10 +01:00
|
|
|
|
2019-12-19 21:26:33 +01:00
|
|
|
from zerver.lib.exceptions import UnexpectedWebhookEventType
|
2016-05-05 07:06:41 +02:00
|
|
|
|
|
|
|
SUPPORTED_BOARD_ACTIONS = [
|
2020-04-09 21:51:58 +02:00
|
|
|
'removeMemberFromBoard',
|
|
|
|
'addMemberToBoard',
|
|
|
|
'createList',
|
|
|
|
'updateBoard',
|
2016-05-05 07:06:41 +02:00
|
|
|
]
|
|
|
|
|
2020-04-09 21:51:58 +02:00
|
|
|
REMOVE_MEMBER = 'removeMemberFromBoard'
|
|
|
|
ADD_MEMBER = 'addMemberToBoard'
|
|
|
|
CREATE_LIST = 'createList'
|
|
|
|
CHANGE_NAME = 'changeName'
|
2016-05-05 07:06:41 +02:00
|
|
|
|
2020-04-09 21:51:58 +02:00
|
|
|
TRELLO_BOARD_URL_TEMPLATE = '[{board_name}]({board_url})'
|
2016-05-05 07:06:41 +02:00
|
|
|
|
|
|
|
ACTIONS_TO_MESSAGE_MAPPER = {
|
2020-04-09 21:51:58 +02:00
|
|
|
REMOVE_MEMBER: 'removed {member_name} from {board_url_template}.',
|
|
|
|
ADD_MEMBER: 'added {member_name} to {board_url_template}.',
|
|
|
|
CREATE_LIST: 'added {list_name} list to {board_url_template}.',
|
|
|
|
CHANGE_NAME: 'renamed the board from {old_name} to {board_url_template}.'
|
2016-05-05 07:06:41 +02:00
|
|
|
}
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def process_board_action(payload: Mapping[str, Any],
|
2018-05-10 19:34:01 +02:00
|
|
|
action_type: Optional[str]) -> Optional[Tuple[str, str]]:
|
2016-05-05 07:06:41 +02:00
|
|
|
action_type = get_proper_action(payload, action_type)
|
2017-09-15 03:11:48 +02:00
|
|
|
if action_type is not None:
|
|
|
|
return get_subject(payload), get_body(payload, action_type)
|
|
|
|
return None
|
2016-05-05 07:06:41 +02:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_proper_action(payload: Mapping[str, Any], action_type: Optional[str]) -> Optional[str]:
|
2016-05-05 07:06:41 +02:00
|
|
|
if action_type == 'updateBoard':
|
|
|
|
data = get_action_data(payload)
|
2017-09-15 03:11:48 +02:00
|
|
|
# we don't support events for when a board's background
|
|
|
|
# is changed
|
|
|
|
if data['old'].get('prefs', {}).get('background') is not None:
|
|
|
|
return None
|
|
|
|
elif data['old']['name']:
|
2016-05-05 07:06:41 +02:00
|
|
|
return CHANGE_NAME
|
2019-12-19 21:26:33 +01:00
|
|
|
raise UnexpectedWebhookEventType("Trello", action_type)
|
2016-05-05 07:06:41 +02:00
|
|
|
return action_type
|
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_subject(payload: Mapping[str, Any]) -> str:
|
2017-09-24 01:07:17 +02:00
|
|
|
return get_action_data(payload)['board']['name']
|
2016-05-05 07:06:41 +02:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_body(payload: Mapping[str, Any], action_type: str) -> str:
|
2016-05-05 07:06:41 +02:00
|
|
|
message_body = ACTIONS_TO_FILL_BODY_MAPPER[action_type](payload, action_type)
|
2017-05-24 04:21:29 +02:00
|
|
|
creator = payload['action']['memberCreator']['fullName']
|
2020-06-09 00:25:09 +02:00
|
|
|
return f'{creator} {message_body}'
|
2016-05-05 07:06:41 +02:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_managed_member_body(payload: Mapping[str, Any], action_type: str) -> str:
|
2016-05-05 07:06:41 +02:00
|
|
|
data = {
|
2017-05-24 04:21:29 +02:00
|
|
|
'member_name': payload['action']['member']['fullName'],
|
2016-05-05 07:06:41 +02:00
|
|
|
}
|
|
|
|
return fill_appropriate_message_content(payload, action_type, data)
|
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_create_list_body(payload: Mapping[str, Any], action_type: str) -> str:
|
2016-05-05 07:06:41 +02:00
|
|
|
data = {
|
2017-05-24 04:21:29 +02:00
|
|
|
'list_name': get_action_data(payload)['list']['name'],
|
2016-05-05 07:06:41 +02:00
|
|
|
}
|
|
|
|
return fill_appropriate_message_content(payload, action_type, data)
|
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_change_name_body(payload: Mapping[str, Any], action_type: str) -> str:
|
2016-05-05 07:06:41 +02:00
|
|
|
data = {
|
2017-05-24 04:21:29 +02:00
|
|
|
'old_name': get_action_data(payload)['old']['name']
|
2016-05-05 07:06:41 +02:00
|
|
|
}
|
|
|
|
return fill_appropriate_message_content(payload, action_type, data)
|
|
|
|
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def fill_appropriate_message_content(payload: Mapping[str, Any],
|
2018-05-10 19:34:01 +02:00
|
|
|
action_type: str,
|
|
|
|
data: Optional[Dict[str, Any]]=None) -> str:
|
2016-05-05 07:06:41 +02:00
|
|
|
data = {} if data is None else data
|
|
|
|
data['board_url_template'] = data.get('board_url_template', get_filled_board_url_template(payload))
|
|
|
|
message_body = get_message_body(action_type)
|
|
|
|
return message_body.format(**data)
|
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_filled_board_url_template(payload: Mapping[str, Any]) -> str:
|
2017-11-05 02:48:25 +01:00
|
|
|
return TRELLO_BOARD_URL_TEMPLATE.format(board_name=get_board_name(payload),
|
|
|
|
board_url=get_board_url(payload))
|
2016-05-05 07:06:41 +02:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_board_name(payload: Mapping[str, Any]) -> str:
|
2017-05-24 04:21:29 +02:00
|
|
|
return get_action_data(payload)['board']['name']
|
2016-05-05 07:06:41 +02:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_board_url(payload: Mapping[str, Any]) -> str:
|
2020-04-09 21:51:58 +02:00
|
|
|
return 'https://trello.com/b/{}'.format(get_action_data(payload)['board']['shortLink'])
|
2016-05-05 07:06:41 +02:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_message_body(action_type: str) -> str:
|
2016-05-05 07:06:41 +02:00
|
|
|
return ACTIONS_TO_MESSAGE_MAPPER[action_type]
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_action_data(payload: Mapping[str, Any]) -> Mapping[str, Any]:
|
2017-05-24 04:21:29 +02:00
|
|
|
return payload['action']['data']
|
2016-05-05 07:06:41 +02:00
|
|
|
|
|
|
|
ACTIONS_TO_FILL_BODY_MAPPER = {
|
|
|
|
REMOVE_MEMBER: get_managed_member_body,
|
|
|
|
ADD_MEMBER: get_managed_member_body,
|
|
|
|
CREATE_LIST: get_create_list_body,
|
|
|
|
CHANGE_NAME: get_change_name_body
|
|
|
|
}
|