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.

webhooks/gitlab: Support Deployment Hook approval events.

Niloth Pc9be36b74e8f
diff --git a/zerver/webhooks/gitlab/fixtures/deployment_hook__approval.json b/zerver/webhooks/gitlab/fixtures/deployment_hook__approval.json
new file mode 100644
index 00000000000..e6a3220a06f
--- /dev/null
+++ b/zerver/webhooks/gitlab/fixtures/deployment_hook__approval.json
@@ -0,0 +1,64 @@
+{
+ "object_kind": "deployment",
+ "status": "approved",
+ "status_changed_at": "2026-08-02T10:41:35.000+00:00",
+ "deployment_id": 1503250272,
+ "deployable_id": 15664214926,
+ "deployable_url": "https://gitlab.com/pritesh-30-group/deployment-webhook-test/-/jobs/15664214926",
+ "environment": "production",
+ "environment_tier": "production",
+ "environment_slug": "production",
+ "environment_external_url": null,
+ "project": {
+ "id": 85038800,
+ "name": "deployment-webhook-test",
+ "description": null,
+ "web_url": "https://gitlab.com/pritesh-30-group/deployment-webhook-test",
+ "avatar_url": null,
+ "git_ssh_url": "[email protected]:pritesh-30-group/deployment-webhook-test.git",
+ "git_http_url": "https://gitlab.com/pritesh-30-group/deployment-webhook-test.git",
+ "namespace": "Pritesh-30-group",
+ "visibility_level": 0,
+ "path_with_namespace": "pritesh-30-group/deployment-webhook-test",
+ "default_branch": "main",
+ "ci_config_path": "",
+ "homepage": "https://gitlab.com/pritesh-30-group/deployment-webhook-test",
+ "url": "[email protected]:pritesh-30-group/deployment-webhook-test.git",
+ "ssh_url": "[email protected]:pritesh-30-group/deployment-webhook-test.git",
+ "http_url": "https://gitlab.com/pritesh-30-group/deployment-webhook-test.git"
+ },
+ "short_sha": "2166d75e",
+ "user": {
+ "id": 32548944,
+ "name": "Pritesh-30",
+ "username": "Pritesh-30",
+ "avatar_url": "https://secure.gravatar.com/avatar/ebe8e7ec7c6cc50451c04e29b9c8b0bf7c439b71fb4c19b778b64e35fa66165e?s=80&d=identicon",
+ "email": "[REDACTED]"
+ },
+ "user_url": "https://gitlab.com/Pritesh-30",
+ "commit_url": "https://gitlab.com/pritesh-30-group/deployment-webhook-test/-/commit/2166d75e44d9b13be5d7fc739fd40e7ba8a8eece",
+ "commit_title": "Edit .gitlab-ci.yml",
+ "ref": "main",
+ "approver": {
+ "id": 32548944,
+ "name": "Pritesh-30",
+ "username": "Pritesh-30",
+ "avatar_url": "https://secure.gravatar.com/avatar/ebe8e7ec7c6cc50451c04e29b9c8b0bf7c439b71fb4c19b778b64e35fa66165e?s=80&d=identicon",
+ "email": "[REDACTED]"
+ },
+ "approval": {
+ "id": 4218110,
+ "status": "approved",
+ "comment": "Approval granted",
+ "created_at": "2026-08-02T10:41:35.573Z",
+ "approval_rule": {
+ "id": 3790807,
+ "user_id": 32548944,
+ "group_id": null,
+ "access_level": null,
+ "access_level_description": "Pritesh-30",
+ "required_approvals": 1,
+ "group_inheritance_type": 0
+ }
+ }
+}
diff --git a/zerver/webhooks/gitlab/tests.py b/zerver/webhooks/gitlab/tests.py
index bdb56439f20..6c4bdc8fb08 100644
--- a/zerver/webhooks/gitlab/tests.py
+++ b/zerver/webhooks/gitlab/tests.py
@@ -752,6 +752,51 @@ A trivial change that should probably be ignored.
custom_payload=payload,
)
+ def test_deployment_approval_event_message(self) -> None:
+ deployment_url = (
+ "https://gitlab.com/pritesh-30-group/deployment-webhook-test/-/jobs/15664214926"
+ )
+ expected_topic_name = "deployment-webhook-test / production"
+
+ # Status, comment, expected message
+ test_cases = [
+ (
+ "approved",
+ "Approval granted",
+ f"Pritesh-30 approved the [deployment]({deployment_url}).\n``` quote\nApproval granted\n```",
+ ),
+ ("approved", "", f"Pritesh-30 approved the [deployment]({deployment_url})."),
+ (
+ "rejected",
+ "Not ready to ship",
+ f"Pritesh-30 rejected the [deployment]({deployment_url}).\n``` quote\nNot ready to ship\n```",
+ ),
+ ("rejected", "", f"Pritesh-30 rejected the [deployment]({deployment_url})."),
+ ]
+
+ payload = orjson.loads(self.get_body("deployment_hook__approval"))
+
+ for status, comment, expected_message in test_cases:
+ with self.subTest(status=status):
+ payload["status"] = status
+ payload["approval"]["status"] = status
+ payload["approval"]["comment"] = comment
+ self.check_webhook(
+ "deployment_hook__approval",
+ expected_topic_name,
+ expected_message,
+ custom_payload=payload,
+ )
+
+ def test_deployment_approval_event_message_silent_mention(self) -> None:
+ realm = get_realm("zulip")
+ gitlab_field = try_add_realm_default_custom_profile_field(realm, "gitlab")
+ hamlet = self.example_user("hamlet")
+ self.set_user_custom_profile_data(hamlet, [{"id": gitlab_field.id, "value": "Pritesh-30"}])
+ expected_topic_name = "deployment-webhook-test / production"
+ expected_message = f"@_**{hamlet.full_name}|{hamlet.id}** approved the [deployment](https://gitlab.com/pritesh-30-group/deployment-webhook-test/-/jobs/15664214926).\n``` quote\nApproval granted\n```"
+ self.check_webhook("deployment_hook__approval", expected_topic_name, expected_message)
+
def test_emoji_award_in_snippet(self) -> None:
expected_topic_name = "sample / snippet #4831194 Sample Snippet"
expected_message = "Varun Kolanu added the emoji :thumbsup:."
diff --git a/zerver/webhooks/gitlab/view.py b/zerver/webhooks/gitlab/view.py
index 9071a9817d1..adf48d79626 100644
--- a/zerver/webhooks/gitlab/view.py
+++ b/zerver/webhooks/gitlab/view.py
@@ -496,13 +496,34 @@ def get_resource_access_token_expiry_event_body(
)
-def get_deployment_event_body(payload: WildValue, include_title: bool, realm: Realm) -> str:
- user_text = f"[{get_user_name(payload, realm)}]({payload['user_url'].tame(check_string)})"
+def get_deployment_approval_event_body(
+ payload: WildValue, approval_status: str, deployment_text: str, realm: Realm
+) -> str:
+ approver = payload["approver"]
+ approver_text = get_user_mention(
+ realm,
+ approver["username"].tame(check_string),
+ approver["name"].tame(check_string),
+ )
+ body = f"{approver_text} {approval_status} the {deployment_text}."
+
+ if comment := payload["approval"]["comment"].tame(check_string):
+ body += CONTENT_MESSAGE_TEMPLATE.format(message=comment, fence=get_unused_fence(comment))
+
+ return body
+
+def get_deployment_event_body(payload: WildValue, include_title: bool, realm: Realm) -> str:
deployment_status = payload["status"].tame(check_string)
deployable_url = payload.get("deployable_url", "").tame(check_string)
deployment_text = f"[deployment]({deployable_url})" if deployable_url else "deployment"
+ if deployment_status in ("approved", "rejected"):
+ return get_deployment_approval_event_body(
+ payload, deployment_status, deployment_text, realm
+ )
+
+ user_text = f"[{get_user_name(payload, realm)}]({payload['user_url'].tame(check_string)})"
commit_title = payload["commit_title"].tame(check_string)
commit_url = payload["commit_url"].tame(check_string)
commit_sha = commit_url.split("/")[-1][:7]