Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

integrations: Create incoming webhook for DBT.

ubaidrmnf3ff0d9fcc48
diff --git a/static/images/integrations/bot_avatars/dbt.png b/static/images/integrations/bot_avatars/dbt.png
new file mode 100644
index 00000000000..b8c16ecb1fe
Binary files /dev/null and b/static/images/integrations/bot_avatars/dbt.png differ
diff --git a/static/images/integrations/dbt/001.png b/static/images/integrations/dbt/001.png
new file mode 100644
index 00000000000..e3700a2824f
Binary files /dev/null and b/static/images/integrations/dbt/001.png differ
diff --git a/static/images/integrations/logos/dbt.png b/static/images/integrations/logos/dbt.png
new file mode 100644
index 00000000000..ddd2f21a6fb
Binary files /dev/null and b/static/images/integrations/logos/dbt.png differ
diff --git a/zerver/lib/integrations.py b/zerver/lib/integrations.py
index 6defa36d7c9..e3915e50dac 100644
--- a/zerver/lib/integrations.py
+++ b/zerver/lib/integrations.py
@@ -610,6 +610,12 @@ INCOMING_WEBHOOK_INTEGRATIONS: list[IncomingWebhookIntegration] = [
IncomingWebhookIntegration(
"crashlytics", ["monitoring"], [WebhookScreenshotConfig("issue_message.json")]
),
+ IncomingWebhookIntegration(
+ "dbt",
+ ["deployment"],
+ [WebhookScreenshotConfig("job_run_completed_errored.json")],
+ display_name="DBT",
+ ),
IncomingWebhookIntegration(
"delighted",
["customer-support", "marketing"],
diff --git a/zerver/webhooks/dbt/__init__.py b/zerver/webhooks/dbt/__init__.py
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/zerver/webhooks/dbt/doc.md b/zerver/webhooks/dbt/doc.md
new file mode 100644
index 00000000000..9f5b634bb6f
--- /dev/null
+++ b/zerver/webhooks/dbt/doc.md
@@ -0,0 +1,33 @@
+# Zulip DBT integration
+
+Get notifications about DBT cloud job runs in Zulip!
+
+{start_tabs}
+
+1. {!create-an-incoming-webhook.md!}
+
+1. {!generate-webhook-url-basic.md!}
+
+1. From your DBT Dashboard, navigate to **Account settings**. Select
+ **Notification Settings**, go to the **Webhooks** section, and
+ click **Create webhook**.
+
+1. Set **Webhook name** to a name of your choice, such as `Zulip`. Select
+ the **Events** and **Jobs** you want to receive notifications for,
+ and set **Endpoint** to the URL generated above. Click **Save**.
+
+1. Click **Test Endpoint** to send a test notification to
+ your Zulip organization.
+
+{end_tabs}
+
+{!congrats.md!}
+
+![](/static/images/integrations/dbt/001.png)
+
+{!event-filtering-additional-feature.md!}
+
+### Related documentation
+
+- [DBT's webhooks documentation](https://docs.getdbt.com/docs/deploy/webhooks)
+{!webhooks-url-specification.md!}
diff --git a/zerver/webhooks/dbt/fixtures/job_run_completed_errored.json b/zerver/webhooks/dbt/fixtures/job_run_completed_errored.json
new file mode 100644
index 00000000000..7321c58e06c
--- /dev/null
+++ b/zerver/webhooks/dbt/fixtures/job_run_completed_errored.json
@@ -0,0 +1,24 @@
+{
+ "accountId": 1,
+ "webhookId": "wsu_12345abcde",
+ "eventId": "wev_2LkSAiWzKkSA59oilyd7FDeacXD",
+ "timestamp": "2025-10-05T19:16:11.818389474Z",
+ "eventType": "job.run.completed",
+ "webhookName": "test",
+ "data": {
+ "jobId": "123",
+ "jobName": "Daily Job (dbt build)",
+ "runId": "12345",
+ "environmentId": "1234",
+ "environmentName": "Production",
+ "dbtVersion": "2025.9.30+21d3fd7",
+ "projectName": "Example Project",
+ "projectId": "167194",
+ "runStatus": "Errored",
+ "runStatusCode": 20,
+ "runStatusMessage": "dbt command failed",
+ "runReason": "Kicked off from the UI by [email protected]",
+ "runStartedAt": "2025-10-05T19:15:56Z",
+ "runFinishedAt": "2025-10-05T19:16:06Z"
+ }
+}
diff --git a/zerver/webhooks/dbt/fixtures/job_run_completed_success.json b/zerver/webhooks/dbt/fixtures/job_run_completed_success.json
new file mode 100644
index 00000000000..8ab72d0c3fa
--- /dev/null
+++ b/zerver/webhooks/dbt/fixtures/job_run_completed_success.json
@@ -0,0 +1,24 @@
+{
+ "accountId": 1,
+ "webhookId": "wsu_12345abcde",
+ "eventId": "wev_2L6ZDoilyiWzKkSA59Gmc2d7FDD",
+ "timestamp": "2023-01-31T19:29:35.789265936Z",
+ "eventType": "job.run.completed",
+ "webhookName": "test",
+ "data": {
+ "jobId": "123",
+ "jobName": "Daily Job (dbt build)",
+ "runId": "12345",
+ "environmentId": "1234",
+ "environmentName": "Production",
+ "dbtVersion": "1.0.0",
+ "projectName": "Example Project",
+ "projectId": "167194",
+ "runStatus": "Success",
+ "runStatusCode": 10,
+ "runStatusMessage": "None",
+ "runReason": "Kicked off from the UI by [email protected]",
+ "runStartedAt": "2023-01-31T19:28:07Z",
+ "runFinishedAt": "2023-01-31T19:29:32Z"
+ }
+}
diff --git a/zerver/webhooks/dbt/fixtures/job_run_errored.json b/zerver/webhooks/dbt/fixtures/job_run_errored.json
new file mode 100644
index 00000000000..42710ab27fd
--- /dev/null
+++ b/zerver/webhooks/dbt/fixtures/job_run_errored.json
@@ -0,0 +1,24 @@
+{
+ "accountId": 1,
+ "webhookId": "wsu_12345abcde",
+ "eventId": "wev_2L6m5BggBw9uPNuSmtg4MUiW4Re",
+ "timestamp": "2023-01-31T21:15:20.419714619Z",
+ "eventType": "job.run.errored",
+ "webhookName": "test",
+ "data": {
+ "jobId": "123",
+ "jobName": "Daily Job (dbt build)",
+ "runId": "12345",
+ "environmentId": "1234",
+ "environmentName": "Production",
+ "dbtVersion": "1.0.0",
+ "projectName": "Example Project",
+ "projectId": "167194",
+ "runStatus": "Errored",
+ "runStatusCode": 20,
+ "runStatusMessage": "None",
+ "runReason": "Kicked off from the UI by [email protected]",
+ "runStartedAt": "2023-01-31T21:14:41Z",
+ "runErroredAt": "2023-01-31T21:15:20Z"
+ }
+}
diff --git a/zerver/webhooks/dbt/fixtures/job_run_started.json b/zerver/webhooks/dbt/fixtures/job_run_started.json
new file mode 100644
index 00000000000..5357392d942
--- /dev/null
+++ b/zerver/webhooks/dbt/fixtures/job_run_started.json
@@ -0,0 +1,23 @@
+{
+ "accountId": 1,
+ "webhookId": "wsu_12345abcde",
+ "eventId": "wev_2L6Z3l8uPedXKPq9D2nWbPIip7Z",
+ "timestamp": "2023-01-31T19:28:15.742843678Z",
+ "eventType": "job.run.started",
+ "webhookName": "test",
+ "data": {
+ "jobId": "123",
+ "jobName": "Daily Job (dbt build)",
+ "runId": "12345",
+ "environmentId": "1234",
+ "environmentName": "Production",
+ "dbtVersion": "1.0.0",
+ "projectName": "Example Project",
+ "projectId": "167194",
+ "runStatus": "Running",
+ "runStatusCode": 3,
+ "runStatusMessage": "None",
+ "runReason": "Kicked off from the UI by [email protected]",
+ "runStartedAt": "2023-01-31T19:28:07Z"
+ }
+}
diff --git a/zerver/webhooks/dbt/tests.py b/zerver/webhooks/dbt/tests.py
new file mode 100644
index 00000000000..2b30419bfb1
--- /dev/null
+++ b/zerver/webhooks/dbt/tests.py
@@ -0,0 +1,27 @@
+from zerver.lib.test_classes import WebhookTestCase
+
+
+class DBTHookTests(WebhookTestCase):
+ CHANNEL_NAME = "DBT"
+ URL_TEMPLATE = "/api/v1/external/dbt?&api_key={api_key}&stream={stream}"
+ WEBHOOK_DIR_NAME = "dbt"
+
+ def test_dbt_webhook_when_job_started(self) -> None:
+ expected_message = """:yellow_circle: Daily Job (dbt build) deployment started in **Production**.\n
+Job #123 was kicked off from the UI by [email protected] at <time:2023-01-31T19:28:07Z>."""
+ self.check_webhook("job_run_started", "Example Project", expected_message)
+
+ def test_dbt_webhook_when_job_completed_success(self) -> None:
+ expected_message = """:green_circle: Daily Job (dbt build) deployment succeeded in **Production**.\n
+Job #123 was kicked off from the UI by [email protected] at <time:2023-01-31T19:28:07Z>."""
+ self.check_webhook("job_run_completed_success", "Example Project", expected_message)
+
+ def test_dbt_webhook_when_job_completed_errored(self) -> None:
+ expected_message = """:cross_mark: Daily Job (dbt build) deployment completed with errors in **Production**.\n
+Job #123 was kicked off from the UI by [email protected] at <time:2025-10-05T19:15:56Z>."""
+ self.check_webhook("job_run_completed_errored", "Example Project", expected_message)
+
+ def test_dbt_webhook_when_job_errored(self) -> None:
+ expected_message = """:cross_mark: Daily Job (dbt build) deployment failed in **Production**.\n
+Job #123 was kicked off from the UI by [email protected] at <time:2023-01-31T21:14:41Z>."""
+ self.check_webhook("job_run_errored", "Example Project", expected_message)
diff --git a/zerver/webhooks/dbt/view.py b/zerver/webhooks/dbt/view.py
new file mode 100644
index 00000000000..801420eab63
--- /dev/null
+++ b/zerver/webhooks/dbt/view.py
@@ -0,0 +1,72 @@
+from django.http import HttpRequest, HttpResponse
+
+from zerver.decorator import webhook_view
+from zerver.lib.response import json_success
+from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
+from zerver.lib.validator import WildValue, check_string
+from zerver.lib.webhooks.common import check_send_webhook_message
+from zerver.models import UserProfile
+
+DBT_NOTIFICATION_TEMPLATE = """
+{emoji} {job_name} deployment {status} in **{environment}**.
+
+Job #{job_id} was {run_reason} at <time:{start_time}>.
+"""
+
+DBT_EVENT_TYPE_MAPPER = {
+ "job.run.started": {
+ "running": (":yellow_circle:", "started"),
+ },
+ "job.run.completed": {
+ "success": (":green_circle:", "succeeded"),
+ "errored": (":cross_mark:", "completed with errors"),
+ },
+ "job.run.errored": {
+ "errored": (":cross_mark:", "failed"),
+ },
+}
+
+ALL_EVENT_TYPES = list(DBT_EVENT_TYPE_MAPPER.keys())
+
+
+def extract_data_from_payload(payload: JsonBodyPayload[WildValue]) -> dict[str, str]:
+ data: dict[str, str] = {
+ "event_type": payload["eventType"].tame(check_string),
+ "job_id": payload["data"]["jobId"].tame(check_string),
+ "job_name": payload["data"]["jobName"].tame(check_string),
+ "project_name": payload["data"]["projectName"].tame(check_string),
+ "environment": payload["data"]["environmentName"].tame(check_string),
+ "start_time": payload["data"]["runStartedAt"].tame(check_string),
+ "run_status": payload["data"]["runStatus"].tame(check_string).lower(),
+ }
+ # We only change the capitalization of the first letter in this
+ # string for the formatting of our notification template.
+ run_reason = payload["data"]["runReason"].tame(check_string)
+ data["run_reason"] = run_reason[:1].lower() + run_reason[1:]
+ return data
+
+
+def get_job_run_body(data: dict[str, str]) -> str:
+ emoji, status = DBT_EVENT_TYPE_MAPPER[data["event_type"]][data["run_status"]]
+ body = DBT_NOTIFICATION_TEMPLATE.format(
+ emoji=emoji,
+ status=status,
+ **data,
+ )
+ return body
+
+
+@webhook_view("DBT", all_event_types=ALL_EVENT_TYPES)
+@typed_endpoint
+def api_dbt_webhook(
+ request: HttpRequest,
+ user_profile: UserProfile,
+ *,
+ payload: JsonBodyPayload[WildValue],
+) -> HttpResponse:
+ data = extract_data_from_payload(payload)
+ body = get_job_run_body(data)
+ topic_name = data["project_name"]
+ event = data["event_type"]
+ check_send_webhook_message(request, user_profile, topic_name, body, event)
+ return json_success(request)