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 non-comment emoji events in GitLab.

Pritesh Shetty90441e1ca2a2
diff --git a/zerver/webhooks/gitlab/fixtures/emoji_hook__award__merge_request.json b/zerver/webhooks/gitlab/fixtures/emoji_hook__award__merge_request.json
new file mode 100644
index 00000000000..78314b08ac5
--- /dev/null
+++ b/zerver/webhooks/gitlab/fixtures/emoji_hook__award__merge_request.json
@@ -0,0 +1,133 @@
+{
+ "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-04 01:14:53 UTC",
+ "id": 33633176,
+ "name": "thumbsup",
+ "awardable_type": "MergeRequest",
+ "awardable_id": 373930372,
+ "updated_at": "2025-04-04 01:14:53 UTC",
+ "awarded_on_url": "https://gitlab.com/kolanuvarun/sample/-/merge_requests/1"
+ },
+ "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/fixtures/emoji_hook__award__snippet.json b/zerver/webhooks/gitlab/fixtures/emoji_hook__award__snippet.json
new file mode 100644
index 00000000000..080667d2a35
--- /dev/null
+++ b/zerver/webhooks/gitlab/fixtures/emoji_hook__award__snippet.json
@@ -0,0 +1,54 @@
+{
+ "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-06 03:56:58 UTC",
+ "id": 33658637,
+ "name": "thumbsup",
+ "awardable_type": "Snippet",
+ "awardable_id": 4831194,
+ "updated_at": "2025-04-06 03:56:58 UTC",
+ "awarded_on_url": "https://gitlab.com/kolanuvarun/sample/-/snippets/4831194"
+ },
+ "project_snippet": {
+ "id": 4831194,
+ "title": "Sample Snippet",
+ "description": "Hello",
+ "content": "console.log(\"hello\");",
+ "author_id": 26076637,
+ "project_id": 68452408,
+ "created_at": "2025-04-02 17:57:11 UTC",
+ "updated_at": "2025-04-06 03:56:53 UTC",
+ "file_name": "hello.js",
+ "type": "ProjectSnippet",
+ "visibility_level": 20,
+ "url": "https://gitlab.com/kolanuvarun/sample/-/snippets/4831194"
+ }
+}
diff --git a/zerver/webhooks/gitlab/fixtures/emoji_hook__revoke__issue.json b/zerver/webhooks/gitlab/fixtures/emoji_hook__revoke__issue.json
new file mode 100644
index 00000000000..4ec519ecb92
--- /dev/null
+++ b/zerver/webhooks/gitlab/fixtures/emoji_hook__revoke__issue.json
@@ -0,0 +1,78 @@
+{
+ "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-06 03:05:26 UTC",
+ "id": 33658617,
+ "name": "thumbsdown",
+ "awardable_type": "Issue",
+ "awardable_id": 164866079,
+ "updated_at": "2025-04-06 03:05:26 UTC",
+ "awarded_on_url": "https://gitlab.com/kolanuvarun/sample/-/issues/1"
+ },
+ "work_item": {
+ "author_id": 26076637,
+ "closed_at": null,
+ "confidential": false,
+ "created_at": "2025-03-28 05:14:04 UTC",
+ "description": "",
+ "discussion_locked": null,
+ "due_date": null,
+ "id": 164866079,
+ "iid": 1,
+ "last_edited_at": null,
+ "last_edited_by_id": null,
+ "milestone_id": null,
+ "moved_to_id": null,
+ "duplicated_to_id": null,
+ "project_id": 68452408,
+ "relative_position": 513,
+ "state_id": 1,
+ "time_estimate": 0,
+ "title": "Sample Issue",
+ "updated_at": "2025-03-31 10:58:16 UTC",
+ "updated_by_id": null,
+ "weight": null,
+ "health_status": null,
+ "type": "Issue",
+ "url": "https://gitlab.com/kolanuvarun/sample/-/issues/1",
+ "total_time_spent": 0,
+ "time_change": 0,
+ "human_total_time_spent": null,
+ "human_time_change": null,
+ "human_time_estimate": null,
+ "assignee_ids": [],
+ "assignee_id": null,
+ "labels": [],
+ "state": "opened",
+ "severity": "unknown",
+ "customer_relations_contacts": []
+ }
+}
diff --git a/zerver/webhooks/gitlab/tests.py b/zerver/webhooks/gitlab/tests.py
index cc6b1009231..9321f97e9e5 100644
--- a/zerver/webhooks/gitlab/tests.py
+++ b/zerver/webhooks/gitlab/tests.py
@@ -766,3 +766,46 @@ A trivial change that should probably be ignored.
expected_message,
HTTP_X_GITLAB_EVENT="Deployment Hook",
)
+
+ def test_emoji_award_in_snippet(self) -> None:
+ expected_topic_name = "sample / snippet #4831194 Sample Snippet"
+ expected_message = "Varun Kolanu added the emoji :thumbsup:."
+
+ self.check_webhook("emoji_hook__award__snippet", expected_topic_name, expected_message)
+
+ def test_emoji_award_in_snippet_with_custom_topic(self) -> None:
+ self.url = self.build_webhook_url(topic="notifications")
+
+ expected_topic_name = "notifications"
+ expected_message = "[[sample](https://gitlab.com/kolanuvarun/sample)] Varun Kolanu added the emoji :thumbsup: to [snippet #4831194](https://gitlab.com/kolanuvarun/sample/-/snippets/4831194)."
+
+ self.check_webhook(
+ "emoji_hook__award__snippet",
+ expected_topic_name,
+ expected_message,
+ )
+
+ def test_emoji_revoke_in_issue(self) -> None:
+ expected_topic_name = "sample / issue #1 Sample Issue"
+ expected_message = "Varun Kolanu removed the emoji :thumbsdown:."
+
+ self.check_webhook("emoji_hook__revoke__issue", expected_topic_name, expected_message)
+
+ def test_emoji_award_in_merge_request_without_merge_request_title(self) -> None:
+ expected_topic_name = "sample / MR #1"
+ expected_message = "Varun Kolanu added the emoji :thumbsup:."
+
+ self.url = self.build_webhook_url(use_merge_request_title="false")
+ self.check_webhook(
+ "emoji_hook__award__merge_request", expected_topic_name, expected_message
+ )
+
+ def test_emoji_award_in_merge_request_with_custom_topic(self) -> None:
+ expected_topic_name = "notifications"
+ expected_message = "[[sample](https://gitlab.com/kolanuvarun/sample)] Varun Kolanu added the emoji :thumbsup: to [MR #1](https://gitlab.com/kolanuvarun/sample/-/merge_requests/1)."
+ self.url = self.build_webhook_url(topic="notifications")
+ self.check_webhook(
+ "emoji_hook__award__merge_request",
+ expected_topic_name,
+ expected_message,
+ )
diff --git a/zerver/webhooks/gitlab/view.py b/zerver/webhooks/gitlab/view.py
index c9574f675e7..1b0b16290df 100644
--- a/zerver/webhooks/gitlab/view.py
+++ b/zerver/webhooks/gitlab/view.py
@@ -40,6 +40,8 @@ FEATURE_FLAG_MESSAGE_TEMPLATE = "{user} {action} the feature flag [{name}]({url}
ACCESS_TOKEN_EXPIRY_MESSAGE_TEMPLATE = "The access token [{name}]({url}) will expire on {date}."
+EMOJI_MESSAGE_TEMPLATE = "{user_name} {action} the emoji :{emoji_text}:{suffix}."
+
def fixture_to_headers(fixture_name: str) -> dict[str, str]:
if fixture_name.startswith("build"):
@@ -484,6 +486,64 @@ def get_deployment_event_body(payload: WildValue, include_title: bool) -> str:
return deployment_event_body_map[deployment_status]
+def get_emoji_event_transformed_type(payload: WildValue, type: str) -> str:
+ if type == "MergeRequest":
+ return "MR"
+ return type.lower()
+
+
+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.
+ # Example:
+ # https://gitlab.com/abc/def/issues/123 → "123"
+ match = re.search(r"/(\d+)(?:/)?$", url)
+ assert match is not None
+ return url, match.group(1)
+
+
+def get_emoji_event_topic_title(
+ payload: WildValue, awardable_type: str, type: str, use_merge_request_title: bool
+) -> str:
+ """
+ The payload format depends on the event target type (MR / issue / snippet).
+ Get topic from the corresponding payload section's title.
+ """
+ event_type = ""
+ if type == "MR":
+ event_type = "merge_request" if use_merge_request_title else ""
+ elif type == "issue":
+ event_type = "issue" if awardable_type == "Note" else "work_item"
+ elif type == "snippet":
+ event_type = "project_snippet"
+
+ if event_type:
+ return payload[event_type]["title"].tame(check_string)
+
+ return ""
+
+
+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)
+ suffix = ""
+
+ if include_title or awardable_type == "Note":
+ suffix = f" {preposition} [{transformed_type} #{id}]({url})"
+
+ return EMOJI_MESSAGE_TEMPLATE.format(
+ user_name=get_issue_user_name(payload),
+ action=transformed_action,
+ emoji_text=emoji["name"].tame(check_string),
+ suffix=suffix,
+ )
+
+
def get_repo_name(payload: WildValue) -> str:
if "project" in payload:
return payload["project"]["name"].tame(check_string)
@@ -574,6 +634,8 @@ EVENT_FUNCTION_MAPPER: dict[str, EventFunction] = {
"Feature Flag Hook": get_feature_flag_event_body,
"Resource Access Token Hook": get_resource_access_token_expiry_event_body,
"Deployment Hook": get_deployment_event_body,
+ "Emoji Hook award": partial(get_emoji_event_body, "award"),
+ "Emoji Hook revoke": partial(get_emoji_event_body, "revoke"),
}
ALL_EVENT_TYPES = list(EVENT_FUNCTION_MAPPER.keys())
@@ -700,6 +762,18 @@ def get_topic_based_on_event(event: str, payload: WildValue, use_merge_request_t
get_repo_name(payload),
payload["environment"].tame(check_string),
)
+
+ elif event.startswith("Emoji Hook"):
+ awardable_type = payload["object_attributes"]["awardable_type"].tame(check_string)
+ transformed_type = get_emoji_event_transformed_type(payload, awardable_type)
+ 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],
+ title=get_emoji_event_topic_title(
+ payload, awardable_type, transformed_type, use_merge_request_title
+ ),
+ )
return get_repo_name(payload)
@@ -723,6 +797,9 @@ def get_event(request: HttpRequest, payload: WildValue, branches: str | None) ->
branch = get_branch_name(payload)
if not is_branch_name_notifiable(branch, branches):
return None
+ elif event == "Emoji Hook":
+ action = payload["event_type"].tame(check_string)
+ event = f"{event} {action}"
if event in EVENT_FUNCTION_MAPPER:
return event