Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
integrations: Add support for GitLab feature flag events.
Varun-Kolanu9f5198bcd110
diff --git a/zerver/webhooks/gitlab/fixtures/feature_flag_hook__activated.json b/zerver/webhooks/gitlab/fixtures/feature_flag_hook__activated.jsonnew file mode 100644index 00000000000..9a18e731825--- /dev/null+++ b/zerver/webhooks/gitlab/fixtures/feature_flag_hook__activated.json@@ -0,0 +1,35 @@+{+ "object_kind": "feature_flag",+ "project": {+ "id": 68452408,+ "name": "sample",+ "description": null,+ "web_url": "https://gitlab.com/kolanuvarun/sample",+ "avatar_url": null,+ "git_ssh_url": "[email protected]:kolanuvarun/sample.git",+ "git_http_url": "https://gitlab.com/kolanuvarun/sample.git",+ "namespace": "kolanuvarun",+ "visibility_level": 0,+ "path_with_namespace": "kolanuvarun/sample",+ "default_branch": "master",+ "ci_config_path": "",+ "homepage": "https://gitlab.com/kolanuvarun/sample",+ "url": "[email protected]:kolanuvarun/sample.git",+ "ssh_url": "[email protected]:kolanuvarun/sample.git",+ "http_url": "https://gitlab.com/kolanuvarun/sample.git"+ },+ "user": {+ "id": 26076637,+ "name": "Varun Kolanu",+ "username": "kolanuvarun739",+ "avatar_url": "https://secure.gravatar.com/avatar/dde05c11435528ac083e918ce384c5b943c7ec54be520b7f4a4bcd3863b36060?s=80&d=identicon",+ "email": "[REDACTED]"+ },+ "user_url": "https://gitlab.com/kolanuvarun739",+ "object_attributes": {+ "id": 2073324,+ "name": "sample-feature-flag",+ "description": "",+ "active": true+ }+}diff --git a/zerver/webhooks/gitlab/fixtures/feature_flag_hook__deactivated.json b/zerver/webhooks/gitlab/fixtures/feature_flag_hook__deactivated.jsonnew file mode 100644index 00000000000..0aee4cbc1e2--- /dev/null+++ b/zerver/webhooks/gitlab/fixtures/feature_flag_hook__deactivated.json@@ -0,0 +1,35 @@+{+ "object_kind": "feature_flag",+ "project": {+ "id": 68452408,+ "name": "sample",+ "description": null,+ "web_url": "https://gitlab.com/kolanuvarun/sample",+ "avatar_url": null,+ "git_ssh_url": "[email protected]:kolanuvarun/sample.git",+ "git_http_url": "https://gitlab.com/kolanuvarun/sample.git",+ "namespace": "kolanuvarun",+ "visibility_level": 0,+ "path_with_namespace": "kolanuvarun/sample",+ "default_branch": "master",+ "ci_config_path": "",+ "homepage": "https://gitlab.com/kolanuvarun/sample",+ "url": "[email protected]:kolanuvarun/sample.git",+ "ssh_url": "[email protected]:kolanuvarun/sample.git",+ "http_url": "https://gitlab.com/kolanuvarun/sample.git"+ },+ "user": {+ "id": 26076637,+ "name": "Varun Kolanu",+ "username": "kolanuvarun739",+ "avatar_url": "https://secure.gravatar.com/avatar/dde05c11435528ac083e918ce384c5b943c7ec54be520b7f4a4bcd3863b36060?s=80&d=identicon",+ "email": "[REDACTED]"+ },+ "user_url": "https://gitlab.com/kolanuvarun739",+ "object_attributes": {+ "id": 2073324,+ "name": "sample-feature-flag",+ "description": "",+ "active": false+ }+}diff --git a/zerver/webhooks/gitlab/tests.py b/zerver/webhooks/gitlab/tests.pyindex 7e04d1df7da..0845d3b14cb 100644--- a/zerver/webhooks/gitlab/tests.py+++ b/zerver/webhooks/gitlab/tests.py@@ -685,3 +685,15 @@ A trivial change that should probably be ignored.expected_message = "Release v1.1 for tag v1.1 was deleted."self.check_webhook("release_hook__delete", expected_topic_name, expected_message)++ def test_feature_flag_activate_event_message(self) -> None:+ expected_topic_name = "sample"+ expected_message = "kolanuvarun739 activated the feature flag [sample-feature-flag](https://gitlab.com/kolanuvarun/sample/-/feature_flags)."++ self.check_webhook("feature_flag_hook__activated", expected_topic_name, expected_message)++ def test_feature_flag_deactivate_event_message(self) -> None:+ expected_topic_name = "sample"+ expected_message = "kolanuvarun739 deactivated the feature flag [sample-feature-flag](https://gitlab.com/kolanuvarun/sample/-/feature_flags)."++ self.check_webhook("feature_flag_hook__deactivated", expected_topic_name, expected_message)diff --git a/zerver/webhooks/gitlab/view.py b/zerver/webhooks/gitlab/view.pyindex 84b267f5b78..27a6ce02820 100644--- a/zerver/webhooks/gitlab/view.py+++ b/zerver/webhooks/gitlab/view.py@@ -36,6 +36,8 @@ DESIGN_COMMENT_MESSAGE_TEMPLATE = ("{user_name} {action} on design [{design_name}]({design_url}):\n{content_message}")+FEATURE_FLAG_MESSAGE_TEMPLATE = "{user} {action} the feature flag [{name}]({url})."+def fixture_to_headers(fixture_name: str) -> dict[str, str]:if fixture_name.startswith("build"):@@ -408,6 +410,19 @@ def get_release_event_body(payload: WildValue, include_title: bool) -> str:return body+def get_feature_flag_event_body(payload: WildValue, include_title: bool) -> str:+ repo_url = payload["project"]["web_url"].tame(check_string)+ feature_flag = payload["object_attributes"]+ action = "activated" if feature_flag["active"] else "deactivated"++ return FEATURE_FLAG_MESSAGE_TEMPLATE.format(+ user=payload["user"]["username"].tame(check_string),+ action=action,+ name=feature_flag["name"].tame(check_string),+ url=f"{repo_url}/-/feature_flags",+ )++def get_repo_name(payload: WildValue) -> str:if "project" in payload:return payload["project"]["name"].tame(check_string)@@ -495,6 +510,7 @@ EVENT_FUNCTION_MAPPER: dict[str, EventFunction] = {"Build Hook": get_build_hook_event_body,"Pipeline Hook": get_pipeline_event_body,"Release Hook": get_release_event_body,+ "Feature Flag Hook": get_feature_flag_event_body,}ALL_EVENT_TYPES = list(EVENT_FUNCTION_MAPPER.keys())