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.
This commit is contained in:
Satyam Bansal 2023-07-02 23:50:30 +05:30 committed by Tim Abbott
parent 62ec51f715
commit bf2d216100
2 changed files with 26 additions and 16 deletions

View File

@ -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~~~" 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) 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: 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) self.check_webhook("discussion_comment__edited", TOPIC_DISCUSSION, expected_message)

View File

@ -36,7 +36,6 @@ fixture_to_headers = get_http_headers_from_filename("HTTP_X_GITHUB_EVENT")
TOPIC_FOR_DISCUSSION = "{repo} discussion #{number}: {title}" 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_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: class Helper:
@ -180,19 +179,12 @@ def get_issue_body(helper: Helper) -> str:
def get_issue_comment_body(helper: Helper) -> str: def get_issue_comment_body(helper: Helper) -> str:
payload = helper.payload payload = helper.payload
include_title = helper.include_title include_title = helper.include_title
action = payload["action"].tame(check_string)
comment = payload["comment"] comment = payload["comment"]
issue = payload["issue"] 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( return get_pull_request_event_message(
user_name=get_sender_name(payload), user_name=get_sender_name(payload),
action=action, action=get_comment_action(payload),
url=issue["html_url"].tame(check_string), url=issue["html_url"].tame(check_string),
number=issue["number"].tame(check_int), number=issue["number"].tame(check_int),
message=comment["body"].tame(check_string), 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: def get_discussion_comment_body(helper: Helper) -> str:
payload = helper.payload payload = helper.payload
return DISCUSSION_COMMENT_TEMPLATE.format( return get_pull_request_event_message(
author=get_sender_name(payload), user_name=get_sender_name(payload),
body=payload["comment"]["body"].tame(check_string), action=get_comment_action(payload),
discussion_url=payload["discussion"]["html_url"].tame(check_string), url=payload["discussion"]["html_url"].tame(check_string),
comment_url=payload["comment"]["html_url"].tame(check_string), number=payload["discussion"]["number"].tame(check_int),
discussion_id=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: def get_public_body(helper: Helper) -> str:
payload = helper.payload payload = helper.payload
return "{} made the repository [{}]({}) public.".format( return "{} made the repository [{}]({}) public.".format(