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

🐛 Handeling the provider not respecting scope by adding Filter in engine #757

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
15 changes: 10 additions & 5 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (r *ruleEngine) RunRulesScoped(ctx context.Context, ruleSets []RuleSet, sco

taggingRules, otherRules, mapRuleSets := r.filterRules(ruleSets, selectors...)

ruleContext := r.runTaggingRules(ctx, taggingRules, mapRuleSets, conditionContext)
ruleContext := r.runTaggingRules(ctx, taggingRules, mapRuleSets, conditionContext, scopes)

// Need a better name for this thing
ret := make(chan response)
Expand All @@ -220,7 +220,7 @@ func (r *ruleEngine) RunRulesScoped(ctx context.Context, ruleSets []RuleSet, sco
rs.Errors[response.Rule.RuleID] = response.Err.Error()
}
} else if response.ConditionResponse.Matched && len(response.ConditionResponse.Incidents) > 0 {
violation, err := r.createViolation(ctx, response.ConditionResponse, response.Rule)
violation, err := r.createViolation(ctx, response.ConditionResponse, response.Rule, scopes)
if err != nil {
r.logger.Error(err, "unable to create violation from response", "ruleID", response.Rule.RuleID)
}
Expand Down Expand Up @@ -345,7 +345,7 @@ func (r *ruleEngine) filterRules(ruleSets []RuleSet, selectors ...RuleSelector)

// runTaggingRules filters and runs info rules synchronously
// returns list of non-info rules, a context to pass to them
func (r *ruleEngine) runTaggingRules(ctx context.Context, infoRules []ruleMessage, mapRuleSets map[string]*konveyor.RuleSet, context ConditionContext) ConditionContext {
func (r *ruleEngine) runTaggingRules(ctx context.Context, infoRules []ruleMessage, mapRuleSets map[string]*konveyor.RuleSet, context ConditionContext, scope Scope) ConditionContext {
// track unique tags per ruleset
rulesetTagsCache := map[string]map[string]bool{}
for _, ruleMessage := range infoRules {
Expand Down Expand Up @@ -407,7 +407,7 @@ func (r *ruleEngine) runTaggingRules(ctx context.Context, infoRules []ruleMessag
mapRuleSets[ruleMessage.ruleSetName] = rs
}
// create an insight for this tag
violation, err := r.createViolation(ctx, response, rule)
violation, err := r.createViolation(ctx, response, rule, scope)
if err != nil {
r.logger.Error(err, "unable to create violation from response", "ruleID", rule.RuleID)
}
Expand Down Expand Up @@ -487,7 +487,7 @@ func (r *ruleEngine) getRelativePathForViolation(fileURI uri.URI) (uri.URI, erro
return fileURI, nil
}

func (r *ruleEngine) createViolation(ctx context.Context, conditionResponse ConditionResponse, rule Rule) (konveyor.Violation, error) {
func (r *ruleEngine) createViolation(ctx context.Context, conditionResponse ConditionResponse, rule Rule, scope Scope) (konveyor.Violation, error) {
incidents := []konveyor.Incident{}
fileCodeSnipCount := map[string]int{}
incidentsSet := map[string]struct{}{} // Set of incidents
Expand All @@ -504,6 +504,11 @@ func (r *ruleEngine) createViolation(ctx context.Context, conditionResponse Cond
if r.incidentLimit != 0 && len(incidents) == r.incidentLimit {
break
}
// If we should remove the incident because the provider didn't filter it
// and the user asked for a certain scope of incidents.
if scope != nil && scope.FilterResponse(m) {
continue
}
trimmedUri, err := r.getRelativePathForViolation(m.FileURI)
if err != nil {
return konveyor.Violation{}, err
Expand Down
24 changes: 22 additions & 2 deletions engine/scopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Scope interface {
// For now this is the only place that we are considering adding a scope
// in the future, we could scope other things
AddToContext(*ConditionContext) error
FilterResponse(IncidentContext) bool
}

type scopeWrapper struct {
Expand All @@ -35,15 +36,25 @@ func (s *scopeWrapper) Name() string {
}

func (s *scopeWrapper) AddToContext(conditionCTX *ConditionContext) error {
for _, s := range s.scopes {
err := s.AddToContext(conditionCTX)
for _, scope := range s.scopes {
err := scope.AddToContext(conditionCTX)
if err != nil {
return err
}
}
return nil
}

func (s *scopeWrapper) FilterResponse(response IncidentContext) bool {
for _, scope := range s.scopes {
shouldFilter := scope.FilterResponse(response)
if shouldFilter {
return true
}
}
return false
}

var _ Scope = &scopeWrapper{}

func NewScope(scopes ...Scope) Scope {
Expand Down Expand Up @@ -80,6 +91,15 @@ func (i *includedPathScope) AddToContext(conditionCTX *ConditionContext) error {

}

func (i *includedPathScope) FilterResponse(response IncidentContext) bool {
for _, path := range i.paths {
if string(response.FileURI) != "" && response.FileURI.Filename() == path {
return false
}
}
return true
}

func IncludedPathsScope(paths []string, log logr.Logger) Scope {
return &includedPathScope{
paths: paths,
Expand Down
Loading