Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
webhooks/gitlab: Update Job Hook fixture and tests.
Niloth Pd0206cfbd292
diff --git a/zerver/webhooks/gitlab/fixtures/job_hook.json b/zerver/webhooks/gitlab/fixtures/job_hook.jsonnew file mode 100644index 00000000000..393f6457335--- /dev/null+++ b/zerver/webhooks/gitlab/fixtures/job_hook.json@@ -0,0 +1,75 @@+{+ "object_kind": "build",+ "ref": "master",+ "tag": false,+ "before_sha": "0934f0e59866244e1bbbba3022e620e087878471",+ "sha": "0c4f4a663e7623616074d47bbea8ac8d4623ac0a",+ "retries_count": 1,+ "build_id": 12758518434,+ "build_name": "build-job",+ "build_stage": "build",+ "build_status": "created",+ "build_created_at": "2026-01-18 16:28:28 UTC",+ "build_started_at": null,+ "build_finished_at": null,+ "build_created_at_iso": "2026-01-18T16:28:28Z",+ "build_started_at_iso": null,+ "build_finished_at_iso": null,+ "build_duration": null,+ "build_queued_duration": null,+ "build_allow_failure": false,+ "build_failure_reason": "unknown_failure",+ "pipeline_id": 2257910668,+ "runner": null,+ "project_id": 77624161,+ "project_name": "HydrallHarsh / TEST-1",+ "user": {+ "id": 18515580,+ "name": "HydrallHarsh",+ "username": "HydrallHarsh",+ "avatar_url": "https://secure.gravatar.com/avatar/552c486d24108fc2455757fe13b4d81a23c6f2d652f979bbddb10a36d3b66298?s=80&d=identicon",+ "email": "[REDACTED]"+ },+ "commit": {+ "id": 2257910668,+ "name": null,+ "sha": "0c4f4a663e7623616074d47bbea8ac8d4623ac0a",+ "message": "Merge branch 'merge-branch' into 'master'\n\nUpdate 2 files\n\nSee merge request HydrallHarsh/test-1!1",+ "author_name": "HydrallHarsh",+ "author_email": "[email protected]",+ "author_url": "https://gitlab.com/HydrallHarsh",+ "status": "success",+ "duration": 150,+ "started_at": "2026-01-12 13:04:45 UTC",+ "finished_at": "2026-01-12 13:07:18 UTC",+ "started_at_iso": "2026-01-12T13:04:45Z",+ "finished_at_iso": "2026-01-12T13:07:18Z"+ },+ "repository": {+ "name": "TEST-1",+ "url": "[email protected]:HydrallHarsh/test-1.git",+ "description": "Testing- Webhook",+ "homepage": "https://gitlab.com/HydrallHarsh/test-1",+ "git_http_url": "https://gitlab.com/HydrallHarsh/test-1.git",+ "git_ssh_url": "[email protected]:HydrallHarsh/test-1.git",+ "visibility_level": 0+ },+ "project": {+ "id": 77624161,+ "name": "TEST-1",+ "description": "Testing- Webhook",+ "web_url": "https://gitlab.com/HydrallHarsh/test-1",+ "avatar_url": null,+ "git_ssh_url": "[email protected]:HydrallHarsh/test-1.git",+ "git_http_url": "https://gitlab.com/HydrallHarsh/test-1.git",+ "namespace": "HydrallHarsh",+ "visibility_level": 0,+ "path_with_namespace": "HydrallHarsh/test-1",+ "default_branch": "master",+ "ci_config_path": ""+ },+ "environment": {+ "name": "production",+ "action": "start"+ }+}diff --git a/zerver/webhooks/gitlab/fixtures/job_hook__build_created.json b/zerver/webhooks/gitlab/fixtures/job_hook__build_created.jsondeleted file mode 100644index 5f96689f741..00000000000--- a/zerver/webhooks/gitlab/fixtures/job_hook__build_created.json+++ /dev/null@@ -1,50 +0,0 @@-{- "object_kind": "build",- "ref": "gitlab-script-trigger",- "tag": false,- "before_sha": "2293ada6b400935a1378653304eaf6221e0fdb8f",- "sha": "2293ada6b400935a1378653304eaf6221e0fdb8f",- "build_id": 1977,- "build_name": "test",- "build_stage": "test",- "build_status": "created",- "build_started_at": null,- "build_finished_at": null,- "build_duration": null,- "build_allow_failure": false,- "build_failure_reason": "script_failure",- "pipeline_id": 2366,- "project_id": 380,- "project_name": "gitlab-org/gitlab-test",- "user": {- "id": 3,- "name": "User",- "email": "[email protected]",- "avatar_url": "http://www.gravatar.com/avatar/e32bd13e2add097461cb96824b7a829c?s=80\u0026d=identicon"- },- "commit": {- "id": 2366,- "sha": "2293ada6b400935a1378653304eaf6221e0fdb8f",- "message": "test\n",- "author_name": "User",- "author_email": "[email protected]",- "status": "created",- "duration": null,- "started_at": null,- "finished_at": null- },- "repository": {- "name": "gitlab_test",- "description": "Atque in sunt eos similique dolores voluptatem.",- "homepage": "http://192.168.64.1:3005/gitlab-org/gitlab-test",- "git_ssh_url": "[email protected]:gitlab-org/gitlab-test.git",- "git_http_url": "http://192.168.64.1:3005/gitlab-org/gitlab-test.git",- "visibility_level": 20- },- "runner": {- "active": true,- "is_shared": false,- "id": 380987,- "description": "shared-runners-manager-6.gitlab.com"- }-}diff --git a/zerver/webhooks/gitlab/tests.py b/zerver/webhooks/gitlab/tests.pyindex fcdef6634ed..92f350b5f15 100644--- a/zerver/webhooks/gitlab/tests.py+++ b/zerver/webhooks/gitlab/tests.py@@ -1,5 +1,7 @@from unittest.mock import MagicMock, patch+import orjson+from zerver.actions.custom_profile_fields import try_add_realm_default_custom_profile_fieldfrom zerver.lib.test_classes import WebhookTestCasefrom zerver.lib.webhooks.git import COMMITS_LIMIT@@ -591,16 +593,40 @@ A trivial change that should probably be ignored.self.assertFalse(check_send_webhook_message_mock.called)self.assert_json_success(result)- def test_job_hook_event(self) -> None:- expected_topic_name = "gitlab_test / gitlab-script-trigger"- expected_message = "Build test from test stage was created."- self.check_webhook("job_hook__build_created", expected_topic_name, expected_message)-- def test_job_hook_event_topic(self) -> None:+ def test_job_event_message_by_build_status(self) -> None:+ expected_topic_name = "TEST-1 / master"+ test_cases = [+ ("created", "Build build-job from build stage was created."),+ ("running", "Build build-job from build stage started."),+ ("success", "Build build-job from build stage changed status to success."),+ ("failed", "Build build-job from build stage changed status to failed."),+ ]++ payload = orjson.loads(self.get_body("job_hook"))++ for build_status, expected_message in test_cases:+ with self.subTest(build_status=build_status):+ payload["build_status"] = build_status+ self.check_webhook(+ "job_hook",+ expected_topic_name,+ expected_message,+ custom_payload=payload,+ )++ def test_job_event_message_without_project_field_with_custom_topic(self) -> None:self.url = self.build_webhook_url(topic="provided topic")expected_topic_name = "provided topic"- expected_message = "[[gitlab_test](http://192.168.64.1:3005/gitlab-org/gitlab-test)] Build test from test stage was created."- self.check_webhook("job_hook__build_created", expected_topic_name, expected_message)+ expected_message = "[[TEST-1](https://gitlab.com/HydrallHarsh/test-1)] Build build-job from build stage was created."++ # The project link falls back to the `repository` section for+ # payloads that have no `project` section.+ payload = orjson.loads(self.get_body("job_hook"))+ del payload["project"]++ self.check_webhook(+ "job_hook", expected_topic_name, expected_message, custom_payload=payload+ )def test_system_push_event_message(self) -> None:expected_topic_name = "gitlab / master"diff --git a/zerver/webhooks/gitlab/view.py b/zerver/webhooks/gitlab/view.pyindex fd29d547844..994ab4a7f75 100644--- a/zerver/webhooks/gitlab/view.py+++ b/zerver/webhooks/gitlab/view.py@@ -607,9 +607,8 @@ def get_repo_name(payload: WildValue) -> str:return payload["project"]["name"].tame(check_string)if "repository" in payload:- # Job Hook payloads don't have a `project` section,- # but the repository name is accessible from the `repository`- # section.+ # Older Job Hook payloads had no `project` section, and GitLab docs+ # don't promise one, so this fallback has been retained.return payload["repository"]["name"].tame(check_string)# Group-level events (e.g. group access token expiry) have neither