Skip to content

Commit

Permalink
file proto missing vars in flow & multi-protocol (#5480)
Browse files Browse the repository at this point in the history
* fix missing template context in file proto

* fix file protocol missing vars

* fix test

* skip example advanced test
  • Loading branch information
tarunKoyalwar authored Aug 4, 2024
1 parent ddcc921 commit 2df1b2e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ jobs:
run: go run .
working-directory: examples/simple/

- name: Example SDK Advanced
run: go run .
working-directory: examples/advanced/
# Temporarily disabled very flaky in github actions
# - name: Example SDK Advanced
# run: go run .
# working-directory: examples/advanced/

- name: Example SDK with speed control
run: go run .
Expand Down
6 changes: 5 additions & 1 deletion pkg/protocols/file/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func (request *Request) ExecuteWithResults(input *contextargs.Context, metadata,
if err != nil {
return err
}
if input.MetaInput.Input == "" {
return errors.New("input cannot be empty file or folder expected")
}
err = request.getInputPaths(input.MetaInput.Input, func(filePath string) {
wg.Add()
func(filePath string) {
Expand Down Expand Up @@ -250,6 +253,8 @@ func (request *Request) findMatchesWithReader(reader io.Reader, input *contextar
for k, v := range previous {
dslMap[k] = v
}
// add vars to template context
request.options.AddTemplateVars(input.MetaInput, request.Type(), request.ID, dslMap)
// add template context variables to DSL map
if request.options.HasTemplateCtx(input.MetaInput) {
dslMap = generators.MergeMaps(dslMap, request.options.GetTemplateCtx(input.MetaInput).GetAll())
Expand Down Expand Up @@ -323,7 +328,6 @@ func (request *Request) buildEvent(input, filePath string, fileMatches []FileMat
exprLines[fileMatch.Expr] = append(exprLines[fileMatch.Expr], fileMatch.Line)
exprBytes[fileMatch.Expr] = append(exprBytes[fileMatch.Expr], fileMatch.ByteIndex)
}

event := eventcreator.CreateEventWithOperatorResults(request, internalEvent, operatorResult)
// Annotate with line numbers if asked by the user
if request.options.Options.ShowMatchLine {
Expand Down
10 changes: 10 additions & 0 deletions pkg/protocols/protocols.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ func (e *ExecutorOptions) AddTemplateVars(input *contextargs.MetaInput, reqType
}
templateCtx := e.GetTemplateCtx(input)
for k, v := range vars {
if stringsutil.HasPrefixAny(k, templateTypes.SupportedProtocolsStrings()...) {
// this was inherited from previous protocols no need to modify it we can directly set it or omit
templateCtx.Set(k, v)
continue
}
if !stringsutil.EqualFoldAny(k, "template-id", "template-info", "template-path") {
if reqID != "" {
k = reqID + "_" + k
Expand All @@ -216,6 +221,11 @@ func (e *ExecutorOptions) AddTemplateVar(input *contextargs.MetaInput, templateT
return
}
templateCtx := e.GetTemplateCtx(input)
if stringsutil.HasPrefixAny(key, templateTypes.SupportedProtocolsStrings()...) {
// this was inherited from previous protocols no need to modify it we can directly set it or omit
templateCtx.Set(key, value)
return
}
if reqID != "" {
key = reqID + "_" + key
} else if templateType < templateTypes.InvalidProtocol {
Expand Down
12 changes: 12 additions & 0 deletions pkg/templates/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ func GetSupportedProtocolTypes() ProtocolTypes {
return result
}

// SupportedProtocolsStrings returns a slice of strings of supported protocols
func SupportedProtocolsStrings() []string {
var result []string
for _, protocol := range GetSupportedProtocolTypes() {
if protocol.String() == "" {
continue
}
result = append(result, protocol.String())
}
return result
}

func toProtocolType(valueToMap string) (ProtocolType, error) {
normalizedValue := normalizeValue(valueToMap)
for key, currentValue := range protocolMappings {
Expand Down
19 changes: 10 additions & 9 deletions pkg/tmplexec/flow/flow_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ func setup() {
progressImpl, _ := progress.NewStatsTicker(0, false, false, false, 0)

executerOpts = protocols.ExecutorOptions{
Output: testutils.NewMockOutputWriter(options.OmitTemplate),
Options: options,
Progress: progressImpl,
ProjectFile: nil,
IssuesClient: nil,
Browser: nil,
Catalog: disk.NewCatalog(config.DefaultConfig.TemplatesDirectory),
RateLimiter: ratelimit.New(context.Background(), uint(options.RateLimit), time.Second),
Parser: templates.NewParser(),
Output: testutils.NewMockOutputWriter(options.OmitTemplate),
Options: options,
Progress: progressImpl,
ProjectFile: nil,
IssuesClient: nil,
Browser: nil,
Catalog: disk.NewCatalog(config.DefaultConfig.TemplatesDirectory),
RateLimiter: ratelimit.New(context.Background(), uint(options.RateLimit), time.Second),
Parser: templates.NewParser(),
}
workflowLoader, err := workflow.NewLoader(&executerOpts)
if err != nil {
Expand Down Expand Up @@ -146,6 +146,7 @@ func TestFlowWithConditionPositive(t *testing.T) {
}

func TestFlowWithNoMatchers(t *testing.T) {
setup()
// when using conditional flow with no matchers at all
// we implicitly assume that request was successful and internally changed the result to true (for scope of condition only)

Expand Down

0 comments on commit 2df1b2e

Please sign in to comment.