Repository: zulip/zulip

Issue:
GitHub webhook: add support for discussions
The GitHub webhook currently doesn't support discussions. This is the response for a "discussion created" event:

```json
{
    "result":"error",
    "msg":"The 'discussion:created' event isn't currently supported by the GitHub webhook",
    "webhook_name":"GitHub",
    "event_type":"discussion:created",
    "code":"UNSUPPORTED_WEBHOOK_EVENT_TYPE"
}
```

It would be nice if discussion events could be supported by the GitHub webhook.


Discussion:
Hello @zulip/server-integrations members, this issue was labeled with the "area: integrations" label, so you may want to check it out!

<!-- areaLabelAddition -->

This seems worth doing, and is a good issue for new contributors.  See https://zulip.com/api/incoming-webhooks-overview for our documentation on writing integrations; in this case, one just needs to add features to the existing GitHub integration.
I would like to try working on this issue !
@zulipbot claim
Welcome to Zulip, @parthn2! We just sent you an invite to collaborate on this repository at https://github.com/zulip/zulip/invitations. Please accept this invite in order to claim this issue and begin a fun, rewarding experience contributing to Zulip!

Here's some tips to get you off to a good start:

- Join me on the [Zulip developers' server](https://chat.zulip.org), to get help, chat about this issue, and meet the other developers.
- [Unwatch this repository](https://help.github.com/articles/unwatching-repositories/), so that you don't get 100 emails a day.

As you work on this issue, you'll also want to refer to the [Zulip code contribution guide](https://zulip.readthedocs.io/en/latest/contributing/index.html), as well as the rest of the developer documentation on that site.

See you on the other side (that is, the pull request side)!

@zulipbot claim
Hello @madrix01, it looks like you've currently claimed 1 issue in this repository. We encourage new contributors to focus their efforts on at most 1 issue at a time, so please complete your work on your other claimed issues before trying to claim this issue again.

We look forward to your valuable contributions!

@zulipbot claim

Resolving patch:
diff --git a/zerver/webhooks/github/view.py b/zerver/webhooks/github/view.py
--- a/zerver/webhooks/github/view.py
+++ b/zerver/webhooks/github/view.py
@@ -29,6 +29,14 @@
 
 fixture_to_headers = get_http_headers_from_filename("HTTP_X_GITHUB_EVENT")
 
+TOPIC_FOR_DISCUSSION = "{repo} discussion #{number}: {title}"
+DISCUSSION_TEMPLATE = (
+    "{author} started a new discussion [{title}]({url}) in {category}:\n```quote\n{body}\n```"
+)
+DISCUSSION_COMMENT_TEMPLATE = (
+    "{author} [commented]({comment_url}) on [discussion]({discussion_url}):\n```quote\n{body}\n```"
+)
+
 
 class Helper:
     def __init__(
@@ -254,6 +262,27 @@ def get_push_commits_body(helper: Helper) -> str:
     )
 
 
+def get_discussion_body(helper: Helper) -> str:
+    payload = helper.payload
+    return DISCUSSION_TEMPLATE.format(
+        author=get_sender_name(payload),
+        title=payload["discussion"]["title"],
+        url=payload["discussion"]["html_url"],
+        body=payload["discussion"]["body"],
+        category=payload["discussion"]["category"]["name"],
+    )
+
+
+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"],
+        discussion_url=payload["discussion"]["html_url"],
+        comment_url=payload["comment"]["html_url"],
+    )
+
+
 def get_public_body(helper: Helper) -> str:
     payload = helper.payload
     return "{} made the repository [{}]({}) public.".format(
@@ -602,6 +631,12 @@ def get_subject_based_on_type(payload: Dict[str, Any], event: str) -> str:
             return get_organization_name(payload)
     elif event == "check_run":
         return f"{get_repository_name(payload)} / checks"
+    elif event.startswith("discussion"):
+        return TOPIC_FOR_DISCUSSION.format(
+            repo=get_repository_name(payload),
+            number=payload["discussion"]["number"],
+            title=payload["discussion"]["title"],
+        )
 
     return get_repository_name(payload)
 
@@ -614,6 +649,8 @@ def get_subject_based_on_type(payload: Dict[str, Any], event: str) -> str:
     "delete": partial(get_create_or_delete_body, action="deleted"),
     "deployment": get_deployment_body,
     "deployment_status": get_change_deployment_status_body,
+    "discussion": get_discussion_body,
+    "discussion_comment": get_discussion_comment_body,
     "fork": get_fork_body,
     "gollum": get_wiki_pages_body,
     "issue_comment": get_issue_comment_body,


Test patch:
diff --git a/zerver/webhooks/github/tests.py b/zerver/webhooks/github/tests.py
--- a/zerver/webhooks/github/tests.py
+++ b/zerver/webhooks/github/tests.py
@@ -12,6 +12,7 @@
 TOPIC_ORGANIZATION = "baxterandthehackers organization"
 TOPIC_BRANCH = "public-repo / changes"
 TOPIC_WIKI = "public-repo / wiki pages"
+TOPIC_DISCUSSION = "public-repo discussion #90: Welcome to discussions!"
 
 
 class GitHubWebhookTest(WebhookTestCase):
@@ -483,3 +484,11 @@ def test_team_edited_with_unsupported_keys(self) -> None:
             msg,
         )
         self.assertTrue(stack_info)
+
+    def test_discussion_msg(self) -> None:
+        expected_message = "Codertocat started a new discussion [Welcome to discussions!](https://github.com/baxterthehacker/public-repo/discussions/90) in General:\n```quote\nWe're glad to have you here!\n```"
+        self.check_webhook("discussion", TOPIC_DISCUSSION, expected_message)
+
+    def test_discussion_comment_msg(self) -> None:
+        expected_message = "Codertocat [commented](https://github.com/baxterthehacker/public-repo/discussions/90#discussioncomment-544078) on [discussion](https://github.com/baxterthehacker/public-repo/discussions/90):\n```quote\nI have so many questions to ask you!\n```"
+        self.check_webhook("discussion_comment", TOPIC_DISCUSSION, expected_message)