Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
integrations: Add JSON Printer webhook integration.
Adam Birds188273d8f518
diff --git a/static/images/integrations/bot_avatars/json.png b/static/images/integrations/bot_avatars/json.pngnew file mode 100644index 00000000000..2f1a2b2553bBinary files /dev/null and b/static/images/integrations/bot_avatars/json.png differdiff --git a/static/images/integrations/json/001.png b/static/images/integrations/json/001.pngnew file mode 100644index 00000000000..924598b7f04Binary files /dev/null and b/static/images/integrations/json/001.png differdiff --git a/static/images/integrations/logos/json.svg b/static/images/integrations/logos/json.svgnew file mode 100644index 00000000000..2e38a32aea0--- /dev/null+++ b/static/images/integrations/logos/json.svg@@ -0,0 +1 @@+<svg width="2500" height="2500" viewBox="0 0 256 256" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMin meet"><defs><linearGradient x1="15.791%" y1="14.737%" x2="91.009%" y2="85.228%" id="a"><stop offset="0%"/><stop stop-color="#FFF" offset="100%"/></linearGradient><linearGradient x1="82.136%" y1="85.237%" x2="-3.113%" y2="14.79%" id="b"><stop offset="0%"/><stop stop-color="#FFF" offset="100%"/></linearGradient></defs><path d="M127.783 190.56c56.637 77.208 112.064-21.55 111.982-80.94C239.67 39.404 168.5.16 127.737.16 62.309.159 0 54.232 0 128.216 0 210.45 71.425 256 127.737 256c-12.743-1.835-55.21-10.934-55.78-108.747-.385-66.154 21.58-92.585 55.688-80.958.764.283 37.622 14.823 37.622 62.32 0 47.296-37.484 61.944-37.484 61.944z" fill="url(#a)"/><path d="M127.717 66.241c-37.424-12.899-83.269 17.946-83.269 79.726C44.448 246.844 119.201 256 128.263 256 193.691 256 256 201.926 256 127.943 256 45.709 184.575.159 128.263.159c15.597-2.16 84.065 16.88 84.065 110.458 0 61.026-51.124 94.248-84.376 80.054-.764-.283-37.623-14.823-37.623-62.32 0-47.297 37.388-62.11 37.388-62.11z" fill="url(#b)"/></svg>\ No newline at end of filediff --git a/templates/zerver/api/incoming-webhooks-overview.md b/templates/zerver/api/incoming-webhooks-overview.mdindex 7b9b9dad453..a62760a7aeb 100644--- a/templates/zerver/api/incoming-webhooks-overview.md+++ b/templates/zerver/api/incoming-webhooks-overview.md@@ -36,10 +36,11 @@ process.* Set up the[Zulip development environment](https://zulip.readthedocs.io/en/latest/development/overview.html).-* Use <https://webhook.site/> or a similar site to capture an example+* Use [Zulip's JSON integration](/integrations/doc/json),+ <https://webhook.site/>, or a similar site to capture an examplewebhook payload from the third-party service. Create a- `zerver/webhooks/<mywebhook>/fixtures/` directory, and add the captured- payload as a test fixture.+ `zerver/webhooks/<mywebhook>/fixtures/` directory, and add the+ captured payload as a test fixture.* Create an `Integration` object, and add it to `WEBHOOK_INTEGRATIONS` in`zerver/lib/integrations.py`. Search for `webhook` in that file to find andiff --git a/templates/zerver/api/incoming-webhooks-walkthrough.md b/templates/zerver/api/incoming-webhooks-walkthrough.mdindex 20fdbfbc01a..dbcaa82f043 100644--- a/templates/zerver/api/incoming-webhooks-walkthrough.md+++ b/templates/zerver/api/incoming-webhooks-walkthrough.md@@ -13,9 +13,10 @@ integration.The first step in creating an incoming webhook is to examine the data that theservice you want to integrate will be sending to Zulip.-Use <https://webhook.site/> or a similar tool to capture-webhook payload(s) from the service you are integrating. Examining this-data allows you to do two things:+* Use [Zulip's JSON integration](/integrations/doc/json),+<https://webhook.site/>, or a similar tool to capture webhook+payload(s) from the service you are integrating. Examining this data+allows you to do two things:1. Determine how you will need to structure your webhook code, including whatmessage types your integration should support and how.diff --git a/zerver/lib/integrations.py b/zerver/lib/integrations.pyindex dc7dd8402d2..7a7eb6e96cd 100644--- a/zerver/lib/integrations.py+++ b/zerver/lib/integrations.py@@ -398,6 +398,7 @@ WEBHOOK_INTEGRATIONS: List[WebhookIntegration] = [WebhookIntegration("intercom", ["customer-support"], display_name="Intercom"),WebhookIntegration("jira", ["project-management"], display_name="JIRA"),WebhookIntegration("jotform", ["misc"], display_name="Jotform"),+ WebhookIntegration("json", ["misc"], display_name="JSON formatter"),WebhookIntegration("librato", ["monitoring"]),WebhookIntegration("mention", ["marketing"], display_name="Mention"),WebhookIntegration("netlify", ["continuous-integration", "deployment"], display_name="Netlify"),@@ -730,6 +731,7 @@ DOC_SCREENSHOT_CONFIG: Dict[str, List[ScreenshotConfig]] = {"intercom": [ScreenshotConfig("conversation_admin_replied.json")],"jira": [ScreenshotConfig("created_v1.json")],"jotform": [ScreenshotConfig("response.json")],+ "json": [ScreenshotConfig("json_github_push__1_commit.json")],"librato": [ScreenshotConfig("three_conditions_alert.json", payload_as_query_param=True)],"mention": [ScreenshotConfig("webfeeds.json")],"netlify": [ScreenshotConfig("deploy_building.json")],diff --git a/zerver/webhooks/json/__init__.py b/zerver/webhooks/json/__init__.pynew file mode 100644index 00000000000..e69de29bb2ddiff --git a/zerver/webhooks/json/doc.md b/zerver/webhooks/json/doc.mdnew file mode 100644index 00000000000..15295b8e9b0--- /dev/null+++ b/zerver/webhooks/json/doc.md@@ -0,0 +1,15 @@+Render JSON payloads nicely in a Zulip code block! This is+particularly useful when you want to capture a webhook payload as part+of [writing an incoming webhook+integration](/api/incoming-webhooks-overview).++1. {!create-stream.md!}++1. {!create-bot-construct-url-indented.md!}++1. Configure your application to send the webhook+ payload to the **URL** generated above.++{!congrats.md!}++diff --git a/zerver/webhooks/json/fixtures/json_github_push__1_commit.json b/zerver/webhooks/json/fixtures/json_github_push__1_commit.jsonnew file mode 120000index 00000000000..6bd60972c0d--- /dev/null+++ b/zerver/webhooks/json/fixtures/json_github_push__1_commit.json@@ -0,0 +1 @@+../../github/fixtures/push__1_commit.json\ No newline at end of filediff --git a/zerver/webhooks/json/fixtures/json_pingdom_http_up_to_down.json b/zerver/webhooks/json/fixtures/json_pingdom_http_up_to_down.jsonnew file mode 120000index 00000000000..94d3767764b--- /dev/null+++ b/zerver/webhooks/json/fixtures/json_pingdom_http_up_to_down.json@@ -0,0 +1 @@+../../pingdom/fixtures/http_up_to_down.json\ No newline at end of filediff --git a/zerver/webhooks/json/fixtures/json_sentry_event_for_exception_js.json b/zerver/webhooks/json/fixtures/json_sentry_event_for_exception_js.jsonnew file mode 120000index 00000000000..5559d019b87--- /dev/null+++ b/zerver/webhooks/json/fixtures/json_sentry_event_for_exception_js.json@@ -0,0 +1 @@+../../sentry/fixtures/event_for_exception_js.json\ No newline at end of filediff --git a/zerver/webhooks/json/tests.py b/zerver/webhooks/json/tests.pynew file mode 100644index 00000000000..5e4e13a57ef--- /dev/null+++ b/zerver/webhooks/json/tests.py@@ -0,0 +1,54 @@+import json++from zerver.lib.test_classes import WebhookTestCase+++class JsonHookTests(WebhookTestCase):+ STREAM_NAME = "json"+ URL_TEMPLATE = "/api/v1/external/json?api_key={api_key}&stream={stream}"+ FIXTURE_DIR_NAME = "json"++ def test_json_github_push__1_commit_message(self) -> None:+ """+ Tests if json github push 1 commit is handled correctly+ """+ with open("zerver/webhooks/json/fixtures/json_github_push__1_commit.json") as f:+ original_fixture = json.load(f)++ expected_topic = "JSON"+ expected_message = """```json+{original_fixture}+```""".format(+ original_fixture=json.dumps(original_fixture, indent=2)+ )+ self.check_webhook("json_github_push__1_commit", expected_topic, expected_message)++ def test_json_pingdom_http_up_to_down_message(self) -> None:+ """+ Tests if json pingdom http up to down is handled correctly+ """+ with open("zerver/webhooks/json/fixtures/json_pingdom_http_up_to_down.json") as f:+ original_fixture = json.load(f)++ expected_topic = "JSON"+ expected_message = """```json+{original_fixture}+```""".format(+ original_fixture=json.dumps(original_fixture, indent=2)+ )+ self.check_webhook("json_pingdom_http_up_to_down", expected_topic, expected_message)++ def test_json_sentry_event_for_exception_js_message(self) -> None:+ """+ Tests if json sentry event for exception js is handled correctly+ """+ with open("zerver/webhooks/json/fixtures/json_sentry_event_for_exception_js.json") as f:+ original_fixture = json.load(f)++ expected_topic = "JSON"+ expected_message = """```json+{original_fixture}+```""".format(+ original_fixture=json.dumps(original_fixture, indent=2)+ )+ self.check_webhook("json_sentry_event_for_exception_js", expected_topic, expected_message)diff --git a/zerver/webhooks/json/view.py b/zerver/webhooks/json/view.pynew file mode 100644index 00000000000..14f1e4af100--- /dev/null+++ b/zerver/webhooks/json/view.py@@ -0,0 +1,39 @@+import json+from typing import Any, Dict++from django.http import HttpRequest, HttpResponse++from zerver.decorator import REQ, has_request_variables, webhook_view+from zerver.lib.response import json_success+from zerver.lib.webhooks.common import check_send_webhook_message+from zerver.models import UserProfile++JSON_MESSAGE_TEMPLATE = """+```json+{webhook_payload}+```+""".strip()+++@webhook_view("JSON")+@has_request_variables+def api_json_webhook(+ request: HttpRequest,+ user_profile: UserProfile,+ payload: Dict[str, Any] = REQ(argument_type="body"),+) -> HttpResponse:++ body = get_body_for_http_request(payload)+ subject = get_subject_for_http_request(payload)++ check_send_webhook_message(request, user_profile, subject, body)+ return json_success()+++def get_subject_for_http_request(payload: Dict[str, Any]) -> str:+ return "JSON"+++def get_body_for_http_request(payload: Dict[str, Any]) -> str:+ prettypayload = json.dumps(payload, indent=2)+ return JSON_MESSAGE_TEMPLATE.format(webhook_payload=prettypayload, sort_keys=True)