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

feat: adds logger to cue transformation #251

Draft
wants to merge 1 commit 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
29 changes: 26 additions & 3 deletions internal/cueutils/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/load"
"github.com/rs/zerolog"
)

const (
Expand All @@ -21,12 +22,14 @@ const (
)

type Transformer struct {
inst cue.Value
inst cue.Value
logger *zerolog.Logger
}

func NewTransformer(ctx *cue.Context, name string) (*Transformer, error) {
func NewTransformer(ctx *cue.Context, name string, options ...OptionFunc) (*Transformer, error) {
var devnull bytes.Buffer
overlay := map[string]load.Source{}

err := fs.WalkDir(EmbeddedFilesystem, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
Expand Down Expand Up @@ -56,7 +59,19 @@ func NewTransformer(ctx *cue.Context, name string) (*Transformer, error) {
if err = inst.Err(); err != nil {
return nil, fmt.Errorf("failed to instanstiate transforms: %w", err)
}
return &Transformer{inst: inst}, nil

t := &Transformer{
inst: inst,
logger: &zerolog.Logger{},
}

for _, option := range options {
option(t)
}

t.logger.Debug().Msgf("NewTransformer: %s", name)

return t, nil
}

func (t *Transformer) Apply(input ast.Expr) (cue.Value, error) {
Expand Down Expand Up @@ -84,3 +99,11 @@ func (t *Transformer) ApplyValue(v cue.Value) (cue.Value, error) {
}
return withOutput, nil
}

type OptionFunc func(*Transformer)

func WithLogger(logger *zerolog.Logger) OptionFunc {
return func(t *Transformer) {
t.logger = logger
}
}
7 changes: 4 additions & 3 deletions pkg/local_workflows/data_transformation_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"cuelang.org/go/cue/cuecontext"
cuejson "cuelang.org/go/pkg/encoding/json"
"github.com/rs/zerolog"
cueutil "github.com/snyk/go-application-framework/internal/cueutils"
"github.com/snyk/go-application-framework/pkg/configuration"
"github.com/snyk/go-application-framework/pkg/local_workflows/content_type"
Expand Down Expand Up @@ -60,7 +61,7 @@ func dataTransformationEntryPoint(invocationCtx workflow.InvocationContext, inpu
logger.Trace().Msg("incomplete input data for transformation")
return output, nil
}
findingsModel, err = transformSarifData(sarifInput)
findingsModel, err = transformSarifData(sarifInput, logger)
if err != nil {
logger.Err(err).Msg(err.Error())
return output, err
Expand Down Expand Up @@ -90,7 +91,7 @@ func dataTransformationEntryPoint(invocationCtx workflow.InvocationContext, inpu
return output, nil
}

func transformSarifData(singleData workflow.Data) (localFinding local_models.LocalFinding, err error) {
func transformSarifData(singleData workflow.Data, logger *zerolog.Logger) (localFinding local_models.LocalFinding, err error) {
jsonData, ok := singleData.GetPayload().([]byte)
if !ok {
return localFinding, err
Expand All @@ -102,7 +103,7 @@ func transformSarifData(singleData workflow.Data) (localFinding local_models.Loc
}

ctx := cuecontext.New()
sarif2apiTransformer, transformerError := cueutil.NewTransformer(ctx, cueutil.ToTestApiFromSarif)
sarif2apiTransformer, transformerError := cueutil.NewTransformer(ctx, cueutil.ToTestApiFromSarif, cueutil.WithLogger(logger))
if transformerError != nil {
return localFinding, transformerError
}
Expand Down