2024-07-12 02:30:25 +02:00
|
|
|
from collections.abc import Mapping
|
2017-11-16 00:43:10 +01:00
|
|
|
|
2022-11-17 09:30:48 +01:00
|
|
|
from zerver.lib.exceptions import UnsupportedWebhookEventTypeError
|
2022-06-27 18:26:40 +02:00
|
|
|
from zerver.lib.validator import WildValue, check_bool, check_none_or, check_string
|
2016-05-05 07:06:41 +02:00
|
|
|
|
|
|
|
SUPPORTED_CARD_ACTIONS = [
|
2021-02-12 08:20:45 +01:00
|
|
|
"updateCard",
|
|
|
|
"createCard",
|
|
|
|
"addLabelToCard",
|
|
|
|
"removeLabelFromCard",
|
|
|
|
"addMemberToCard",
|
|
|
|
"removeMemberFromCard",
|
|
|
|
"addAttachmentToCard",
|
|
|
|
"addChecklistToCard",
|
|
|
|
"commentCard",
|
|
|
|
"updateCheckItemStateOnCard",
|
2016-05-05 07:06:41 +02:00
|
|
|
]
|
|
|
|
|
2019-02-21 23:16:09 +01:00
|
|
|
IGNORED_CARD_ACTIONS = [
|
2020-09-03 16:00:59 +02:00
|
|
|
"copyCard",
|
2020-08-20 18:06:30 +02:00
|
|
|
"createCheckItem",
|
|
|
|
"updateCheckItem",
|
|
|
|
"updateList",
|
2019-02-21 23:16:09 +01:00
|
|
|
]
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
CREATE = "createCard"
|
|
|
|
CHANGE_LIST = "changeList"
|
|
|
|
CHANGE_NAME = "changeName"
|
|
|
|
SET_DESC = "setDesc"
|
|
|
|
CHANGE_DESC = "changeDesc"
|
|
|
|
REMOVE_DESC = "removeDesc"
|
|
|
|
ARCHIVE = "archiveCard"
|
|
|
|
REOPEN = "reopenCard"
|
|
|
|
SET_DUE_DATE = "setDueDate"
|
|
|
|
CHANGE_DUE_DATE = "changeDueDate"
|
|
|
|
REMOVE_DUE_DATE = "removeDueDate"
|
|
|
|
ADD_LABEL = "addLabelToCard"
|
|
|
|
REMOVE_LABEL = "removeLabelFromCard"
|
|
|
|
ADD_MEMBER = "addMemberToCard"
|
|
|
|
REMOVE_MEMBER = "removeMemberFromCard"
|
|
|
|
ADD_ATTACHMENT = "addAttachmentToCard"
|
|
|
|
ADD_CHECKLIST = "addChecklistToCard"
|
|
|
|
COMMENT = "commentCard"
|
|
|
|
UPDATE_CHECK_ITEM_STATE = "updateCheckItemStateOnCard"
|
|
|
|
|
|
|
|
TRELLO_CARD_URL_TEMPLATE = "[{card_name}]({card_url})"
|
2016-05-05 07:06:41 +02:00
|
|
|
|
|
|
|
ACTIONS_TO_MESSAGE_MAPPER = {
|
2021-02-12 08:20:45 +01:00
|
|
|
CREATE: "created {card_url_template}.",
|
|
|
|
CHANGE_LIST: "moved {card_url_template} from {old_list} to {new_list}.",
|
2020-04-09 21:51:58 +02:00
|
|
|
CHANGE_NAME: 'renamed the card from "{old_name}" to {card_url_template}.',
|
2021-02-12 08:20:45 +01:00
|
|
|
SET_DESC: "set description for {card_url_template} to:\n~~~ quote\n{desc}\n~~~\n",
|
2021-02-12 08:19:30 +01:00
|
|
|
CHANGE_DESC: (
|
2021-02-12 08:20:45 +01:00
|
|
|
"changed description for {card_url_template} from\n"
|
2023-01-03 02:16:53 +01:00
|
|
|
"~~~ quote\n{old_desc}\n~~~\nto\n~~~ quote\n{desc}\n~~~\n"
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
REMOVE_DESC: "removed description from {card_url_template}.",
|
|
|
|
ARCHIVE: "archived {card_url_template}.",
|
|
|
|
REOPEN: "reopened {card_url_template}.",
|
|
|
|
SET_DUE_DATE: "set due date for {card_url_template} to {due_date}.",
|
|
|
|
CHANGE_DUE_DATE: "changed due date for {card_url_template} from {old_due_date} to {due_date}.",
|
|
|
|
REMOVE_DUE_DATE: "removed the due date from {card_url_template}.",
|
|
|
|
ADD_LABEL: 'added a {color} label with "{text}" to {card_url_template}.',
|
|
|
|
REMOVE_LABEL: 'removed a {color} label with "{text}" from {card_url_template}.',
|
|
|
|
ADD_MEMBER: "added {member_name} to {card_url_template}.",
|
|
|
|
REMOVE_MEMBER: "removed {member_name} from {card_url_template}.",
|
|
|
|
ADD_ATTACHMENT: "added [{attachment_name}]({attachment_url}) to {card_url_template}.",
|
|
|
|
ADD_CHECKLIST: "added the {checklist_name} checklist to {card_url_template}.",
|
|
|
|
COMMENT: "commented on {card_url_template}:\n~~~ quote\n{text}\n~~~\n",
|
|
|
|
UPDATE_CHECK_ITEM_STATE: "{action} **{item_name}** in **{checklist_name}** ({card_url_template}).",
|
2016-05-05 07:06:41 +02:00
|
|
|
}
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def prettify_date(date_string: str) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
return date_string.replace("T", " ").replace(".000", "").replace("Z", " UTC")
|
2017-02-26 09:10:14 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
def process_card_action(payload: WildValue, action_type: str) -> tuple[str, str] | None:
|
2017-08-06 08:31:36 +02:00
|
|
|
proper_action = get_proper_action(payload, action_type)
|
|
|
|
if proper_action is not None:
|
2023-07-12 13:37:08 +02:00
|
|
|
return get_topic(payload), get_body(payload, proper_action)
|
2017-07-27 06:33:57 +02:00
|
|
|
return None
|
2016-05-05 07:06:41 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
def get_proper_action(payload: WildValue, action_type: str) -> str | None:
|
2021-02-12 08:20:45 +01:00
|
|
|
if action_type == "updateCard":
|
2016-05-05 07:06:41 +02:00
|
|
|
data = get_action_data(payload)
|
2021-02-12 08:20:45 +01:00
|
|
|
old_data = data["old"]
|
|
|
|
card_data = data["card"]
|
|
|
|
if data.get("listBefore"):
|
2016-05-05 07:06:41 +02:00
|
|
|
return CHANGE_LIST
|
2022-06-27 18:26:40 +02:00
|
|
|
if old_data.get("name").tame(check_none_or(check_string)):
|
2016-05-05 07:06:41 +02:00
|
|
|
return CHANGE_NAME
|
2022-06-27 18:26:40 +02:00
|
|
|
if old_data.get("desc").tame(check_none_or(check_string)) == "":
|
2017-07-04 19:24:59 +02:00
|
|
|
return SET_DESC
|
2022-06-27 18:26:40 +02:00
|
|
|
if old_data.get("desc").tame(check_none_or(check_string)):
|
|
|
|
if card_data.get("desc").tame(check_none_or(check_string)) == "":
|
2017-07-04 19:24:59 +02:00
|
|
|
return REMOVE_DESC
|
|
|
|
else:
|
|
|
|
return CHANGE_DESC
|
2022-06-27 18:26:40 +02:00
|
|
|
if old_data.get("due", "").tame(check_none_or(check_string)) is None:
|
2016-05-05 07:06:41 +02:00
|
|
|
return SET_DUE_DATE
|
2022-06-27 18:26:40 +02:00
|
|
|
if old_data.get("due").tame(check_none_or(check_string)):
|
|
|
|
if card_data.get("due", "").tame(check_none_or(check_string)) is None:
|
2016-05-05 07:06:41 +02:00
|
|
|
return REMOVE_DUE_DATE
|
|
|
|
else:
|
|
|
|
return CHANGE_DUE_DATE
|
2022-06-27 18:26:40 +02:00
|
|
|
if old_data.get("closed").tame(check_none_or(check_bool)) is False and card_data.get(
|
|
|
|
"closed", False
|
|
|
|
).tame(check_bool):
|
2016-05-05 07:06:41 +02:00
|
|
|
return ARCHIVE
|
2022-06-27 18:26:40 +02:00
|
|
|
if (
|
|
|
|
old_data.get("closed").tame(check_none_or(check_bool))
|
|
|
|
and card_data.get("closed").tame(check_none_or(check_bool)) is False
|
|
|
|
):
|
2016-05-05 07:06:41 +02:00
|
|
|
return REOPEN
|
2020-08-22 14:33:01 +02:00
|
|
|
# We don't support events for when a card is moved up or down
|
|
|
|
# within a single list (pos), or when the cover changes (cover).
|
|
|
|
# We also don't know if "dueComplete" is just a new name for "due".
|
2020-08-21 14:12:49 +02:00
|
|
|
ignored_fields = [
|
|
|
|
"cover",
|
2020-08-22 14:33:01 +02:00
|
|
|
"dueComplete",
|
2020-08-21 14:12:49 +02:00
|
|
|
"idAttachmentCover",
|
|
|
|
"pos",
|
|
|
|
]
|
|
|
|
for field in ignored_fields:
|
2022-06-27 18:26:40 +02:00
|
|
|
if field in old_data:
|
2020-08-21 14:12:49 +02:00
|
|
|
return None
|
2022-11-17 09:30:48 +01:00
|
|
|
raise UnsupportedWebhookEventTypeError(action_type)
|
2016-05-05 07:06:41 +02:00
|
|
|
|
|
|
|
return action_type
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2023-07-12 13:37:08 +02:00
|
|
|
def get_topic(payload: WildValue) -> str:
|
2022-06-27 18:26:40 +02:00
|
|
|
return get_action_data(payload)["board"]["name"].tame(check_string)
|
2016-05-05 07:06:41 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 18:26:40 +02:00
|
|
|
def get_body(payload: WildValue, action_type: str) -> str:
|
2017-05-24 23:03:06 +02:00
|
|
|
message_body = ACTIONS_TO_FILL_BODY_MAPPER[action_type](payload, action_type)
|
2022-06-27 18:26:40 +02:00
|
|
|
creator = payload["action"]["memberCreator"].get("fullName").tame(check_none_or(check_string))
|
2021-02-12 08:20:45 +01:00
|
|
|
return f"{creator} {message_body}"
|
2016-05-05 07:06:41 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 18:26:40 +02:00
|
|
|
def get_added_checklist_body(payload: WildValue, action_type: str) -> str:
|
2016-05-05 07:06:41 +02:00
|
|
|
data = {
|
2022-06-27 18:26:40 +02:00
|
|
|
"checklist_name": get_action_data(payload)["checklist"]["name"].tame(check_string),
|
2016-05-05 07:06:41 +02:00
|
|
|
}
|
|
|
|
return fill_appropriate_message_content(payload, action_type, data)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 18:26:40 +02:00
|
|
|
def get_update_check_item_body(payload: WildValue, action_type: str) -> str:
|
2019-02-22 00:00:06 +01:00
|
|
|
action = get_action_data(payload)
|
2022-06-27 18:26:40 +02:00
|
|
|
state = action["checkItem"]["state"].tame(check_string)
|
2019-02-22 00:00:06 +01:00
|
|
|
data = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"action": "checked" if state == "complete" else "unchecked",
|
2022-06-27 18:26:40 +02:00
|
|
|
"checklist_name": action["checklist"]["name"].tame(check_string),
|
|
|
|
"item_name": action["checkItem"]["name"].tame(check_string),
|
2019-02-22 00:00:06 +01:00
|
|
|
}
|
|
|
|
return fill_appropriate_message_content(payload, action_type, data)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 18:26:40 +02:00
|
|
|
def get_added_attachment_body(payload: WildValue, action_type: str) -> str:
|
2016-05-05 07:06:41 +02:00
|
|
|
data = {
|
2022-06-27 18:26:40 +02:00
|
|
|
"attachment_url": get_action_data(payload)["attachment"]["url"].tame(check_string),
|
|
|
|
"attachment_name": get_action_data(payload)["attachment"]["name"].tame(check_string),
|
2016-05-05 07:06:41 +02:00
|
|
|
}
|
|
|
|
return fill_appropriate_message_content(payload, action_type, data)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 18:26:40 +02:00
|
|
|
def get_updated_card_body(payload: WildValue, action_type: str) -> str:
|
2016-05-05 07:06:41 +02:00
|
|
|
data = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"card_name": get_card_name(payload),
|
2022-06-27 18:26:40 +02:00
|
|
|
"old_list": get_action_data(payload)["listBefore"]["name"].tame(check_string),
|
|
|
|
"new_list": get_action_data(payload)["listAfter"]["name"].tame(check_string),
|
2016-05-05 07:06:41 +02:00
|
|
|
}
|
|
|
|
return fill_appropriate_message_content(payload, action_type, data)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 18:26:40 +02:00
|
|
|
def get_renamed_card_body(payload: WildValue, action_type: str) -> str:
|
2016-05-05 07:06:41 +02:00
|
|
|
data = {
|
2022-06-27 18:26:40 +02:00
|
|
|
"old_name": get_action_data(payload)["old"]["name"].tame(check_string),
|
|
|
|
"new_name": get_action_data(payload)["old"]["name"].tame(check_string),
|
2016-05-05 07:06:41 +02:00
|
|
|
}
|
|
|
|
return fill_appropriate_message_content(payload, action_type, data)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 18:26:40 +02:00
|
|
|
def get_added_label_body(payload: WildValue, action_type: str) -> str:
|
2016-05-05 07:06:41 +02:00
|
|
|
data = {
|
2022-06-27 18:26:40 +02:00
|
|
|
"color": get_action_data(payload)["value"].tame(check_string),
|
|
|
|
"text": get_action_data(payload)["text"].tame(check_string),
|
2016-05-05 07:06:41 +02:00
|
|
|
}
|
|
|
|
return fill_appropriate_message_content(payload, action_type, data)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 18:26:40 +02:00
|
|
|
def get_managed_member_body(payload: WildValue, action_type: str) -> str:
|
2016-05-05 07:06:41 +02:00
|
|
|
data = {
|
2022-06-27 18:26:40 +02:00
|
|
|
"member_name": payload["action"]["member"]["fullName"].tame(check_string),
|
2016-05-05 07:06:41 +02:00
|
|
|
}
|
|
|
|
return fill_appropriate_message_content(payload, action_type, data)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 18:26:40 +02:00
|
|
|
def get_comment_body(payload: WildValue, action_type: str) -> str:
|
2017-07-04 18:58:49 +02:00
|
|
|
data = {
|
2022-06-27 18:26:40 +02:00
|
|
|
"text": get_action_data(payload)["text"].tame(check_string),
|
2017-07-04 18:58:49 +02:00
|
|
|
}
|
|
|
|
return fill_appropriate_message_content(payload, action_type, data)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 18:26:40 +02:00
|
|
|
def get_managed_due_date_body(payload: WildValue, action_type: str) -> str:
|
2016-05-05 07:06:41 +02:00
|
|
|
data = {
|
2022-06-27 18:26:40 +02:00
|
|
|
"due_date": prettify_date(get_action_data(payload)["card"]["due"].tame(check_string)),
|
2016-05-05 07:06:41 +02:00
|
|
|
}
|
|
|
|
return fill_appropriate_message_content(payload, action_type, data)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 18:26:40 +02:00
|
|
|
def get_changed_due_date_body(payload: WildValue, action_type: str) -> str:
|
2016-05-05 07:06:41 +02:00
|
|
|
data = {
|
2022-06-27 18:26:40 +02:00
|
|
|
"due_date": prettify_date(get_action_data(payload)["card"]["due"].tame(check_string)),
|
|
|
|
"old_due_date": prettify_date(get_action_data(payload)["old"]["due"].tame(check_string)),
|
2016-05-05 07:06:41 +02:00
|
|
|
}
|
|
|
|
return fill_appropriate_message_content(payload, action_type, data)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 18:26:40 +02:00
|
|
|
def get_managed_desc_body(payload: WildValue, action_type: str) -> str:
|
2017-07-04 19:24:59 +02:00
|
|
|
data = {
|
2022-06-27 18:26:40 +02:00
|
|
|
"desc": get_action_data(payload)["card"]["desc"].tame(check_string),
|
2017-07-04 19:24:59 +02:00
|
|
|
}
|
|
|
|
return fill_appropriate_message_content(payload, action_type, data)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 18:26:40 +02:00
|
|
|
def get_changed_desc_body(payload: WildValue, action_type: str) -> str:
|
2017-07-04 19:24:59 +02:00
|
|
|
data = {
|
2022-06-27 18:26:40 +02:00
|
|
|
"desc": get_action_data(payload)["card"]["desc"].tame(check_string),
|
|
|
|
"old_desc": get_action_data(payload)["old"]["desc"].tame(check_string),
|
2017-07-04 19:24:59 +02:00
|
|
|
}
|
|
|
|
return fill_appropriate_message_content(payload, action_type, data)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 18:26:40 +02:00
|
|
|
def get_body_by_action_type_without_data(payload: WildValue, action_type: str) -> str:
|
2016-05-05 07:06:41 +02:00
|
|
|
return fill_appropriate_message_content(payload, action_type)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def fill_appropriate_message_content(
|
2022-06-27 18:26:40 +02:00
|
|
|
payload: WildValue, action_type: str, data: Mapping[str, str] = {}
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> str:
|
2020-06-13 03:34:01 +02:00
|
|
|
data = dict(data)
|
2021-02-12 08:20:45 +01:00
|
|
|
if "card_url_template" not in data:
|
|
|
|
data["card_url_template"] = get_filled_card_url_template(payload)
|
2016-05-05 07:06:41 +02:00
|
|
|
message_body = get_message_body(action_type)
|
|
|
|
return message_body.format(**data)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 18:26:40 +02:00
|
|
|
def get_filled_card_url_template(payload: WildValue) -> str:
|
2021-02-12 08:19:30 +01:00
|
|
|
return TRELLO_CARD_URL_TEMPLATE.format(
|
|
|
|
card_name=get_card_name(payload), card_url=get_card_url(payload)
|
|
|
|
)
|
|
|
|
|
2016-05-05 07:06:41 +02:00
|
|
|
|
2022-06-27 18:26:40 +02:00
|
|
|
def get_card_url(payload: WildValue) -> str:
|
|
|
|
return "https://trello.com/c/{}".format(
|
|
|
|
get_action_data(payload)["card"]["shortLink"].tame(check_string)
|
|
|
|
)
|
2016-05-05 07:06:41 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_message_body(action_type: str) -> str:
|
2017-05-24 23:03:06 +02:00
|
|
|
return ACTIONS_TO_MESSAGE_MAPPER[action_type]
|
2016-05-05 07:06:41 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 18:26:40 +02:00
|
|
|
def get_card_name(payload: WildValue) -> str:
|
|
|
|
return get_action_data(payload)["card"]["name"].tame(check_string)
|
2016-05-05 07:06:41 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 18:26:40 +02:00
|
|
|
def get_action_data(payload: WildValue) -> WildValue:
|
|
|
|
return payload["action"]["data"]
|
2016-05-05 07:06:41 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2016-05-05 07:06:41 +02:00
|
|
|
ACTIONS_TO_FILL_BODY_MAPPER = {
|
|
|
|
CREATE: get_body_by_action_type_without_data,
|
|
|
|
CHANGE_LIST: get_updated_card_body,
|
|
|
|
CHANGE_NAME: get_renamed_card_body,
|
2017-07-04 19:24:59 +02:00
|
|
|
SET_DESC: get_managed_desc_body,
|
|
|
|
CHANGE_DESC: get_changed_desc_body,
|
|
|
|
REMOVE_DESC: get_body_by_action_type_without_data,
|
2016-05-05 07:06:41 +02:00
|
|
|
ARCHIVE: get_body_by_action_type_without_data,
|
|
|
|
REOPEN: get_body_by_action_type_without_data,
|
|
|
|
SET_DUE_DATE: get_managed_due_date_body,
|
|
|
|
CHANGE_DUE_DATE: get_changed_due_date_body,
|
|
|
|
REMOVE_DUE_DATE: get_body_by_action_type_without_data,
|
|
|
|
ADD_LABEL: get_added_label_body,
|
|
|
|
REMOVE_LABEL: get_added_label_body,
|
|
|
|
ADD_MEMBER: get_managed_member_body,
|
|
|
|
REMOVE_MEMBER: get_managed_member_body,
|
|
|
|
ADD_ATTACHMENT: get_added_attachment_body,
|
|
|
|
ADD_CHECKLIST: get_added_checklist_body,
|
2017-07-04 18:58:49 +02:00
|
|
|
COMMENT: get_comment_body,
|
2019-02-22 00:00:06 +01:00
|
|
|
UPDATE_CHECK_ITEM_STATE: get_update_check_item_body,
|
2016-05-05 07:06:41 +02:00
|
|
|
}
|