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: Add support for linking to job and job run in DBT Cloud.

ubaidrmncd8d30a1e39e
diff --git a/zerver/lib/integrations.py b/zerver/lib/integrations.py
index e3915e50dac..6264de85b91 100644
--- a/zerver/lib/integrations.py
+++ b/zerver/lib/integrations.py
@@ -13,7 +13,7 @@ from django.views.decorators.csrf import csrf_exempt
from django_stubs_ext import StrPromise
from zerver.lib.storage import static_path
-from zerver.lib.validator import check_bool
+from zerver.lib.validator import check_bool, check_string
from zerver.lib.webhooks.common import PresetUrlOption, WebhookConfigOption, WebhookUrlOption
from zerver.webhooks import fixtureless_integrations
@@ -615,6 +615,9 @@ INCOMING_WEBHOOK_INTEGRATIONS: list[IncomingWebhookIntegration] = [
["deployment"],
[WebhookScreenshotConfig("job_run_completed_errored.json")],
display_name="DBT",
+ url_options=[
+ WebhookUrlOption(name="access_url", label="DBT Access URL", validator=check_string)
+ ],
),
IncomingWebhookIntegration(
"delighted",
diff --git a/zerver/webhooks/dbt/doc.md b/zerver/webhooks/dbt/doc.md
index 9f5b634bb6f..42dd9ae3144 100644
--- a/zerver/webhooks/dbt/doc.md
+++ b/zerver/webhooks/dbt/doc.md
@@ -8,6 +8,10 @@ Get notifications about DBT cloud job runs in Zulip!
1. {!generate-webhook-url-basic.md!}
+ If you'd like your notification messages to link to your DBT job
+ runs, make sure you include the [**DBT Access URL**][DBT Access URLs]
+ when generating the integration URL.
+
1. From your DBT Dashboard, navigate to **Account settings**. Select
**Notification Settings**, go to the **Webhooks** section, and
click **Create webhook**.
@@ -30,4 +34,7 @@ Get notifications about DBT cloud job runs in Zulip!
### Related documentation
- [DBT's webhooks documentation](https://docs.getdbt.com/docs/deploy/webhooks)
+- [DBT Access URLs][DBT Access URLs]
{!webhooks-url-specification.md!}
+
+[DBT Access URLs]: https://docs.getdbt.com/docs/cloud/about-cloud/access-regions-ip-addresses#api-access-urls
diff --git a/zerver/webhooks/dbt/tests.py b/zerver/webhooks/dbt/tests.py
index 2b30419bfb1..86704a871da 100644
--- a/zerver/webhooks/dbt/tests.py
+++ b/zerver/webhooks/dbt/tests.py
@@ -25,3 +25,14 @@ Job #123 was kicked off from the UI by [email protected] at <time:2025-10-05
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)
+
+
+class DBTHookWithAccessUrlTests(WebhookTestCase):
+ CHANNEL_NAME = "DBT"
+ URL_TEMPLATE = "/api/v1/external/dbt?&api_key={api_key}&stream={stream}&access_url=https%3A%2F%2Fexample.us1.dbt.com"
+ WEBHOOK_DIR_NAME = "dbt"
+
+ def test_dbt_webhook_with_valid_access_url(self) -> None:
+ expected_message = """:yellow_circle: Daily Job (dbt build) [deployment](https://example.us1.dbt.com/deploy/1/projects/167194/runs/12345) started in **Production**.\n
+[Job #123](https://example.us1.dbt.com/deploy/1/projects/167194/jobs/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)
diff --git a/zerver/webhooks/dbt/view.py b/zerver/webhooks/dbt/view.py
index 801420eab63..9d146abd5cc 100644
--- a/zerver/webhooks/dbt/view.py
+++ b/zerver/webhooks/dbt/view.py
@@ -1,16 +1,18 @@
+from urllib.parse import urljoin
+
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.validator import WildValue, check_int, 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}**.
+{emoji} {job_name} {run_text} {status} in **{environment}**.
-Job #{job_id} was {run_reason} at <time:{start_time}>.
+{job_text} was {run_reason} at <time:{start_time}>.
"""
DBT_EVENT_TYPE_MAPPER = {
@@ -31,11 +33,14 @@ ALL_EVENT_TYPES = list(DBT_EVENT_TYPE_MAPPER.keys())
def extract_data_from_payload(payload: JsonBodyPayload[WildValue]) -> dict[str, str]:
data: dict[str, str] = {
+ "account_id": str(payload["accountId"].tame(check_int)),
"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),
+ "project_id": payload["data"]["projectId"].tame(check_string),
"environment": payload["data"]["environmentName"].tame(check_string),
+ "run_id": payload["data"]["runId"].tame(check_string),
"start_time": payload["data"]["runStartedAt"].tame(check_string),
"run_status": payload["data"]["runStatus"].tame(check_string).lower(),
}
@@ -46,11 +51,29 @@ def extract_data_from_payload(payload: JsonBodyPayload[WildValue]) -> dict[str,
return data
-def get_job_run_body(data: dict[str, str]) -> str:
+def get_job_run_body(data: dict[str, str], access_url: str | None) -> str:
emoji, status = DBT_EVENT_TYPE_MAPPER[data["event_type"]][data["run_status"]]
+
+ project_url = (
+ urljoin(
+ access_url,
+ f"/deploy/{data['account_id']}/projects/{data['project_id']}",
+ )
+ if access_url
+ else None
+ )
+ job_text = (
+ f"[Job #{data['job_id']}]({project_url}/jobs/{data['job_id']})"
+ if project_url
+ else f"Job #{data['job_id']}"
+ )
+ run_text = f"[deployment]({project_url}/runs/{data['run_id']})" if project_url else "deployment"
+
body = DBT_NOTIFICATION_TEMPLATE.format(
emoji=emoji,
status=status,
+ run_text=run_text,
+ job_text=job_text,
**data,
)
return body
@@ -63,9 +86,10 @@ def api_dbt_webhook(
user_profile: UserProfile,
*,
payload: JsonBodyPayload[WildValue],
+ access_url: str | None = None,
) -> HttpResponse:
data = extract_data_from_payload(payload)
- body = get_job_run_body(data)
+ body = get_job_run_body(data, access_url)
topic_name = data["project_name"]
event = data["event_type"]
check_send_webhook_message(request, user_profile, topic_name, body, event)