Repository: xanzy/go-gitlab
Pull request #966 (2020-10-31 12:02:28)

Added support for group/project webhooks deployment events


Patch:
diff --git a/event_parsing.go b/event_parsing.go
index a09b356..8afbd82 100644
--- a/event_parsing.go
+++ b/event_parsing.go
@@ -23,6 +23,7 @@ const (
 	EventTypeSystemHook    EventType = "System Hook"
 	EventTypeTagPush       EventType = "Tag Push Hook"
 	EventTypeWikiPage      EventType = "Wiki Page Hook"
+	EventTypeDeployment    EventType = "Deployment Hook"
 )
 
 const (
@@ -197,6 +198,8 @@ func ParseWebhook(eventType EventType, payload []byte) (event interface{}, err e
 		event = &TagEvent{}
 	case EventTypeWikiPage:
 		event = &WikiPageEvent{}
+	case EventTypeDeployment:
+		event = &DeploymentEvent{}
 	case EventTypeNote, EventConfidentialNote:
 		note := &noteEvent{}
 		err := json.Unmarshal(payload, note)
diff --git a/event_webhook_types.go b/event_webhook_types.go
index d41379a..b847ade 100644
--- a/event_webhook_types.go
+++ b/event_webhook_types.go
@@ -832,3 +832,43 @@ type BuildEvent struct {
 	} `json:"commit"`
 	Repository *Repository `json:"repository"`
 }
+
+// DeploymentEvent represents a deployment event
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ce/user/project/integrations/webhooks.html#deployment-events
+type DeploymentEvent struct {
+	ObjectKind    string `json:"object_kind"`
+	Status        string `json:"status"`
+	DeployableID  int    `json:"deployable_id"`
+	DeployableURL string `json:"deployable_url"`
+	Environment   string `json:"environment"`
+	Project       struct {
+		ID                int     `json:"id"`
+		Name              string  `json:"name"`
+		Description       string  `json:"description"`
+		WebURL            string  `json:"web_url"`
+		AvatarURL         *string `json:"avatar_url"`
+		GitSSHURL         string  `json:"git_ssh_url"`
+		GitHTTPURL        string  `json:"git_http_url"`
+		Namespace         string  `json:"namespace"`
+		VisibilityLevel   int     `json:"visibility_level"`
+		PathWithNamespace string  `json:"path_with_namespace"`
+		DefaultBranch     string  `json:"default_branch"`
+		CIConfigPath      string  `json:"ci_config_path"`
+		Homepage          string  `json:"homepage"`
+		URL               string  `json:"url"`
+		SSHURL            string  `json:"ssh_url"`
+		HTTPURL           string  `json:"http_url"`
+	} `json:"project"`
+	ShortSHA string `json:"short_sha"`
+	User     struct {
+		Name      string `json:"name"`
+		Username  string `json:"username"`
+		AvatarURL string `json:"avatar_url"`
+		Email     string `json:"email"`
+	} `json:"user"`
+	UserURL     string `json:"user_url"`
+	CommitURL   string `json:"commit_url"`
+	CommitTitle string `json:"commit_title"`
+}
diff --git a/group_hooks.go b/group_hooks.go
index 704232b..fa2ac4f 100644
--- a/group_hooks.go
+++ b/group_hooks.go
@@ -38,6 +38,7 @@ type GroupHook struct {
 	JobEvents                bool       `json:"job_events"`
 	PipelineEvents           bool       `json:"pipeline_events"`
 	WikiPageEvents           bool       `json:"wiki_page_events"`
+	DeploymentEvents         bool       `json:"deployment_events"`
 	EnableSSLVerification    bool       `json:"enable_ssl_verification"`
 	CreatedAt                *time.Time `json:"created_at"`
 }
@@ -105,6 +106,7 @@ type AddGroupHookOptions struct {
 	JobEvents                *bool   `url:"job_events,omitempty"  json:"job_events,omitempty"`
 	PipelineEvents           *bool   `url:"pipeline_events,omitempty"  json:"pipeline_events,omitempty"`
 	WikiPageEvents           *bool   `url:"wiki_page_events,omitempty"  json:"wiki_page_events,omitempty"`
+	DeploymentEvents         *bool   `url:"deployment_events,omitempty" json:"deployment_events,omitempty"`
 	EnableSSLVerification    *bool   `url:"enable_ssl_verification,omitempty"  json:"enable_ssl_verification,omitempty"`
 	Token                    *string `url:"token,omitempty" json:"token,omitempty"`
 }
@@ -149,6 +151,7 @@ type EditGroupHookOptions struct {
 	JobEvents                *bool   `url:"job_events,omitempty" json:"job_events,omitempty"`
 	PipelineEvents           *bool   `url:"pipeline_events,omitempty" json:"pipeline_events,omitempty"`
 	WikiPageEvents           *bool   `url:"wiki_page_events,omitempty" json:"wiki_page_events,omitempty"`
+	DeploymentEvents         *bool   `url:"deployment_events,omitempty" json:"deployment_events,omitempty"`
 	EnableSSLVerification    *bool   `url:"enable_ssl_verification,omitempty" json:"enable_ssl_verification,omitempty"`
 	Token                    *string `url:"token,omitempty" json:"token,omitempty"`
 }
diff --git a/projects.go b/projects.go
index 5d5ca8c..311cdaf 100644
--- a/projects.go
+++ b/projects.go
@@ -886,6 +886,7 @@ type ProjectHook struct {
 	JobEvents                bool       `json:"job_events"`
 	PipelineEvents           bool       `json:"pipeline_events"`
 	WikiPageEvents           bool       `json:"wiki_page_events"`
+	DeploymentEvents         bool       `json:"deployment_events"`
 	EnableSSLVerification    bool       `json:"enable_ssl_verification"`
 	CreatedAt                *time.Time `json:"created_at"`
 }
@@ -962,6 +963,7 @@ type AddProjectHookOptions struct {
 	JobEvents                *bool   `url:"job_events,omitempty" json:"job_events,omitempty"`
 	PipelineEvents           *bool   `url:"pipeline_events,omitempty" json:"pipeline_events,omitempty"`
 	WikiPageEvents           *bool   `url:"wiki_page_events,omitempty" json:"wiki_page_events,omitempty"`
+	DeploymentEvents         *bool   `url:"deployment_events,omitempty" json:"deployment_events,omitempty"`
 	EnableSSLVerification    *bool   `url:"enable_ssl_verification,omitempty" json:"enable_ssl_verification,omitempty"`
 	Token                    *string `url:"token,omitempty" json:"token,omitempty"`
 }
@@ -1008,6 +1010,7 @@ type EditProjectHookOptions struct {
 	JobEvents                *bool   `url:"job_events,omitempty" json:"job_events,omitempty"`
 	PipelineEvents           *bool   `url:"pipeline_events,omitempty" json:"pipeline_events,omitempty"`
 	WikiPageEvents           *bool   `url:"wiki_page_events,omitempty" json:"wiki_page_events,omitempty"`
+	DeploymentEvents         *bool   `url:"deployment_events,omitempty" json:"deployment_events,omitempty"`
 	EnableSSLVerification    *bool   `url:"enable_ssl_verification,omitempty" json:"enable_ssl_verification,omitempty"`
 	Token                    *string `url:"token,omitempty" json:"token,omitempty"`
 }
diff --git a/services.go b/services.go
index 0f36c4e..a7b5f75 100644
--- a/services.go
+++ b/services.go
@@ -52,6 +52,7 @@ type Service struct {
 	PipelineEvents           bool       `json:"pipeline_events"`
 	JobEvents                bool       `json:"job_events"`
 	WikiPageEvents           bool       `json:"wiki_page_events"`
+	DeploymentEvents         bool       `json:"deployment_events"`
 }
 
 // ListServices gets a list of all active services.


Test patch:
diff --git a/event_webhook_types_test.go b/event_webhook_types_test.go
index 3973dd5..d1d7ae4 100644
--- a/event_webhook_types_test.go
+++ b/event_webhook_types_test.go
@@ -189,3 +189,30 @@ func TestMergeEventUnmarshalFromGroup(t *testing.T) {
 		t.Errorf("Commit Username is %s, want %s", name, "Test User")
 	}
 }
+
+func TestDeploymentEventUnmarshal(t *testing.T) {
+	jsonObject := loadFixture("testdata/webhooks/deployment.json")
+
+	var event *DeploymentEvent
+	err := json.Unmarshal(jsonObject, &event)
+
+	if err != nil {
+		t.Errorf("Deployment Event can not unmarshaled: %v\n ", err.Error())
+	}
+
+	if event == nil {
+		t.Errorf("Deployment Event is null")
+	}
+
+	if event.Project.ID != 30 {
+		t.Errorf("Project.ID is %v, want %v", event.Project.ID, 30)
+	}
+
+	if event.User.Name == "" {
+		t.Errorf("Username is %s, want %s", event.User.Name, "Administrator")
+	}
+
+	if event.CommitTitle != "Add new file" {
+		t.Errorf("CommitTitle is %s, want %s", event.CommitTitle, "Add new file")
+	}
+}
diff --git a/group_hooks_test.go b/group_hooks_test.go
index 131883c..839b4cf 100644
--- a/group_hooks_test.go
+++ b/group_hooks_test.go
@@ -45,6 +45,7 @@ func TestListGroupHooks(t *testing.T) {
 		"job_events": true,
 		"pipeline_events": true,
 		"wiki_page_events": true,
+		"deployment_events": true,
 		"enable_ssl_verification": true,
 		"created_at": "2012-10-12T17:04:47Z"
 	}
@@ -70,6 +71,7 @@ func TestListGroupHooks(t *testing.T) {
 		JobEvents:                true,
 		PipelineEvents:           true,
 		WikiPageEvents:           true,
+		DeploymentEvents:         true,
 		EnableSSLVerification:    true,
 		CreatedAt:                &datePointer,
 	}}
@@ -99,6 +101,7 @@ func TestGetGroupHook(t *testing.T) {
 	"job_events": true,
 	"pipeline_events": true,
 	"wiki_page_events": true,
+	"deployment_events": true,
 	"enable_ssl_verification": true,
 	"created_at": "2012-10-12T17:04:47Z"
 }`)
@@ -123,6 +126,7 @@ func TestGetGroupHook(t *testing.T) {
 		JobEvents:                true,
 		PipelineEvents:           true,
 		WikiPageEvents:           true,
+		DeploymentEvents:         true,
 		EnableSSLVerification:    true,
 		CreatedAt:                &datePointer,
 	}
@@ -152,6 +156,7 @@ func TestAddGroupHook(t *testing.T) {
 	"job_events": true,
 	"pipeline_events": true,
 	"wiki_page_events": true,
+	"deployment_events": true,
 	"enable_ssl_verification": true,
 	"created_at": "2012-10-12T17:04:47Z"
 }`)
@@ -182,6 +187,7 @@ func TestAddGroupHook(t *testing.T) {
 		JobEvents:                true,
 		PipelineEvents:           true,
 		WikiPageEvents:           true,
+		DeploymentEvents:         true,
 		EnableSSLVerification:    true,
 		CreatedAt:                &datePointer,
 	}
@@ -211,6 +217,7 @@ func TestEditGroupHook(t *testing.T) {
 	"job_events": true,
 	"pipeline_events": true,
 	"wiki_page_events": true,
+	"deployment_events": true,
 	"enable_ssl_verification": true,
 	"created_at": "2012-10-12T17:04:47Z"
 }`)
@@ -241,6 +248,7 @@ func TestEditGroupHook(t *testing.T) {
 		JobEvents:                true,
 		PipelineEvents:           true,
 		WikiPageEvents:           true,
+		DeploymentEvents:         true,
 		EnableSSLVerification:    true,
 		CreatedAt:                &datePointer,
 	}
diff --git a/testdata/webhooks/deployment.json b/testdata/webhooks/deployment.json
new file mode 100644
index 0000000..1785bed
--- /dev/null
+++ b/testdata/webhooks/deployment.json
@@ -0,0 +1,35 @@
+{
+  "object_kind": "deployment",
+  "status": "success",
+  "deployable_id": 796,
+  "deployable_url": "http://10.126.0.2:3000/root/test-deployment-webhooks/-/jobs/796",
+  "environment": "staging",
+  "project": {
+    "id": 30,
+    "name": "test-deployment-webhooks",
+    "description": "",
+    "web_url": "http://10.126.0.2:3000/root/test-deployment-webhooks",
+    "avatar_url": null,
+    "git_ssh_url": "ssh://vlad@10.126.0.2:2222/root/test-deployment-webhooks.git",
+    "git_http_url": "http://10.126.0.2:3000/root/test-deployment-webhooks.git",
+    "namespace": "Administrator",
+    "visibility_level": 0,
+    "path_with_namespace": "root/test-deployment-webhooks",
+    "default_branch": "master",
+    "ci_config_path": "",
+    "homepage": "http://10.126.0.2:3000/root/test-deployment-webhooks",
+    "url": "ssh://vlad@10.126.0.2:2222/root/test-deployment-webhooks.git",
+    "ssh_url": "ssh://vlad@10.126.0.2:2222/root/test-deployment-webhooks.git",
+    "http_url": "http://10.126.0.2:3000/root/test-deployment-webhooks.git"
+  },
+  "short_sha": "279484c0",
+  "user": {
+    "name": "Administrator",
+    "username": "root",
+    "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
+    "email": "admin@example.com"
+  },
+  "user_url": "http://10.126.0.2:3000/root",
+  "commit_url": "http://10.126.0.2:3000/root/test-deployment-webhooks/-/commit/279484c09fbe69ededfced8c1bb6e6d24616b468",
+  "commit_title": "Add new file"
+}
\ No newline at end of file