mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 05:23:35 +00:00
webhooks/papertrail: Improve message formatting and punctuation.
This commit is contained in:
@@ -6,36 +6,48 @@ class PapertrailHookTests(WebhookTestCase):
|
||||
URL_TEMPLATE = "/api/v1/external/papertrail?&api_key={api_key}&stream={stream}"
|
||||
FIXTURE_DIR_NAME = 'papertrail'
|
||||
|
||||
# Note: Include a test function per each distinct message condition your integration supports
|
||||
def test_short_message(self) -> None:
|
||||
expected_topic = u"logs"
|
||||
expected_message = u'''**"Important stuff"** search found **2** matches - https://papertrailapp.com/searches/42
|
||||
```
|
||||
May 18 20:30:02 abc cron OR server1:
|
||||
message body
|
||||
May 18 20:30:02 server1 cron OR server1:
|
||||
A short event
|
||||
```'''
|
||||
expected_message = """
|
||||
[Search for "Important stuff"](https://papertrailapp.com/searches/42) found **2** matches:
|
||||
|
||||
May 18 20:30:02 - abc - cron OR server1:
|
||||
``` quote
|
||||
message body
|
||||
```
|
||||
May 18 20:30:02 - server1 - cron OR server1:
|
||||
``` quote
|
||||
A short event
|
||||
```
|
||||
""".strip()
|
||||
|
||||
# use fixture named papertrail_logs
|
||||
self.send_and_test_stream_message('short_post', expected_topic, expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
def test_long_message(self) -> None:
|
||||
expected_topic = u"logs"
|
||||
expected_message = u'''**"Important stuff"** search found **5** matches - https://papertrailapp.com/searches/42
|
||||
expected_message = """
|
||||
[Search for "Important stuff"](https://papertrailapp.com/searches/42) found **5** matches:
|
||||
|
||||
May 18 20:30:02 - abc - cron OR server1:
|
||||
``` quote
|
||||
message body 1
|
||||
```
|
||||
May 18 20:30:02 abc cron OR server1:
|
||||
message body 1
|
||||
May 18 20:30:02 abc cron OR server1:
|
||||
message body 2
|
||||
May 18 20:30:02 abc cron OR server1:
|
||||
message body 3
|
||||
May 18 20:30:02 abc cron OR server1:
|
||||
message body 4
|
||||
May 18 20:30:02 - abc - cron OR server1:
|
||||
``` quote
|
||||
message body 2
|
||||
```
|
||||
[See more](https://papertrailapp.com/searches/42)'''
|
||||
# use fixture named papertrail_logs
|
||||
May 18 20:30:02 - abc - cron OR server1:
|
||||
``` quote
|
||||
message body 3
|
||||
```
|
||||
May 18 20:30:02 - abc - cron OR server1:
|
||||
``` quote
|
||||
message body 4
|
||||
```
|
||||
[See more](https://papertrailapp.com/searches/42)
|
||||
""".strip()
|
||||
|
||||
self.send_and_test_stream_message('long_post', expected_topic, expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
|
||||
@@ -8,33 +8,42 @@ from zerver.lib.response import json_success
|
||||
from zerver.lib.webhooks.common import check_send_webhook_message
|
||||
from zerver.models import UserProfile
|
||||
|
||||
MATCHES_TEMPLATE = '[Search for "{name}"]({url}) found **{number}** matches:\n'
|
||||
SEARCH_TEMPLATE = """
|
||||
{timestamp} - {source} - {query}:
|
||||
``` quote
|
||||
{message}
|
||||
```
|
||||
""".strip()
|
||||
|
||||
@api_key_only_webhook_view('Papertrail')
|
||||
@has_request_variables
|
||||
def api_papertrail_webhook(request: HttpRequest, user_profile: UserProfile,
|
||||
payload: Dict[str, Any]=REQ(argument_type='body')) -> HttpResponse:
|
||||
|
||||
# construct the message of the message
|
||||
message_template = '**"{}"** search found **{}** matches - {}\n```'
|
||||
message = [message_template.format(payload["saved_search"]["name"],
|
||||
str(len(payload["events"])),
|
||||
payload["saved_search"]["html_search_url"])]
|
||||
for i, event in enumerate(payload["events"]):
|
||||
event_text = '{} {} {}:\n {}'.format(event["display_received_at"],
|
||||
event["source_name"],
|
||||
payload["saved_search"]["query"],
|
||||
event["message"])
|
||||
message.append(event_text)
|
||||
if i >= 3:
|
||||
message.append('```\n[See more]({})'.format(payload["saved_search"]["html_search_url"]))
|
||||
break
|
||||
else:
|
||||
message.append('```')
|
||||
post = '\n'.join(message)
|
||||
matches = MATCHES_TEMPLATE.format(
|
||||
name=payload["saved_search"]["name"],
|
||||
url=payload["saved_search"]["html_search_url"],
|
||||
number=str(len(payload["events"]))
|
||||
)
|
||||
message = [matches]
|
||||
|
||||
for i, event in enumerate(payload["events"]):
|
||||
event_text = SEARCH_TEMPLATE.format(
|
||||
timestamp=event["display_received_at"],
|
||||
source=event["source_name"],
|
||||
query=payload["saved_search"]["query"],
|
||||
message=event["message"]
|
||||
)
|
||||
|
||||
message.append(event_text)
|
||||
|
||||
if i >= 3:
|
||||
message.append('[See more]({})'.format(payload["saved_search"]["html_search_url"]))
|
||||
break
|
||||
|
||||
post = '\n'.join(message)
|
||||
topic = 'logs'
|
||||
|
||||
# send the message
|
||||
check_send_webhook_message(request, user_profile, topic, post)
|
||||
|
||||
# return json result
|
||||
return json_success()
|
||||
|
||||
Reference in New Issue
Block a user