Repository: xanzy/go-gitlab
Pull request #1671 (2023-03-14 08:50:07)

fix: update pipeline & merge request webhook event types
Updated Pipeline Event type to include source pipeline details. Also added some fields to the Merge Event type that were not available.


Patch:
diff --git a/event_webhook_types.go b/event_webhook_types.go
index 8d68279..ad5909b 100644
--- a/event_webhook_types.go
+++ b/event_webhook_types.go
@@ -572,12 +572,15 @@ type MergeEvent struct {
 				Email string `json:"email"`
 			} `json:"author"`
 		} `json:"last_commit"`
-		BlockingDiscussionsResolved bool       `json:"blocking_discussions_resolved"`
-		WorkInProgress              bool       `json:"work_in_progress"`
-		URL                         string     `json:"url"`
-		Action                      string     `json:"action"`
-		OldRev                      string     `json:"oldrev"`
-		Assignee                    *EventUser `json:"assignee"`
+		BlockingDiscussionsResolved bool          `json:"blocking_discussions_resolved"`
+		WorkInProgress              bool          `json:"work_in_progress"`
+		FirstContribution           bool          `json:"first_contribution"`
+		URL                         string        `json:"url"`
+		Labels                      []*EventLabel `json:"labels"`
+		Action                      string        `json:"action"`
+		DetailedMergeStatus         string        `json:"detailed_merge_status"`
+		OldRev                      string        `json:"oldrev"`
+		Assignee                    *EventUser    `json:"assignee"`
 	} `json:"object_attributes"`
 	Repository *Repository   `json:"repository"`
 	Assignee   *EventUser    `json:"assignee"`
@@ -754,6 +757,15 @@ type PipelineEvent struct {
 			Email string `json:"email"`
 		} `json:"author"`
 	} `json:"commit"`
+	SourcePipline struct {
+		Project struct {
+			ID                int    `json:"id"`
+			WebURL            string `json:"web_url"`
+			PathWithNamespace string `json:"path_with_namespace"`
+		} `json:"project"`
+		PipelineID int `json:"pipeline_id"`
+		JobID      int `json:"job_id"`
+	} `json:"source_pipeline"`
 	Builds []struct {
 		ID             int        `json:"id"`
 		Stage          string     `json:"stage"`


Test patch:
diff --git a/event_webhook_types_test.go b/event_webhook_types_test.go
index 6f13596..6201515 100644
--- a/event_webhook_types_test.go
+++ b/event_webhook_types_test.go
@@ -376,6 +376,10 @@ func TestMergeEventUnmarshal(t *testing.T) {
 		t.Errorf("BlockingDiscussionsResolved isn't true")
 	}
 
+	if event.ObjectAttributes.FirstContribution != true {
+		t.Errorf("FirstContribution isn't true")
+	}
+
 	if event.Assignees[0].ID != expectedID {
 		t.Errorf("Assignees[0].ID is %v, want %v", event.Assignees[0].ID, expectedID)
 	}
@@ -408,6 +412,10 @@ func TestMergeEventUnmarshal(t *testing.T) {
 		t.Errorf("Reviewers[0].AvatarURL is %v, want %v", event.Reviewers[0].AvatarURL, excpectedAvatar)
 	}
 
+	if event.ObjectAttributes.DetailedMergeStatus != "mergeable" {
+		t.Errorf("DetailedMergeStatus is %s, want %s", event.ObjectAttributes.DetailedMergeStatus, "mergeable")
+	}
+
 	assert.Equal(t, []*EventLabel{
 		{
 			ID:          206,
@@ -423,6 +431,21 @@ func TestMergeEventUnmarshal(t *testing.T) {
 		},
 	}, event.Labels)
 
+	assert.Equal(t, []*EventLabel{
+		{
+			ID:          206,
+			Title:       "API",
+			Color:       "#ffffff",
+			ProjectID:   14,
+			CreatedAt:   "2013-12-03T17:15:43Z",
+			UpdatedAt:   "2013-12-03T17:15:43Z",
+			Template:    false,
+			Description: "API related issues",
+			Type:        "ProjectLabel",
+			GroupID:     41,
+		},
+	}, event.ObjectAttributes.Labels)
+
 	assert.Equal(t, []*EventLabel{
 		{
 			ID:          206,
@@ -659,6 +682,18 @@ func TestPipelineEventUnmarshal(t *testing.T) {
 	if event.Builds[1].FailureReason != "" {
 		t.Errorf("Builds[0].Failurereason is %v, want %v", event.Builds[0].FailureReason, "''")
 	}
+
+	if event.SourcePipline.PipelineID != 30 {
+		t.Errorf("Source Pipline ID is %v, want %v", event.SourcePipline.PipelineID, 30)
+	}
+
+	if event.SourcePipline.JobID != 3401 {
+		t.Errorf("Source Pipline JobID is %v, want %v", event.SourcePipline.JobID, 3401)
+	}
+
+	if event.SourcePipline.Project.ID != 41 {
+		t.Errorf("Source Pipline Project ID is %v, want %v", event.SourcePipline.Project.ID, 41)
+	}
 }
 
 func TestPushEventUnmarshal(t *testing.T) {
diff --git a/testdata/webhooks/merge_request.json b/testdata/webhooks/merge_request.json
index 803c2d0..cffeac5 100644
--- a/testdata/webhooks/merge_request.json
+++ b/testdata/webhooks/merge_request.json
@@ -107,7 +107,8 @@
       "type": "ProjectLabel",
       "group_id": 41
     }],
-    "action": "open"
+    "action": "open",
+    "detailed_merge_status": "mergeable"
   },
   "labels": [{
     "id": 206,
diff --git a/testdata/webhooks/pipeline.json b/testdata/webhooks/pipeline.json
index 38bd38c..55fe118 100644
--- a/testdata/webhooks/pipeline.json
+++ b/testdata/webhooks/pipeline.json
@@ -67,6 +67,15 @@
       "email": "user@gitlab.com"
     }
   },
+  "source_pipeline":{
+    "project":{
+      "id": 41,
+      "web_url": "https://gitlab.example.com/gitlab-org/upstream-project",
+      "path_with_namespace": "gitlab-org/upstream-project"
+    },
+    "pipeline_id": 30,
+    "job_id": 3401
+ },
   "builds": [
     {
       "id": 380,