Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to Mia-Platform Console webhook events #22

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ linters:
- tenv
- thelper
- unconvert
- unparam
- unused
- usestdlibvars
- whitespace
Expand All @@ -59,8 +58,6 @@ linters-settings:
yaml: camel
tenv:
all: true
unparam:
check-exported: false
mnd:
checks:
- case
Expand Down
3 changes: 2 additions & 1 deletion internal/config/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"type": {
"type": "string",
"enum": [
"jira"
"jira",
"console"
]
},
"webhookPath": {
Expand Down
40 changes: 35 additions & 5 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package config

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -72,12 +73,41 @@ func TestLoadServiceConfiguration(t *testing.T) {
},
expectedSinkConfig: getExpectedSinkConfig(t),
expectedProcessorConfig: getExpectedProcessorConfig(t),
expectedSourceConfig: getExpectedSourceConfig(t),
expectedSourceConfig: getExpectedSourceConfig(t, "jira"),
},
"invalid config if integrations is empty": {
path: "./testdata/empty-integrations.json",
expectedError: "configuration not valid: json schema validation errors:",
},
"console config is parsed correctly": {
path: "./testdata/console-config.json",
expectedContent: &Configuration{
Integrations: []Integration{
{
Source: GenericConfig{
Type: "console",
},
Pipelines: []Pipeline{
{
Processors: Processors{
{
Type: "mapper",
},
},
Sinks: Sinks{
{
Type: "mongo",
},
},
},
},
},
},
},
expectedSinkConfig: getExpectedSinkConfig(t),
expectedProcessorConfig: getExpectedProcessorConfig(t),
expectedSourceConfig: getExpectedSourceConfig(t, "console"),
},
}

for testName, test := range tests {
Expand Down Expand Up @@ -178,16 +208,16 @@ func getExpectedProcessorConfig(t *testing.T) string {
}`
}

func getExpectedSourceConfig(t *testing.T) string {
func getExpectedSourceConfig(t *testing.T, sourceType string) string {
t.Helper()

return `{
"type": "jira",
return fmt.Sprintf(`{
"type": "%s",
"webhookPath": "/custom-webhook-path",
"authentication": {
"secret": {
"fromFile": "testdata/secret"
}
}
}`
}`, sourceType)
}
39 changes: 39 additions & 0 deletions internal/config/testdata/console-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"integrations": [
{
"source": {
"type": "console",
"webhookPath": "/custom-webhook-path",
"authentication": {
"secret": {
"fromFile": "testdata/secret"
}
}
},
"pipelines": [
{
"processors": [
{
"type": "mapper",
"outputEvent": {
"key": "{{ issue.key }}",
"summary": "{{ issue.fields.summary }}",
"createdAt": "{{ issue.fields.created }}",
"description": "{{ issue.fields.description }}"
}
}
],
"sinks": [
{
"type": "mongo",
"url": {
"fromEnv": "TEST_LOAD_SERVICE_MONGO_URL"
},
"collection": "my-collection"
}
]
}
]
}
]
}
8 changes: 2 additions & 6 deletions internal/pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ package pipeline
import (
"context"
"errors"
"reflect"

"github.com/mia-platform/integration-connector-agent/internal/entities"
"github.com/mia-platform/integration-connector-agent/internal/processors"
"github.com/mia-platform/integration-connector-agent/internal/processors/filter"
"github.com/mia-platform/integration-connector-agent/internal/sinks"
"github.com/mia-platform/integration-connector-agent/internal/utils"

"github.com/sirupsen/logrus"
)
Expand All @@ -45,7 +45,7 @@ func (p Pipeline) AddMessage(data entities.PipelineEvent) {
}

func (p Pipeline) Start(ctx context.Context) error {
if isNil(p.sinks) {
if utils.IsNil(p.sinks) {
return ErrWriterNotDefined
}

Expand Down Expand Up @@ -112,7 +112,3 @@ func New(logger *logrus.Logger, p *processors.Processors, sinks sinks.Sink[entit

return pipeline, nil
}

func isNil(i any) bool {
return i == nil || (reflect.ValueOf(i).Kind() == reflect.Ptr && reflect.ValueOf(i).IsNil())
}
9 changes: 1 addition & 8 deletions internal/server/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/mia-platform/integration-connector-agent/internal/sinks/mongo"
"github.com/mia-platform/integration-connector-agent/internal/sources"
"github.com/mia-platform/integration-connector-agent/internal/sources/jira"
"github.com/mia-platform/integration-connector-agent/internal/sources/webhook"

swagger "github.com/davidebianchi/gswagger"
"github.com/gofiber/fiber/v2"
Expand Down Expand Up @@ -68,15 +67,9 @@ func setupPipelines(ctx context.Context, log *logrus.Logger, cfg *config.Configu
source := cfgIntegration.Source
switch source.Type {
case sources.Jira:
jiraConfig, err := config.GetConfig[*jira.Config](cfgIntegration.Source)
if err != nil {
return err
}

if err := webhook.SetupService(ctx, oasRouter, &jiraConfig.Configuration, pg); err != nil {
if err := jira.AddSourceToRouter(ctx, source, pg, oasRouter); err != nil {
return fmt.Errorf("%w: %s", errSetupSource, err)
}

case "test":
// do nothing only for testing
return nil
Expand Down
7 changes: 7 additions & 0 deletions internal/sinks/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ func (f *Writer) Calls() Calls {
return f.stub
}

func (f *Writer) ResetCalls() {
f.mtx.Lock()
defer f.mtx.Unlock()

f.stub = Calls{}
}

func (f *Writer) AddMock(mock Mock) {
f.mtx.Lock()
defer f.mtx.Unlock()
Expand Down
15 changes: 15 additions & 0 deletions internal/sinks/fake/fake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ func TestImplementWriter(t *testing.T) {
}, f.Calls().LastCall())
})

t.Run("ResetCalls clean calls", func(t *testing.T) {
f := New(config)

event := &entities.Event{
ID: "id",
OperationType: entities.Write,
}
err := f.WriteData(context.Background(), event)
require.NoError(t, err)

require.Len(t, f.Calls(), 1)
f.ResetCalls()
require.Len(t, f.Calls(), 0)
})

t.Run("mock error write", func(t *testing.T) {
f := New(config)

Expand Down
3 changes: 1 addition & 2 deletions internal/sinks/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,9 @@ func (w *Writer[T]) Delete(ctx context.Context, data T) error {
return err
}

opts := options.Delete()
result, err := w.client.Database(w.database).
Collection(w.collection).
DeleteOne(ctxWithCancel, queryFilter, opts)
DeleteOne(ctxWithCancel, queryFilter)
if err != nil {
return err
}
Expand Down
65 changes: 0 additions & 65 deletions internal/sources/jira/config.go

This file was deleted.

Loading
Loading