Skip to content

Commit 8adf035

Browse files
authored
🐛 Cache Maven Search Err & Fix artifactID (#893)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - Bug Fixes - Corrected artifact path resolution for exploded JARs, ensuring proper local repository layout and copy destinations. - Reliability - More robust handling of Maven search outages and non-OK responses with clearer errors, reducing noisy failures and stabilizing dependency retrieval. - Performance - Caches server-side Maven search errors to avoid repeated lookups, reducing unnecessary network calls and speeding up repeated operations. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Signed-off-by: Emily McMullan <[email protected]>
1 parent feb8b99 commit 8adf035

File tree

1 file changed

+18
-1
lines changed
  • external-providers/java-external-provider/pkg/java_external_provider

1 file changed

+18
-1
lines changed

external-providers/java-external-provider/pkg/java_external_provider/util.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ func explode(ctx context.Context, log logr.Logger, archivePath, projectPath stri
407407
dependencies = append(dependencies, dep)
408408
// copy this into m2 repo to avoid downloading again
409409
groupPath := filepath.Join(strings.Split(dep.GroupId, ".")...)
410-
artifactPath := filepath.Join(strings.Split(dep.ArtifactId, ".")...)
410+
artifactPath, _ := strings.CutSuffix(filepath.Base(explodedFilePath), ".jar")
411411
destPath := filepath.Join(m2Repo, groupPath, artifactPath,
412412
dep.Version, filepath.Base(explodedFilePath))
413413
if err := CopyFile(explodedFilePath, destPath); err != nil {
@@ -553,6 +553,8 @@ func toDependency(_ context.Context, log logr.Logger, depToLabels map[string]*de
553553
return dep, err
554554
}
555555

556+
var mavenSearchErrorCache error
557+
556558
func constructArtifactFromSHA(log logr.Logger, jarFile string) (javaArtifact, error) {
557559
dep := javaArtifact{}
558560
// we look up the jar in maven
@@ -570,6 +572,12 @@ func constructArtifactFromSHA(log logr.Logger, jarFile string) (javaArtifact, er
570572

571573
sha1sum := hex.EncodeToString(hash.Sum(nil))
572574

575+
// if maven search is down, we do not want to keep trying on each dep
576+
if mavenSearchErrorCache != nil {
577+
log.Info("maven search is down, returning cached error", "error", mavenSearchErrorCache)
578+
return dep, mavenSearchErrorCache
579+
}
580+
573581
// Make an HTTPS request to search.maven.org
574582
searchURL := fmt.Sprintf("https://search.maven.org/solrsearch/select?q=1:%s&rows=20&wt=json", sha1sum)
575583
resp, err := http.Get(searchURL)
@@ -578,6 +586,15 @@ func constructArtifactFromSHA(log logr.Logger, jarFile string) (javaArtifact, er
578586
}
579587
defer resp.Body.Close()
580588

589+
if resp.StatusCode != http.StatusOK {
590+
statusErr := fmt.Errorf("Maven search is unavailable: %s", resp.Status)
591+
// cache the server errors
592+
if resp.StatusCode >= 500 {
593+
mavenSearchErrorCache = statusErr
594+
}
595+
return dep, statusErr
596+
}
597+
581598
// Read and parse the JSON response
582599
body, err := io.ReadAll(resp.Body)
583600
if err != nil {

0 commit comments

Comments
 (0)