Skip to content

Commit

Permalink
🐛 Fix file URI prefix for JARs and do not move found JARs so dependen…
Browse files Browse the repository at this point in the history
…cy CLI can still find them (#543)

Fixes #544

Signed-off-by: Pranav Gaikwad <[email protected]>
  • Loading branch information
pranavgaikwad authored Mar 19, 2024
1 parent 6dc3bb5 commit 650120f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
8 changes: 4 additions & 4 deletions provider/internal/java/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ func (p *javaServiceClient) GetDependenciesFallback(ctx context.Context, locatio
dep.Version = *d.Version
}
if m2Repo != "" && d.ArtifactID != nil && d.GroupID != nil {
dep.FileURIPrefix = filepath.Join(m2Repo,
strings.Replace(*d.GroupID, ".", "/", -1), *d.ArtifactID, dep.Version)
dep.FileURIPrefix = fmt.Sprintf("file://%s", filepath.Join(m2Repo,
strings.Replace(*d.GroupID, ".", "/", -1), *d.ArtifactID, dep.Version))
}
}
deps = append(deps, &dep)
Expand Down Expand Up @@ -359,8 +359,8 @@ func (w *walker) walkDirForJar(path string, info fs.DirEntry, err error) error {
// 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)
d.FileURIPrefix = fmt.Sprintf("file://%s", filepath.Join(w.m2RepoPath,
strings.Replace(artifact.GroupId, ".", "/", -1), artifact.ArtifactId, artifact.Version))
}
}

Expand Down
27 changes: 17 additions & 10 deletions provider/internal/java/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,10 @@ func explode(ctx context.Context, log logr.Logger, archivePath, projectPath stri
artifactPath := filepath.Join(strings.Split(dep.ArtifactId, ".")...)
destPath := filepath.Join(m2Repo, groupPath, artifactPath,
dep.Version, filepath.Base(filePath))
if err := moveFile(filePath, destPath); err != nil {
log.V(8).Error(err, "failed moving jar to m2 local repo")
if err := copyFile(filePath, destPath); err != nil {
log.V(8).Error(err, "failed copying jar to m2 local repo")
} else {
log.V(8).Info("moved jar file", "src", filePath, "dest", destPath)
log.V(8).Info("copied jar file", "src", filePath, "dest", destPath)
}
} else {
// when it isn't found online, decompile it
Expand Down Expand Up @@ -405,28 +405,35 @@ func createJavaProject(ctx context.Context, dir string, dependencies []javaArtif
}

func moveFile(srcPath string, destPath string) error {
err := copyFile(srcPath, destPath)
if err != nil {
return err
}
err = os.Remove(srcPath)
if err != nil {
return err
}
return nil
}

func copyFile(srcPath string, destPath string) error {
if err := os.MkdirAll(filepath.Dir(destPath), 0755); err != nil {
return err
}
inputFile, err := os.Open(srcPath)
if err != nil {
return err
}
defer inputFile.Close()
outputFile, err := os.Create(destPath)
if err != nil {
inputFile.Close()
return err
}
defer outputFile.Close()
_, err = io.Copy(outputFile, inputFile)
inputFile.Close()
if err != nil {
return err
}
err = os.Remove(srcPath)
if err != nil {
return err
}
defer outputFile.Close()
return nil
}

Expand Down
3 changes: 2 additions & 1 deletion provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,8 @@ func matchDepLabelSelector(s *labels.LabelSelector[*Dep], inc IncidentContext, d
return false, err
}
for _, d := range depList {
if strings.HasPrefix(string(inc.FileURI), d.FileURIPrefix) {
if d.FileURIPrefix != "" &&
strings.HasPrefix(string(inc.FileURI), d.FileURIPrefix) {
matched = true
}
}
Expand Down

0 comments on commit 650120f

Please sign in to comment.