Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
integrations: Add support for emoji events in GitLab design comments.
Pritesh Shetty68136fa2af74
diff --git a/zerver/webhooks/gitlab/fixtures/emoji_hook__award__design_note.json b/zerver/webhooks/gitlab/fixtures/emoji_hook__award__design_note.jsonnew file mode 100644index 00000000000..7adb167bc34--- /dev/null+++ b/zerver/webhooks/gitlab/fixtures/emoji_hook__award__design_note.json@@ -0,0 +1,97 @@+{+ "object_kind": "emoji",+ "event_type": "award",+ "user": {+ "id": 26076637,+ "name": "Varun Kolanu",+ "username": "kolanuvarun739",+ "avatar_url": "https://secure.gravatar.com/avatar/dde05c11435528ac083e918ce384c5b943c7ec54be520b7f4a4bcd3863b36060?s=80&d=identicon",+ "email": "[REDACTED]"+ },+ "project_id": 68452408,+ "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"+ },+ "object_attributes": {+ "user_id": 26076637,+ "created_at": "2025-04-05 08:18:27 UTC",+ "id": 33657545,+ "name": "heart",+ "awardable_type": "Note",+ "awardable_id": 2421525600,+ "updated_at": "2025-04-05 08:18:27 UTC",+ "awarded_on_url": null+ },+ "note": {+ "author_id": 26076637,+ "change_position": {+ "base_sha": null,+ "start_sha": null,+ "head_sha": null,+ "old_path": null,+ "new_path": null,+ "position_type": "text",+ "old_line": null,+ "new_line": null,+ "line_range": null+ },+ "commit_id": null,+ "created_at": "2025-03-28 05:18:17 UTC",+ "discussion_id": "a16734cac60d334a42d27a471d3ca8e6970bf8b4",+ "id": 2421525600,+ "line_code": null,+ "note": "Hi",+ "noteable_id": 2819666,+ "noteable_type": "DesignManagement::Design",+ "original_position": {+ "base_sha": "0000000000000000000000000000000000000000",+ "start_sha": "0000000000000000000000000000000000000000",+ "head_sha": "e38b95db9dfe59d772b60ce9c99990bb535cbce8",+ "old_path": null,+ "new_path": "designs/issue-1/Sample.png",+ "position_type": "image",+ "width": 1056,+ "height": 595,+ "x": 290,+ "y": 441+ },+ "position": {+ "base_sha": "0000000000000000000000000000000000000000",+ "start_sha": "0000000000000000000000000000000000000000",+ "head_sha": "e38b95db9dfe59d772b60ce9c99990bb535cbce8",+ "old_path": null,+ "new_path": "designs/issue-1/Sample.png",+ "position_type": "image",+ "width": 1056,+ "height": 595,+ "x": 544,+ "y": 443+ },+ "project_id": 68452408,+ "resolved_at": null,+ "resolved_by_id": null,+ "resolved_by_push": null,+ "st_diff": null,+ "system": false,+ "type": "DiffNote",+ "updated_at": "2025-04-05 08:18:27 UTC",+ "updated_by_id": 26076637,+ "description": "Hi",+ "url": null+ }+}diff --git a/zerver/webhooks/gitlab/tests.py b/zerver/webhooks/gitlab/tests.pyindex 552140ec49f..322277c25ab 100644--- a/zerver/webhooks/gitlab/tests.py+++ b/zerver/webhooks/gitlab/tests.py@@ -817,3 +817,20 @@ A trivial change that should probably be ignored.self.check_webhook("emoji_hook__revoke__merge_request_note", expected_topic_name, expected_message)++ def test_emoji_award_in_design_note(self) -> None:+ expected_topic_name = "sample / design Sample.png"+ expected_message = "Varun Kolanu added the emoji :heart: to [a comment](https://gitlab.com/kolanuvarun/sample/-/issues/1/designs/Sample.png#note_2421525600)."++ self.check_webhook("emoji_hook__award__design_note", expected_topic_name, expected_message)++ def test_emoji_revoke_in_merge_request_note_with_custom_topic(self) -> None:+ expected_topic_name = "notifications"+ expected_message = "[[sample](https://gitlab.com/kolanuvarun/sample)] Varun Kolanu removed the emoji :heart: from [a comment](https://gitlab.com/kolanuvarun/sample/-/merge_requests/1#note_2431339003)."++ self.url = self.build_webhook_url(topic="notifications")+ self.check_webhook(+ "emoji_hook__revoke__merge_request_note",+ expected_topic_name,+ expected_message,+ )diff --git a/zerver/webhooks/gitlab/view.py b/zerver/webhooks/gitlab/view.pyindex f052477a640..e502edbe140 100644--- a/zerver/webhooks/gitlab/view.py+++ b/zerver/webhooks/gitlab/view.py@@ -492,6 +492,8 @@ def get_emoji_event_transformed_type(payload: WildValue, type: str) -> str:elif type == "Note":event_type = payload["note"]["noteable_type"].tame(check_string)return get_emoji_event_transformed_type(payload, event_type)+ elif type == "DesignManagement::Design":+ return "design"return type.lower()@@ -499,7 +501,13 @@ def get_emoji_event_subtype_message(type: str) -> str:return "a comment" if type == "Note" else ""-def get_emoji_event_url_id(payload: WildValue) -> tuple[str, str]:+def get_emoji_event_url_id(payload: WildValue, type: str) -> tuple[str, str]:+ if type == "design":+ comment_url, _, design_name = parse_design_comment(+ payload["note"], payload["project"]["web_url"].tame(check_string)+ )+ return comment_url, design_name+url = payload["object_attributes"]["awarded_on_url"].tame(check_string)# Extract the last numeric ID in the URL path before any '#' fragment.@@ -533,18 +541,23 @@ def get_emoji_event_topic_title(return ""+def get_emoji_event_number_sign(type: str) -> str:+ return "" if type == "design" else "#"++def get_emoji_event_body(action: str, payload: WildValue, include_title: bool) -> str:transformed_action = {"award": "added", "revoke": "removed"}.get(action, "reacted")preposition = {"award": "to", "revoke": "from"}.get(action, "to")emoji = payload["object_attributes"]awardable_type = emoji["awardable_type"].tame(check_string)transformed_type = get_emoji_event_transformed_type(payload, awardable_type)- url, id = get_emoji_event_url_id(payload)+ url, id = get_emoji_event_url_id(payload, transformed_type)subtype = get_emoji_event_subtype_message(awardable_type)+ number_sign = get_emoji_event_number_sign(transformed_type)suffix = ""if include_title or awardable_type == "Note":- target = "" if awardable_type == "Note" else f"{transformed_type} #{id}"+ target = "" if awardable_type == "Note" else f"{transformed_type} {number_sign}{id}"suffix = f" {preposition} [{subtype}{target}]({url})"return EMOJI_MESSAGE_TEMPLATE.format(@@ -599,6 +612,8 @@ def skip_previews(event: str) -> bool:# doesn't work."Note Hook DesignManagement::Design","Confidential Note Hook DesignManagement::Design",+ "Emoji Hook award",+ "Emoji Hook revoke",]@@ -777,10 +792,16 @@ def get_topic_based_on_event(event: str, payload: WildValue, use_merge_request_telif event.startswith("Emoji Hook"):awardable_type = payload["object_attributes"]["awardable_type"].tame(check_string)transformed_type = get_emoji_event_transformed_type(payload, awardable_type)+ id = get_emoji_event_url_id(payload, transformed_type)[1]+ if transformed_type == "design":+ return TOPIC_WITH_DESIGN_INFO_TEMPLATE.format(+ repo=get_repo_name(payload), type="design", design_name=id+ )+return TOPIC_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format(repo=get_repo_name(payload),type=transformed_type,- id=get_emoji_event_url_id(payload)[1],+ id=id,title=get_emoji_event_topic_title(payload, awardable_type, transformed_type, use_merge_request_title),