From 6c0e0452c0a3424153b3cf03f4bf223513dcbf18 Mon Sep 17 00:00:00 2001 From: Pranav Gaikwad Date: Tue, 12 Mar 2024 09:22:44 -0400 Subject: [PATCH] :bug: do not move jar file Signed-off-by: Pranav Gaikwad --- provider/internal/java/dependency.go | 8 ++++---- provider/internal/java/util.go | 27 +++++++++++++++++---------- provider/provider.go | 3 ++- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/provider/internal/java/dependency.go b/provider/internal/java/dependency.go index 86026db6..38ba7633 100644 --- a/provider/internal/java/dependency.go +++ b/provider/internal/java/dependency.go @@ -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) @@ -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)) } } diff --git a/provider/internal/java/util.go b/provider/internal/java/util.go index 6f4b2550..e1ad9064 100644 --- a/provider/internal/java/util.go +++ b/provider/internal/java/util.go @@ -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 @@ -405,6 +405,18 @@ 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 } @@ -412,21 +424,16 @@ func moveFile(srcPath string, destPath string) error { 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 } diff --git a/provider/provider.go b/provider/provider.go index 0f9b7bac..4421d0b4 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -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 } }