Repository: xanzy/go-gitlab
Pull request #1694 (2023-04-12 15:31:53)

Updated Merge comment event
Added TestMergeCommentEventUnmarshal

Also, added missing fields MergeCommentEvent.EventType, MergeCommentEvent.Project.ID, MergeCommentEvent.MergeRequest.Labels, MergeCommentEvent.MergeRequest.Asignee, MergeCommentEvent.MergeRequest.DetailedMergeStatus


Patch:
diff --git a/event_webhook_types.go b/event_webhook_types.go
index 5044444..f2b7bdb 100644
--- a/event_webhook_types.go
+++ b/event_webhook_types.go
@@ -408,9 +408,11 @@ type MemberEvent struct {
 // https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html#comment-on-a-merge-request
 type MergeCommentEvent struct {
 	ObjectKind string     `json:"object_kind"`
+	EventType  string     `json:"event_type"`
 	User       *EventUser `json:"user"`
 	ProjectID  int        `json:"project_id"`
 	Project    struct {
+		ID                int             `json:"id"`
 		Name              string          `json:"name"`
 		Description       string          `json:"description"`
 		AvatarURL         string          `json:"avatar_url"`
@@ -454,41 +456,42 @@ type MergeCommentEvent struct {
 	} `json:"object_attributes"`
 	Repository   *Repository `json:"repository"`
 	MergeRequest struct {
-		ID                        int          `json:"id"`
-		TargetBranch              string       `json:"target_branch"`
-		SourceBranch              string       `json:"source_branch"`
-		SourceProjectID           int          `json:"source_project_id"`
-		AuthorID                  int          `json:"author_id"`
-		AssigneeID                int          `json:"assignee_id"`
-		AssigneeIDs               []int        `json:"assignee_ids"`
-		Title                     string       `json:"title"`
-		CreatedAt                 string       `json:"created_at"`
-		UpdatedAt                 string       `json:"updated_at"`
-		MilestoneID               int          `json:"milestone_id"`
-		State                     string       `json:"state"`
-		MergeStatus               string       `json:"merge_status"`
-		TargetProjectID           int          `json:"target_project_id"`
-		IID                       int          `json:"iid"`
-		Description               string       `json:"description"`
-		Position                  int          `json:"position"`
-		LockedAt                  string       `json:"locked_at"`
-		UpdatedByID               int          `json:"updated_by_id"`
-		MergeError                string       `json:"merge_error"`
-		MergeParams               *MergeParams `json:"merge_params"`
-		MergeWhenPipelineSucceeds bool         `json:"merge_when_pipeline_succeeds"`
-		MergeUserID               int          `json:"merge_user_id"`
-		MergeCommitSHA            string       `json:"merge_commit_sha"`
-		DeletedAt                 string       `json:"deleted_at"`
-		InProgressMergeCommitSHA  string       `json:"in_progress_merge_commit_sha"`
-		LockVersion               int          `json:"lock_version"`
-		ApprovalsBeforeMerge      string       `json:"approvals_before_merge"`
-		RebaseCommitSHA           string       `json:"rebase_commit_sha"`
-		TimeEstimate              int          `json:"time_estimate"`
-		Squash                    bool         `json:"squash"`
-		LastEditedAt              string       `json:"last_edited_at"`
-		LastEditedByID            int          `json:"last_edited_by_id"`
-		Source                    *Repository  `json:"source"`
-		Target                    *Repository  `json:"target"`
+		ID                        int           `json:"id"`
+		TargetBranch              string        `json:"target_branch"`
+		SourceBranch              string        `json:"source_branch"`
+		SourceProjectID           int           `json:"source_project_id"`
+		AuthorID                  int           `json:"author_id"`
+		AssigneeID                int           `json:"assignee_id"`
+		AssigneeIDs               []int         `json:"assignee_ids"`
+		Title                     string        `json:"title"`
+		CreatedAt                 string        `json:"created_at"`
+		UpdatedAt                 string        `json:"updated_at"`
+		MilestoneID               int           `json:"milestone_id"`
+		State                     string        `json:"state"`
+		MergeStatus               string        `json:"merge_status"`
+		TargetProjectID           int           `json:"target_project_id"`
+		IID                       int           `json:"iid"`
+		Description               string        `json:"description"`
+		Position                  int           `json:"position"`
+		Labels                    []*EventLabel `json:"labels"`
+		LockedAt                  string        `json:"locked_at"`
+		UpdatedByID               int           `json:"updated_by_id"`
+		MergeError                string        `json:"merge_error"`
+		MergeParams               *MergeParams  `json:"merge_params"`
+		MergeWhenPipelineSucceeds bool          `json:"merge_when_pipeline_succeeds"`
+		MergeUserID               int           `json:"merge_user_id"`
+		MergeCommitSHA            string        `json:"merge_commit_sha"`
+		DeletedAt                 string        `json:"deleted_at"`
+		InProgressMergeCommitSHA  string        `json:"in_progress_merge_commit_sha"`
+		LockVersion               int           `json:"lock_version"`
+		ApprovalsBeforeMerge      string        `json:"approvals_before_merge"`
+		RebaseCommitSHA           string        `json:"rebase_commit_sha"`
+		TimeEstimate              int           `json:"time_estimate"`
+		Squash                    bool          `json:"squash"`
+		LastEditedAt              string        `json:"last_edited_at"`
+		LastEditedByID            int           `json:"last_edited_by_id"`
+		Source                    *Repository   `json:"source"`
+		Target                    *Repository   `json:"target"`
 		LastCommit                struct {
 			ID        string     `json:"id"`
 			Title     string     `json:"title"`
@@ -500,9 +503,11 @@ type MergeCommentEvent struct {
 				Email string `json:"email"`
 			} `json:"author"`
 		} `json:"last_commit"`
-		WorkInProgress bool `json:"work_in_progress"`
-		TotalTimeSpent int  `json:"total_time_spent"`
-		HeadPipelineID int  `json:"head_pipeline_id"`
+		WorkInProgress      bool       `json:"work_in_progress"`
+		TotalTimeSpent      int        `json:"total_time_spent"`
+		HeadPipelineID      int        `json:"head_pipeline_id"`
+		Assignee            *EventUser `json:"assignee"`
+		DetailedMergeStatus string     `json:"detailed_merge_status"`
 	} `json:"merge_request"`
 }
 


Test patch:
diff --git a/event_webhook_types_test.go b/event_webhook_types_test.go
index 421e3c6..2f6c1b8 100644
--- a/event_webhook_types_test.go
+++ b/event_webhook_types_test.go
@@ -337,6 +337,125 @@ func TestIssueEventUnmarshal(t *testing.T) {
 	assert.Equal(t, "2017-09-15 16:52:00 UTC", event.Changes.UpdatedAt.Current)
 }
 
+// Generate unit test for MergeCommentEvent
+func TestMergeCommentEventUnmarshal(t *testing.T) {
+	jsonObject := loadFixture("testdata/webhooks/note_merge_request.json")
+
+	var event *MergeCommentEvent
+	err := json.Unmarshal(jsonObject, &event)
+	if err != nil {
+		t.Errorf("Merge Comment Event can not unmarshaled: %v\n ", err.Error())
+	}
+
+	if event == nil {
+		t.Errorf("Merge Comment Event is null")
+	}
+
+	if event.ObjectAttributes.ID != 1244 {
+		t.Errorf("ObjectAttributes.ID is %v, want %v", event.ObjectAttributes.ID, 1244)
+	}
+
+	if event.ObjectAttributes.Note != "This MR needs work." {
+		t.Errorf("ObjectAttributes.Note is %v, want %v", event.ObjectAttributes.Note, "This MR needs work.")
+	}
+
+	if event.ObjectAttributes.NoteableType != "MergeRequest" {
+		t.Errorf("ObjectAttributes.NoteableType is %v, want %v", event.ObjectAttributes.NoteableType, "MergeRequest")
+	}
+
+	if event.ObjectAttributes.AuthorID != 1 {
+		t.Errorf("ObjectAttributes.AuthorID is %v, want %v", event.ObjectAttributes.AuthorID, 1)
+	}
+
+	if event.ObjectAttributes.CreatedAt != "2015-05-17 18:21:36 UTC" {
+		t.Errorf("ObjectAttributes.CreatedAt is %v, want %v", event.ObjectAttributes.CreatedAt, "2015-05-17 18:21:36 UTC")
+	}
+
+	if event.ObjectAttributes.UpdatedAt != "2015-05-17 18:21:36 UTC" {
+		t.Errorf("ObjectAttributes.UpdatedAt is %v, want %v", event.ObjectAttributes.UpdatedAt, "2015-05-17 18:21:36 UTC")
+	}
+
+	if event.ObjectAttributes.ProjectID != 5 {
+		t.Errorf("ObjectAttributes.ProjectID is %v, want %v", event.ObjectAttributes.ProjectID, 5)
+	}
+
+	if event.MergeRequest.ID != 7 {
+		t.Errorf("MergeRequest.ID is %v, want %v", event.MergeRequest.ID, 7)
+	}
+
+	if event.MergeRequest.TargetBranch != "markdown" {
+		t.Errorf("MergeRequest.TargetBranch is %v, want %v", event.MergeRequest.TargetBranch, "markdown")
+	}
+
+	// generate test code for rest of the event.MergeRequest fields
+	if event.MergeRequest.SourceBranch != "master" {
+		t.Errorf("MergeRequest.SourceBranch is %v, want %v", event.MergeRequest.SourceBranch, "ms-viewport")
+	}
+
+	if event.MergeRequest.SourceProjectID != 5 {
+		t.Errorf("MergeRequest.SourceProjectID is %v, want %v", event.MergeRequest.SourceProjectID, 5)
+	}
+
+	if event.MergeRequest.AuthorID != 8 {
+		t.Errorf("MergeRequest.AuthorID is %v, want %v", event.MergeRequest.AuthorID, 8)
+	}
+
+	if event.MergeRequest.AssigneeID != 28 {
+		t.Errorf("MergeRequest.AssigneeID is %v, want %v", event.MergeRequest.AssigneeID, 28)
+	}
+
+	if event.MergeRequest.State != "opened" {
+		t.Errorf("MergeRequest.state is %v, want %v", event.MergeRequest.State, "opened")
+	}
+
+	if event.MergeRequest.MergeStatus != "cannot_be_merged" {
+		t.Errorf("MergeRequest.merge_status is %v, want %v", event.MergeRequest.MergeStatus, "cannot_be_merged")
+	}
+
+	if event.MergeRequest.TargetProjectID != 5 {
+		t.Errorf("MergeRequest.target_project_id is %v, want %v", event.MergeRequest.TargetProjectID, 5)
+	}
+
+	assert.Equal(t, []*EventLabel{
+		{
+			ID:          206,
+			Title:       "Afterpod",
+			Color:       "#3e8068",
+			ProjectID:   0,
+			CreatedAt:   "2019-06-05T14:32:20.211Z",
+			UpdatedAt:   "2019-06-05T14:32:20.211Z",
+			Template:    false,
+			Description: "",
+			Type:        "GroupLabel",
+			GroupID:     4,
+		},
+		{
+			ID:          86,
+			Title:       "Element",
+			Color:       "#231afe",
+			ProjectID:   4,
+			CreatedAt:   "2019-06-05T14:32:20.637Z",
+			UpdatedAt:   "2019-06-05T14:32:20.637Z",
+			Template:    false,
+			Description: "",
+			Type:        "ProjectLabel",
+			GroupID:     0,
+		},
+	}, event.MergeRequest.Labels)
+
+	assert.Equal(t, &EventUser{
+		ID:        0,
+		Name:      "User1",
+		Username:  "user1",
+		AvatarURL: "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon",
+		Email:     "",
+	}, event.MergeRequest.Assignee)
+
+	if event.MergeRequest.DetailedMergeStatus != "checking" {
+		t.Errorf("MergeRequest.DetailedMergeStatus is %v, want %v", event.MergeRequest.DetailedMergeStatus, "checking")
+	}
+}
+
 func TestMergeEventUnmarshal(t *testing.T) {
 	jsonObject := loadFixture("testdata/webhooks/merge_request.json")
 
diff --git a/testdata/webhooks/note_merge_request.json b/testdata/webhooks/note_merge_request.json
index ab1df99..f644c92 100644
--- a/testdata/webhooks/note_merge_request.json
+++ b/testdata/webhooks/note_merge_request.json
@@ -1,11 +1,12 @@
 {
   "object_kind": "note",
+  "event_type": "note",
   "user": {
-    "id": 42,
-    "name": "User1",
-    "username": "user1",
-    "email": "user1@example.com",
-    "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
+    "id": 1,
+    "name": "Administrator",
+    "username": "root",
+    "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon",
+    "email": "admin@example.com"
   },
   "project_id": 5,
   "project": {
@@ -45,7 +46,6 @@
     "noteable_id": 7,
     "system": false,
     "st_diff": null,
-    "description": "This MR needs work.",
     "url": "http://example.com/gitlab-org/gitlab-test/merge_requests/1#note_1244"
   },
   "merge_request": {
@@ -65,6 +65,32 @@
     "iid": 1,
     "description": "Et voluptas corrupti assumenda temporibus. Architecto cum animi eveniet amet asperiores. Vitae numquam voluptate est natus sit et ad id.",
     "position": 0,
+    "labels": [
+      {
+        "id": 206,
+        "title": "Afterpod",
+        "color": "#3e8068",
+        "project_id": null,
+        "created_at": "2019-06-05T14:32:20.211Z",
+        "updated_at": "2019-06-05T14:32:20.211Z",
+        "template": false,
+        "description": null,
+        "type": "GroupLabel",
+        "group_id": 4
+      },
+      {
+        "id": 86,
+        "title": "Element",
+        "color": "#231afe",
+        "project_id": 4,
+        "created_at": "2019-06-05T14:32:20.637Z",
+        "updated_at": "2019-06-05T14:32:20.637Z",
+        "template": false,
+        "description": null,
+        "type": "ProjectLabel",
+        "group_id": null
+      }
+    ],
     "source": {
       "name": "Gitlab Test",
       "description": "Aut reprehenderit ut est.",
@@ -110,11 +136,10 @@
     },
     "work_in_progress": false,
     "assignee": {
-      "id": 16,
       "name": "User1",
       "username": "user1",
-      "email": "user1@example.com",
       "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
-    }
+    },
+    "detailed_merge_status": "checking"
   }
 }