From 3885a6e235e635fb3a51274ff5f3101b8c81dcec Mon Sep 17 00:00:00 2001 From: Davide Bianchi <10374360+davidebianchi@users.noreply.github.com> Date: Wed, 20 Nov 2024 13:15:55 +0100 Subject: [PATCH] fix: event logger --- internal/sources/webhook/events.go | 7 +++---- internal/sources/webhook/events_test.go | 6 +++++- internal/sources/webhook/service.go | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/internal/sources/webhook/events.go b/internal/sources/webhook/events.go index b4d6fac..1bf5c31 100644 --- a/internal/sources/webhook/events.go +++ b/internal/sources/webhook/events.go @@ -31,7 +31,6 @@ var ( type Events struct { Supported map[string]Event EventTypeFieldPath string - logger logrus.Logger } type Event struct { @@ -39,13 +38,13 @@ type Event struct { 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") @@ -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") diff --git a/internal/sources/webhook/events_test.go b/internal/sources/webhook/events_test.go index 426cb98..03d4543 100644 --- a/internal/sources/webhook/events_test.go +++ b/internal/sources/webhook/events_test.go @@ -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 @@ -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) diff --git a/internal/sources/webhook/service.go b/internal/sources/webhook/service.go index 9e07bbd..5ed9757 100644 --- a/internal/sources/webhook/service.go +++ b/internal/sources/webhook/service.go @@ -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()))