Skip to content

Commit

Permalink
✨ add dependency location resolver
Browse files Browse the repository at this point in the history
Signed-off-by: Pranav Gaikwad <[email protected]>
  • Loading branch information
pranavgaikwad committed Nov 9, 2023
1 parent d295615 commit 91bb5ff
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
57 changes: 57 additions & 0 deletions provider/internal/java/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"

"github.com/konveyor/analyzer-lsp/engine"
"github.com/konveyor/analyzer-lsp/engine/labels"
"github.com/konveyor/analyzer-lsp/output/v1/konveyor"
"github.com/konveyor/analyzer-lsp/provider"
Expand All @@ -28,6 +30,15 @@ const (
providerSpecificConfigExcludePackagesKey = "excludePackages"
)

// keys used in dep.Extras for extra information about a dep
const (
artifactIdKey = "artifactId"
groupIdKey = "groupId"
pomPathKey = "pomPath"
)

var _ provider.DependencyLocationResolver = &javaServiceClient{}

// TODO implement this for real
func (p *javaServiceClient) findPom() string {
var depPath string
Expand Down Expand Up @@ -517,3 +528,49 @@ func loadDepLabelItems(r io.Reader, depToLabels map[string]*depLabelItem, label
}
return nil
}

// getLineNumberForDep given a dep and pom file, attempts to find line number
func (j *javaServiceClient) GetLocation(ctx context.Context, dep konveyor.Dep) (engine.Location, error) {
if dep.Extras == nil {
return engine.Location{}, fmt.Errorf("unable to get location for dep %s", dep.Name)
}
extrasKeys := []string{artifactIdKey, groupIdKey, pomPathKey}
for _, key := range extrasKeys {
if _, ok := dep.Extras[key]; !ok {
return engine.Location{}, fmt.Errorf("unable to get location for dep %s, missing extras", dep.Name)
}
}
groupId := dep.Extras[groupIdKey]
artifactId := dep.Extras[artifactIdKey]
path := dep.Extras[pomPathKey]
cmd := exec.CommandContext(ctx, "pcre2grep", []string{
"-M",
"-n",
"--line-offsets",
fmt.Sprintf("%s(\\n|.)*?%s.*", groupId, artifactId),
}...)
output, err := cmd.CombinedOutput()
if err != nil {
return engine.Location{}, fmt.Errorf("failed to run pcre2grep to get line number (%w)", err)
}
outputLines := strings.Split(string(output), "\n")
if len(outputLines) < 1 {
return engine.Location{}, fmt.Errorf("no match found for %s.%s in %s", groupId, artifactId, path)
}
parts := strings.Split(outputLines[0], ":")
if len(parts) < 1 {
return engine.Location{}, fmt.Errorf("unparseable pcre2grep output - %s", outputLines[0])
}
lineNumber, err := strconv.Atoi(parts[0])
if err != nil {
return engine.Location{}, fmt.Errorf("unparseable pcre2grep output - %s", outputLines[0])
}
return engine.Location{
StartPosition: engine.Position{
Line: lineNumber,
},
EndPosition: engine.Position{
Line: lineNumber,
},
}, nil
}
28 changes: 25 additions & 3 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"regexp"
"strings"
"time"

"github.com/cbroglie/mustache"
"github.com/getkin/kin-openapi/openapi3"
Expand Down Expand Up @@ -336,6 +337,10 @@ type ServiceClient interface {
GetDependenciesDAG(ctx context.Context) (map[uri.URI][]DepDAGItem, error)
}

type DependencyLocationResolver interface {
GetLocation(ctx context.Context, dep konveyor.Dep) (engine.Location, error)
}

type Dep = konveyor.Dep
type DepDAGItem = konveyor.DepDAGItem
type Startable interface {
Expand Down Expand Up @@ -561,17 +566,34 @@ func (dc DependencyCondition) Evaluate(ctx context.Context, log logr.Logger, con
return resp, nil
}

var depLocationResolver DependencyLocationResolver
if val, ok := dc.Client.(DependencyLocationResolver); ok {
depLocationResolver = val
}

for _, matchedDep := range matchedDeps {
if matchedDep.dep.Version == "" || (dc.Lowerbound == "" && dc.Upperbound == "") {
resp.Matched = true
resp.Incidents = append(resp.Incidents, engine.IncidentContext{
incident := engine.IncidentContext{
FileURI: matchedDep.uri,
Variables: map[string]interface{}{
"name": matchedDep.dep.Name,
"version": matchedDep.dep.Version,
"type": matchedDep.dep.Type,
},
})
}
if depLocationResolver != nil {
// this is a best-effort step and we don't want to block if resolver misbehaves
timeoutContext, cancelFunc := context.WithTimeout(ctx, time.Second*3)
location, err := depLocationResolver.GetLocation(timeoutContext, *matchedDep.dep)
if err == nil {
incident.LineNumber = &location.StartPosition.Line
} else {
log.V(7).Error(err, "failed to get location for dependency", "dep", matchedDep.dep.Name)
}
cancelFunc()
}
resp.Matched = true
resp.Incidents = append(resp.Incidents, incident)
// For now, lets leave this TODO to figure out what we should be setting in the context
resp.TemplateContext = map[string]interface{}{
"name": matchedDep.dep.Name,
Expand Down

0 comments on commit 91bb5ff

Please sign in to comment.