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

🐛 Try pom packaging if jar packaging fails #483

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Changes from 2 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
61 changes: 41 additions & 20 deletions provider/internal/java/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,38 +405,59 @@ func (p *javaServiceClient) parseDepString(dep, localRepoPath, pomPath string) (
} else {
return d, fmt.Errorf("unable to split dependency string %s", dep)
}
d.Name = fmt.Sprintf("%s.%s", parts[0], parts[1])

var fp string
if d.Classifier == "" {
fp = filepath.Join(localRepoPath, strings.Replace(parts[0], ".", "/", -1), parts[1], d.Version, fmt.Sprintf("%v-%v.jar.sha1", parts[1], d.Version))
} else {
fp = filepath.Join(localRepoPath, strings.Replace(parts[0], ".", "/", -1), parts[1], d.Version, fmt.Sprintf("%v-%v-%v.jar.sha1", parts[1], d.Version, d.Classifier))
}
b, err := os.ReadFile(fp)
if err != nil {
// Log the error and continue with the next dependency.
p.log.V(5).Error(err, "error reading SHA hash file for dependency", "dep", d.Name)
// Set some default or empty resolved identifier for the dependency.
d.ResolvedIdentifier = ""
} else {
// sometimes sha file contains name of the jar followed by the actual sha
sha, _, _ := strings.Cut(string(b), " ")
d.ResolvedIdentifier = sha
}
group := parts[0]
artifact := parts[1]
d.Name = fmt.Sprintf("%s.%s", group, artifact)

fp := resolveDepFilepath(&d, p, group, artifact, localRepoPath)

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

d.Extras = map[string]interface{}{
groupIdKey: parts[0],
artifactIdKey: parts[1],
groupIdKey: group,
artifactIdKey: artifact,
pomPathKey: pomPath,
}

return d, nil
}

// resolveDepFilepath tries to extract a valid filepath for the dependency with either JAR or POM packaging
func resolveDepFilepath(dep *provider.Dep, p *javaServiceClient, group string, artifact string, localRepoPath string) string {
groupPath := strings.Replace(group, ".", "/", -1)
// Try jar packaging
fp := getFilepathForPackaging(dep, localRepoPath, groupPath, artifact, "jar")
b, err := os.ReadFile(fp)
if err != nil {
// Try pom packaging
jmle marked this conversation as resolved.
Show resolved Hide resolved
fp := getFilepathForPackaging(dep, localRepoPath, groupPath, artifact, "pom")
b, err = os.ReadFile(fp)
jmle marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
// Log the error and continue with the next dependency.
p.log.V(5).Error(err, "error reading SHA hash file for dependency", "dep", dep.Name)
// Set some default or empty resolved identifier for the dependency.
dep.ResolvedIdentifier = ""
}
} else {
// sometimes sha file contains name of the jar followed by the actual sha
sha, _, _ := strings.Cut(string(b), " ")
dep.ResolvedIdentifier = sha
}
return fp
}

func getFilepathForPackaging(dep *provider.Dep, localRepoPath string, groupPath string, artifact string, packaging string) string {
jmle marked this conversation as resolved.
Show resolved Hide resolved
var fp string
if dep.Classifier == "" {
fp = filepath.Join(localRepoPath, groupPath, artifact, dep.Version, fmt.Sprintf("%v-%v.%v.sha1", artifact, dep.Version, packaging))
} else {
fp = filepath.Join(localRepoPath, groupPath, artifact, dep.Version, fmt.Sprintf("%v-%v-%v.%v.sha1", artifact, dep.Version, dep.Classifier, packaging))
}
return fp
}

func addDepLabels(depToLabels map[string]*depLabelItem, depName string) []string {
m := map[string]interface{}{}
for _, d := range depToLabels {
Expand Down
Loading