Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
integrations: Support adding multiple labels at a time for Clubhouse.
PIG208e1a37d2e0a63
diff --git a/zerver/webhooks/clubhouse/fixtures/story_update_add_multiple_labels.json b/zerver/webhooks/clubhouse/fixtures/story_update_add_multiple_labels.jsonnew file mode 100644index 00000000000..6a0f0d9debd--- /dev/null+++ b/zerver/webhooks/clubhouse/fixtures/story_update_add_multiple_labels.json@@ -0,0 +1,37 @@+{+ "id":"5c37d3a3-6e3d-4b17-963c-adfa0a023d84",+ "changed_at":"2019-01-10T23:22:11.694Z",+ "primary_id":23,+ "version":"v1",+ "member_id":"5b1fda16-626f-487f-9ecf-f3a4abf42b8b",+ "actions":[+ {+ "id":23,+ "entity_type":"story",+ "action":"update",+ "name":"An epic story!",+ "story_type":"feature",+ "app_url":"https://app.clubhouse.io/zulip/story/23",+ "changes":{+ "label_ids":{+ "adds":[+ 18,+ 19+ ]+ }+ }+ }+ ],+ "references":[+ {+ "id":18,+ "entity_type":"label",+ "name":"mockup"+ },+ {+ "id":19,+ "entity_type":"label",+ "name":"label"+ }+ ]+}diff --git a/zerver/webhooks/clubhouse/tests.py b/zerver/webhooks/clubhouse/tests.pyindex 98121d6e83c..aef566e4daf 100644--- a/zerver/webhooks/clubhouse/tests.py+++ b/zerver/webhooks/clubhouse/tests.py@@ -172,6 +172,10 @@ class ClubhouseWebhookTest(WebhookTestCase):expected_message = "The label **mockup** was added to the story [An epic story!](https://app.clubhouse.io/zulip/story/23)."self.check_webhook("story_update_add_label", "An epic story!", expected_message)+ def test_story_label_multiple_added(self) -> None:+ expected_message = "The labels **mockup**, **label** were added to the story [An epic story!](https://app.clubhouse.io/zulip/story/23)."+ self.check_webhook("story_update_add_multiple_labels", "An epic story!", expected_message)+def test_story_label_added_label_name_in_actions(self) -> None:expected_message = "The label **sad** was added to the story [An emotional story!](https://app.clubhouse.io/zulip/story/28)."self.check_webhook(diff --git a/zerver/webhooks/clubhouse/view.py b/zerver/webhooks/clubhouse/view.pyindex c01adcc4be5..2eb06a50ce0 100644--- a/zerver/webhooks/clubhouse/view.py+++ b/zerver/webhooks/clubhouse/view.py@@ -1,5 +1,5 @@from functools import partial-from typing import Any, Callable, Dict, Generator, Optional+from typing import Any, Callable, Dict, Generator, List, Optionalfrom django.http import HttpRequest, HttpResponse@@ -43,7 +43,9 @@ STORY_ESTIMATE_TEMPLATE = "The estimate for the story {story_name_template} wasFILE_ATTACHMENT_TEMPLATE = ("A {type} attachment `{file_name}` was added to the story {name_template}.")-STORY_LABEL_TEMPLATE = "The label **{label_name}** was added to the story {name_template}."+LABEL_TEMPLATE = "**{name}**"+STORY_LABEL_TEMPLATE = "The label {labels} was added to the story {name_template}."+STORY_LABEL_PLURAL_TEMPLATE = "The labels {labels} were added to the story {name_template}."STORY_UPDATE_PROJECT_TEMPLATE = ("The story {name_template} was moved from the **{old}** project to **{new}**.")@@ -410,6 +412,26 @@ def get_story_update_attachment_body(return FILE_ATTACHMENT_TEMPLATE.format(**kwargs)+def get_story_joined_label_list(+ payload: Dict[str, Any], action: Dict[str, Any], label_ids_added: List[int]+) -> str:+ labels = []++ for label_id in label_ids_added:+ label_name = ""++ for action in payload["actions"]:+ if action.get("id") == label_id:+ label_name = action.get("name", "")++ if label_name == "":+ label_name = get_reference_by_id(payload, label_id).get("name", "")++ labels.append(LABEL_TEMPLATE.format(name=label_name))++ return ", ".join(labels)++def get_story_label_body(payload: Dict[str, Any], action: Dict[str, Any]) -> Optional[str]:kwargs = {"name_template": STORY_NAME_TEMPLATE.format(@@ -419,25 +441,17 @@ def get_story_label_body(payload: Dict[str, Any], action: Dict[str, Any]) -> Opt}label_ids_added = action["changes"]["label_ids"].get("adds")- # If this is a payload for when a label is removed, ignore it+ # If this is a payload for when no label is added, ignore itif not label_ids_added:return None- label_id = label_ids_added[0]-- label_name = ""- for action in payload["actions"]:- if action["id"] == label_id:- label_name = action.get("name", "")-- if not label_name:- for reference in payload["references"]:- if reference["id"] == label_id:- label_name = reference.get("name", "")-- kwargs.update(label_name=label_name)+ kwargs.update(labels=get_story_joined_label_list(payload, action, label_ids_added))- return STORY_LABEL_TEMPLATE.format(**kwargs)+ return (+ STORY_LABEL_TEMPLATE.format(**kwargs)+ if len(label_ids_added) == 1+ else STORY_LABEL_PLURAL_TEMPLATE.format(**kwargs)+ )def get_story_update_project_body(payload: Dict[str, Any], action: Dict[str, Any]) -> str: