Skip to content

Commit

Permalink
Tweak to accommodate changes made to MavenPomDownloader
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Nov 7, 2023
1 parent e3489ea commit dab7dd3
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public String getDescription() {
}

public static class Accumulator {
Set<MavenRepository> mavenRepositories = new HashSet<>();
Set<MavenRepository> repositoriesFromGradle = new HashSet<>();
Set<MavenRepository> repositoriesFromMaven = new HashSet<>();
}

@Override
Expand All @@ -71,54 +72,88 @@ public TreeVisitor<?, ExecutionContext> 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;
}
};
}

@Override
public Collection<? extends SourceFile> generate(Accumulator acc, ExecutionContext ctx) {
Set<String> succeeded = new HashSet<>();
Map<String, String> 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<String, String> uriToFailure : failed.entrySet()) {
report.insertRow(ctx, new RepositoryAccessibilityReport.Row(uriToFailure.getKey(), uriToFailure.getValue()));
}

return emptyList();
}

private static void record(boolean addMavenDefaultRepositories, Collection<MavenRepository> repos, Set<String> succeeded, Map<String, String> 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<MavenRepository> 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<String> succeeded = new HashSet<>();
Map<String, String> 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<MavenRepository, String> 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<MavenRepository, String> 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<String, String> uriToFailure : failed.entrySet()) {
report.insertRow(ctx, new RepositoryAccessibilityReport.Row(uriToFailure.getKey(), uriToFailure.getValue()));
}
}

return emptyList();
private static void record(MavenRepository repo, String message, Set<String> succeeded, Map<String, String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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("""
Expand Down Expand Up @@ -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("""
<project>
<groupId>com.example</groupId>
<artifactId>test</artifactId>
<version>0.1.0</version>
<repositories>
<repository>
<id>nonexistent</id>
<url>https://nonexistent.moderne.io/maven2</url>
</repository>
</repositories>
</project>
""")
);
}
}

0 comments on commit dab7dd3

Please sign in to comment.