Skip to content

Commit

Permalink
✨ only apply dep label selector on dep incidents (#371)
Browse files Browse the repository at this point in the history
Closes #329

---------

Signed-off-by: Pranav Gaikwad <[email protected]>
  • Loading branch information
pranavgaikwad committed Oct 11, 2023
1 parent 53a7f0f commit 8e38995
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
16 changes: 14 additions & 2 deletions provider/internal/java/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (p *javaServiceClient) convertToIncidentContext(symbol protocol.WorkspaceSy
}

lineNumber := int(locationRange.Start.Line) + 1

incident := provider.IncidentContext{
FileURI: u,
LineNumber: &lineNumber,
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion provider/internal/java/service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions provider/internal/java/snipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 8 additions & 7 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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": {
Expand Down

0 comments on commit 8e38995

Please sign in to comment.