Repository: zulip/zulip

Issue:
github webhook: Re-visit ignored events and actions
Please only try this issue if you have a little bit of knowledge of the github webhook, as it will require making some judgment calls and getting consensus on new features.

We have the following:

~~~ py
522 IGNORED_EVENTS = [
523     "check_suite",
524     "label",
525     "meta",
526     "milestone",
527     "organization",
528     "project_card",
529     "repository_vulnerability_alert",
530 ]
531
532 IGNORED_PULL_REQUEST_ACTIONS = [
533     "approved",
534     "converted_to_draft",
535     "labeled",
536     "review_request_removed",
537     "unlabeled",
538 ]
~~~

(The latter list hasn't been merged at the time of this writing.)

Sometimes when we ignore events (or actions) it's very deliberate, because the events/actions would be too spammy for users.  Other times we might just have been lazy.

If you look at the actions for pull request, consider "approved" and "converted_to_draft" as examples.  For the "approved" event, that seems like something that folks may want to see, so we might consider getting a good test fixture for it and supporting it in the code.  Then the "converted_to_draft" action seems likely to be spammy, and we should probably just continue to ignore it.




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

<!-- areaLabelAddition -->
Also look for:

~~~ py
+IGNORED_TEAM_ACTIONS = [
+    # These are actions that are well documented by github
+    # (https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads)
+    # but we ignore them for now, possibly just due to laziness.
+    # One curious example here is team/added_to_repository, which is
+    # possibly the same as team_add.
+    "added_to_repository",
+    "created",
+    "deleted",
+    "removed_from_repository",
+]
~~~
I think with the GitHub webhook, it's reasonable to support essentially all actions, since GitHub supports filtering events on the integration configuration side.
For convenience, here are the docs: https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads
@showell There is a `submitted` action with state `approved` but I can't seem to find any docs related to `approved` pull request action.
@shanukun I would try to help you, but I don't really know their docs too well.  I assume the link I provided above is their most current reference, but it's worth poking around on GitHub to see if you can find out what's up here.
@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
@@ -381,6 +381,37 @@ def get_status_body(helper: Helper) -> str:
     )
 
 
+def get_locked_or_unlocked_pull_request_body(helper: Helper) -> str:
+    payload = helper.payload
+
+    action = payload["action"]
+
+    message = "{sender} has locked [PR #{pr_number}]({pr_url}) as {reason} and limited conversation to collaborators."
+    if action == "unlocked":
+        message = "{sender} has unlocked [PR #{pr_number}]({pr_url})."
+    return message.format(
+        sender=get_sender_name(payload),
+        pr_number=payload["pull_request"]["number"],
+        pr_url=payload["pull_request"]["html_url"],
+        reason=payload["pull_request"]["active_lock_reason"],
+    )
+
+
+def get_pull_request_auto_merge_body(helper: Helper) -> str:
+    payload = helper.payload
+
+    action = payload["action"]
+
+    message = "{sender} has enabled auto merge for [PR #{pr_number}]({pr_url})."
+    if action == "auto_merge_disabled":
+        message = "{sender} has disabled auto merge for [PR #{pr_number}]({pr_url})."
+    return message.format(
+        sender=get_sender_name(payload),
+        pr_number=payload["pull_request"]["number"],
+        pr_url=payload["pull_request"]["html_url"],
+    )
+
+
 def get_pull_request_ready_for_review_body(helper: Helper) -> str:
     payload = helper.payload
 
@@ -603,6 +634,8 @@ def get_subject_based_on_type(payload: Dict[str, Any], event: str) -> str:
     "pull_request_review": get_pull_request_review_body,
     "pull_request_review_comment": get_pull_request_review_comment_body,
     "pull_request_review_requested": get_pull_request_review_requested_body,
+    "pull_request_auto_merge": get_pull_request_auto_merge_body,
+    "locked_or_unlocked_pull_request": get_locked_or_unlocked_pull_request_body,
     "push_commits": get_push_commits_body,
     "push_tags": get_push_tags_body,
     "release": get_release_body,
@@ -707,6 +740,10 @@ def get_zulip_event_name(
             return "pull_request_review_requested"
         if action == "ready_for_review":
             return "pull_request_ready_for_review"
+        if action in ("locked", "unlocked"):
+            return "locked_or_unlocked_pull_request"
+        if action in ("auto_merge_enabled", "auto_merge_disabled"):
+            return "pull_request_auto_merge"
         if action in IGNORED_PULL_REQUEST_ACTIONS:
             return None
     elif header_event == "push":


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
@@ -250,6 +250,24 @@ def test_pull_request_review_comment_with_custom_topic_in_url(self) -> None:
         expected_message = "baxterthehacker created [PR Review Comment on #1 Update the README with new information](https://github.com/baxterthehacker/public-repo/pull/1#discussion_r29724692):\n\n~~~ quote\nMaybe you should use more emojji on this line.\n~~~"
         self.check_webhook("pull_request_review_comment", expected_topic, expected_message)
 
+    def test_pull_request_locked(self) -> None:
+        expected_message = "tushar912 has locked [PR #1](https://github.com/tushar912/public-repo/pull/1) as off-topic and limited conversation to collaborators."
+        self.check_webhook("pull_request__locked", TOPIC_PR, expected_message)
+
+    def test_pull_request_unlocked(self) -> None:
+        expected_message = (
+            "tushar912 has unlocked [PR #1](https://github.com/tushar912/public-repo/pull/1)."
+        )
+        self.check_webhook("pull_request__unlocked", TOPIC_PR, expected_message)
+
+    def test_pull_request_auto_merge_enabled(self) -> None:
+        expected_message = "tushar912 has enabled auto merge for [PR #1](https://github.com/tushar912/public-repo/pull/1)."
+        self.check_webhook("pull_request__auto_merge_enabled", TOPIC_PR, expected_message)
+
+    def test_pull_request_auto_merge_disabled(self) -> None:
+        expected_message = "tushar912 has disabled auto merge for [PR #1](https://github.com/tushar912/public-repo/pull/1)."
+        self.check_webhook("pull_request__auto_merge_disabled", TOPIC_PR, expected_message)
+
     def test_push_tag_msg(self) -> None:
         expected_message = "baxterthehacker pushed tag abc."
         self.check_webhook("push__tag", TOPIC_REPO, expected_message)