integrations: Improve GitHub issue labeled and unlabeled notifications.

Earlier, the notifications had no information about the labels
being added or removed.
This commit is contained in:
Satyam Bansal 2023-06-03 01:52:01 +05:30 committed by Tim Abbott
parent 8fc28be8ca
commit 842e9d1aca
3 changed files with 54 additions and 3 deletions

View File

@ -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:

View File

@ -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:

View File

@ -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"]