Skip to content

Commit

Permalink
fix: event logger
Browse files Browse the repository at this point in the history
  • Loading branch information
davidebianchi committed Nov 20, 2024
1 parent 0d662fe commit 3885a6e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
7 changes: 3 additions & 4 deletions internal/sources/webhook/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,20 @@ var (
type Events struct {
Supported map[string]Event
EventTypeFieldPath string
logger logrus.Logger
}

type Event struct {
Operation entities.Operation
FieldID string
}

func (e *Events) getPipelineEvent(rawData []byte) (entities.PipelineEvent, error) {
func (e *Events) getPipelineEvent(logger *logrus.Entry, rawData []byte) (entities.PipelineEvent, error) {
parsed := gjson.ParseBytes(rawData)
webhookEvent := parsed.Get(e.EventTypeFieldPath).String()

event, ok := e.Supported[webhookEvent]
if !ok {
e.logger.WithFields(logrus.Fields{
logger.WithFields(logrus.Fields{
"webhookEvent": webhookEvent,
"event": string(rawData),
}).Trace("unsupported webhook event")
Expand All @@ -54,7 +53,7 @@ func (e *Events) getPipelineEvent(rawData []byte) (entities.PipelineEvent, error

id := parsed.Get(event.FieldID).String()
if id == "" {
e.logger.WithFields(logrus.Fields{
logger.WithFields(logrus.Fields{
"webhookEvent": webhookEvent,
"event": string(rawData),
}).Trace("unsupported webhook event")
Expand Down
6 changes: 5 additions & 1 deletion internal/sources/webhook/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ import (
"testing"

"github.com/mia-platform/integration-connector-agent/internal/entities"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"

"github.com/stretchr/testify/require"
)

func TestEvent(t *testing.T) {
logger, _ := test.NewNullLogger()

testCases := map[string]struct {
rawData string
events *Events
Expand Down Expand Up @@ -86,7 +90,7 @@ func TestEvent(t *testing.T) {

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
event, err := tc.events.getPipelineEvent([]byte(tc.rawData))
event, err := tc.events.getPipelineEvent(logrus.NewEntry(logger), []byte(tc.rawData))
if tc.expectError != "" {
require.Error(t, err)
require.EqualError(t, err, tc.expectError)
Expand Down
2 changes: 1 addition & 1 deletion internal/sources/webhook/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func webhookHandler(config *Configuration, p pipeline.IPipeline) fiber.Handler {
return c.SendStatus(http.StatusOK)
}

event, err := config.Events.getPipelineEvent(body)
event, err := config.Events.getPipelineEvent(log, body)
if err != nil {
log.WithError(err).Error("error unmarshaling event")
return c.Status(http.StatusBadRequest).JSON(utils.ValidationError(err.Error()))
Expand Down

0 comments on commit 3885a6e

Please sign in to comment.