diff --git a/zerver/webhooks/gitlab/fixtures/system_hook__merge_request_closed.json b/zerver/webhooks/gitlab/fixtures/system_hook__merge_request_closed.json new file mode 100755 index 0000000000..7cef9542ef --- /dev/null +++ b/zerver/webhooks/gitlab/fixtures/system_hook__merge_request_closed.json @@ -0,0 +1,136 @@ +{ + "object_kind": "merge_request", + "event_type": "merge_request", + "user":{ + "name":"Tomasz Kolek", + "username":"tomaszkolek0", + "avatar_url":"https://secure.gravatar.com/avatar/116a6fdbcfd00466297a39174da11fbb?s=80\u0026d=identicon", + "email": "test@test.com.au" + }, + "project":{ + "id": 241, + "name":"my-awesome-project", + "description":"", + "web_url":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "avatar_url":null, + "git_ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "git_http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git", + "namespace":"tomaszkolek0", + "visibility_level":0, + "path_with_namespace":"tomaszkolek0/my-awesome-project", + "default_branch":"master", + "ci_config_path": null, + "homepage":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git" + }, + "object_attributes":{ + "head_pipeline_id": null, + "state_id": 2, + "last_edited_at": null, + "last_edited_by_id": null, + "time_estimate": 0, + "id":756208, + "target_branch":"master", + "source_branch":"tomek", + "source_project_id":1534233, + "author_id":670043, + "assignee_id":null, + "title":"NEW MR", + "created_at":"2016-08-22 20:08:13 UTC", + "updated_at":"2016-08-22 20:14:21 UTC", + "milestone_id":null, + "state":"closed", + "merge_status":"can_be_merged", + "target_project_id":1534233, + "iid":2, + "description":"", + "position":0, + "locked_at":null, + "updated_by_id":null, + "merge_error":null, + "merge_params":{ + "force_remove_source_branch":null + }, + "merge_when_build_succeeds":false, + "merge_user_id":null, + "merge_commit_sha":null, + "deleted_at":null, + "approvals_before_merge":null, + "rebase_commit_sha":null, + "in_progress_merge_commit_sha":null, + "source":{ + "name":"my-awesome-project", + "description":"", + "web_url":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "avatar_url":null, + "git_ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "git_http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git", + "namespace":"tomaszkolek0", + "visibility_level":0, + "path_with_namespace":"tomaszkolek0/my-awesome-project", + "default_branch":"master", + "homepage":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git" + }, + "target":{ + "id": 241, + "name":"my-awesome-project", + "description":"", + "web_url":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "avatar_url":null, + "git_ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "git_http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git", + "namespace":"tomaszkolek0", + "visibility_level":0, + "path_with_namespace":"tomaszkolek0/my-awesome-project", + "default_branch":"master", + "homepage":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git" + }, + "last_commit":{ + "id":"600060393d326486c12f222ba78d569dab6b4114", + "message":"new commit\n", + "timestamp":"2016-08-22T21:31:34+02:00", + "url":"https://gitlab.com/tomaszkolek0/my-awesome-project/commit/600060393d326486c12f222ba78d569dab6b4114", + "author":{ + "name":"Tomasz Kolek", + "email":"tomasz-kolek@o2.pl" + } + }, + "total_time_spent": 0, + "human_total_time_spent": null, + "human_time_estimate": null, + "assignee_ids": [ + + ], + "work_in_progress":false, + "url":"https://gitlab.com/tomaszkolek0/my-awesome-project/merge_requests/2", + "action":"close", + "changes": { + "state_id": { + "previous": 1, + "current": 2 + }, + "updated_at": { + "previous": "2020-04-22 10:12:52 UTC", + "current": "2020-04-22 10:59:42 UTC" + }, + "total_time_spent": { + "previous": null, + "current": 0 + } + } + }, + "repository":{ + "name":"my-awesome-project", + "url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "description":"", + "homepage":"https://gitlab.com/tomaszkolek0/my-awesome-project" + } +} diff --git a/zerver/webhooks/gitlab/fixtures/system_hook__merge_request_created_with_assignee.json b/zerver/webhooks/gitlab/fixtures/system_hook__merge_request_created_with_assignee.json new file mode 100755 index 0000000000..01f6d832e4 --- /dev/null +++ b/zerver/webhooks/gitlab/fixtures/system_hook__merge_request_created_with_assignee.json @@ -0,0 +1,111 @@ +{ + "object_kind":"merge_request", + "user":{ + "name":"Tomasz Kolek", + "username":"tomaszkolek0", + "avatar_url":"https://secure.gravatar.com/avatar/116a6fdbcfd00466297a39174da11fbb?s=80\u0026d=identicon" + }, + "project":{ + "name":"my-awesome-project", + "description":"", + "web_url":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "avatar_url":null, + "git_ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "git_http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git", + "namespace":"tomaszkolek0", + "visibility_level":0, + "path_with_namespace":"tomaszkolek0/my-awesome-project", + "default_branch":"master", + "homepage":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git" + }, + "object_attributes":{ + "id":756509, + "target_branch":"master", + "source_branch":"tomek", + "source_project_id":1534233, + "author_id":670043, + "assignee_id":670043, + "title":"New Merge Request", + "created_at":"2016-08-22 20:27:22 UTC", + "updated_at":"2016-08-22 20:27:22 UTC", + "milestone_id":null, + "state":"opened", + "merge_status":"unchecked", + "target_project_id":1534233, + "iid":3, + "description":"description of merge request", + "position":0, + "locked_at":null, + "updated_by_id":null, + "merge_error":null, + "merge_params":{ + "force_remove_source_branch":null + }, + "merge_when_build_succeeds":false, + "merge_user_id":null, + "merge_commit_sha":null, + "deleted_at":null, + "approvals_before_merge":null, + "rebase_commit_sha":null, + "in_progress_merge_commit_sha":null, + "source":{ + "name":"my-awesome-project", + "description":"", + "web_url":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "avatar_url":null, + "git_ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "git_http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git", + "namespace":"tomaszkolek0", + "visibility_level":0, + "path_with_namespace":"tomaszkolek0/my-awesome-project", + "default_branch":"master", + "homepage":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git" + }, + "target":{ + "name":"my-awesome-project", + "description":"", + "web_url":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "avatar_url":null, + "git_ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "git_http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git", + "namespace":"tomaszkolek0", + "visibility_level":0, + "path_with_namespace":"tomaszkolek0/my-awesome-project", + "default_branch":"master", + "homepage":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git" + }, + "last_commit":{ + "id":"600060393d326486c12f222ba78d569dab6b4114", + "message":"new commit\n", + "timestamp":"2016-08-22T21:31:34+02:00", + "url":"https://gitlab.com/tomaszkolek0/my-awesome-project/commit/600060393d326486c12f222ba78d569dab6b4114", + "author":{ + "name":"Tomasz Kolek", + "email":"tomasz-kolek@o2.pl" + } + }, + "work_in_progress":false, + "url":"https://gitlab.com/tomaszkolek0/my-awesome-project/merge_requests/3", + "action":"open" + }, + "repository":{ + "name":"my-awesome-project", + "url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "description":"", + "homepage":"https://gitlab.com/tomaszkolek0/my-awesome-project" + }, + "assignee":{ + "name":"Tomasz Kolek", + "username":"tomaszkolek0", + "avatar_url":"https://secure.gravatar.com/avatar/116a6fdbcfd00466297a39174da11fbb?s=80\u0026d=identicon" + } +} diff --git a/zerver/webhooks/gitlab/fixtures/system_hook__merge_request_created_without_assignee.json b/zerver/webhooks/gitlab/fixtures/system_hook__merge_request_created_without_assignee.json new file mode 100755 index 0000000000..f7cd442e96 --- /dev/null +++ b/zerver/webhooks/gitlab/fixtures/system_hook__merge_request_created_without_assignee.json @@ -0,0 +1,106 @@ +{ + "object_kind":"merge_request", + "user":{ + "name":"Tomasz Kolek", + "username":"tomaszkolek0", + "avatar_url":"https://secure.gravatar.com/avatar/116a6fdbcfd00466297a39174da11fbb?s=80\u0026d=identicon" + }, + "project":{ + "name":"my-awesome-project", + "description":"", + "web_url":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "avatar_url":null, + "git_ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "git_http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git", + "namespace":"tomaszkolek0", + "visibility_level":0, + "path_with_namespace":"tomaszkolek0/my-awesome-project", + "default_branch":"master", + "homepage":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git" + }, + "object_attributes":{ + "id":756208, + "target_branch":"master", + "source_branch":"tomek", + "source_project_id":1534233, + "author_id":670043, + "assignee_id":null, + "title":"NEW MR", + "created_at":"2016-08-22 20:08:13 UTC", + "updated_at":"2016-08-22 20:08:13 UTC", + "milestone_id":null, + "state":"opened", + "merge_status":"unchecked", + "target_project_id":1534233, + "iid":2, + "description":"description of merge request", + "position":0, + "locked_at":null, + "updated_by_id":null, + "merge_error":null, + "merge_params":{ + "force_remove_source_branch":null + }, + "merge_when_build_succeeds":false, + "merge_user_id":null, + "merge_commit_sha":null, + "deleted_at":null, + "approvals_before_merge":null, + "rebase_commit_sha":null, + "in_progress_merge_commit_sha":null, + "source":{ + "name":"my-awesome-project", + "description":"", + "web_url":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "avatar_url":null, + "git_ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "git_http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git", + "namespace":"tomaszkolek0", + "visibility_level":0, + "path_with_namespace":"tomaszkolek0/my-awesome-project", + "default_branch":"master", + "homepage":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git" + }, + "target":{ + "name":"my-awesome-project", + "description":"", + "web_url":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "avatar_url":null, + "git_ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "git_http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git", + "namespace":"tomaszkolek0", + "visibility_level":0, + "path_with_namespace":"tomaszkolek0/my-awesome-project", + "default_branch":"master", + "homepage":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git" + }, + "last_commit":{ + "id":"600060393d326486c12f222ba78d569dab6b4114", + "message":"new commit\n", + "timestamp":"2016-08-22T21:31:34+02:00", + "url":"https://gitlab.com/tomaszkolek0/my-awesome-project/commit/600060393d326486c12f222ba78d569dab6b4114", + "author":{ + "name":"Tomasz Kolek", + "email":"tomasz-kolek@o2.pl" + } + }, + "work_in_progress":false, + "url":"https://gitlab.com/tomaszkolek0/my-awesome-project/merge_requests/2", + "action":"open" + }, + "repository":{ + "name":"my-awesome-project", + "url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "description":"", + "homepage":"https://gitlab.com/tomaszkolek0/my-awesome-project" + } +} diff --git a/zerver/webhooks/gitlab/fixtures/system_hook__merge_request_merged.json b/zerver/webhooks/gitlab/fixtures/system_hook__merge_request_merged.json new file mode 100755 index 0000000000..09ce2588f3 --- /dev/null +++ b/zerver/webhooks/gitlab/fixtures/system_hook__merge_request_merged.json @@ -0,0 +1,111 @@ +{ + "object_kind":"merge_request", + "user":{ + "name":"Tomasz Kolek", + "username":"tomaszkolek0", + "avatar_url":"https://secure.gravatar.com/avatar/116a6fdbcfd00466297a39174da11fbb?s=80\u0026d=identicon" + }, + "project":{ + "name":"my-awesome-project", + "description":"", + "web_url":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "avatar_url":null, + "git_ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "git_http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git", + "namespace":"tomaszkolek0", + "visibility_level":0, + "path_with_namespace":"tomaszkolek0/my-awesome-project", + "default_branch":"master", + "homepage":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git" + }, + "object_attributes":{ + "id":756509, + "target_branch":"master", + "source_branch":"tomek", + "source_project_id":1534233, + "author_id":670043, + "assignee_id":670043, + "title":"New Merge Request", + "created_at":"2016-08-22 20:27:22 UTC", + "updated_at":"2016-08-22 20:41:06 UTC", + "milestone_id":null, + "state":"merged", + "merge_status":"can_be_merged", + "target_project_id":1534233, + "iid":3, + "description":"updated desc", + "position":0, + "locked_at":null, + "updated_by_id":670043, + "merge_error":null, + "merge_params":{ + "force_remove_source_branch":null + }, + "merge_when_build_succeeds":false, + "merge_user_id":null, + "merge_commit_sha":"5bcfb805534d13224e8fa7698a2819bf0d055f29", + "deleted_at":null, + "approvals_before_merge":null, + "rebase_commit_sha":null, + "in_progress_merge_commit_sha":null, + "source":{ + "name":"my-awesome-project", + "description":"", + "web_url":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "avatar_url":null, + "git_ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "git_http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git", + "namespace":"tomaszkolek0", + "visibility_level":0, + "path_with_namespace":"tomaszkolek0/my-awesome-project", + "default_branch":"master", + "homepage":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git" + }, + "target":{ + "name":"my-awesome-project", + "description":"", + "web_url":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "avatar_url":null, + "git_ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "git_http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git", + "namespace":"tomaszkolek0", + "visibility_level":0, + "path_with_namespace":"tomaszkolek0/my-awesome-project", + "default_branch":"master", + "homepage":"https://gitlab.com/tomaszkolek0/my-awesome-project", + "url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "ssh_url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "http_url":"https://gitlab.com/tomaszkolek0/my-awesome-project.git" + }, + "last_commit":{ + "id":"f423fe3fedbc09f64ef438bedbde12caa161ee14", + "message":"new\n", + "timestamp":"2016-08-22T22:34:57+02:00", + "url":"https://gitlab.com/tomaszkolek0/my-awesome-project/commit/f423fe3fedbc09f64ef438bedbde12caa161ee14", + "author":{ + "name":"Tomasz Kolek", + "email":"tomasz-kolek@o2.pl" + } + }, + "work_in_progress":false, + "url":"https://gitlab.com/tomaszkolek0/my-awesome-project/merge_requests/3", + "action":"merge" + }, + "repository":{ + "name":"my-awesome-project", + "url":"git@gitlab.com:tomaszkolek0/my-awesome-project.git", + "description":"", + "homepage":"https://gitlab.com/tomaszkolek0/my-awesome-project" + }, + "assignee":{ + "name":"Tomasz Kolek", + "username":"tomaszkolek0", + "avatar_url":"https://secure.gravatar.com/avatar/116a6fdbcfd00466297a39174da11fbb?s=80\u0026d=identicon" + } +} diff --git a/zerver/webhooks/gitlab/fixtures/system_hook__push_hook.json b/zerver/webhooks/gitlab/fixtures/system_hook__push_hook.json new file mode 100644 index 0000000000..fe80b8ccb3 --- /dev/null +++ b/zerver/webhooks/gitlab/fixtures/system_hook__push_hook.json @@ -0,0 +1,46 @@ +{ + "object_kind": "push", + "event_name": "push", + "before": "95790bf891e76fee5e1747ab589903a6a1f80f22", + "after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", + "ref": "refs/heads/master", + "checkout_sha": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", + "message": "Hello World", + "user_id": 4, + "user_name": "John Smith", + "user_email": "john@example.com", + "user_avatar": "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80", + "project_id": 15, + "project": { + "id": 15, + "name": "gitlab", + "description": "", + "web_url": "http://test.example.com/gitlab/gitlab", + "avatar_url": "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80", + "git_ssh_url": "git@test.example.com:gitlab/gitlab.git", + "git_http_url": "http://test.example.com/gitlab/gitlab.git", + "namespace": "gitlab", + "visibility_level": 0, + "path_with_namespace": "gitlab/gitlab", + "default_branch": "master" + }, + "commits": [ + { + "id": "c5feabde2d8cd023215af4d2ceeb7a64839fc428", + "message": "Add simple search to projects in public area\n\ncommit message body", + "title": "Add simple search to projects in public area", + "timestamp": "2013-05-13T18:18:08+00:00", + "url": "https://test.example.com/gitlab/gitlab/-/commit/c5feabde2d8cd023215af4d2ceeb7a64839fc428", + "author": { + "name": "Test User", + "email": "test@example.com" + } + } + ], + "total_commits_count": 1, + "push_options": { + "ci": { + "skip": true + } + } +} diff --git a/zerver/webhooks/gitlab/tests.py b/zerver/webhooks/gitlab/tests.py index 200cfedf7a..38908c726c 100644 --- a/zerver/webhooks/gitlab/tests.py +++ b/zerver/webhooks/gitlab/tests.py @@ -546,3 +546,63 @@ class GitlabHookTests(WebhookTestCase): result = self.client_post(self.url, payload, HTTP_X_GITLAB_EVENT='Push Hook', content_type="application/json") self.assertFalse(check_send_webhook_message_mock.called) self.assert_json_success(result) + + def test_system_push_event_message(self) -> None: + expected_topic = "gitlab / master" + expected_message = "John Smith [pushed](http://test.example.com/gitlab/gitlab/compare/95790bf891e76fee5e1747ab589903a6a1f80f22...da1560886d4f094c3e6c9ef40349f7d38b5d27d7) 1 commit to branch master. Commits by Test User (1).\n\n* Add simple search to projects in public area ([c5feabd](https://test.example.com/gitlab/gitlab/-/commit/c5feabde2d8cd023215af4d2ceeb7a64839fc428))" + self.send_and_test_stream_message('system_hook__push_hook', expected_topic, expected_message) + + def test_system_merge_request_created_without_assignee_event_message(self) -> None: + expected_topic = "my-awesome-project / MR #2 NEW MR" + expected_message = "Tomasz Kolek created [MR #2](https://gitlab.com/tomaszkolek0/my-awesome-project/merge_requests/2) from `tomek` to `master`:\n\n~~~ quote\ndescription of merge request\n~~~" + + self.send_and_test_stream_message( + 'system_hook__merge_request_created_without_assignee', + expected_topic, + expected_message) + + def test_system_merge_request_created_with_custom_topic_in_url(self) -> None: + self.url = self.build_webhook_url(topic='notifications') + expected_topic = "notifications" + expected_message = "[[my-awesome-project](https://gitlab.com/tomaszkolek0/my-awesome-project)] Tomasz Kolek created [MR #2 NEW MR](https://gitlab.com/tomaszkolek0/my-awesome-project/merge_requests/2) from `tomek` to `master`:\n\n~~~ quote\ndescription of merge request\n~~~" + + self.send_and_test_stream_message( + 'system_hook__merge_request_created_without_assignee', + expected_topic, + expected_message) + + def test_system_merge_request_created_with_assignee_event_message(self) -> None: + expected_topic = "my-awesome-project / MR #3 New Merge Request" + expected_message = "Tomasz Kolek created [MR #3](https://gitlab.com/tomaszkolek0/my-awesome-project/merge_requests/3) (assigned to Tomasz Kolek) from `tomek` to `master`:\n\n~~~ quote\ndescription of merge request\n~~~" + self.send_and_test_stream_message( + 'system_hook__merge_request_created_with_assignee', + expected_topic, + expected_message) + + def test_system_merge_request_closed_event_message(self) -> None: + expected_topic = "my-awesome-project / MR #2 NEW MR" + expected_message = "Tomasz Kolek closed [MR #2](https://gitlab.com/tomaszkolek0/my-awesome-project/merge_requests/2)." + + self.send_and_test_stream_message( + 'system_hook__merge_request_closed', + expected_topic, + expected_message) + + def test_system_merge_request_merged_event_message(self) -> None: + expected_topic = "my-awesome-project / MR #3 New Merge Request" + expected_message = "Tomasz Kolek merged [MR #3](https://gitlab.com/tomaszkolek0/my-awesome-project/merge_requests/3)." + + self.send_and_test_stream_message( + 'system_hook__merge_request_merged', + expected_topic, + expected_message) + + def test_system_merge_request_closed_with_custom_topic_in_url(self) -> None: + self.url = self.build_webhook_url(topic='notifications') + expected_topic = "notifications" + expected_message = "[[my-awesome-project](https://gitlab.com/tomaszkolek0/my-awesome-project)] Tomasz Kolek closed [MR #2 NEW MR](https://gitlab.com/tomaszkolek0/my-awesome-project/merge_requests/2)." + + self.send_and_test_stream_message( + 'system_hook__merge_request_closed', + expected_topic, + expected_message) diff --git a/zerver/webhooks/gitlab/view.py b/zerver/webhooks/gitlab/view.py index 64479ed78e..e19b12ded4 100644 --- a/zerver/webhooks/gitlab/view.py +++ b/zerver/webhooks/gitlab/view.py @@ -387,12 +387,17 @@ def get_subject_based_on_event(event: str, payload: Dict[str, Any]) -> str: def get_event(request: HttpRequest, payload: Dict[str, Any], branches: Optional[str]) -> Optional[str]: event = validate_extract_webhook_http_header(request, 'X_GITLAB_EVENT', 'GitLab') + if event == "System Hook": + # Convert the event name to a Gitlab event title + event_name = payload.get('event_name', payload.get('object_kind')) + event = event_name.split("__")[0].replace("_", " ").title() + event = f"{event} Hook" if event in ['Confidential Issue Hook', 'Issue Hook', 'Merge Request Hook', 'Wiki Page Hook']: - action = payload['object_attributes'].get('action') - event = "{} {}".format(event, action) + action = payload['object_attributes'].get('action', 'open') + event = f"{event} {action}" elif event in ['Confidential Note Hook', 'Note Hook']: action = payload['object_attributes'].get('noteable_type') - event = "{} {}".format(event, action) + event = f"{event} {action}" elif event == 'Push Hook': if branches is not None: branch = get_branch_name(payload)