Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

integrations: Improve GitHub issue labeled and unlabeled notifications.

Satyam Bansal842e9d1acab2
diff --git a/zerver/lib/webhooks/git.py b/zerver/lib/webhooks/git.py
index c7ce59423f7..332d2a988b3 100644
--- a/zerver/lib/webhooks/git.py
+++ b/zerver/lib/webhooks/git.py
@@ -50,6 +50,9 @@ CREATE_BRANCH_MESSAGE_TEMPLATE = "{user_name} created [{branch_name}]({url}) bra
CREATE_BRANCH_WITHOUT_URL_MESSAGE_TEMPLATE = "{user_name} created {branch_name} branch."
REMOVE_BRANCH_MESSAGE_TEMPLATE = "{user_name} deleted branch {branch_name}."
+ISSUE_LABELED_OR_UNLABELED_MESSAGE_TEMPLATE = "[{user_name}]({user_url}) {action} the [{label_name}]({label_url}) label {preposition} [Issue #{id}]({url})."
+ISSUE_LABELED_OR_UNLABELED_MESSAGE_TEMPLATE_WITH_TITLE = "[{user_name}]({user_url}) {action} the [{label_name}]({label_url}) label {preposition} [Issue #{id} {title}]({url})."
+
PULL_REQUEST_OR_ISSUE_MESSAGE_TEMPLATE = "{user_name} {action} [{type}{id}]({url})"
PULL_REQUEST_OR_ISSUE_MESSAGE_TEMPLATE_WITH_TITLE = (
"{user_name} {action} [{type}{id} {title}]({url})"
@@ -273,6 +276,32 @@ def get_issue_event_message(
)
+def get_issue_labeled_or_unlabeled_event_message(
+ user_name: str,
+ action: str,
+ url: str,
+ number: int,
+ label_name: str,
+ label_url: str,
+ user_url: str,
+ title: Optional[str] = None,
+) -> str:
+ args = {
+ "user_name": user_name,
+ "action": action,
+ "url": url,
+ "id": number,
+ "label_name": label_name,
+ "label_url": label_url,
+ "user_url": user_url,
+ "title": title,
+ "preposition": "to" if action == "added" else "from",
+ }
+ if title is not None:
+ return ISSUE_LABELED_OR_UNLABELED_MESSAGE_TEMPLATE_WITH_TITLE.format(**args)
+ return ISSUE_LABELED_OR_UNLABELED_MESSAGE_TEMPLATE.format(**args)
+
+
def get_push_tag_event_message(
user_name: str, tag_name: str, tag_url: Optional[str] = None, action: str = "pushed"
) -> str:
diff --git a/zerver/webhooks/github/tests.py b/zerver/webhooks/github/tests.py
index 8e5814ad72f..9f2b909fb97 100644
--- a/zerver/webhooks/github/tests.py
+++ b/zerver/webhooks/github/tests.py
@@ -179,18 +179,18 @@ class GitHubWebhookTest(WebhookTestCase):
def test_issue_labeled(self) -> None:
expected_topic = "testing-gh / issue #9 idk man"
- expected_message = "sbansal1999 labeled [issue #9](https://github.com/sbansal1999/testing-gh/issues/9):\n\n~~~ quote\nThis is some random Issue description which will be used to test the Zulip GitHub Integration.\n~~~"
+ expected_message = "[sbansal1999](https://github.com/sbansal1999) added the [bug](https://api.github.com/repos/sbansal1999/testing-gh/labels/bug) label to [Issue #9](https://github.com/sbansal1999/testing-gh/issues/9)."
self.check_webhook("issues__labeled", expected_topic, expected_message)
def test_issue_labeled_with_custom_topic_in_url(self) -> None:
self.url = self.build_webhook_url(topic="notifications")
expected_topic = "notifications"
- expected_message = "sbansal1999 labeled [issue #9 idk man](https://github.com/sbansal1999/testing-gh/issues/9):\n\n~~~ quote\nThis is some random Issue description which will be used to test the Zulip GitHub Integration.\n~~~"
+ expected_message = "[sbansal1999](https://github.com/sbansal1999) added the [bug](https://api.github.com/repos/sbansal1999/testing-gh/labels/bug) label to [Issue #9 idk man](https://github.com/sbansal1999/testing-gh/issues/9)."
self.check_webhook("issues__labeled", expected_topic, expected_message)
def test_issue_unlabeled(self) -> None:
expected_topic = "testing-gh / issue #9 idk man"
- expected_message = "sbansal1999 unlabeled [issue #9](https://github.com/sbansal1999/testing-gh/issues/9):\n\n~~~ quote\nThis is some random Issue description which will be used to test the Zulip GitHub Integration.\n~~~"
+ expected_message = "[sbansal1999](https://github.com/sbansal1999) removed the [bug](https://api.github.com/repos/sbansal1999/testing-gh/labels/bug) label from [Issue #9](https://github.com/sbansal1999/testing-gh/issues/9)."
self.check_webhook("issues__unlabeled", expected_topic, expected_message)
def test_membership_msg(self) -> None:
diff --git a/zerver/webhooks/github/view.py b/zerver/webhooks/github/view.py
index dbc284684f6..632ec9a1ce6 100644
--- a/zerver/webhooks/github/view.py
+++ b/zerver/webhooks/github/view.py
@@ -28,6 +28,7 @@ from zerver.lib.webhooks.git import (
TOPIC_WITH_PR_OR_ISSUE_INFO_TEMPLATE,
get_commits_comment_action_message,
get_issue_event_message,
+ get_issue_labeled_or_unlabeled_event_message,
get_pull_request_event_message,
get_push_commits_event_message,
get_push_tag_event_message,
@@ -160,6 +161,10 @@ def get_issue_body(helper: Helper) -> str:
action = payload["action"].tame(check_string)
issue = payload["issue"]
has_assignee = "assignee" in payload
+
+ if action in ("labeled", "unlabeled"):
+ return get_issue_labeled_or_unlabeled_body(helper)
+
base_message = get_issue_event_message(
user_name=get_sender_name(payload),
action=action,
@@ -204,6 +209,23 @@ def get_issue_comment_body(helper: Helper) -> str:
)
+def get_issue_labeled_or_unlabeled_body(helper: Helper) -> str:
+ payload = helper.payload
+ include_title = helper.include_title
+ issue = payload["issue"]
+
+ return get_issue_labeled_or_unlabeled_event_message(
+ user_name=get_sender_name(payload),
+ action="added" if payload["action"].tame(check_string) == "labeled" else "removed",
+ url=issue["html_url"].tame(check_string),
+ number=issue["number"].tame(check_int),
+ label_name=payload["label"]["name"].tame(check_string),
+ label_url=payload["label"]["url"].tame(check_string),
+ user_url=get_sender_url(payload),
+ title=issue["title"].tame(check_string) if include_title else None,
+ )
+
+
def get_fork_body(helper: Helper) -> str:
payload = helper.payload
forkee = payload["forkee"]