integrations: Move `get_setup_webhook_message` to `common.py`.

Move `get_setup_webhook_message` to
`zerver/lib/webhooks/common.py` so multiple integrations can use this
rather than just those which import `zerver/lib/webhooks/git.py`. Also
added the documentation for this.
This commit is contained in:
Adam Birds 2021-05-06 14:02:36 +00:00 committed by Tim Abbott
parent c598a84dd6
commit c72ef7be12
4 changed files with 19 additions and 12 deletions

View File

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

View File

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

View File

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

View File

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