Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
webhooks: Use 200 status code for unknown events.
Zixuan James Li84723654c884
diff --git a/zerver/decorator.py b/zerver/decorator.pyindex 8d3ece157e0..2390ed78ebc 100644--- a/zerver/decorator.py+++ b/zerver/decorator.py@@ -33,6 +33,7 @@ from django.utils.timezone import now as timezone_nowfrom django.utils.translation import gettext as _from django.views.decorators.csrf import csrf_exemptfrom django_otp import user_has_device+from sentry_sdk import capture_exceptionfrom two_factor.utils import default_devicefrom typing_extensions import Concatenate, ParamSpec@@ -366,6 +367,8 @@ def webhook_view(else:if isinstance(err, WebhookError):err.webhook_name = webhook_client_name+ if isinstance(err, UnsupportedWebhookEventTypeError):+ capture_exception(err)log_exception_to_webhook_logger(err)raise err@@ -775,6 +778,8 @@ def authenticated_rest_api_view(if isinstance(err, WebhookError):err.webhook_name = webhook_client_name+ if isinstance(err, UnsupportedWebhookEventTypeError):+ capture_exception(err)log_exception_to_webhook_logger(err)raise errdiff --git a/zerver/lib/exceptions.py b/zerver/lib/exceptions.pyindex 089d7c57627..51d57f557bc 100644--- a/zerver/lib/exceptions.py+++ b/zerver/lib/exceptions.py@@ -370,6 +370,7 @@ class UnsupportedWebhookEventTypeError(WebhookError):"""code = ErrorCode.UNSUPPORTED_WEBHOOK_EVENT_TYPE+ http_status_code = 200data_fields = ["webhook_name", "event_type"]def __init__(self, event_type: Optional[str]) -> None:@@ -378,7 +379,9 @@ class UnsupportedWebhookEventTypeError(WebhookError):@staticmethoddef msg_format() -> str:- return _("The '{event_type}' event isn't currently supported by the {webhook_name} webhook")+ return _(+ "The '{event_type}' event isn't currently supported by the {webhook_name} webhook; ignoring"+ )class AnomalousWebhookPayloadError(WebhookError):diff --git a/zerver/lib/response.py b/zerver/lib/response.pyindex 13c0a4c2569..451d1770378 100644--- a/zerver/lib/response.py+++ b/zerver/lib/response.py@@ -117,8 +117,11 @@ def json_response_from_error(exception: JsonableError) -> MutableJsonResponse:middleware takes care of transforming it into a response bycalling this function."""+ response_type = "error"+ if 200 <= exception.http_status_code < 300:+ response_type = "success"response = json_response(- "error", msg=exception.msg, data=exception.data, status=exception.http_status_code+ response_type, msg=exception.msg, data=exception.data, status=exception.http_status_code)for header, value in exception.extra_headers.items():diff --git a/zerver/tests/test_decorators.py b/zerver/tests/test_decorators.pyindex a6485c5f5c8..a86078e9451 100644--- a/zerver/tests/test_decorators.py+++ b/zerver/tests/test_decorators.py@@ -406,7 +406,9 @@ class DecoratorTestCase(ZulipTestCase):request = HostRequestMock()request.host = "zulip.testserver"request.POST["api_key"] = webhook_bot_api_key- exception_msg = "The 'test_event' event isn't currently supported by the ClientName webhook"+ exception_msg = (+ "The 'test_event' event isn't currently supported by the ClientName webhook; ignoring"+ )with self.assertLogs("zulip.zerver.webhooks.unsupported", level="ERROR") as log:with self.assertRaisesRegex(UnsupportedWebhookEventTypeError, exception_msg):request.body = b"invalidjson"@@ -577,9 +579,7 @@ class DecoratorLoggingTestCase(ZulipTestCase):with mock.patch("zerver.decorator.webhook_unsupported_events_logger.exception") as mock_exception:- exception_msg = (- "The 'test_event' event isn't currently supported by the ClientName webhook"- )+ exception_msg = "The 'test_event' event isn't currently supported by the ClientName webhook; ignoring"with self.assertRaisesRegex(UnsupportedWebhookEventTypeError, exception_msg):my_webhook_raises_exception(request)diff --git a/zerver/webhooks/github/tests.py b/zerver/webhooks/github/tests.pyindex 4aee3e6c3a9..1bb3c564a51 100644--- a/zerver/webhooks/github/tests.py+++ b/zerver/webhooks/github/tests.py@@ -565,7 +565,7 @@ A temporary team so that I can get some webhook fixtures!stack_info = m.call_args[1]["stack_info"]self.assertIn(- "The 'team/edited (changes: bogus_key1/bogus_key2)' event isn't currently supported by the GitHub webhook",+ "The 'team/edited (changes: bogus_key1/bogus_key2)' event isn't currently supported by the GitHub webhook; ignoring",msg,)self.assertTrue(stack_info)diff --git a/zerver/webhooks/github/view.py b/zerver/webhooks/github/view.pyindex 6bee8778216..9c12280c1c3 100644--- a/zerver/webhooks/github/view.py+++ b/zerver/webhooks/github/view.py@@ -55,7 +55,7 @@ class Helper:self.include_title = include_titledef log_unsupported(self, event: str) -> None:- summary = f"The '{event}' event isn't currently supported by the GitHub webhook"+ summary = f"The '{event}' event isn't currently supported by the GitHub webhook; ignoring"log_unsupported_webhook_event(summary=summary,)diff --git a/zerver/webhooks/pagerduty/tests.py b/zerver/webhooks/pagerduty/tests.pyindex 6d7bc47e51e..5d1ff39ffc5 100644--- a/zerver/webhooks/pagerduty/tests.py+++ b/zerver/webhooks/pagerduty/tests.py@@ -80,7 +80,8 @@ class PagerDutyHookTests(WebhookTestCase):for version in range(1, 4):payload = self.get_body(f"unsupported_v{version}")result = self.client_post(self.url, payload, content_type="application/json")- self.assert_json_error(+ self.assert_json_success(result)+ self.assert_in_response(+ "The 'incident.unsupported' event isn't currently supported by the PagerDuty webhook; ignoring",result,- "The 'incident.unsupported' event isn't currently supported by the PagerDuty webhook",)diff --git a/zerver/webhooks/updown/fixtures/unknown_event.json b/zerver/webhooks/updown/fixtures/unknown_event.jsonnew file mode 100644index 00000000000..bcb3fee657e--- /dev/null+++ b/zerver/webhooks/updown/fixtures/unknown_event.json@@ -0,0 +1,27 @@+[{+ "event": "unknown",+ "check": {+ "token": "ngg8",+ "url": "https://updown.io",+ "alias": "",+ "last_status": 200,+ "uptime": 99.954,+ "down": false,+ "down_since": null,+ "error": null,+ "period": 30,+ "apdex_t": 0.25,+ "string_match": "",+ "enabled": true,+ "published": true,+ "last_check_at": "2016-02-07T13:16:07Z",+ "next_check_at": "2016-02-07T13:16:37Z",+ "favicon_url": "https://updown.io/favicon.png"+ },+ "downtime": {+ "error": null,+ "started_at": null,+ "ended_at": null,+ "duration": null+ }+}]diff --git a/zerver/webhooks/updown/tests.py b/zerver/webhooks/updown/tests.pyindex 23d5a813c1a..a5abbfc8a51 100644--- a/zerver/webhooks/updown/tests.py+++ b/zerver/webhooks/updown/tests.py@@ -52,3 +52,6 @@ class UpdownHookTests(WebhookTestCase):topic_name=topic_name,content=up_content,)++ def test_unknown_event(self) -> None:+ self.check_webhook("unknown_event", expect_noop=True)