From 01b5e6c2842f4df4271866ef6c089b9278f7c5f0 Mon Sep 17 00:00:00 2001 From: Ayush Thakur <100013900+ayusht2810@users.noreply.github.com> Date: Wed, 10 Jul 2024 16:12:01 +0530 Subject: [PATCH] [MM-340] Fix issue of receiving incorrect notification for various events in DM (#471) * [MM-340] Fix issue of receiving incorrect notification for various events in DM * [MM-340] Fix flaky tests * [MM-340] Update list of users to notify during multiple events update * [MM-340] Update function name * [MM-340] Remove additional events notification received from gitlab * [MM-340] Break comment in multiple lines * [MM-340] Update multi line comment to single line comment --- server/webhook/merge_request.go | 96 ++- server/webhook/merge_request_fixture_test.go | 610 ++++++++++++++----- server/webhook/merge_request_test.go | 54 +- server/webhook/webhook_test.go | 2 + 4 files changed, 590 insertions(+), 172 deletions(-) diff --git a/server/webhook/merge_request.go b/server/webhook/merge_request.go index 2af4284b..5b22dcc0 100644 --- a/server/webhook/merge_request.go +++ b/server/webhook/merge_request.go @@ -29,57 +29,91 @@ func (w *webhook) handleDMMergeRequest(event *gitlab.MergeEvent) ([]*HandleWebho } message := "" - + handlers := []*HandleWebhook{} switch event.ObjectAttributes.State { case stateOpened: switch event.ObjectAttributes.Action { case actionOpen: message = fmt.Sprintf("[%s](%s) requested your review on [%s!%v](%s)", senderGitlabUsername, w.gitlabRetreiver.GetUserURL(senderGitlabUsername), event.ObjectAttributes.Target.PathWithNamespace, event.ObjectAttributes.IID, event.ObjectAttributes.URL) + handlers = []*HandleWebhook{{ + Message: message, + ToUsers: toUsers, + From: senderGitlabUsername, + }} case actionReopen: message = fmt.Sprintf("[%s](%s) reopened your merge request [%s!%v](%s)", senderGitlabUsername, w.gitlabRetreiver.GetUserURL(senderGitlabUsername), event.ObjectAttributes.Target.PathWithNamespace, event.ObjectAttributes.IID, event.ObjectAttributes.URL) + handlers = []*HandleWebhook{{ + Message: message, + ToUsers: toUsers, + From: senderGitlabUsername, + }} case actionUpdate: - toUsers = []string{authorGitlabUsername} - // Not going to show notification in case of commit push. if event.ObjectAttributes.OldRev != "" { break } - for _, currentAssigneeID := range event.ObjectAttributes.AssigneeIDs { - assignedInPrevious := false - for _, previousAssignee := range event.Changes.Assignees.Previous { - if previousAssignee.ID == currentAssigneeID { - assignedInPrevious = true - break - } - } - if !assignedInPrevious { - toUsers = append(toUsers, w.gitlabRetreiver.GetUsernameByID(currentAssigneeID)) + // Handle change in assignees + if event.Changes.Assignees.Current != nil || event.Changes.Assignees.Previous != nil { + updatedCurrentUsers := w.calculateUserDiffs(event.Changes.Assignees.Previous, event.Changes.Assignees.Current) + + if len(updatedCurrentUsers) != 0 { + message = fmt.Sprintf("[%s](%s) assigned you to merge request [%s!%v](%s)", senderGitlabUsername, w.gitlabRetreiver.GetUserURL(senderGitlabUsername), event.ObjectAttributes.Target.PathWithNamespace, event.ObjectAttributes.IID, event.ObjectAttributes.URL) + handlers = append(handlers, &HandleWebhook{ + Message: message, + ToUsers: updatedCurrentUsers, + From: senderGitlabUsername, + }) } } - message = fmt.Sprintf("[%s](%s) assigned you to merge request [%s!%v](%s)", senderGitlabUsername, w.gitlabRetreiver.GetUserURL(senderGitlabUsername), event.ObjectAttributes.Target.PathWithNamespace, event.ObjectAttributes.IID, event.ObjectAttributes.URL) + // Handle change in reviewers + if event.Changes.Reviewers.Current != nil || event.Changes.Reviewers.Previous != nil { + updatedCurrentUsers := w.calculateUserDiffs(event.Changes.Reviewers.Previous, event.Changes.Reviewers.Current) + + if len(updatedCurrentUsers) != 0 { + message = fmt.Sprintf("[%s](%s) requested your review on merge request [%s!%v](%s)", senderGitlabUsername, w.gitlabRetreiver.GetUserURL(senderGitlabUsername), event.ObjectAttributes.Target.PathWithNamespace, event.ObjectAttributes.IID, event.ObjectAttributes.URL) + handlers = append(handlers, &HandleWebhook{ + Message: message, + ToUsers: updatedCurrentUsers, + From: senderGitlabUsername, + }) + } + } case actionApproved: message = fmt.Sprintf("[%s](%s) approved your merge request [%s!%v](%s)", senderGitlabUsername, w.gitlabRetreiver.GetUserURL(senderGitlabUsername), event.ObjectAttributes.Target.PathWithNamespace, event.ObjectAttributes.IID, event.ObjectAttributes.URL) + handlers = []*HandleWebhook{{ + Message: message, + ToUsers: toUsers, + From: senderGitlabUsername, + }} case actionUnapproved: message = fmt.Sprintf("[%s](%s) requested changes to your merge request [%s!%v](%s)", senderGitlabUsername, w.gitlabRetreiver.GetUserURL(senderGitlabUsername), event.ObjectAttributes.Target.PathWithNamespace, event.ObjectAttributes.IID, event.ObjectAttributes.URL) + handlers = []*HandleWebhook{{ + Message: message, + ToUsers: toUsers, + From: senderGitlabUsername, + }} } case stateClosed: message = fmt.Sprintf("[%s](%s) closed your merge request [%s!%v](%s)", senderGitlabUsername, w.gitlabRetreiver.GetUserURL(senderGitlabUsername), event.ObjectAttributes.Target.PathWithNamespace, event.ObjectAttributes.IID, event.ObjectAttributes.URL) + handlers = []*HandleWebhook{{ + Message: message, + ToUsers: toUsers, + From: senderGitlabUsername, + }} case stateMerged: if event.ObjectAttributes.Action == actionMerge { message = fmt.Sprintf("[%s](%s) merged your merge request [%s!%v](%s)", senderGitlabUsername, w.gitlabRetreiver.GetUserURL(senderGitlabUsername), event.ObjectAttributes.Target.PathWithNamespace, event.ObjectAttributes.IID, event.ObjectAttributes.URL) + handlers = []*HandleWebhook{{ + Message: message, + ToUsers: toUsers, + From: senderGitlabUsername, + }} } } - if len(message) > 0 { - handlers := []*HandleWebhook{{ - Message: message, - ToUsers: toUsers, - ToChannels: []string{}, - From: senderGitlabUsername, - }} - + if len(handlers) > 0 { if mention := w.handleMention(mentionDetails{ senderUsername: senderGitlabUsername, pathWithNamespace: event.Project.PathWithNamespace, @@ -148,3 +182,21 @@ func (w *webhook) handleChannelMergeRequest(ctx context.Context, event *gitlab.M return res, nil } + +// calculateUserDiffs function takes previousUsers and currentUsers of an event, +// finds the change in the user list, and returns the updated current user list. +func (w *webhook) calculateUserDiffs(previousUsers, currentUsers []*gitlab.EventUser) []string { + mapPreviousUsers := map[int]*gitlab.EventUser{} + updatedCurrentUsers := []string{} + for _, previousUser := range previousUsers { + mapPreviousUsers[previousUser.ID] = previousUser + } + + for _, currentUser := range currentUsers { + if _, exist := mapPreviousUsers[currentUser.ID]; !exist { + updatedCurrentUsers = append(updatedCurrentUsers, w.gitlabRetreiver.GetUsernameByID(currentUser.ID)) + } + } + + return updatedCurrentUsers +} diff --git a/server/webhook/merge_request_fixture_test.go b/server/webhook/merge_request_fixture_test.go index a92451cc..ab72f54d 100644 --- a/server/webhook/merge_request_fixture_test.go +++ b/server/webhook/merge_request_fixture_test.go @@ -548,151 +548,477 @@ const CloseMergeRequestByCreator = `{ } } ` -const AssigneeMergeRequest = `{ - "object_kind":"merge_request", - "event_type":"merge_request", - "user":{ +const RootUpdateAssigneeMergeRequest = `{ + "object_kind":"merge_request", + "event_type":"merge_request", + "user":{ + "name":"Administrator", + "username":"root", + "avatar_url":"https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\\u0026d=identicon" + }, + "project":{ + "id":24, + "name":"webhook", + "namespace":"manland", + "visibility_level":20, + "path_with_namespace":"manland/webhook", + "http_url":"http://localhost:3000/manland/webhook.git" + }, + "object_attributes":{ + "assignee_id":50, + "author_id":1, + "created_at":"2019-04-03 21:07:32 UTC", + "description":"test open merge request", + "id":35, + "iid":4, + "merge_status":"can_be_merged", + "state":"opened", + "title":"Master-2", + "url":"http://localhost:3000/manland/webhook/merge_requests/4", + "source":{ + "id":25, + "name":"webhook", + "namespace":"root", + "visibility_level":20, + "path_with_namespace":"root/webhook", + "http_url":"http://localhost:3000/root/webhook.git" + }, + "target":{ + "id":24, + "name":"webhook", + "namespace":"manland", + "visibility_level":20, + "path_with_namespace":"manland/webhook", + "http_url":"http://localhost:3000/manland/webhook.git" + }, + "last_commit":{ + "id":"1fd967c14f8265a6056525c343d984ce56472d5c", + "message":"Update README.md", + "timestamp":"2019-04-03T21:04:58Z", + "url":"http://localhost:3000/manland/webhook/commit/1fd967c14f8265a6056525c343d984ce56472d5c", + "author":{ "name":"Administrator", - "username":"root", - "avatar_url":"https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\\u0026d=identicon" - }, - "project":{ - "id":24, - "name":"webhook", - "description":"", - "web_url":"http://localhost:3000/manland/webhook", - "avatar_url":null, - "git_ssh_url":"ssh://rmaneschi@localhost:2222/manland/webhook.git", - "git_http_url":"http://localhost:3000/manland/webhook.git", - "namespace":"manland", - "visibility_level":20, - "path_with_namespace":"manland/webhook", - "default_branch":"master", - "ci_config_path":null, - "homepage":"http://localhost:3000/manland/webhook", - "url":"ssh://rmaneschi@localhost:2222/manland/webhook.git", - "ssh_url":"ssh://rmaneschi@localhost:2222/manland/webhook.git", - "http_url":"http://localhost:3000/manland/webhook.git" - }, - "object_attributes":{ - "assignee_id":50, - "author_id":1, - "created_at":"2019-04-03 21:07:32 UTC", - "description":"test open merge request", - "head_pipeline_id":57, - "id":35, - "iid":4, - "last_edited_at":null, - "last_edited_by_id":null, - "merge_commit_sha":null, - "merge_error":null, - "merge_params":{ - "force_remove_source_branch":null - }, - "merge_status":"can_be_merged", - "merge_user_id":null, - "merge_when_pipeline_succeeds":false, - "milestone_id":null, - "source_branch":"master", - "source_project_id":25, - "state":"opened", - "target_branch":"master", - "target_project_id":24, - "time_estimate":0, - "title":"Master", - "updated_at":"2019-04-04 21:03:46 UTC", - "updated_by_id":1, - "url":"http://localhost:3000/manland/webhook/merge_requests/4", - "source":{ - "id":25, - "name":"webhook", - "description":"", - "web_url":"http://localhost:3000/root/webhook", - "avatar_url":null, - "git_ssh_url":"ssh://rmaneschi@localhost:2222/root/webhook.git", - "git_http_url":"http://localhost:3000/root/webhook.git", - "namespace":"root", - "visibility_level":20, - "path_with_namespace":"root/webhook", - "default_branch":"master", - "ci_config_path":null, - "homepage":"http://localhost:3000/root/webhook", - "url":"ssh://rmaneschi@localhost:2222/root/webhook.git", - "ssh_url":"ssh://rmaneschi@localhost:2222/root/webhook.git", - "http_url":"http://localhost:3000/root/webhook.git" - }, - "target":{ - "id":24, - "name":"webhook", - "description":"", - "web_url":"http://localhost:3000/manland/webhook", - "avatar_url":null, - "git_ssh_url":"ssh://rmaneschi@localhost:2222/manland/webhook.git", - "git_http_url":"http://localhost:3000/manland/webhook.git", - "namespace":"manland", - "visibility_level":20, - "path_with_namespace":"manland/webhook", - "default_branch":"master", - "ci_config_path":null, - "homepage":"http://localhost:3000/manland/webhook", - "url":"ssh://rmaneschi@localhost:2222/manland/webhook.git", - "ssh_url":"ssh://rmaneschi@localhost:2222/manland/webhook.git", - "http_url":"http://localhost:3000/manland/webhook.git" - }, - "last_commit":{ - "id":"1fd967c14f8265a6056525c343d984ce56472d5c", - "message":"Update README.md", - "timestamp":"2019-04-03T21:04:58Z", - "url":"http://localhost:3000/manland/webhook/commit/1fd967c14f8265a6056525c343d984ce56472d5c", - "author":{ - "name":"Administrator", - "email":"admin@example.com" - } - }, - "work_in_progress":false, - "total_time_spent":0, - "human_total_time_spent":null, - "human_time_estimate":null, - "assignee_ids": [ - 50 - ], - "action":"update" - }, - "labels":[], - "changes":{ - "assignee_id":{ - "previous":1, - "current":50 - }, - "updated_at":{ - "previous":"2019-04-04 21:02:42 UTC", - "current":"2019-04-04 21:03:46 UTC" - }, - "assignee":{ - "previous":{ - "name":"Administrator", - "username":"root", - "avatar_url":"https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\\u0026d=identicon" - }, - "current":{ - "name":"manland", - "username":"manland", - "avatar_url":"https://www.gravatar.com/avatar/c6b552a4cd47f7cf1701ea5b650cd2e3?s=80\\u0026d=identicon" - } + "email":"admin@example.com" + } + }, + "assignee_ids": [50], + "action":"update" + }, + "changes":{ + "assignees":{ + "previous":[ + { + "id": 100, + "name": "user", + "username": "user", + "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\\u0026d=identicon" } - }, - "repository":{ - "name":"webhook", - "url":"ssh://rmaneschi@localhost:2222/manland/webhook.git", - "description":"", - "homepage":"http://localhost:3000/manland/webhook" - }, - "assignee":{ - "name":"manland", - "username":"manland", - "avatar_url":"https://www.gravatar.com/avatar/c6b552a4cd47f7cf1701ea5b650cd2e3?s=80\\u0026d=identicon" + ], + "current":[ + { + "id": 50, + "name": "manland", + "username": "manland", + "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\\u0026d=identicon" + } + ] + } + }, + "assignees": [ + { + "id": 50, + "name": "manland", + "username": "manland", + "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\\u0026d=identicon" + } + ], + "repository":{ + "name":"webhook", + "url":"ssh://rmaneschi@localhost:2222/manland/webhook.git", + "description":"", + "homepage":"http://localhost:3000/manland/webhook" + } + }` + +const UserUpdateAssigneeToManlandMergeRequest = `{ + "object_kind":"merge_request", + "event_type":"merge_request", + "user":{ + "name":"user", + "username":"user", + "avatar_url":"https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\\u0026d=identicon" + }, + "project":{ + "id":24, + "name":"webhook", + "namespace":"manland", + "visibility_level":20, + "path_with_namespace":"manland/webhook", + "http_url":"http://localhost:3000/manland/webhook.git" + }, + "object_attributes":{ + "assignee_id":50, + "author_id":1, + "created_at":"2019-04-03 21:07:32 UTC", + "description":"test open merge request", + "id":35, + "iid":4, + "merge_status":"can_be_merged", + "state":"opened", + "title":"Master-2", + "url":"http://localhost:3000/manland/webhook/merge_requests/4", + "source":{ + "id":25, + "name":"webhook", + "namespace":"root", + "visibility_level":20, + "path_with_namespace":"root/webhook", + "http_url":"http://localhost:3000/root/webhook.git" + }, + "target":{ + "id":24, + "name":"webhook", + "namespace":"manland", + "visibility_level":20, + "path_with_namespace":"manland/webhook", + "http_url":"http://localhost:3000/manland/webhook.git" + }, + "last_commit":{ + "id":"1fd967c14f8265a6056525c343d984ce56472d5c", + "message":"Update README.md", + "timestamp":"2019-04-03T21:04:58Z", + "url":"http://localhost:3000/manland/webhook/commit/1fd967c14f8265a6056525c343d984ce56472d5c", + "author":{ + "name":"Administrator", + "email":"admin@example.com" + } + }, + "assignee_ids": [50], + "action":"update" + }, + "changes":{ + "assignees":{ + "previous":[ + { + "id": 1, + "name": "Administrator", + "username": "root", + "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\\u0026d=identicon" + } + ], + "current":[ + { + "id": 50, + "name": "manland", + "username": "manland", + "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\\u0026d=identicon" + } + ] + } + }, + "assignees": [ + { + "id": 50, + "name": "manland", + "username": "manland", + "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\\u0026d=identicon" + } + ], + "repository":{ + "name":"webhook", + "url":"ssh://rmaneschi@localhost:2222/manland/webhook.git", + "description":"", + "homepage":"http://localhost:3000/manland/webhook" + } + }` + +const UserUpdateAssigneeToUserMergeRequest = `{ + "object_kind":"merge_request", + "event_type":"merge_request", + "user":{ + "name":"user", + "username":"user", + "avatar_url":"https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\\u0026d=identicon" + }, + "project":{ + "id":24, + "name":"webhook", + "namespace":"manland", + "visibility_level":20, + "path_with_namespace":"manland/webhook", + "http_url":"http://localhost:3000/manland/webhook.git" + }, + "object_attributes":{ + "assignee_id":100, + "author_id":1, + "created_at":"2019-04-03 21:07:32 UTC", + "description":"test open merge request", + "id":35, + "iid":4, + "merge_status":"can_be_merged", + "state":"opened", + "title":"Master-2", + "url":"http://localhost:3000/manland/webhook/merge_requests/4", + "source":{ + "id":25, + "name":"webhook", + "namespace":"root", + "visibility_level":20, + "path_with_namespace":"root/webhook", + "http_url":"http://localhost:3000/root/webhook.git" + }, + "target":{ + "id":24, + "name":"webhook", + "namespace":"manland", + "visibility_level":20, + "path_with_namespace":"manland/webhook", + "http_url":"http://localhost:3000/manland/webhook.git" + }, + "last_commit":{ + "id":"1fd967c14f8265a6056525c343d984ce56472d5c", + "message":"Update README.md", + "timestamp":"2019-04-03T21:04:58Z", + "url":"http://localhost:3000/manland/webhook/commit/1fd967c14f8265a6056525c343d984ce56472d5c", + "author":{ + "name":"Administrator", + "email":"admin@example.com" } - }` + }, + "assignee_ids": [100], + "action":"update" + }, + "changes":{ + "assignees":{ + "previous":[ + { + "id": 50, + "name": "manland", + "username": "manland", + "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\\u0026d=identicon" + } + ], + "current":[ + { + "id": 100, + "name": "user", + "username": "user", + "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\\u0026d=identicon" + } + ] + } + }, + "assignees": [ + { + "id": 100, + "name": "user", + "username": "user", + "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\\u0026d=identicon" + } + ], + "repository":{ + "name":"webhook", + "url":"ssh://rmaneschi@localhost:2222/manland/webhook.git", + "description":"", + "homepage":"http://localhost:3000/manland/webhook" + } + }` + +const RootUpdateReviewerMergeRequest = `{ + "object_kind":"merge_request", + "event_type":"merge_request", + "user":{ + "name":"Administrator", + "username":"root", + "avatar_url":"https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\\u0026d=identicon" + }, + "project":{ + "id":24, + "name":"webhook", + "namespace":"manland", + "visibility_level":20, + "path_with_namespace":"manland/webhook", + "http_url":"http://localhost:3000/manland/webhook.git" + }, + "object_attributes":{ + "assignee_id":50, + "author_id":1, + "created_at":"2019-04-03 21:07:32 UTC", + "description":"test open merge request", + "id":35, + "iid":4, + "merge_status":"can_be_merged", + "state":"opened", + "title":"Master-2", + "url":"http://localhost:3000/manland/webhook/merge_requests/4", + "source":{ + "id":25, + "name":"webhook", + "namespace":"root", + "visibility_level":20, + "path_with_namespace":"root/webhook", + "http_url":"http://localhost:3000/root/webhook.git" + }, + "target":{ + "id":24, + "name":"webhook", + "namespace":"manland", + "visibility_level":20, + "path_with_namespace":"manland/webhook", + "http_url":"http://localhost:3000/manland/webhook.git" + }, + "last_commit":{ + "id":"1fd967c14f8265a6056525c343d984ce56472d5c", + "message":"Update README.md", + "timestamp":"2019-04-03T21:04:58Z", + "url":"http://localhost:3000/manland/webhook/commit/1fd967c14f8265a6056525c343d984ce56472d5c", + "author":{ + "name":"Administrator", + "email":"admin@example.com" + } + }, + "reviewer_ids": [50], + "action":"update" + }, + "changes":{ + "reviewers":{ + "previous":[ + { + "id": 100, + "name": "user", + "username": "user", + "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\\u0026d=identicon" + } + ], + "current":[ + { + "id": 50, + "name": "manland", + "username": "manland", + "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\\u0026d=identicon" + } + ] + } + }, + "reviewers": [ + { + "id": 50, + "name": "manland", + "username": "manland", + "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\\u0026d=identicon" + } + ], + "repository":{ + "name":"webhook", + "url":"ssh://rmaneschi@localhost:2222/manland/webhook.git", + "description":"", + "homepage":"http://localhost:3000/manland/webhook" + } + }` + +const MultipleEventsMergeRequest = `{ + "object_kind":"merge_request", + "event_type":"merge_request", + "user":{ + "name":"Administrator", + "username":"root", + "avatar_url":"https://www.gravatar.com/avatar/c6b552a4cd47f7cf1701ea5b650cd2e3?s=80\\u0026d=identicon" + }, + "project":{ + "id":24, + "name":"webhook", + "namespace":"manland", + "visibility_level":20, + "path_with_namespace":"manland/webhook", + "http_url":"http://localhost:3000/manland/webhook.git" + }, + "object_attributes":{ + "assignee_id":50, + "author_id":1, + "created_at":"2019-04-03 21:07:32 UTC", + "description":"test open merge request", + "id":35, + "iid":4, + "merge_status":"can_be_merged", + "state":"opened", + "title":"Master-2", + "url":"http://localhost:3000/manland/webhook/merge_requests/4", + "source":{ + "id":25, + "name":"webhook", + "namespace":"root", + "visibility_level":20, + "path_with_namespace":"root/webhook", + "http_url":"http://localhost:3000/root/webhook.git" + }, + "target":{ + "id":24, + "name":"webhook", + "namespace":"manland", + "visibility_level":20, + "path_with_namespace":"manland/webhook", + "http_url":"http://localhost:3000/manland/webhook.git" + }, + "last_commit":{ + "id":"1fd967c14f8265a6056525c343d984ce56472d5c", + "message":"Update README.md", + "timestamp":"2019-04-03T21:04:58Z", + "url":"http://localhost:3000/manland/webhook/commit/1fd967c14f8265a6056525c343d984ce56472d5c", + "author":{ + "name":"Administrator", + "email":"admin@example.com" + } + }, + "assignee_ids": [50], + "action":"update" + }, + "labels":[ + { + "id": 1, + "title": "label-1" + } + ], + "changes":{ + "title":{ + "previous":"Master", + "current":"Master-2" + }, + "description":{ + "previous":"test open merge request", + "current":"testing open merge request" + }, + "last_edited_at":{ + "previous":"2019-04-04 21:02:42 UTC", + "current":"2019-04-04 21:03:46 UTC" + }, + "updated_at":{ + "previous":"2019-04-04 21:02:42 UTC", + "current":"2019-04-04 21:03:46 UTC" + }, + "labels":{ + "previous":[], + "current":[{ + "id": 1, + "title": "label-1" + }] + } + }, + "assignees": [ + { + "id": 50, + "name": "manland", + "username": "manland", + "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\\u0026d=identicon" + } + ], + "repository":{ + "name":"webhook", + "url":"ssh://rmaneschi@localhost:2222/manland/webhook.git", + "description":"", + "homepage":"http://localhost:3000/manland/webhook" + } + }` + const MergeRequestMerged = `{ "object_kind":"merge_request", "event_type":"merge_request", diff --git a/server/webhook/merge_request_test.go b/server/webhook/merge_request_test.go index fa5dcbd3..be367eea 100644 --- a/server/webhook/merge_request_test.go +++ b/server/webhook/merge_request_test.go @@ -89,15 +89,53 @@ var testDataMergeRequest = []testDataMergeRequestStr{ From: "manland", }}, }, { - testTitle: "root affect manland to merge-request", - fixture: AssigneeMergeRequest, + testTitle: "root assign manland to the merge-request", + fixture: RootUpdateAssigneeMergeRequest, gitlabRetreiver: newFakeWebhook([]*subscription.Subscription{}), - res: []*HandleWebhook{{ - Message: "[root](http://my.gitlab.com/root) assigned you to merge request [manland/webhook!4](http://localhost:3000/manland/webhook/merge_requests/4)", - ToUsers: []string{"manland"}, - ToChannels: []string{}, - From: "root", - }}, + res: []*HandleWebhook{ + { + Message: "[root](http://my.gitlab.com/root) assigned you to merge request [manland/webhook!4](http://localhost:3000/manland/webhook/merge_requests/4)", + ToUsers: []string{"manland"}, + ToChannels: []string{}, + From: "root", + }, + }, + }, { + testTitle: "root assign manland as reviewer to the merge-request", + fixture: RootUpdateReviewerMergeRequest, + gitlabRetreiver: newFakeWebhook([]*subscription.Subscription{}), + res: []*HandleWebhook{ + { + Message: "[root](http://my.gitlab.com/root) requested your review on merge request [manland/webhook!4](http://localhost:3000/manland/webhook/merge_requests/4)", + ToUsers: []string{"manland"}, + ToChannels: []string{}, + From: "root", + }, + }, + }, { + testTitle: "user assign manland as assignee to the merge-request", + fixture: UserUpdateAssigneeToManlandMergeRequest, + gitlabRetreiver: newFakeWebhook([]*subscription.Subscription{}), + res: []*HandleWebhook{ + { + Message: "[user](http://my.gitlab.com/user) assigned you to merge request [manland/webhook!4](http://localhost:3000/manland/webhook/merge_requests/4)", + ToUsers: []string{"manland"}, + ToChannels: []string{}, + From: "user", + }, + }, + }, { + testTitle: "user assign itself to the merge-request", + fixture: UserUpdateAssigneeToUserMergeRequest, + gitlabRetreiver: newFakeWebhook([]*subscription.Subscription{}), + res: []*HandleWebhook{ + { + Message: "[user](http://my.gitlab.com/user) assigned you to merge request [manland/webhook!4](http://localhost:3000/manland/webhook/merge_requests/4)", + ToUsers: []string{}, + ToChannels: []string{}, + From: "user", + }, + }, }, { testTitle: "manland merge root merge-request and display in channel1", fixture: MergeRequestMerged, diff --git a/server/webhook/webhook_test.go b/server/webhook/webhook_test.go index 8c7eb381..8a29f6e8 100644 --- a/server/webhook/webhook_test.go +++ b/server/webhook/webhook_test.go @@ -38,6 +38,8 @@ func (*fakeWebhook) GetUsernameByID(id int) string { return "root" case 50: return "manland" + case 100: + return "user" default: return "" }