diff --git a/templates/zerver/api/incoming-webhooks-overview.md b/templates/zerver/api/incoming-webhooks-overview.md index a62760a7ae..beaee929a7 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 your diff --git a/zerver/lib/webhooks/common.py b/zerver/lib/webhooks/common.py index 212e0f42ab..96570a80c6 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 format that 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.py index 52b18f7fcc..452c01a065 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.py index 593bb0bfe3..5d58a61547 100644 --- a/zerver/webhooks/github/view.py +++ b/zerver/webhooks/github/view.py @@ -11,6 +11,7 @@ from zerver.lib.response import json_success from 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