Skip to content

Commit

Permalink
feat:[CDS-101592]: Add support for parsing Gitlab System Hooks (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
puthrayaharness authored Oct 7, 2024
1 parent 607a215 commit 63abea2
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
27 changes: 27 additions & 0 deletions scm/driver/gitlab/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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"`
Expand Down
67 changes: 67 additions & 0 deletions scm/driver/gitlab/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -51,13 +63,31 @@ 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",
before: "testdata/webhooks/push.json",
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",
Expand All @@ -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",
Expand Down Expand Up @@ -115,13 +152,43 @@ 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",
before: "testdata/webhooks/merge_request_comment_create.json",
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 {
Expand Down

0 comments on commit 63abea2

Please sign in to comment.