From 63abea20a1babd8a7a686feb79e651e7b859888a Mon Sep 17 00:00:00 2001 From: Deepak Puthraya Date: Mon, 7 Oct 2024 11:29:49 +0530 Subject: [PATCH] feat:[CDS-101592]: Add support for parsing Gitlab System Hooks (#324) --- scm/driver/gitlab/webhook.go | 27 +++++++++++++ scm/driver/gitlab/webhook_test.go | 67 +++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/scm/driver/gitlab/webhook.go b/scm/driver/gitlab/webhook.go index 30992e61..c7056947 100644 --- a/scm/driver/gitlab/webhook.go +++ b/scm/driver/gitlab/webhook.go @@ -39,6 +39,8 @@ func (s *webhookService) Parse(req *http.Request, fn scm.SecretFunc) (scm.Webhoo hook, err = parsePullRequestHook(data) case "Note Hook": hook, err = parseIssueCommentHook(data) + case "System Hook": + hook, err = parseSystemHook(data) default: return nil, scm.ErrUnknownEvent } @@ -63,6 +65,26 @@ func (s *webhookService) Parse(req *http.Request, fn scm.SecretFunc) (scm.Webhoo return hook, nil } +func parseSystemHook(data []byte) (scm.Webhook, error) { + src := new(event) + err := json.Unmarshal(data, src) + if err != nil { + return nil, err + } + switch src.ObjectKind { + case "push", "tag_push": + return parsePushHook(data) + case "issue": + return nil, scm.ErrUnknownEvent + case "merge_request": + return parsePullRequestHook(data) + case "note": + return parseIssueCommentHook(data) + default: + return nil, scm.ErrUnknownEvent + } +} + func parseIssueCommentHook(data []byte) (scm.Webhook, error) { src := new(commentHook) err := json.Unmarshal(data, src) @@ -399,6 +421,11 @@ func parseTimeString(timeString string) time.Time { } type ( + // Generic struct to detect event type + event struct { + ObjectKind string `json:"object_kind"` + } + pushHook struct { ObjectKind string `json:"object_kind"` EventName string `json:"event_name"` diff --git a/scm/driver/gitlab/webhook_test.go b/scm/driver/gitlab/webhook_test.go index e766cada..16041c67 100644 --- a/scm/driver/gitlab/webhook_test.go +++ b/scm/driver/gitlab/webhook_test.go @@ -38,6 +38,18 @@ func TestWebhooks(t *testing.T) { after: "testdata/webhooks/branch_delete.json.golden", obj: new(scm.BranchHook), }, + { + event: "System Hook", + before: "testdata/webhooks/branch_create.json", + after: "testdata/webhooks/branch_create.json.golden", + obj: new(scm.PushHook), + }, + { + event: "System Hook", + before: "testdata/webhooks/branch_delete.json", + after: "testdata/webhooks/branch_delete.json.golden", + obj: new(scm.BranchHook), + }, // tag hooks { event: "Tag Push Hook", @@ -51,6 +63,18 @@ func TestWebhooks(t *testing.T) { after: "testdata/webhooks/tag_delete.json.golden", obj: new(scm.TagHook), }, + { + event: "System Hook", + before: "testdata/webhooks/tag_create.json", + after: "testdata/webhooks/tag_create.json.golden", + obj: new(scm.PushHook), + }, + { + event: "System Hook", + before: "testdata/webhooks/tag_delete.json", + after: "testdata/webhooks/tag_delete.json.golden", + obj: new(scm.TagHook), + }, // push hooks { event: "Push Hook", @@ -58,6 +82,12 @@ func TestWebhooks(t *testing.T) { after: "testdata/webhooks/push.json.golden", obj: new(scm.PushHook), }, + { + event: "System Hook", + before: "testdata/webhooks/push.json", + after: "testdata/webhooks/push.json.golden", + obj: new(scm.PushHook), + }, // // issue hooks // { // event: "issues", @@ -79,6 +109,13 @@ func TestWebhooks(t *testing.T) { after: "testdata/webhooks/pull_request_create.json.golden", obj: new(scm.PullRequestHook), }, + { + event: "System Hook", + before: "testdata/webhooks/pull_request_create.json", + after: "testdata/webhooks/pull_request_create.json.golden", + obj: new(scm.PullRequestHook), + }, + // { // event: "Merge Request Hook", // before: "testdata/webhooks/pull_request_edited.json", @@ -115,6 +152,30 @@ func TestWebhooks(t *testing.T) { after: "testdata/webhooks/pull_request_merge.json.golden", obj: new(scm.PullRequestHook), }, + { + event: "System Hook", + before: "testdata/webhooks/pull_request_close.json", + after: "testdata/webhooks/pull_request_close.json.golden", + obj: new(scm.PullRequestHook), + }, + { + event: "System Hook", + before: "testdata/webhooks/pull_request_review_ready.json", + after: "testdata/webhooks/pull_request_review_ready.json.golden", + obj: new(scm.PullRequestHook), + }, + { + event: "System Hook", + before: "testdata/webhooks/pull_request_reopen.json", + after: "testdata/webhooks/pull_request_reopen.json.golden", + obj: new(scm.PullRequestHook), + }, + { + event: "System Hook", + before: "testdata/webhooks/pull_request_merge.json", + after: "testdata/webhooks/pull_request_merge.json.golden", + obj: new(scm.PullRequestHook), + }, // Note hook for Gitlab Merge Request comment { event: "Note Hook", @@ -122,6 +183,12 @@ func TestWebhooks(t *testing.T) { after: "testdata/webhooks/merge_request_comment_create.json.golden", obj: new(scm.IssueCommentHook), }, + { + event: "System Hook", + before: "testdata/webhooks/merge_request_comment_create.json", + after: "testdata/webhooks/merge_request_comment_create.json.golden", + obj: new(scm.IssueCommentHook), + }, } for _, test := range tests {