From b848c642fc519de18cfd32a62b2c746a1a7432ac Mon Sep 17 00:00:00 2001 From: Akhilesh Pandey Date: Wed, 30 Oct 2024 16:32:06 +0000 Subject: [PATCH] feat: [CODE-2425]: Add webhook and trigger for all comment types (#2815) * feat: [CODE-2425]: Arg format * Merge branch 'main' into akp/CODE-2425 * feat: [CODE-2425]: return immediately in notification service if its a code-comment * feat: [CODE-2425]: send gitness comment type * Merge branch 'main' into akp/CODE-2425 * feat: [CODE-2425]: Add webhook and trigger for all comment types --- app/api/controller/pullreq/comment_create.go | 11 ++++++++--- app/services/notification/comment_created.go | 5 +++++ app/services/webhook/handler_pullreq.go | 1 + app/services/webhook/types.go | 7 ++++--- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/app/api/controller/pullreq/comment_create.go b/app/api/controller/pullreq/comment_create.go index 8ac8b3fd1..1d17298f6 100644 --- a/app/api/controller/pullreq/comment_create.go +++ b/app/api/controller/pullreq/comment_create.go @@ -220,9 +220,14 @@ func (c *Controller) CommentCreate( log.Ctx(ctx).Warn().Err(err).Msg("failed to publish PR changed event") } - // if it's a regular comment publish a comment create event - if act.Type == enum.PullReqActivityTypeComment && act.Kind == enum.PullReqActivityKindComment { - c.reportCommentCreated(ctx, pr, session.Principal.ID, act.ID, act.IsReply()) + // publish event for all comments + if act.Type == enum.PullReqActivityTypeComment || act.Type == enum.PullReqActivityTypeCodeComment { + c.reportCommentCreated( + ctx, + pr, + session.Principal.ID, + act.ID, act.IsReply(), + ) } err = c.instrumentation.Track(ctx, instrument.Event{ diff --git a/app/services/notification/comment_created.go b/app/services/notification/comment_created.go index 19f2d37d6..b4d9a99cf 100644 --- a/app/services/notification/comment_created.go +++ b/app/services/notification/comment_created.go @@ -21,6 +21,7 @@ import ( pullreqevents "github.com/harness/gitness/app/events/pullreq" "github.com/harness/gitness/events" "github.com/harness/gitness/types" + gitnessenum "github.com/harness/gitness/types/enum" ) type CommentPayload struct { @@ -106,6 +107,10 @@ func (s *Service) processCommentCreatedEvent( return nil, nil, nil, nil, fmt.Errorf("failed to fetch activity from pullReqActivityStore: %w", err) } + if activity.Type != gitnessenum.PullReqActivityTypeComment { + return nil, nil, nil, nil, fmt.Errorf("code-comments are not supported currently") + } + commenter, err := s.principalInfoView.Find(ctx, activity.CreatedBy) if err != nil { return nil, nil, nil, nil, fmt.Errorf("failed to fetch commenter from principalInfoView: %w", err) diff --git a/app/services/webhook/handler_pullreq.go b/app/services/webhook/handler_pullreq.go index 7d14e0429..3bcd9de4e 100644 --- a/app/services/webhook/handler_pullreq.go +++ b/app/services/webhook/handler_pullreq.go @@ -359,6 +359,7 @@ func (s *Service) handleEventPullReqComment( Text: activity.Text, ID: activity.ID, ParentID: activity.ParentID, + Kind: activity.Kind, }, }, }, nil diff --git a/app/services/webhook/types.go b/app/services/webhook/types.go index 059d52c48..c3264b0b6 100644 --- a/app/services/webhook/types.go +++ b/app/services/webhook/types.go @@ -299,9 +299,10 @@ type ReferenceInfo struct { } type CommentInfo struct { - ID int64 `json:"id"` - ParentID *int64 `json:"parent_id,omitempty"` - Text string `json:"text"` + ID int64 `json:"id"` + ParentID *int64 `json:"parent_id,omitempty"` + Text string `json:"text"` + Kind enum.PullReqActivityKind `json:"kind"` } type LabelInfo struct {