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.

webhooks/github: Add concise_edit_format for edited issues and PRs.

Niloth P026449858a95
diff --git a/zerver/lib/integrations.py b/zerver/lib/integrations.py
index 1b5fa5e4c43..14a11438f12 100644
--- a/zerver/lib/integrations.py
+++ b/zerver/lib/integrations.py
@@ -682,6 +682,11 @@ INCOMING_WEBHOOK_INTEGRATIONS: list[IncomingWebhookIntegration] = [
label="Include emoji indicators in the notifications",
input_type="checkbox_enabled",
),
+ WebhookUrlOption(
+ name="concise_edit_format",
+ label="Use concise edited issue and pull request notifications",
+ input_type="checkbox_enabled",
+ ),
],
),
IncomingWebhookIntegration(
diff --git a/zerver/webhooks/github/doc.md b/zerver/webhooks/github/doc.md
index 6b242140095..727cee67af6 100644
--- a/zerver/webhooks/github/doc.md
+++ b/zerver/webhooks/github/doc.md
@@ -21,6 +21,7 @@ Get GitHub notifications in Zulip!
- Whether to exclude notifications from private repositories.
- Whether to include the repository name in the notifications.
- Whether to include emoji indicators in the notifications.
+ - Whether to use a concise message format for edited issue and pull request notifications.
1. On your repository's web page, go to **Settings**. Select **Webhooks**,
diff --git a/zerver/webhooks/github/tests.py b/zerver/webhooks/github/tests.py
index 7fa413486c8..2499e91dfb8 100644
--- a/zerver/webhooks/github/tests.py
+++ b/zerver/webhooks/github/tests.py
@@ -168,16 +168,25 @@ class GitHubWebhookTest(WebhookTestCase):
self.check_webhook("fork", TOPIC_REPO, expected_message)
def test_issues_edited_body(self) -> None:
+ self.url = self.build_webhook_url(concise_edit_format="false")
expected_topic_name = "test-repo / issue #6 New Issue edited"
expected_message = "Pritesh-30 edited [issue #6](https://github.com/Pritesh-30/test-repo/issues/6):\n\n``` quote\nThe body of the issue is edited.\n```"
self.check_webhook("issues__edited_body", expected_topic_name, expected_message)
def test_issues_edited_title(self) -> None:
+ self.url = self.build_webhook_url(concise_edit_format="false")
long_title = "This is a very long issue title used to exceed Zulip's maximum topic length so that truncation logic is exercised when the issue title is edited via the GitHub webhook"
expected_topic_name = truncate_topic(f"test-repo / issue #6 {long_title}")
expected_message = "Pritesh-30 edited [issue #6](https://github.com/Pritesh-30/test-repo/issues/6):\n\n``` quote\nThe body of the issue is edited.\n```"
self.check_webhook("issues__edited_title", expected_topic_name, expected_message)
+ def test_issues_edited_body_concise(self) -> None:
+ expected_topic_name = "test-repo / issue #6 New Issue edited"
+ expected_message = (
+ "Pritesh-30 edited [issue #6](https://github.com/Pritesh-30/test-repo/issues/6)."
+ )
+ self.check_webhook("issues__edited_body", expected_topic_name, expected_message)
+
def test_issue_comment_msg(self) -> None:
expected_message = "baxterthehacker [commented](https://github.com/baxterthehacker/public-repo/issues/2#issuecomment-99262140) on [issue #2](https://github.com/baxterthehacker/public-repo/issues/2):\n\n``` quote\nYou are totally right! I'll get this fixed right away.\n```"
self.check_webhook("issue_comment", TOPIC_ISSUE, expected_message)
@@ -475,9 +484,16 @@ class GitHubWebhookTest(WebhookTestCase):
self.check_webhook("pull_request__edited", TOPIC_PR, expected_message)
def test_pull_request_edited_with_body_change(self) -> None:
+ self.url = self.build_webhook_url(concise_edit_format="false")
expected_message = "cozyrohan edited [PR #1](https://github.com/cozyrohan/public-repo/pull/1):\n\n``` quote\nPR EDITED\n```"
self.check_webhook("pull_request__edited_with_body_change", TOPIC_PR, expected_message)
+ def test_pull_request_edited_with_body_change_concise(self) -> None:
+ expected_message = (
+ "cozyrohan edited [PR #1](https://github.com/cozyrohan/public-repo/pull/1)."
+ )
+ self.check_webhook("pull_request__edited_with_body_change", TOPIC_PR, expected_message)
+
def test_pull_request_synchronized_with_body(self) -> None:
expected_message = "baxterthehacker updated [PR #1](https://github.com/baxterthehacker/public-repo/pull/1)."
self.check_webhook("pull_request__synchronized_with_body", TOPIC_PR, expected_message)
diff --git a/zerver/webhooks/github/view.py b/zerver/webhooks/github/view.py
index e316fcef6af..a6acfc82904 100644
--- a/zerver/webhooks/github/view.py
+++ b/zerver/webhooks/github/view.py
@@ -100,6 +100,7 @@ class Helper:
include_repository_name: bool,
include_emoji_indicators: bool,
user_profile: UserProfile,
+ concise_edit_format: bool,
) -> None:
self.request = request
self.payload = payload
@@ -107,6 +108,7 @@ class Helper:
self.include_repository_name = include_repository_name
self.include_emoji_indicators = include_emoji_indicators
self.realm = user_profile.realm
+ self.concise_edit_format = concise_edit_format
def log_unsupported(self, event: str) -> None:
summary = f"The '{event}' event isn't currently supported by the GitHub webhook; ignoring"
@@ -125,7 +127,9 @@ def get_opened_or_update_pull_request_body(helper: Helper) -> str:
assignee = pull_request["assignee"]["login"].tame(check_string)
description = None
changes = payload.get("changes", {})
- if "body" in changes or action == "opened":
+ if action == "opened" or (
+ action == "edited" and not helper.concise_edit_format and "body" in changes
+ ):
description = pull_request["body"].tame(check_none_or(check_string))
target_branch = None
base_branch = None
@@ -210,16 +214,20 @@ def get_issue_body(helper: Helper) -> str:
include_title = helper.include_title
action = payload["action"].tame(check_string)
issue = payload["issue"]
+ use_concise_format = action == "edited" and helper.concise_edit_format
+ is_assignment_action = action in ("assigned", "unassigned")
+ message = (
+ None
+ if (use_concise_format or is_assignment_action)
+ else issue["body"].tame(check_none_or(check_string))
+ )
+
return get_issue_event_message(
user_name=get_sender_name(helper),
action=action,
url=issue["html_url"].tame(check_string),
number=issue["number"].tame(check_int),
- message=(
- None
- if action in ("assigned", "unassigned")
- else issue["body"].tame(check_none_or(check_string))
- ),
+ message=message,
title=issue["title"].tame(check_string) if include_title else None,
assignee_updated=(
payload["assignee"]["login"].tame(check_string) if "assignee" in payload else None
@@ -1191,6 +1199,7 @@ def api_github_webhook(
ignore_private_repositories: Json[bool] = False,
include_repository_name: Json[bool] = False,
include_emoji_indicators: Json[bool] = True,
+ concise_edit_format: Json[bool] = True,
) -> HttpResponse:
"""
GitHub sends the event as an HTTP header. We have our
@@ -1220,6 +1229,7 @@ def api_github_webhook(
include_repository_name=include_repository_name,
include_emoji_indicators=include_emoji_indicators,
user_profile=user_profile,
+ concise_edit_format=concise_edit_format,
)
body = body_function(helper)