From bf2d216100cedda80a935f37bc1895e03a0d4dfe Mon Sep 17 00:00:00 2001 From: Satyam Bansal Date: Sun, 2 Jul 2023 23:50:30 +0530 Subject: [PATCH] integrations: Add action to GitHub discussion comment notifications. Previously, the notifications had "commented" as the action word for every event. As part of these changes, we extract a shared comment action function in GitHub Integration that's used for both issue and discussion comment events. --- zerver/webhooks/github/tests.py | 8 +++++++- zerver/webhooks/github/view.py | 34 ++++++++++++++++++--------------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/zerver/webhooks/github/tests.py b/zerver/webhooks/github/tests.py index c56668d55d..6cf7ca9dba 100644 --- a/zerver/webhooks/github/tests.py +++ b/zerver/webhooks/github/tests.py @@ -582,6 +582,12 @@ A temporary team so that I can get some webhook fixtures! expected_message = "sbansal1999 [commented](https://github.com/sbansal1999/testing-gh/discussions/20#discussioncomment-6332416) on [discussion #20](https://github.com/sbansal1999/testing-gh/discussions/20):\n\n~~~ quote\nsome random comment\n~~~" self.check_webhook("discussion_comment", TOPIC_DISCUSSION, expected_message) + def test_discussion_comment_msg_with_custom_topic_in_url(self) -> None: + self.url = self.build_webhook_url(topic="notifications") + expected_topic = "notifications" + expected_message = "sbansal1999 [commented](https://github.com/sbansal1999/testing-gh/discussions/20#discussioncomment-6332416) on [discussion #20 Lets discuss](https://github.com/sbansal1999/testing-gh/discussions/20):\n\n~~~ quote\nsome random comment\n~~~" + self.check_webhook("discussion_comment", expected_topic, expected_message) + def test_discussion_comment_edited_msg(self) -> None: - expected_message = "sbansal1999 [commented](https://github.com/sbansal1999/testing-gh/discussions/20#discussioncomment-6332416) on [discussion #20](https://github.com/sbansal1999/testing-gh/discussions/20):\n\n~~~ quote\nsome random comment edited\n~~~" + expected_message = "sbansal1999 edited a [comment](https://github.com/sbansal1999/testing-gh/discussions/20#discussioncomment-6332416) on [discussion #20](https://github.com/sbansal1999/testing-gh/discussions/20):\n\n~~~ quote\nsome random comment edited\n~~~" self.check_webhook("discussion_comment__edited", TOPIC_DISCUSSION, expected_message) diff --git a/zerver/webhooks/github/view.py b/zerver/webhooks/github/view.py index b4d173b379..40533758f5 100644 --- a/zerver/webhooks/github/view.py +++ b/zerver/webhooks/github/view.py @@ -36,7 +36,6 @@ fixture_to_headers = get_http_headers_from_filename("HTTP_X_GITHUB_EVENT") TOPIC_FOR_DISCUSSION = "{repo} discussion #{number}: {title}" DISCUSSION_TEMPLATE = "{author} created [discussion #{discussion_id}]({url}) in {category}:\n\n~~~ quote\n### {title}\n{body}\n~~~" -DISCUSSION_COMMENT_TEMPLATE = "{author} [commented]({comment_url}) on [discussion #{discussion_id}]({discussion_url}):\n\n~~~ quote\n{body}\n~~~" class Helper: @@ -180,19 +179,12 @@ def get_issue_body(helper: Helper) -> str: def get_issue_comment_body(helper: Helper) -> str: payload = helper.payload include_title = helper.include_title - action = payload["action"].tame(check_string) comment = payload["comment"] issue = payload["issue"] - if action == "created": - action = "[commented]" - else: - action = f"{action} a [comment]" - action += "({}) on".format(comment["html_url"].tame(check_string)) - return get_pull_request_event_message( user_name=get_sender_name(payload), - action=action, + action=get_comment_action(payload), url=issue["html_url"].tame(check_string), number=issue["number"].tame(check_int), message=comment["body"].tame(check_string), @@ -331,15 +323,27 @@ def get_discussion_body(helper: Helper) -> str: def get_discussion_comment_body(helper: Helper) -> str: payload = helper.payload - return DISCUSSION_COMMENT_TEMPLATE.format( - author=get_sender_name(payload), - body=payload["comment"]["body"].tame(check_string), - discussion_url=payload["discussion"]["html_url"].tame(check_string), - comment_url=payload["comment"]["html_url"].tame(check_string), - discussion_id=payload["discussion"]["number"].tame(check_int), + return get_pull_request_event_message( + user_name=get_sender_name(payload), + action=get_comment_action(payload), + url=payload["discussion"]["html_url"].tame(check_string), + number=payload["discussion"]["number"].tame(check_int), + message=payload["comment"]["body"].tame(check_string), + title=payload["discussion"]["title"].tame(check_string) if helper.include_title else None, + type="discussion", ) +def get_comment_action(payload: WildValue) -> str: + action = payload["action"].tame(check_string) + if action == "created": + action = "[commented]" + else: + action = f"{action} a [comment]" + action += "({}) on".format(payload["comment"]["html_url"].tame(check_string)) + return action + + def get_public_body(helper: Helper) -> str: payload = helper.payload return "{} made the repository [{}]({}) public.".format(