From dab7dd30d35ce0fc8d52feb7a456d2dc9f6e5364 Mon Sep 17 00:00:00 2001 From: Sam Snyder Date: Tue, 7 Nov 2023 09:47:26 -0800 Subject: [PATCH] Tweak to accommodate changes made to MavenPomDownloader --- .../DependencyResolutionDiagnostic.java | 81 +++++++++++++------ .../DependencyResolutionDiagnosticTest.java | 40 ++++++++- 2 files changed, 94 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/openrewrite/java/dependencies/DependencyResolutionDiagnostic.java b/src/main/java/org/openrewrite/java/dependencies/DependencyResolutionDiagnostic.java index 7800155..a7509c4 100644 --- a/src/main/java/org/openrewrite/java/dependencies/DependencyResolutionDiagnostic.java +++ b/src/main/java/org/openrewrite/java/dependencies/DependencyResolutionDiagnostic.java @@ -54,7 +54,8 @@ public String getDescription() { } public static class Accumulator { - Set mavenRepositories = new HashSet<>(); + Set repositoriesFromGradle = new HashSet<>(); + Set repositoriesFromMaven = new HashSet<>(); } @Override @@ -71,11 +72,11 @@ public TreeVisitor getScanner(Accumulator acc) { return null; } tree.getMarkers().findFirst(GradleProject.class).ifPresent(gp -> { - acc.mavenRepositories.addAll(gp.getMavenRepositories()); - acc.mavenRepositories.addAll(gp.getMavenPluginRepositories()); + acc.repositoriesFromGradle.addAll(gp.getMavenRepositories()); + acc.repositoriesFromGradle.addAll(gp.getMavenPluginRepositories()); }); tree.getMarkers().findFirst(MavenResolutionResult.class).ifPresent(mrr -> - acc.mavenRepositories.addAll(mrr.getPom().getRepositories())); + acc.repositoriesFromMaven.addAll(mrr.getPom().getRepositories())); return tree; } }; @@ -83,42 +84,76 @@ public TreeVisitor getScanner(Accumulator acc) { @Override public Collection generate(Accumulator acc, ExecutionContext ctx) { + Set succeeded = new HashSet<>(); + Map failed = new HashMap<>(); + record(true, acc.repositoriesFromMaven, succeeded, failed, ctx); + record(false, acc.repositoriesFromGradle, succeeded, failed, ctx); + for(String uri : succeeded) { + report.insertRow(ctx, new RepositoryAccessibilityReport.Row(uri, "")); + } + for (Map.Entry uriToFailure : failed.entrySet()) { + report.insertRow(ctx, new RepositoryAccessibilityReport.Row(uriToFailure.getKey(), uriToFailure.getValue())); + } + + return emptyList(); + } + + private static void record(boolean addMavenDefaultRepositories, Collection repos, Set succeeded, Map failed, ExecutionContext ctx) { + // Use MavenPomDownloader without any default repositories, so we can test exactly one repository at a time MavenPomDownloader mpd = new MavenPomDownloader(ctx); + Collection effectiveRepos = repos; + if(addMavenDefaultRepositories) { + if(!effectiveRepos.contains(MavenRepository.MAVEN_LOCAL_DEFAULT)) { + effectiveRepos = new ArrayList<>(effectiveRepos); + effectiveRepos.add(MavenRepository.MAVEN_LOCAL_DEFAULT); + } + if(!effectiveRepos.contains(MavenRepository.MAVEN_CENTRAL)) { + effectiveRepos = new ArrayList<>(effectiveRepos); + effectiveRepos.add(MavenRepository.MAVEN_CENTRAL); + } + } + + // Some repositories don't respond to a simple ping, so try requesting a dependency // Since there's no dependency that every repository can be guaranteed to have, try to download one that // doesn't exist and interpret non-404 results as failure GroupArtifactVersion gav = new GroupArtifactVersion("org.openrewrite.nonexistent", "nonexistent", "0.0.0"); - Set succeeded = new HashSet<>(); - Map failed = new HashMap<>(); - for (MavenRepository repo : acc.mavenRepositories) { + + for (MavenRepository repo : effectiveRepos) { String uri = noTrailingSlash(repo.getUri()); if(succeeded.contains(uri) || failed.containsKey(uri)) { continue; } + if(uri.startsWith("file:/")) { + // Local repositories are always accessible + succeeded.add(uri); + continue; + } try { mpd.download(gav, null, null, Collections.singletonList(repo)); } catch (MavenDownloadingException e) { if(e.getRepositoryResponses().isEmpty()) { - failed.put(uri, "No response from repository"); - } - for (Map.Entry result : e.getRepositoryResponses().entrySet()) { - if (result.getValue().contains("404") || - "Did not attempt to download because of a previous failure to retrieve from this repository.".equals(result.getValue()) || - "Local repository does not contain pom".equals(result.getValue())) { - succeeded.add(noTrailingSlash(result.getKey().getUri())); - } else { - failed.put(noTrailingSlash(result.getKey().getUri()), result.getValue()); + record(repo, e.getMessage(), succeeded, failed); + } else { + for (Map.Entry result : e.getRepositoryResponses().entrySet()) { + record(result.getKey(), result.getValue(), succeeded, failed); } } } } - for(String uri : succeeded) { - report.insertRow(ctx, new RepositoryAccessibilityReport.Row(uri, "")); - } - for (Map.Entry uriToFailure : failed.entrySet()) { - report.insertRow(ctx, new RepositoryAccessibilityReport.Row(uriToFailure.getKey(), uriToFailure.getValue())); - } + } - return emptyList(); + private static void record(MavenRepository repo, String message, Set succeeded, Map failed) { + if (message.contains("404") || + "Did not attempt to download because of a previous failure to retrieve from this repository.".equals(message) || + "Local repository does not contain pom".equals(message)) { + succeeded.add(noTrailingSlash(repo.getUri())); + } else { + if("org.openrewrite.nonexistent:nonexistent:0.0.0 failed. Unable to download POM.".equals(message)) { + failed.put(noTrailingSlash(repo.getUri()), "No response from repository"); + } else { + failed.put(noTrailingSlash(repo.getUri()), message); + } + } } private static String noTrailingSlash(String uri) { diff --git a/src/test/java/org/openrewrite/java/dependencies/DependencyResolutionDiagnosticTest.java b/src/test/java/org/openrewrite/java/dependencies/DependencyResolutionDiagnosticTest.java index afac816..83bb222 100644 --- a/src/test/java/org/openrewrite/java/dependencies/DependencyResolutionDiagnosticTest.java +++ b/src/test/java/org/openrewrite/java/dependencies/DependencyResolutionDiagnosticTest.java @@ -23,7 +23,9 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.openrewrite.gradle.Assertions.buildGradle; import static org.openrewrite.gradle.Assertions.withToolingApi; +import static org.openrewrite.maven.Assertions.pomXml; +@SuppressWarnings("GroovyAssignabilityCheck") public class DependencyResolutionDiagnosticTest implements RewriteTest { @Override @@ -36,15 +38,14 @@ void gradle() { rewriteRun( spec -> spec.beforeRecipe(withToolingApi()) .dataTable(RepositoryAccessibilityReport.Row.class, rows -> { + assertThat(rows).hasSize(4); assertThat(rows).contains( new RepositoryAccessibilityReport.Row("https://repo.maven.apache.org/maven2", "")); assertThat(rows).filteredOn(row -> row.getUri().startsWith("file:/") && "".equals(row.getErrorMessage())).hasSize(1); assertThat(rows).contains( - new RepositoryAccessibilityReport.Row("https://repo.maven.apache.org/maven2", "") - ); + new RepositoryAccessibilityReport.Row("https://plugins.gradle.org/m2", "")); assertThat(rows).contains( - new RepositoryAccessibilityReport.Row("https://nonexistent.moderne.io/maven2", "No response from repository") - ); + new RepositoryAccessibilityReport.Row("https://nonexistent.moderne.io/maven2", "No response from repository")); }), //language=groovy buildGradle(""" @@ -84,4 +85,35 @@ void gradle() { ) ); } + + + @Test + void maven() { + rewriteRun( + spec -> spec.beforeRecipe(withToolingApi()) + .dataTable(RepositoryAccessibilityReport.Row.class, rows -> { + assertThat(rows).contains( + new RepositoryAccessibilityReport.Row("https://repo.maven.apache.org/maven2", "")); + assertThat(rows).filteredOn(row -> row.getUri().startsWith("file:/") && "".equals(row.getErrorMessage())).hasSize(1); + assertThat(rows).contains( + new RepositoryAccessibilityReport.Row("https://nonexistent.moderne.io/maven2", "No response from repository") + ); + }), + //language=xml + pomXml(""" + + com.example + test + 0.1.0 + + + + nonexistent + https://nonexistent.moderne.io/maven2 + + + + """) + ); + } }