Skip to content

Commit

Permalink
✨ add new scope for excluded patterns [Part 1] (#758)
Browse files Browse the repository at this point in the history
This only does post discovery filtering. We need to do pre-filtering
which is incoming in another PR.

---------

Signed-off-by: Pranav Gaikwad <pgaikwad@redhat.com>
  • Loading branch information
pranavgaikwad authored Jan 13, 2025
1 parent bdda7bf commit 5896cc0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
5 changes: 3 additions & 2 deletions engine/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ func gatherChain(start ConditionEntry, entries []ConditionEntry) []ConditionEntr

// Chain Templates are used by rules and providers to pass context around during rule execution.
type ChainTemplate struct {
Filepaths []string `yaml:"filepaths"`
Extras map[string]interface{} `yaml:"extras"`
Filepaths []string `yaml:"filepaths,omitempty"`
Extras map[string]interface{} `yaml:"extras,omitempty"`
ExcludedPaths []string `yaml:"excludedPaths,omitempty"`
}
55 changes: 55 additions & 0 deletions engine/scopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package engine

import (
"fmt"
"net/url"
"regexp"

"github.com/go-logr/logr"
"go.lsp.dev/uri"
)

const TemplateContextPathScopeKey = "konveyor.io/path-scope"
Expand Down Expand Up @@ -92,6 +95,10 @@ func (i *includedPathScope) AddToContext(conditionCTX *ConditionContext) error {
}

func (i *includedPathScope) FilterResponse(response IncidentContext) bool {
// when there are no included paths set, everything is included
if len(i.paths) == 0 {
return false
}
for _, path := range i.paths {
if string(response.FileURI) != "" && response.FileURI.Filename() == path {
return false
Expand All @@ -106,3 +113,51 @@ func IncludedPathsScope(paths []string, log logr.Logger) Scope {
log: log,
}
}

type excludedPathsScope struct {
paths []string
log logr.Logger
}

var _ Scope = &excludedPathsScope{}

func (e *excludedPathsScope) Name() string {
return "ExcludedPathsScope"
}

func (e *excludedPathsScope) AddToContext(conditionCtx *ConditionContext) error {
templ := ChainTemplate{}
if existingTempl, ok := conditionCtx.Template[TemplateContextPathScopeKey]; ok {
templ = existingTempl
}
templ.ExcludedPaths = e.paths
conditionCtx.Template[TemplateContextPathScopeKey] = templ
return nil
}

func (e *excludedPathsScope) FilterResponse(response IncidentContext) bool {
if response.FileURI == "" {
return false
}
for _, path := range e.paths {
e.log.V(5).Info("using path for filtering response", "path", path)
pattern, err := regexp.Compile(path)
if err != nil {
e.log.V(5).Error(err, "invalid pattern", "pattern", path)
continue
}
u, err := url.ParseRequestURI(string(response.FileURI))
if err == nil && u.Scheme == uri.FileScheme && pattern.MatchString(response.FileURI.Filename()) {
e.log.V(5).Info("excluding the file", "file", response.FileURI.Filename(), "pattern", pattern)
return true
}
}
return false
}

func ExcludedPathsScope(paths []string, log logr.Logger) Scope {
return &excludedPathsScope{
paths: paths,
log: log.WithName("excludedPathScope"),
}
}
4 changes: 2 additions & 2 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ type ProviderContext struct {
}

func (p *ProviderContext) GetScopedFilepaths() (bool, []string) {
for key, value := range p.Template {
if key == engine.TemplateContextPathScopeKey {
if value, ok := p.Template[engine.TemplateContextPathScopeKey]; ok {
if len(value.Filepaths) > 0 {
return true, value.Filepaths
}
}
Expand Down

0 comments on commit 5896cc0

Please sign in to comment.