Skip to content

Commit

Permalink
Merge pull request #547 from jonesbusy/feature/allow-openrewrite-outd…
Browse files Browse the repository at this point in the history
…ated-plugins

Allow openrewrite outdated plugins
  • Loading branch information
jonesbusy authored Jan 1, 2025
2 parents 97699ea + abd9373 commit 14333ce
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ public class MavenInvoker {
/**
* Invoke a goal on a plugin
* @param plugin The plugin to run the goal on
* @param goal The goal to run. For example, "clean"
* @param goals The goals to run. For example, "clean"
*/
public void invokeGoal(Plugin plugin, String goal) {
LOG.debug("Running {} phase for plugin {}", goal, plugin.getName());
public void invokeGoal(Plugin plugin, String... goals) {
LOG.debug("Running {} phase for plugin {}", goals, plugin.getName());
LOG.debug(
"Running maven on directory {}",
plugin.getLocalRepository().toAbsolutePath().toFile());
invokeGoals(plugin, goal);
invokeGoals(plugin, goals);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.jenkins.tools.pluginmodernizer.core.extractor.PluginMetadata;
import io.jenkins.tools.pluginmodernizer.core.github.GHService;
import io.jenkins.tools.pluginmodernizer.core.model.JDK;
import io.jenkins.tools.pluginmodernizer.core.model.ModernizerException;
import io.jenkins.tools.pluginmodernizer.core.model.Plugin;
import io.jenkins.tools.pluginmodernizer.core.model.PluginProcessingException;
import io.jenkins.tools.pluginmodernizer.core.utils.PluginService;
Expand Down Expand Up @@ -228,7 +229,7 @@ private void process(Plugin plugin) {

// Collect metadata and move metadata from the target directory of the plugin to the common cache
if (!plugin.hasMetadata() || config.isFetchMetadataOnly()) {
collectMetadata(plugin);
collectMetadata(plugin, true);

} else {
LOG.debug("Metadata already computed for plugin {}. Using cached metadata.", plugin.getName());
Expand All @@ -253,7 +254,7 @@ private void process(Plugin plugin) {

// Retry to collect metadata after remediation to get up-to-date results
if (!config.isFetchMetadataOnly()) {
collectMetadata(plugin);
collectMetadata(plugin, true);
}
}

Expand All @@ -267,6 +268,12 @@ private void process(Plugin plugin) {
}

// Run OpenRewrite
if (plugin.getMetadata().getJdks().stream().allMatch(jdk -> jdk.equals(JDK.JAVA_8))) {
LOG.info("Plugin support only Java 8. Need a first compile to general classes");
plugin.verifyWithoutTests(mavenInvoker, JDK.JAVA_8);
} else {
plugin.withJDK(JDK.min(plugin.getMetadata().getJdks()));
}
plugin.runOpenRewrite(mavenInvoker);
if (plugin.hasErrors()) {
LOG.warn(
Expand All @@ -292,7 +299,7 @@ private void process(Plugin plugin) {
if (!config.isFetchMetadataOnly()) {
plugin.withJDK(JDK.JAVA_17);
plugin.clean(mavenInvoker);
collectMetadata(plugin);
collectMetadata(plugin, false);
LOG.debug(
"Plugin {} metadata after modernization: {}",
plugin.getName(),
Expand Down Expand Up @@ -325,8 +332,34 @@ private void process(Plugin plugin) {
* Collect metadata for a plugin
* @param plugin The plugin
*/
private void collectMetadata(Plugin plugin) {
plugin.collectMetadata(mavenInvoker);
private void collectMetadata(Plugin plugin, boolean retryAfterFirstCompile) {
LOG.debug("Collecting metadata for plugin {}... Please be patient", plugin.getName());
plugin.withJDK(JDK.JAVA_17);
try {
plugin.collectMetadata(mavenInvoker);
if (plugin.hasErrors()) {
plugin.raiseLastError();
}
} catch (ModernizerException e) {
if (retryAfterFirstCompile) {
plugin.removeErrors();
LOG.warn(
"Failed to collect metadata for plugin {}. Will retry after a first compile using lowest JDK",
plugin.getName());
plugin.verifyWithoutTests(mavenInvoker, JDK.JAVA_8);
if (plugin.hasErrors()) {
LOG.debug(
"Plugin {} failed to compile with JDK 8. Skipping metadata collection after retry",
plugin.getName());
plugin.raiseLastError();
}
plugin.withJDK(JDK.JAVA_17);
plugin.collectMetadata(mavenInvoker);
} else {
LOG.info("Failed to collect metadata for plugin {}. Not retrying.", plugin.getName());
throw e;
}
}
plugin.copyMetadata(cacheManager);
plugin.loadMetadata(cacheManager);
plugin.enrichMetadata(pluginService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,13 @@ public void raiseLastError() throws PluginProcessingException {
throw errors.get(errors.size() - 1);
}

/**
* Remove all errors from the plugin
*/
public void removeErrors() {
errors.clear();
}

/**
* Add a tag to the plugin
* @param tag Tag to add
Expand Down Expand Up @@ -489,7 +496,7 @@ public void clean(MavenInvoker maven) {
}

/**
* Execute maven compile on this plugin
* Execute maven compile on this plugin. Compile is skipped if only metadata is required
* @param maven The maven invoker instance
*/
public void compile(MavenInvoker maven) {
Expand All @@ -507,6 +514,21 @@ public void compile(MavenInvoker maven) {
}
}

/**
* Verify the plugin without tests using the given maven invoker and JDK.
* This is useful to run recipes on very outdated plugin
* @param maven The maven invoker instance
* @param jdk The JDK to use
*/
public void verifyWithoutTests(MavenInvoker maven, JDK jdk) {
LOG.info("Verifying plugin without tests {} using with JDK {} ... Please be patient", name, jdk.getMajor());
this.withJDK(jdk);
maven.invokeGoal(this, "verify", "-DskipTests");
if (!hasErrors()) {
LOG.info("Done");
}
}

/**
* Execute maven verify on this plugin
* @param maven The maven invoker instance
Expand Down Expand Up @@ -572,7 +594,7 @@ public void collectMetadata(MavenInvoker maven) {
// Static parse of the pom file and check for pattern preventing minimal build
Path pom = getLocalRepository().resolve("pom.xml");
if (!getLocalRepository().resolve("target").toFile().mkdir()) {
LOG.debug("Failed to create target directory for plugin {}", name);
LOG.trace("Failed to create target directory for plugin {}", name);
}
Document document = staticPomParse(pom);

Expand All @@ -598,6 +620,7 @@ public void collectMetadata(MavenInvoker maven) {
* @param maven The maven invoker instance
*/
public void runOpenRewrite(MavenInvoker maven) {
withJDK(JDK.JAVA_17);
if (config.isFetchMetadataOnly()) {
LOG.info("Skipping OpenRewrite recipe application for plugin {} as only metadata is required", name);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ recipeList:
- io.jenkins.tools.pluginmodernizer.RemoveExtraMavenProperties
- io.jenkins.tools.pluginmodernizer.UpgradeBomVersion
- io.jenkins.tools.pluginmodernizer.MigrateToJenkinsBaseLineProperty
- org.openrewrite.java.RemoveUnusedImports
---
type: specs.openrewrite.org/v1beta/recipe
name: io.jenkins.tools.pluginmodernizer.SetupDependabot
Expand Down

0 comments on commit 14333ce

Please sign in to comment.