Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix file URI prefix for JARs and do not move found JARs so dependency CLI can still find them #543

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading