Skip to content

Commit

Permalink
✨ get artifact information in deps
Browse files Browse the repository at this point in the history
Signed-off-by: Pranav Gaikwad <[email protected]>
  • Loading branch information
pranavgaikwad committed Oct 6, 2023
1 parent 35c7610 commit f62c3f7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
28 changes: 23 additions & 5 deletions provider/internal/java/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,17 @@ func (p *javaServiceClient) GetDependenciesDAG(ctx context.Context) (map[uri.URI
func (p *javaServiceClient) discoverDepsFromJars(path string, ll map[uri.URI][]konveyor.DepDAGItem) {
// for binaries we only find JARs embedded in archive
w := walker{
deps: ll,
deps: ll,
depToLabels: p.depToLabels,
m2RepoPath: getMavenLocalRepoPath(p.mvnSettingsFile),
}
filepath.WalkDir(path, w.walkDirForJar)
}

type walker struct {
deps map[uri.URI][]provider.DepDAGItem
deps map[uri.URI][]provider.DepDAGItem
depToLabels map[string]*depLabelItem
m2RepoPath string
}

func (w *walker) walkDirForJar(path string, info fs.DirEntry, err error) error {
Expand All @@ -229,6 +233,20 @@ func (w *walker) walkDirForJar(path string, info fs.DirEntry, err error) error {
d := provider.Dep{
Name: info.Name(),
}
artifact, _ := toDependency(context.TODO(), path)
if (artifact != javaArtifact{}) {
d.Name = fmt.Sprintf("%s.%s", artifact.GroupId, artifact.ArtifactId)
d.Version = artifact.Version
d.Labels = addDepLabels(w.depToLabels, d.Name)
d.ResolvedIdentifier = artifact.sha1
// when we can successfully get javaArtifact from a jar
// we added it to the pom and it should be in m2Repo path
if w.m2RepoPath != "" {
d.FileURIPrefix = filepath.Join(w.m2RepoPath,
strings.Replace(artifact.GroupId, ".", "/", -1), artifact.ArtifactId, artifact.Version)
}
}

w.deps[uri.URI(filepath.Join(path, info.Name()))] = []provider.DepDAGItem{
{
Dep: d,
Expand Down Expand Up @@ -271,15 +289,15 @@ func (p *javaServiceClient) parseDepString(dep, localRepoPath string) (provider.
d.ResolvedIdentifier = string(b)
}

d.Labels = p.addDepLabels(d.Name)
d.Labels = addDepLabels(p.depToLabels, d.Name)
d.FileURIPrefix = fmt.Sprintf("file://%v", filepath.Dir(fp))

return d, nil
}

func (p *javaServiceClient) addDepLabels(depName string) []string {
func addDepLabels(depToLabels map[string]*depLabelItem, depName string) []string {
m := map[string]interface{}{}
for _, d := range p.depToLabels {
for _, d := range depToLabels {
if d.r.Match([]byte(depName)) {
for label, _ := range d.labels {
m[label] = nil
Expand Down
22 changes: 19 additions & 3 deletions provider/internal/java/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type javaArtifact struct {
GroupId string
ArtifactId string
Version string
sha1 string
}

type decompileFilter interface {
Expand Down Expand Up @@ -177,14 +178,14 @@ func decompileJava(ctx context.Context, log logr.Logger, archivePath string) (ex
return "", "", err
}

err = createJavaProject(ctx, projectPath, deps)
err = createJavaProject(ctx, projectPath, deduplicateJavaArtifacts(deps))
if err != nil {
log.Error(err, "failed to create java project", "path", projectPath)
return "", "", err
}
log.V(5).Info("created java project", "path", projectPath)

err = decompile(context.TODO(), log, decompFilter, 10, decompJobs, projectPath)
err = decompile(ctx, log, decompFilter, 10, decompJobs, projectPath)
if err != nil {
log.Error(err, "failed to decompile", "path", archivePath)
return "", "", err
Expand All @@ -193,6 +194,20 @@ func decompileJava(ctx context.Context, log logr.Logger, archivePath string) (ex
return explodedPath, projectPath, err
}

func deduplicateJavaArtifacts(artifacts []javaArtifact) []javaArtifact {
uniq := []javaArtifact{}
seen := map[string]bool{}
for _, a := range artifacts {
key := fmt.Sprintf("%s-%s-%s%s",
a.ArtifactId, a.GroupId, a.Version, a.packaging)
if _, ok := seen[key]; !ok {
seen[key] = true
uniq = append(uniq, a)
}
}
return uniq
}

// explode explodes the given JAR, WAR or EAR archive, generates javaArtifact struct for given archive
// and identifies all .class found recursively. returns output path, a list of decompileJob for .class files
// it also returns a list of any javaArtifact we could interpret from jars
Expand All @@ -204,7 +219,7 @@ func explode(ctx context.Context, log logr.Logger, archivePath, projectPath stri
}

// Create the destDir directory using the same permissions as the Java archive file
// java.jar should become java-jar-decompiled
// java.jar should become java-jar-exploded
destDir := filepath.Join(path.Dir(archivePath), strings.Replace(path.Base(archivePath), ".", "-", -1)+"-exploded")
// make sure execute bits are set so that fernflower can decompile
err = os.MkdirAll(destDir, fileInfo.Mode()|0111)
Expand Down Expand Up @@ -468,6 +483,7 @@ func toDependency(ctx context.Context, jarFile string) (javaArtifact, error) {
dep.GroupId = jarInfo["g"].(string)
dep.ArtifactId = jarInfo["a"].(string)
dep.Version = jarInfo["v"].(string)
dep.sha1 = sha1sum
return dep, nil
}
return dep, fmt.Errorf("failed to construct artifact from jar")
Expand Down

0 comments on commit f62c3f7

Please sign in to comment.