Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
integrations: Support params for events filtering.
PIG208542516b07b7a
diff --git a/zerver/lib/webhooks/common.py b/zerver/lib/webhooks/common.pyindex b2535c204e4..a962f698561 100644--- a/zerver/lib/webhooks/common.py+++ b/zerver/lib/webhooks/common.py@@ -1,6 +1,7 @@+import fnmatchimport importlibfrom datetime import datetime-from typing import Any, Callable, Dict, Optional, Union+from typing import Any, Callable, Dict, List, Optional, Unionfrom urllib.parse import unquotefrom django.http import HttpRequest@@ -15,6 +16,7 @@ from zerver.lib.exceptions import ErrorCode, JsonableError, StreamDoesNotExistErfrom zerver.lib.request import REQ, has_request_variablesfrom zerver.lib.send_email import FromAddressfrom zerver.lib.timestamp import timestamp_to_datetime+from zerver.lib.validator import check_list, check_stringfrom zerver.models import UserProfileMISSING_EVENT_HEADER_MESSAGE = """@@ -75,10 +77,34 @@ def check_send_webhook_message(user_profile: UserProfile,topic: str,body: str,+ complete_event_type: Optional[str] = None,stream: Optional[str] = REQ(default=None),user_specified_topic: Optional[str] = REQ("topic", default=None),+ only_events: Optional[List[str]] = REQ(default=None, json_validator=check_list(check_string)),+ exclude_events: Optional[List[str]] = REQ(+ default=None, json_validator=check_list(check_string)+ ),unquote_url_parameters: bool = False,) -> None:+ if complete_event_type is not None:+ # Here, we implement Zulip's generic support for filtering+ # events sent by the third-party service.+ #+ # If complete_event_type is passed to this function, we will check the event+ # type against user configured lists of only_events and exclude events.+ # If the event does not satisfy the configuration, the function will return+ # without sending any messages.+ #+ # We match items in only_events and exclude_events using Unix+ # shell-style wildcards.+ if (+ only_events is not None+ and all([not fnmatch.fnmatch(complete_event_type, pattern) for pattern in only_events])+ ) or (+ exclude_events is not None+ and any([fnmatch.fnmatch(complete_event_type, pattern) for pattern in exclude_events])+ ):+ returnif stream is None:assert user_profile.bot_owner is not Nonediff --git a/zerver/webhooks/travis/tests.py b/zerver/webhooks/travis/tests.pyindex eba5262a7b2..30d4ef1e6ff 100644--- a/zerver/webhooks/travis/tests.py+++ b/zerver/webhooks/travis/tests.py@@ -50,6 +50,93 @@ Details: [changes](https://github.com/hl7-fhir/fhir-svn/compare/6dccb98bcfd9...6content_type="application/x-www-form-urlencoded",)+ def test_travis_only_push_event(self) -> None:+ self.subscribe(self.test_user, self.STREAM_NAME)+ self.url = f'{self.build_webhook_url()}&only_events=["push"]'++ self.check_webhook(+ "build",+ self.TOPIC,+ self.EXPECTED_MESSAGE,+ content_type="application/x-www-form-urlencoded",+ )++ def test_travis_only_push_event_not_sent(self) -> None:+ self.subscribe(self.test_user, self.STREAM_NAME)+ self.url = f'{self.build_webhook_url()}&only_events=["push"]&ignore_pull_requests=false'++ result = self.client_post(+ self.url,+ self.get_body("pull_request"),+ content_type="application/x-www-form-urlencoded",+ )+ self.assert_json_success(result)+ msg = self.get_last_message()+ self.assertNotEqual(msg.topic_name(), self.TOPIC)++ def test_travis_exclude_push_event(self) -> None:+ self.subscribe(self.test_user, self.STREAM_NAME)+ self.url = f'{self.build_webhook_url()}&exclude_events=["push"]'++ result = self.client_post(+ self.url,+ self.get_body("build"),+ content_type="application/x-www-form-urlencoded",+ )+ self.assert_json_success(result)+ msg = self.get_last_message()+ self.assertNotEqual(msg.topic_name(), self.TOPIC)++ def test_travis_exlude_push_event_sent(self) -> None:+ self.subscribe(self.test_user, self.STREAM_NAME)+ self.url = f'{self.build_webhook_url()}&exclude_events=["push"]&ignore_pull_requests=false'++ self.check_webhook(+ "pull_request",+ self.TOPIC,+ self.EXPECTED_MESSAGE,+ content_type="application/x-www-form-urlencoded",+ )++ def test_travis_include_glob_events(self) -> None:+ self.subscribe(self.test_user, self.STREAM_NAME)+ self.url = f'{self.build_webhook_url()}&include_events=["*"]&ignore_pull_requests=false'++ self.check_webhook(+ "pull_request",+ self.TOPIC,+ self.EXPECTED_MESSAGE,+ content_type="application/x-www-form-urlencoded",+ )++ self.check_webhook(+ "build",+ self.TOPIC,+ self.EXPECTED_MESSAGE,+ content_type="application/x-www-form-urlencoded",+ )++ def test_travis_exclude_glob_events(self) -> None:+ self.subscribe(self.test_user, self.STREAM_NAME)+ self.url = f'{self.build_webhook_url()}&exclude_events=["*"]&ignore_pull_requests=false'++ result = self.client_post(+ self.url,+ self.get_body("pull_request"),+ content_type="application/x-www-form-urlencoded",+ )+ self.assert_json_success(result)++ result = self.client_post(+ self.url,+ self.get_body("build"),+ content_type="application/x-www-form-urlencoded",+ )+ self.assert_json_success(result)++ msg = self.get_last_message()+ self.assertNotEqual(msg.topic_name(), self.TOPIC)+def get_body(self, fixture_name: str) -> str:return urllib.parse.urlencode({"payload": self.webhook_fixture_data("travis", fixture_name, file_type="json")}diff --git a/zerver/webhooks/travis/view.py b/zerver/webhooks/travis/view.pyindex 492ebe03775..6cda0cfa56f 100644--- a/zerver/webhooks/travis/view.py+++ b/zerver/webhooks/travis/view.py@@ -37,7 +37,7 @@ def api_travis_webhook(),),) -> HttpResponse:-+ event = str(message["type"])message_status = message["status_message"]if ignore_pull_requests and message["type"] == "pull_request":return json_success()@@ -60,5 +60,5 @@ def api_travis_webhook()topic = "builds"- check_send_webhook_message(request, user_profile, topic, body)+ check_send_webhook_message(request, user_profile, topic, body, event)return json_success()