Repository: xanzy/go-gitlab
Pull request #1616 (2023-01-03 15:53:03)

Add failure_reason to PipelineEvent Builds
Adds the missing `failure_reason` field to the Builds object in a PipelineEvent: https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html#pipeline-events


Patch:
diff --git a/event_webhook_types.go b/event_webhook_types.go
index 9b6dfd4..f6f86d4 100644
--- a/event_webhook_types.go
+++ b/event_webhook_types.go
@@ -760,6 +760,7 @@ type PipelineEvent struct {
 		FinishedAt     string     `json:"finished_at"`
 		Duration       float64    `json:"duration"`
 		QueuedDuration float64    `json:"queued_duration"`
+		FailureReason  string     `json:"failure_reason"`
 		When           string     `json:"when"`
 		Manual         bool       `json:"manual"`
 		AllowFailure   bool       `json:"allow_failure"`


Test patch:
diff --git a/event_webhook_types_test.go b/event_webhook_types_test.go
index bbea4a9..584ebaf 100644
--- a/event_webhook_types_test.go
+++ b/event_webhook_types_test.go
@@ -39,7 +39,6 @@ func TestBuildEventUnmarshal(t *testing.T) {
 
 	var event *BuildEvent
 	err := json.Unmarshal(jsonObject, &event)
-
 	if err != nil {
 		t.Errorf("Build Event can not unmarshaled: %v\n ", err.Error())
 	}
@@ -70,7 +69,6 @@ func TestDeploymentEventUnmarshal(t *testing.T) {
 
 	var event *DeploymentEvent
 	err := json.Unmarshal(jsonObject, &event)
-
 	if err != nil {
 		t.Errorf("Deployment Event can not unmarshaled: %v\n ", err.Error())
 	}
@@ -105,7 +103,6 @@ func TestIssueCommentEventUnmarshal(t *testing.T) {
 
 	var event *IssueCommentEvent
 	err := json.Unmarshal(jsonObject, &event)
-
 	if err != nil {
 		t.Errorf("Issue Comment Event can not unmarshaled: %v\n ", err.Error())
 	}
@@ -167,7 +164,6 @@ func TestIssueEventUnmarshal(t *testing.T) {
 
 	var event *IssueEvent
 	err := json.Unmarshal(jsonObject, &event)
-
 	if err != nil {
 		t.Errorf("Issue Event can not unmarshaled: %v\n ", err.Error())
 	}
@@ -252,7 +248,6 @@ func TestMergeEventUnmarshal(t *testing.T) {
 
 	var event *MergeEvent
 	err := json.Unmarshal(jsonObject, &event)
-
 	if err != nil {
 		t.Errorf("Merge Event can not unmarshaled: %v\n ", err.Error())
 	}
@@ -393,7 +388,6 @@ func TestMemberEventUnmarshal(t *testing.T) {
 
 	var event *MemberEvent
 	err := json.Unmarshal(jsonObject, &event)
-
 	if err != nil {
 		t.Errorf("Member Event can not unmarshaled: %v\n ", err.Error())
 	}
@@ -463,7 +457,6 @@ func TestMergeEventUnmarshalFromGroup(t *testing.T) {
 
 	var event *MergeEvent
 	err := json.Unmarshal(jsonObject, &event)
-
 	if err != nil {
 		t.Errorf("Group Merge Event can not unmarshaled: %v\n ", err.Error())
 	}
@@ -522,7 +515,6 @@ func TestPipelineEventUnmarshal(t *testing.T) {
 
 	var event *PipelineEvent
 	err := json.Unmarshal(jsonObject, &event)
-
 	if err != nil {
 		t.Errorf("Pipeline Event can not unmarshaled: %v\n ", err.Error())
 	}
@@ -582,13 +574,20 @@ func TestPipelineEventUnmarshal(t *testing.T) {
 	if event.Builds[0].QueuedDuration != 3.5 {
 		t.Errorf("Builds[0].QueuedDuration is %v, want %v", event.Builds[0].QueuedDuration, 3.5)
 	}
+
+	if event.Builds[0].FailureReason != "script_failure" {
+		t.Errorf("Builds[0].Failurereason is %v, want %v", event.Builds[0].FailureReason, "script_failure")
+	}
+
+	if event.Builds[1].FailureReason != "" {
+		t.Errorf("Builds[0].Failurereason is %v, want %v", event.Builds[0].FailureReason, "''")
+	}
 }
 
 func TestPushEventUnmarshal(t *testing.T) {
 	jsonObject := loadFixture("testdata/webhooks/push.json")
 	var event *PushEvent
 	err := json.Unmarshal(jsonObject, &event)
-
 	if err != nil {
 		t.Errorf("Push Event can not unmarshaled: %v\n ", err.Error())
 	}
@@ -627,7 +626,6 @@ func TestReleaseEventUnmarshal(t *testing.T) {
 
 	var event *ReleaseEvent
 	err := json.Unmarshal(jsonObject, &event)
-
 	if err != nil {
 		t.Errorf("Release Event can not unmarshaled: %v\n ", err.Error())
 	}
@@ -670,7 +668,6 @@ func TestSubGroupEventUnmarshal(t *testing.T) {
 
 	var event *SubGroupEvent
 	err := json.Unmarshal(jsonObject, &event)
-
 	if err != nil {
 		t.Errorf("SubGroup Event can not unmarshaled: %v\n ", err.Error())
 	}
@@ -700,7 +697,6 @@ func TestTagEventUnmarshal(t *testing.T) {
 	jsonObject := loadFixture("testdata/webhooks/tag_push.json")
 	var event *TagEvent
 	err := json.Unmarshal(jsonObject, &event)
-
 	if err != nil {
 		t.Errorf("Tag Event can not unmarshaled: %v\n ", err.Error())
 	}
diff --git a/testdata/webhooks/pipeline.json b/testdata/webhooks/pipeline.json
index 6f1ba2e..38bd38c 100644
--- a/testdata/webhooks/pipeline.json
+++ b/testdata/webhooks/pipeline.json
@@ -9,7 +9,11 @@
     "source": "merge_request_event",
     "status": "success",
     "detailed_status": "passed",
-    "stages": ["build", "test", "deploy"],
+    "stages": [
+      "build",
+      "test",
+      "deploy"
+    ],
     "created_at": "2016-08-12 15:23:28 UTC",
     "finished_at": "2016-08-12 15:26:29 UTC",
     "duration": 63,
@@ -77,6 +81,7 @@
       "when": "manual",
       "manual": true,
       "allow_failure": true,
+      "failure_reason": "script_failure",
       "user": {
         "id": 42,
         "name": "User1",
@@ -90,7 +95,10 @@
         "runner_type": "instance_type",
         "active": true,
         "is_shared": true,
-        "tags": ["docker", "gce"]
+        "tags": [
+          "docker",
+          "gce"
+        ]
       },
       "artifacts_file": {
         "filename": null,
@@ -223,3 +231,4 @@
     }
   ]
 }
+