Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
integrations: Move `get_setup_webhook_message` to `common.py`.
Adam Birdsc72ef7be1213
diff --git a/templates/zerver/api/incoming-webhooks-overview.md b/templates/zerver/api/incoming-webhooks-overview.mdindex a62760a7aeb..beaee929a71 100644--- a/templates/zerver/api/incoming-webhooks-overview.md+++ b/templates/zerver/api/incoming-webhooks-overview.md@@ -106,6 +106,13 @@ below are for a webhook named `MyWebHook`.associate it with the function called `api_mywebhook_webhook` in`zerver/webhooks/mywebhook/view.py`.+## Common Helpers++* If your integration will receive a test webhook payload, you can use+ `get_setup_webhook_message` to create our standard message for test payloads.+ You can import this from `zerver/lib/webhooks/common.py`, and it will generate+ a message like this: "GitHub webhook is successfully configured! 🎉"+## General advice* Consider using our Zulip markup to make the output from yourdiff --git a/zerver/lib/webhooks/common.py b/zerver/lib/webhooks/common.pyindex 212e0f42ab4..96570a80c63 100644--- a/zerver/lib/webhooks/common.py+++ b/zerver/lib/webhooks/common.py@@ -32,10 +32,21 @@ but didn't correctly configure the webhook to send data in the JSON formatthat this integration expects!"""+SETUP_MESSAGE_TEMPLATE = "{integration} webhook has been successfully configured"+SETUP_MESSAGE_USER_PART = " by {user_name}"+# Django prefixes all custom HTTP headers with `HTTP_`DJANGO_HTTP_PREFIX = "HTTP_"+def get_setup_webhook_message(integration: str, user_name: Optional[str] = None) -> str:+ content = SETUP_MESSAGE_TEMPLATE.format(integration=integration)+ if user_name:+ content += SETUP_MESSAGE_USER_PART.format(user_name=user_name)+ content = f"{content}."+ return content++def notify_bot_owner_about_invalid_json(user_profile: UserProfile, webhook_client_name: str) -> None:diff --git a/zerver/lib/webhooks/git.py b/zerver/lib/webhooks/git.pyindex 52b18f7fcc7..452c01a0657 100644--- a/zerver/lib/webhooks/git.py+++ b/zerver/lib/webhooks/git.py@@ -54,9 +54,6 @@ PULL_REQUEST_OR_ISSUE_MESSAGE_TEMPLATE_WITH_TITLE = (PULL_REQUEST_OR_ISSUE_ASSIGNEE_INFO_TEMPLATE = "(assigned to {assignee})"PULL_REQUEST_BRANCH_INFO_TEMPLATE = "from `{target}` to `{base}`"-SETUP_MESSAGE_TEMPLATE = "{integration} webhook has been successfully configured"-SETUP_MESSAGE_USER_PART = " by {user_name}"-CONTENT_MESSAGE_TEMPLATE = "\n~~~ quote\n{message}\n~~~"COMMITS_COMMENT_MESSAGE_TEMPLATE = "{user_name} {action} on [{sha}]({url})"@@ -225,14 +222,6 @@ def get_pull_request_event_message(return main_message.rstrip()-def get_setup_webhook_message(integration: str, user_name: Optional[str] = None) -> str:- content = SETUP_MESSAGE_TEMPLATE.format(integration=integration)- if user_name:- content += SETUP_MESSAGE_USER_PART.format(user_name=user_name)- content = f"{content}."- return content--def get_issue_event_message(user_name: str,action: str,diff --git a/zerver/webhooks/github/view.py b/zerver/webhooks/github/view.pyindex 593bb0bfe38..5d58a615470 100644--- a/zerver/webhooks/github/view.py+++ b/zerver/webhooks/github/view.py@@ -11,6 +11,7 @@ from zerver.lib.response import json_successfrom zerver.lib.webhooks.common import (check_send_webhook_message,get_http_headers_from_filename,+ get_setup_webhook_message,validate_extract_webhook_http_header,)from zerver.lib.webhooks.git import (@@ -23,7 +24,6 @@ from zerver.lib.webhooks.git import (get_push_commits_event_message,get_push_tag_event_message,get_release_event_message,- get_setup_webhook_message,)from zerver.models import UserProfile