From 8e3899546ca927553b5e291238d7fa29a5ad023d Mon Sep 17 00:00:00 2001 From: Pranav Gaikwad Date: Wed, 11 Oct 2023 12:56:39 -0400 Subject: [PATCH] :sparkles: only apply dep label selector on dep incidents (#371) Closes #329 --------- Signed-off-by: Pranav Gaikwad --- provider/internal/java/filter.go | 16 ++++++++++++++-- provider/internal/java/service_client.go | 2 +- provider/internal/java/snipper.go | 4 ++-- provider/provider.go | 15 ++++++++------- provider/provider_test.go | 6 ++++-- 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/provider/internal/java/filter.go b/provider/internal/java/filter.go index b52b8394..263ea941 100644 --- a/provider/internal/java/filter.go +++ b/provider/internal/java/filter.go @@ -144,7 +144,7 @@ func (p *javaServiceClient) convertToIncidentContext(symbol protocol.WorkspaceSy } lineNumber := int(locationRange.Start.Line) + 1 - + incident := provider.IncidentContext{ FileURI: u, LineNumber: &lineNumber, @@ -155,6 +155,12 @@ func (p *javaServiceClient) convertToIncidentContext(symbol protocol.WorkspaceSy FILE_KEY: u, }, } + + // based on original URI we got, we can tell if this incident appeared in a dep + if locationURI != "" && strings.HasPrefix(locationURI, JDT_CLASS_FILE_URI_PREFIX) { + incident.IsDependencyIncident = true + } + if locationRange.Start.Line == 0 && locationRange.Start.Character == 0 && locationRange.End.Line == 0 && locationRange.End.Character == 0 { return incident, nil } @@ -185,6 +191,12 @@ func (p *javaServiceClient) convertSymbolRefToIncidentContext(symbol protocol.Wo FILE_KEY: u, }, } + + // based on original URI we got, we can tell if this incident appeared in a dep + if strings.HasPrefix(ref.URI, JDT_CLASS_FILE_URI_PREFIX) { + incident.IsDependencyIncident = true + } + if ref.Range.Start.Line == 0 && ref.Range.Start.Character == 0 && ref.Range.End.Line == 0 && ref.Range.End.Character == 0 { return incident, nil } @@ -207,7 +219,7 @@ func (p *javaServiceClient) convertSymbolRefToIncidentContext(symbol protocol.Wo } func (p *javaServiceClient) getURI(refURI string) (uri.URI, error) { - if !strings.HasPrefix(refURI, FILE_URI_PREFIX) { + if !strings.HasPrefix(refURI, JDT_CLASS_FILE_URI_PREFIX) { return uri.Parse(refURI) } diff --git a/provider/internal/java/service_client.go b/provider/internal/java/service_client.go index 29f30670..4e1f03ca 100644 --- a/provider/internal/java/service_client.go +++ b/provider/internal/java/service_client.go @@ -137,7 +137,7 @@ func (p *javaServiceClient) GetAllReferences(ctx context.Context, symbol protoco locationRange = protocol.Range{} } - if strings.Contains(locationURI, FILE_URI_PREFIX) { + if strings.Contains(locationURI, JDT_CLASS_FILE_URI_PREFIX) { return []protocol.Location{ { URI: locationURI, diff --git a/provider/internal/java/snipper.go b/provider/internal/java/snipper.go index 4aca3d67..a15f4228 100644 --- a/provider/internal/java/snipper.go +++ b/provider/internal/java/snipper.go @@ -12,14 +12,14 @@ import ( ) const ( - FILE_URI_PREFIX = "konveyor-jdt" + JDT_CLASS_FILE_URI_PREFIX = "konveyor-jdt" ) var _ engine.CodeSnip = &javaProvider{} func (p *javaProvider) GetCodeSnip(u uri.URI, loc engine.Location) (string, error) { if !strings.Contains(string(u), uri.FileScheme) { - return "", fmt.Errorf("invalid file uri, must be for %s", FILE_URI_PREFIX) + return "", fmt.Errorf("invalid file uri, must be for %s", JDT_CLASS_FILE_URI_PREFIX) } snip, err := p.scanFile(u.Filename(), loc) if err != nil { diff --git a/provider/provider.go b/provider/provider.go index 4e6e9d87..a3837b4a 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -197,12 +197,13 @@ type ProviderEvaluateResponse struct { } type IncidentContext struct { - FileURI uri.URI `yaml:"fileURI"` - Effort *int `yaml:"effort,omitempty"` - LineNumber *int `yaml:"lineNumber,omitempty"` - Variables map[string]interface{} `yaml:"variables,omitempty"` - Links []ExternalLinks `yaml:"externalLink,omitempty"` - CodeLocation *Location `yaml:"location,omitempty"` + FileURI uri.URI `yaml:"fileURI"` + Effort *int `yaml:"effort,omitempty"` + LineNumber *int `yaml:"lineNumber,omitempty"` + Variables map[string]interface{} `yaml:"variables,omitempty"` + Links []ExternalLinks `yaml:"externalLink,omitempty"` + CodeLocation *Location `yaml:"location,omitempty"` + IsDependencyIncident bool } type Location struct { @@ -471,7 +472,7 @@ func (p ProviderCondition) Evaluate(ctx context.Context, log logr.Logger, condCt // matchDepLabelSelector evaluates the dep label selector on incident func matchDepLabelSelector(s *labels.LabelSelector[*Dep], inc IncidentContext, deps map[uri.URI][]*konveyor.Dep) (bool, error) { // always match non dependency URIs or when there are no deps or no dep selector - if s == nil || deps == nil || len(deps) == 0 || inc.FileURI == "" { + if !inc.IsDependencyIncident || s == nil || deps == nil || len(deps) == 0 || inc.FileURI == "" { return true, nil } matched := false diff --git a/provider/provider_test.go b/provider/provider_test.go index dfb15508..6b6eddcf 100644 --- a/provider/provider_test.go +++ b/provider/provider_test.go @@ -183,7 +183,8 @@ func Test_matchDepLabelSelector(t *testing.T) { name: "label selector matches", labelSelector: "konveyor.io/dep-source=open-source", incident: IncidentContext{ - FileURI: "file://test-file-uri/test-file", + FileURI: "file://test-file-uri/test-file", + IsDependencyIncident: true, }, deps: map[uri.URI][]*konveyor.Dep{ "pom.xml": { @@ -204,7 +205,8 @@ func Test_matchDepLabelSelector(t *testing.T) { name: "label selector does not match", labelSelector: "!konveyor.io/dep-source=exclude", incident: IncidentContext{ - FileURI: "file://test-file-uri/test-file", + FileURI: "file://test-file-uri/test-file", + IsDependencyIncident: true, }, deps: map[uri.URI][]*konveyor.Dep{ "pom.xml": {