Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

integrations: Add support for emoji reactions on GitLab comments.

Pritesh Shetty1300e7d96d7b
diff --git a/zerver/webhooks/gitlab/fixtures/emoji_hook__revoke__merge_request_note.json b/zerver/webhooks/gitlab/fixtures/emoji_hook__revoke__merge_request_note.json
new file mode 100644
index 00000000000..a35471c54ed
--- /dev/null
+++ b/zerver/webhooks/gitlab/fixtures/emoji_hook__revoke__merge_request_note.json
@@ -0,0 +1,158 @@
+{
+ "object_kind": "emoji",
+ "event_type": "revoke",
+ "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:03:52 UTC",
+ "id": 33657523,
+ "name": "heart",
+ "awardable_type": "Note",
+ "awardable_id": 2431339003,
+ "updated_at": "2025-04-05 08:03:52 UTC",
+ "awarded_on_url": "https://gitlab.com/kolanuvarun/sample/-/merge_requests/1#note_2431339003"
+ },
+ "note": {
+ "author_id": 26076637,
+ "change_position": null,
+ "commit_id": null,
+ "created_at": "2025-04-02 17:53:19 UTC",
+ "discussion_id": "c5291e4d171dc43ccff84e2446e0f07b9c73f8d9",
+ "id": 2431339003,
+ "line_code": null,
+ "note": "hello",
+ "noteable_id": 373930372,
+ "noteable_type": "MergeRequest",
+ "original_position": null,
+ "position": null,
+ "project_id": 68452408,
+ "resolved_at": null,
+ "resolved_by_id": null,
+ "resolved_by_push": null,
+ "st_diff": null,
+ "system": false,
+ "type": null,
+ "updated_at": "2025-04-05 08:04:38 UTC",
+ "updated_by_id": null,
+ "description": "hello",
+ "url": "https://gitlab.com/kolanuvarun/sample/-/merge_requests/1#note_2431339003"
+ },
+ "merge_request": {
+ "assignee_id": null,
+ "author_id": 26076637,
+ "created_at": "2025-04-02 17:51:11 UTC",
+ "description": "Some",
+ "draft": false,
+ "head_pipeline_id": null,
+ "id": 373930372,
+ "iid": 1,
+ "last_edited_at": null,
+ "last_edited_by_id": null,
+ "merge_commit_sha": null,
+ "merge_error": null,
+ "merge_params": {
+ "force_remove_source_branch": "1"
+ },
+ "merge_status": "can_be_merged",
+ "merge_user_id": null,
+ "merge_when_pipeline_succeeds": false,
+ "milestone_id": null,
+ "source_branch": "for-pr",
+ "source_project_id": 68452408,
+ "state_id": 1,
+ "target_branch": "master",
+ "target_project_id": 68452408,
+ "time_estimate": 0,
+ "title": "Edit README.md",
+ "updated_at": "2025-04-02 17:53:19 UTC",
+ "updated_by_id": null,
+ "prepared_at": "2025-04-02 17:51:13 UTC",
+ "assignee_ids": [],
+ "blocking_discussions_resolved": true,
+ "detailed_merge_status": "mergeable",
+ "first_contribution": true,
+ "human_time_change": null,
+ "human_time_estimate": null,
+ "human_total_time_spent": null,
+ "labels": [],
+ "last_commit": {
+ "id": "ed045981aa4105d74ad6b0cb4ac63c5b1a88b158",
+ "message": "Edit README.md",
+ "title": "Edit README.md",
+ "timestamp": "2025-04-02T17:50:49+00:00",
+ "url": "https://gitlab.com/kolanuvarun/sample/-/commit/ed045981aa4105d74ad6b0cb4ac63c5b1a88b158",
+ "author": {
+ "name": "Varun Kolanu",
+ "email": "[email protected]"
+ }
+ },
+ "reviewer_ids": [],
+ "source": {
+ "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"
+ },
+ "state": "opened",
+ "target": {
+ "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"
+ },
+ "time_change": 0,
+ "total_time_spent": 0,
+ "url": "https://gitlab.com/kolanuvarun/sample/-/merge_requests/1",
+ "work_in_progress": false,
+ "approval_rules": []
+ }
+}
diff --git a/zerver/webhooks/gitlab/tests.py b/zerver/webhooks/gitlab/tests.py
index 9321f97e9e5..552140ec49f 100644
--- a/zerver/webhooks/gitlab/tests.py
+++ b/zerver/webhooks/gitlab/tests.py
@@ -809,3 +809,11 @@ A trivial change that should probably be ignored.
expected_topic_name,
expected_message,
)
+
+ def test_emoji_revoke_in_merge_request_note(self) -> None:
+ expected_topic_name = "sample / MR #1 Edit README.md"
+ expected_message = "Varun Kolanu removed the emoji :heart: from [a comment](https://gitlab.com/kolanuvarun/sample/-/merge_requests/1#note_2431339003)."
+
+ 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.py
index 1b0b16290df..f052477a640 100644
--- a/zerver/webhooks/gitlab/view.py
+++ b/zerver/webhooks/gitlab/view.py
@@ -489,16 +489,25 @@ def get_deployment_event_body(payload: WildValue, include_title: bool) -> str:
def get_emoji_event_transformed_type(payload: WildValue, type: str) -> str:
if type == "MergeRequest":
return "MR"
+ elif type == "Note":
+ event_type = payload["note"]["noteable_type"].tame(check_string)
+ return get_emoji_event_transformed_type(payload, event_type)
return type.lower()
+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]:
url = payload["object_attributes"]["awarded_on_url"].tame(check_string)
- # Extract the last numeric ID in the URL path.
+ # Extract the last numeric ID in the URL path before any '#' fragment.
# Example:
# https://gitlab.com/abc/def/issues/123 → "123"
- match = re.search(r"/(\d+)(?:/)?$", url)
+ # https://gitlab.com/abc/def/-/merge_requests/456#note_789 → "456"
+ clean_url = url.split("#")[0]
+ match = re.search(r"/(\d+)(?:/)?$", clean_url)
assert match is not None
return url, match.group(1)
@@ -531,10 +540,12 @@ def get_emoji_event_body(action: str, payload: WildValue, include_title: bool) -
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)
+ subtype = get_emoji_event_subtype_message(awardable_type)
suffix = ""
if include_title or awardable_type == "Note":
- suffix = f" {preposition} [{transformed_type} #{id}]({url})"
+ target = "" if awardable_type == "Note" else f"{transformed_type} #{id}"
+ suffix = f" {preposition} [{subtype}{target}]({url})"
return EMOJI_MESSAGE_TEMPLATE.format(
user_name=get_issue_user_name(payload),