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 new scope for excluded patterns [Part 1] #758

Merged
merged 4 commits into from
Jan 13, 2025
Merged
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
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
Loading