|
| 1 | +package com.giovds; |
| 2 | + |
| 3 | +import com.giovds.collector.github.GithubCollector; |
| 4 | +import com.giovds.collector.github.GithubGuesser; |
| 5 | +import com.giovds.dto.PomResponse; |
| 6 | +import com.giovds.dto.github.internal.Collected; |
| 7 | +import com.giovds.evaluator.MaintenanceEvaluator; |
| 8 | +import org.apache.maven.model.Dependency; |
| 9 | +import org.apache.maven.plugin.AbstractMojo; |
| 10 | +import org.apache.maven.plugin.MojoFailureException; |
| 11 | +import org.apache.maven.plugin.logging.Log; |
| 12 | +import org.apache.maven.plugin.logging.SystemStreamLog; |
| 13 | +import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 14 | +import org.apache.maven.plugins.annotations.Mojo; |
| 15 | +import org.apache.maven.plugins.annotations.Parameter; |
| 16 | +import org.apache.maven.plugins.annotations.ResolutionScope; |
| 17 | +import org.apache.maven.project.MavenProject; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.concurrent.ExecutionException; |
| 22 | +import java.util.stream.Collectors; |
| 23 | + |
| 24 | +@Mojo(name = "unmaintained", |
| 25 | + defaultPhase = LifecyclePhase.TEST_COMPILE, |
| 26 | + requiresOnline = true, |
| 27 | + requiresDependencyResolution = ResolutionScope.TEST) |
| 28 | +public class UnmaintainedMojo extends AbstractMojo { |
| 29 | + |
| 30 | + private final PomClientInterface client; |
| 31 | + private final GithubGuesser githubGuesser; |
| 32 | + private final GithubCollector githubCollector; |
| 33 | + private final MaintenanceEvaluator maintenanceEvaluator; |
| 34 | + |
| 35 | + @Parameter(readonly = true, required = true, defaultValue = "${project}") |
| 36 | + private MavenProject project; |
| 37 | + |
| 38 | + /** |
| 39 | + * Required for initialization by Maven |
| 40 | + */ |
| 41 | + public UnmaintainedMojo() { |
| 42 | + this(new SystemStreamLog()); |
| 43 | + } |
| 44 | + |
| 45 | + public UnmaintainedMojo(Log log) { |
| 46 | + this(new PomClient(log), new GithubGuesser(), new GithubCollector(log), new MaintenanceEvaluator()); |
| 47 | + } |
| 48 | + |
| 49 | + public UnmaintainedMojo( |
| 50 | + final PomClientInterface client, |
| 51 | + final GithubGuesser githubGuesser, |
| 52 | + final GithubCollector githubCollector, |
| 53 | + final MaintenanceEvaluator maintenanceEvaluator) { |
| 54 | + this.client = client; |
| 55 | + this.githubGuesser = githubGuesser; |
| 56 | + this.githubCollector = githubCollector; |
| 57 | + this.maintenanceEvaluator = maintenanceEvaluator; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void execute() throws MojoFailureException { |
| 62 | + final List<Dependency> dependencies = project.getDependencies(); |
| 63 | + |
| 64 | + if (dependencies.isEmpty()) { |
| 65 | + // When building a POM without any dependencies there will be nothing to query. |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + final Map<Dependency, PomResponse> pomResponses = dependencies.stream() |
| 70 | + .map(dependency -> { |
| 71 | + try { |
| 72 | + PomResponse pomResponse = client.getPom(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion()); |
| 73 | + |
| 74 | + return new DependencyPomResponsePair(dependency, pomResponse); |
| 75 | + } catch (Exception e) { |
| 76 | + getLog().error("Failed to fetch POM for %s:%s:%s".formatted(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion()), e); |
| 77 | + return new DependencyPomResponsePair(dependency, PomResponse.empty()); |
| 78 | + } |
| 79 | + }) |
| 80 | + .collect(Collectors.toMap(DependencyPomResponsePair::dependency, DependencyPomResponsePair::pomResponse)); |
| 81 | + |
| 82 | + for (Dependency dependency : pomResponses.keySet()) { |
| 83 | + final PomResponse pomResponse = pomResponses.get(dependency); |
| 84 | + final String projectUrl = pomResponse.url(); |
| 85 | + final String projectScmUrl = pomResponse.scmUrl(); |
| 86 | + |
| 87 | + // First try to get the Github owner and repo from the url otherwise try to get it from the SCM url |
| 88 | + var guess = projectUrl != null ? githubGuesser.guess(projectUrl) : null; |
| 89 | + if (guess == null && projectScmUrl != null) { |
| 90 | + guess = githubGuesser.guess(projectScmUrl); |
| 91 | + } |
| 92 | + |
| 93 | + if (guess == null) { |
| 94 | + getLog().warn("Could not guess Github owner and repo for %s".formatted(dependency.getManagementKey())); |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + Collected collected; |
| 99 | + try { |
| 100 | + collected = githubCollector.collect(guess.owner(), guess.repo()); |
| 101 | + } catch (ExecutionException | InterruptedException e) { |
| 102 | + throw new MojoFailureException("Failed to collect Github data for %s".formatted(dependency.getManagementKey()), e); |
| 103 | + } |
| 104 | + |
| 105 | + double score = maintenanceEvaluator.evaluateCommitsFrequency(collected); |
| 106 | + getLog().info("Maintenance score for %s: %f".formatted(dependency.getManagementKey(), score)); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private record DependencyPomResponsePair(Dependency dependency, PomResponse pomResponse) { |
| 111 | + } |
| 112 | +} |
0 commit comments