Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
webhooks/gitlab: Avoid repeating context when title is in the topic.
Pritesh Shetty3a502d73b41f
diff --git a/zerver/lib/webhooks/git.py b/zerver/lib/webhooks/git.pyindex b5153bff059..4156a16cc93 100644--- a/zerver/lib/webhooks/git.py+++ b/zerver/lib/webhooks/git.py@@ -55,6 +55,7 @@ ISSUE_MILESTONED_OR_DEMILESTONED_MESSAGE_TEMPLATE = "[{user_name}]({user_url}) {ISSUE_MILESTONED_OR_DEMILESTONED_MESSAGE_TEMPLATE_WITH_TITLE = "[{user_name}]({user_url}) {action} milestone [{milestone_name}]({milestone_url}) {preposition} [issue #{id} {title}]({url})."PULL_REQUEST_OR_ISSUE_MESSAGE_TEMPLATE = "{user_name} {action}{assignee} [{type}{id}{title}]({url})"+PULL_REQUEST_OR_ISSUE_MESSAGE_TEMPLATE_WITHOUT_REFERENCE = "{user_name} {action}"PULL_REQUEST_OR_ISSUE_ASSIGNEE_INFO_TEMPLATE = "(assigned to {assignee})"PULL_REQUEST_BRANCH_INFO_TEMPLATE = "from `{target}` to `{base}`"@@ -206,6 +207,7 @@ def get_pull_request_event_message(type: str = "PR",title: str | None = None,suffix: str | None = None,+ include_topic_reference: bool = True,) -> str:action_messages = {"approval": "added their approval for",@@ -229,7 +231,12 @@ def get_pull_request_event_message(}.get(action, ""),}- main_message = PULL_REQUEST_OR_ISSUE_MESSAGE_TEMPLATE.format(**kwargs)+ message_template = (+ PULL_REQUEST_OR_ISSUE_MESSAGE_TEMPLATE+ if include_topic_reference+ else PULL_REQUEST_OR_ISSUE_MESSAGE_TEMPLATE_WITHOUT_REFERENCE+ )+ main_message = message_template.format(**kwargs)if target_branch and base_branch:branch_info = PULL_REQUEST_BRANCH_INFO_TEMPLATE.format(diff --git a/zerver/webhooks/gitlab/tests.py b/zerver/webhooks/gitlab/tests.pyindex cdcd4acfc90..c5ea73220d5 100644--- a/zerver/webhooks/gitlab/tests.py+++ b/zerver/webhooks/gitlab/tests.py@@ -229,13 +229,13 @@ class GitlabHookTests(WebhookTestCase):def test_note_merge_request_event_message(self) -> None:expected_topic_name = "testing-zulip-gitlab-integration / MR #1 add new-feature"- expected_message = "Satyam Bansal [commented](https://gitlab.com/sbansal1999/testing-zulip-gitlab-integration/-/merge_requests/1#note_1406328457) on [MR #1](https://gitlab.com/sbansal1999/testing-zulip-gitlab-integration/-/merge_requests/1):\n\n~~~ quote\nI am not sure if this new feature is even required or not.\n~~~"+ expected_message = "Satyam Bansal [commented](https://gitlab.com/sbansal1999/testing-zulip-gitlab-integration/-/merge_requests/1#note_1406328457):\n\n~~~ quote\nI am not sure if this new feature is even required or not.\n~~~"self.check_webhook("note_hook__merge_request_note", expected_topic_name, expected_message)def test_note_merge_request_event_message_without_merge_request_title(self) -> None:expected_topic_name = "testing-zulip-gitlab-integration / MR #1"- expected_message = "Satyam Bansal [commented](https://gitlab.com/sbansal1999/testing-zulip-gitlab-integration/-/merge_requests/1#note_1406328457) on [MR #1](https://gitlab.com/sbansal1999/testing-zulip-gitlab-integration/-/merge_requests/1):\n\n~~~ quote\nI am not sure if this new feature is even required or not.\n~~~"+ expected_message = "Satyam Bansal [commented](https://gitlab.com/sbansal1999/testing-zulip-gitlab-integration/-/merge_requests/1#note_1406328457):\n\n~~~ quote\nI am not sure if this new feature is even required or not.\n~~~"# To keep things as valid JSON.self.url = self.build_webhook_url(use_merge_request_title="false")self.check_webhook("note_hook__merge_request_note", expected_topic_name, expected_message)@@ -249,19 +249,19 @@ class GitlabHookTests(WebhookTestCase):def test_note_issue_event_message(self) -> None:expected_topic_name = "testing-zulip-gitlab-integration / issue #1 Add more lines"- expected_message = "Satyam Bansal [commented](https://gitlab.com/sbansal1999/testing-zulip-gitlab-integration/-/issues/1#note_1406279810) on [issue #1](https://gitlab.com/sbansal1999/testing-zulip-gitlab-integration/-/issues/1):\n\n~~~ quote\nThis is again a random comment.\n~~~"+ expected_message = "Satyam Bansal [commented](https://gitlab.com/sbansal1999/testing-zulip-gitlab-integration/-/issues/1#note_1406279810):\n\n~~~ quote\nThis is again a random comment.\n~~~"self.check_webhook("note_hook__issue_note", expected_topic_name, expected_message)def test_note_design_event_message(self) -> None:expected_topic_name = "testing-zulip-gitlab-integration / design Screenshot.png"- expected_message = "Satyam Bansal [commented](https://gitlab.com/sbansal1999/testing-zulip-gitlab-integration/-/issues/1/designs/Screenshot.png#note_1458583152) on design [Screenshot.png](https://gitlab.com/sbansal1999/testing-zulip-gitlab-integration/-/issues/1/designs/Screenshot.png):\n\n~~~ quote\nhello\n~~~"+ expected_message = "Satyam Bansal [commented](https://gitlab.com/sbansal1999/testing-zulip-gitlab-integration/-/issues/1/designs/Screenshot.png#note_1458583152):\n\n~~~ quote\nhello\n~~~"self.check_webhook("note_hook__design_note", expected_topic_name, expected_message)def test_note_confidential_issue_event_message(self) -> None:expected_subject = "testing-zulip-gitlab-integration / issue #1 Add more lines"- expected_message = "Satyam Bansal [commented](https://gitlab.com/sbansal1999/testing-zulip-gitlab-integration/-/issues/1#note_1406130881) on [issue #1](https://gitlab.com/sbansal1999/testing-zulip-gitlab-integration/-/issues/1):\n\n~~~ quote\nSome more comments\n~~~"+ expected_message = "Satyam Bansal [commented](https://gitlab.com/sbansal1999/testing-zulip-gitlab-integration/-/issues/1#note_1406130881):\n\n~~~ quote\nSome more comments\n~~~"self.check_webhook("note_hook__confidential_issue_note", expected_subject, expected_message)@@ -281,13 +281,13 @@ class GitlabHookTests(WebhookTestCase):def test_note_snippet_old_event_message(self) -> None:expected_topic_name = "my-awesome-project / snippet #2 test"- expected_message = "Tomasz Kolek [commented](https://gitlab.com/tomaszkolek0/my-awesome-project/snippets/2#note_14172058) on [snippet #2](https://gitlab.com/tomaszkolek0/my-awesome-project/-/snippets/2):\n\n~~~ quote\nNice snippet\n~~~"+ expected_message = "Tomasz Kolek [commented](https://gitlab.com/tomaszkolek0/my-awesome-project/snippets/2#note_14172058):\n\n~~~ quote\nNice snippet\n~~~"self.check_webhook("note_hook__snippet_note_old", expected_topic_name, expected_message)def test_note_snippet_event_message(self) -> None:expected_topic_name = "testing-zulip-gitlab-integration / snippet #2547713 a ver..."- expected_message = "Satyam Bansal [commented](https://gitlab.com/sbansal1999/testing-zulip-gitlab-integration/-/snippets/2547713#note_1424268837) on [snippet #2547713](https://gitlab.com/sbansal1999/testing-zulip-gitlab-integration/-/snippets/2547713):\n\n~~~ quote\nsome comment\n~~~"+ expected_message = "Satyam Bansal [commented](https://gitlab.com/sbansal1999/testing-zulip-gitlab-integration/-/snippets/2547713#note_1424268837):\n\n~~~ quote\nsome comment\n~~~"self.check_webhook("note_hook__snippet_note", expected_topic_name, expected_message)diff --git a/zerver/webhooks/gitlab/view.py b/zerver/webhooks/gitlab/view.pyindex de47f74060f..92724b8a565 100644--- a/zerver/webhooks/gitlab/view.py+++ b/zerver/webhooks/gitlab/view.py@@ -33,9 +33,8 @@ from zerver.lib.webhooks.git import (from zerver.models import UserProfileTOPIC_WITH_DESIGN_INFO_TEMPLATE = "{repo} / {type} {design_name}"-DESIGN_COMMENT_MESSAGE_TEMPLATE = (- "{user_name} {action} on design [{design_name}]({design_url}):\n{content_message}"-)+DESIGN_COMMENT_MESSAGE_TEMPLATE = "{user_name} {action}{design_part}:\n{content_message}"+DESIGN_PART_TEMPLATE = " on design [{design_name}]({design_url})"FEATURE_FLAG_MESSAGE_TEMPLATE = "{user} {action} the feature flag [{name}]({url})."@@ -250,7 +249,8 @@ def get_commented_commit_event_body(payload: WildValue, include_title: bool) ->def get_commented_merge_request_event_body(payload: WildValue, include_title: bool) -> str:comment = payload["object_attributes"]- action = "[commented]({}) on".format(comment["url"].tame(check_string))+ comment_url = comment["url"].tame(check_string)+ action = f"[commented]({comment_url}) on" if include_title else f"[commented]({comment_url})"url = payload["merge_request"]["url"].tame(check_string)return get_pull_request_event_message(@@ -260,13 +260,15 @@ def get_commented_merge_request_event_body(payload: WildValue, include_title: bonumber=payload["merge_request"]["iid"].tame(check_int),message=comment["note"].tame(check_string),type="MR",- title=payload["merge_request"]["title"].tame(check_string) if include_title else None,+ title=payload["merge_request"]["title"].tame(check_string),+ include_topic_reference=include_title,)def get_commented_issue_event_body(payload: WildValue, include_title: bool) -> str:comment = payload["object_attributes"]- action = "[commented]({}) on".format(comment["url"].tame(check_string))+ comment_url = comment["url"].tame(check_string)+ action = f"[commented]({comment_url}) on" if include_title else f"[commented]({comment_url})"url = payload["issue"]["url"].tame(check_string)return get_pull_request_event_message(@@ -276,7 +278,8 @@ def get_commented_issue_event_body(payload: WildValue, include_title: bool) -> snumber=payload["issue"]["iid"].tame(check_int),message=comment["note"].tame(check_string),type="issue",- title=payload["issue"]["title"].tame(check_string) if include_title else None,+ title=payload["issue"]["title"].tame(check_string),+ include_topic_reference=include_title,)@@ -288,18 +291,24 @@ def get_commented_design_event_body(payload: WildValue, include_title: bool) ->action = f"[commented]({comment_url})"content_message = CONTENT_MESSAGE_TEMPLATE.format(message=comment["note"].tame(check_string))+ design_part = (+ DESIGN_PART_TEMPLATE.format(design_name=design_name, design_url=design_url)+ if include_title+ else ""+ )+return DESIGN_COMMENT_MESSAGE_TEMPLATE.format(user_name=get_issue_user_name(payload),action=action,- design_name=design_name,- design_url=design_url,content_message=content_message,+ design_part=design_part,)def get_commented_snippet_event_body(payload: WildValue, include_title: bool) -> str:comment = payload["object_attributes"]- action = "[commented]({}) on".format(comment["url"].tame(check_string))+ comment_url = comment["url"].tame(check_string)+ action = f"[commented]({comment_url}) on" if include_title else f"[commented]({comment_url})"# Snippet URL is only available in GitLab 16.1+if "url" in payload["snippet"]:url = payload["snippet"]["url"].tame(check_string)@@ -316,7 +325,8 @@ def get_commented_snippet_event_body(payload: WildValue, include_title: bool) ->number=payload["snippet"]["id"].tame(check_int),message=comment["note"].tame(check_string),type="snippet",- title=payload["snippet"]["title"].tame(check_string) if include_title else None,+ title=payload["snippet"]["title"].tame(check_string),+ include_topic_reference=include_title,)