diff --git a/gitlab/gitlab.go b/gitlab/gitlab.go index 4218cf5..e2bf5f1 100644 --- a/gitlab/gitlab.go +++ b/gitlab/gitlab.go @@ -35,6 +35,7 @@ const ( PipelineEvents Event = "Pipeline Hook" BuildEvents Event = "Build Hook" JobEvents Event = "Job Hook" + DeploymentEvents Event = "Deployment Hook" SystemHookEvents Event = "System Hook" objectPush string = "push" objectTag string = "tag_push" @@ -208,6 +209,14 @@ func eventParsing(gitLabEvent Event, events []Event, payload []byte) (interface{ } return pl, nil + case DeploymentEvents: + var pl DeploymentEventPayload + err := json.Unmarshal([]byte(payload), &pl) + if err != nil { + return nil, err + } + return pl, nil + case SystemHookEvents: var pl SystemHookPayload err := json.Unmarshal([]byte(payload), &pl) diff --git a/gitlab/gitlab_test.go b/gitlab/gitlab_test.go index c23b741..d713160 100644 --- a/gitlab/gitlab_test.go +++ b/gitlab/gitlab_test.go @@ -231,6 +231,15 @@ func TestWebhooks(t *testing.T) { "X-Gitlab-Event": []string{"Build Hook"}, }, }, + { + name: "DeploymentEvent", + event: DeploymentEvents, + typ: DeploymentEventPayload{}, + filename: "../testdata/gitlab/deployment-event.json", + headers: http.Header{ + "X-Gitlab-Event": []string{"Deployment Hook"}, + }, + }, } for _, tt := range tests { diff --git a/gitlab/payload.go b/gitlab/payload.go index b575d37..68c52a5 100644 --- a/gitlab/payload.go +++ b/gitlab/payload.go @@ -182,6 +182,23 @@ type JobEventPayload struct { Runner Runner `json:"runner"` } +// DeploymentEventPayload contains the information for GitLab's triggered when a deployment +type DeploymentEventPayload struct { + ObjectKind string `json:"object_kind"` + Status string `json:"status"` + StatusChangeAt string `json:"status_changed_at"` + DeploymentId int64 `json:"deployment_id"` + DeployableId int64 `json:"deployable_id"` + DeployableUrl string `json:"deployable_url"` + Environment string `json:"environment"` + Project Project `json:"project"` + ShortSha string `json:"short_sha"` + User User `json:"user"` + UserUrl string `json:"user_url"` + CommitUrl string `json:"commit_url"` + CommitTitle string `json:"commit_title"` +} + // SystemHookPayload contains the ObjectKind to match with real hook events type SystemHookPayload struct { ObjectKind string `json:"object_kind"` diff --git a/testdata/gitlab/deployment-event.json b/testdata/gitlab/deployment-event.json new file mode 100644 index 0000000..fe2143c --- /dev/null +++ b/testdata/gitlab/deployment-event.json @@ -0,0 +1,38 @@ +{ + "object_kind": "deployment", + "status": "success", + "status_changed_at":"2021-04-28 21:50:00 +0200", + "deployment_id": 15, + "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": { + "id": 1, + "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