From 9f5198bcd1102fc4fc79dc227521e8a491a7b0da Mon Sep 17 00:00:00 2001 From: Varun-Kolanu Date: Tue, 29 Apr 2025 06:58:49 +0530 Subject: [PATCH] integrations: Add support for GitLab feature flag events. Fixes part of #34405. --- .../feature_flag_hook__activated.json | 35 +++++++++++++++++++ .../feature_flag_hook__deactivated.json | 35 +++++++++++++++++++ zerver/webhooks/gitlab/tests.py | 12 +++++++ zerver/webhooks/gitlab/view.py | 16 +++++++++ 4 files changed, 98 insertions(+) create mode 100644 zerver/webhooks/gitlab/fixtures/feature_flag_hook__activated.json create mode 100644 zerver/webhooks/gitlab/fixtures/feature_flag_hook__deactivated.json diff --git a/zerver/webhooks/gitlab/fixtures/feature_flag_hook__activated.json b/zerver/webhooks/gitlab/fixtures/feature_flag_hook__activated.json new file mode 100644 index 0000000000..9a18e73182 --- /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": "git@gitlab.com: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": "git@gitlab.com:kolanuvarun/sample.git", + "ssh_url": "git@gitlab.com: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.json new file mode 100644 index 0000000000..0aee4cbc1e --- /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": "git@gitlab.com: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": "git@gitlab.com:kolanuvarun/sample.git", + "ssh_url": "git@gitlab.com: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.py index 7e04d1df7d..0845d3b14c 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.py index 84b267f5b7..27a6ce0282 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())