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_string
|
2016-05-05 07:06:41 +02:00
|
|
|
|
|
|
|
SUPPORTED_BOARD_ACTIONS = [
|
2021-02-12 08:20:45 +01:00
|
|
|
"removeMemberFromBoard",
|
|
|
|
"addMemberToBoard",
|
|
|
|
"createList",
|
|
|
|
"updateBoard",
|
2016-05-05 07:06:41 +02:00
|
|
|
]
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
REMOVE_MEMBER = "removeMemberFromBoard"
|
|
|
|
ADD_MEMBER = "addMemberToBoard"
|
|
|
|
CREATE_LIST = "createList"
|
|
|
|
CHANGE_NAME = "changeName"
|
2016-05-05 07:06:41 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
TRELLO_BOARD_URL_TEMPLATE = "[{board_name}]({board_url})"
|
2016-05-05 07:06:41 +02:00
|
|
|
|
|
|
|
ACTIONS_TO_MESSAGE_MAPPER = {
|
2021-02-12 08:20:45 +01: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
|
|
|
}
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
def process_board_action(payload: WildValue, action_type: str | None) -> tuple[str, str] | None:
|
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:
|
2023-07-12 13:37:08 +02:00
|
|
|
return get_topic(payload), get_body(payload, action_type)
|
2017-09-15 03:11:48 +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 | None) -> str | None:
|
2021-02-12 08:20:45 +01:00
|
|
|
if action_type == "updateBoard":
|
2016-05-05 07:06:41 +02:00
|
|
|
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
|
2022-06-27 18:26:40 +02:00
|
|
|
if "background" in data["old"].get("prefs", {}):
|
2017-09-15 03:11:48 +02:00
|
|
|
return None
|
2022-06-27 18:26:40 +02:00
|
|
|
elif data["old"]["name"].tame(check_string):
|
2016-05-05 07:06:41 +02:00
|
|
|
return CHANGE_NAME
|
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:
|
2016-05-05 07:06:41 +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"]["fullName"].tame(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_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_create_list_body(payload: WildValue, action_type: str) -> str:
|
2016-05-05 07:06:41 +02:00
|
|
|
data = {
|
2022-06-27 18:26:40 +02:00
|
|
|
"list_name": get_action_data(payload)["list"]["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_change_name_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),
|
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
|
|
|
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 "board_url_template" not in data:
|
|
|
|
data["board_url_template"] = get_filled_board_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_board_url_template(payload: WildValue) -> str:
|
2021-02-12 08:19:30 +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
|
|
|
|
2022-06-27 18:26:40 +02:00
|
|
|
def get_board_name(payload: WildValue) -> str:
|
|
|
|
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_board_url(payload: WildValue) -> str:
|
|
|
|
return "https://trello.com/b/{}".format(
|
|
|
|
get_action_data(payload)["board"]["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:
|
2016-05-05 07:06:41 +02:00
|
|
|
return ACTIONS_TO_MESSAGE_MAPPER[action_type]
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 18:26:40 +02:00
|
|
|
def get_action_data(payload: WildValue) -> WildValue:
|
2021-02-12 08:20:45 +01:00
|
|
|
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 = {
|
|
|
|
REMOVE_MEMBER: get_managed_member_body,
|
|
|
|
ADD_MEMBER: get_managed_member_body,
|
|
|
|
CREATE_LIST: get_create_list_body,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
CHANGE_NAME: get_change_name_body,
|
2016-05-05 07:06:41 +02:00
|
|
|
}
|