Skip to content

Commit

Permalink
Merge pull request #759 from CodexRaunak/feature/Scanning-recipe-for-…
Browse files Browse the repository at this point in the history
…SplitPlugins

Scanning recipe to add dependency on detached plugin
  • Loading branch information
jonesbusy authored Feb 3, 2025
2 parents dc62694 + 289c136 commit 8817219
Show file tree
Hide file tree
Showing 5 changed files with 907 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package io.jenkins.tools.pluginmodernizer.core.model;

/**
* # Note that all split plugins between and including matrix-auth and jaxb incorrectly use the first
* # core release without the plugin's functionality when they should use the immediately prior release.
* # Fixing these retroactively won't help, as the difference only matters to those specific versions.
*/
import java.util.Set;

/**
* Detached plugins.
*/
public enum DetachedPlugins {
MAVEN_PLUGIN(
"maven-plugin",
"1.296",
"1.296",
"org.jenkins-ci.main",
Set.of("hudson.maven"),
Set.of(
"hudson.maven.MavenModule",
"hudson.maven.MavenModuleSet",
"hudson.maven.MavenModuleSetBuild",
"hudson.maven.MavenBuild.java")),
SUBVERSION("subversion", "1.310", "1.0", "org.jenkins-ci.plugins", Set.of(), Set.of("hudson.scm.SubversionSCM")),
CVS("cvs", "1.340", "0.1", "org.jenkins-ci.plugins", Set.of(), Set.of("hudson.scm.CVSSCM")),
ANT("ant", "1.430", "1.0", "org.jenkins-ci.plugins", Set.of(), Set.of("hudson.tasks.Ant")),
JAVADOC("javadoc", "1.430", "1.0", "org.jenkins-ci.plugins", Set.of(), Set.of("hudson.tasks.JavadocArchiver")),
EXTERNAL_MONITOR_JOB(
"external-monitor-job",
"1.467",
"1.0",
"org.jenkins-ci.plugins",
Set.of(),
Set.of("hudson.model.ExternalJob", "hudson.model.ExternalRun")),
LDAP(
"ldap",
"1.467",
"1.0",
"org.jenkins-ci.plugins",
Set.of(),
Set.of("hudson.security.LDAPSecurityRealm", "hudson.security.GeneralizedTime")),
PAM_AUTH(
"pam-auth", "1.467", "1.0", "org.jenkins-ci.plugins", Set.of(), Set.of("hudson.security.PAMSecurityRealm")),
MAILER("mailer", "1.493", "1.2", "org.jenkins-ci.plugins", Set.of(), Set.of("hudson.tasks.Mailer")),
MATRIX_AUTH(
"matrix-auth",
"1.535",
"1.0.2",
"org.jenkins-ci.plugins",
Set.of(),
Set.of(
"hudson.security.GlobalMatrixAuthorizationStrategy",
"hudson.security.ProjectMatrixAuthorizationStrategy",
"hudson.security.AuthorizationMatrixProperty")),
ANTISAMY_MARKUP_FORMATTER(
"antisamy-markup-formatter",
"1.553",
"1.0",
"org.jenkins-ci.plugins",
Set.of(),
Set.of("hudson.markup.RawHtmlMarkupFormatter")),
MATRIX_PROJECT(
"matrix-project",
"1.561",
"1.0",
"org.jenkins-ci.plugins",
Set.of("hudson.matrix"),
Set.of("hudson.matrix.MatrixProject")),
JUNIT(
"junit",
"1.577",
"1.0",
"org.jenkins-ci.plugins",
Set.of(),
Set.of("hudson.tasks.junit.JUnitResultArchiver")),
BOUNCYCASTLE_API(
"bouncycastle-api",
"2.16",
"2.16.0",
"org.jenkins-ci.plugins",
Set.of("jenkins.bouncycastle.api"),
Set.of("jenkins.bouncycastle.api.BouncyCastlePlugin")),
COMMAND_LAUNCHER(
"command-launcher",
"2.86",
"1.0",
"org.jenkins-ci.plugins",
Set.of(),
Set.of("hudson.slaves.CommandLauncher", "hudson.slaves.CommandConnector")),
JDK_TOOL("jdk-tool", "2.112", "1.0", "org.jenkins-ci.plugins", Set.of(), Set.of("hudson.tools.JDKInstaller")),
JAXB("jaxb", "2.163", "2.3.0", "io.jenkins.plugins", Set.of(), Set.of("javax.xml.bind.JAXBContext")),
TRILEAD_API(
"trilead-api",
"2.184",
"1.0.4",
"org.jenkins-ci.plugins",
Set.of(),
Set.of("com.trilead.ssh2.Connection", "com.trilead.ssh2.Session")),
SSHD(
"sshd",
"2.281",
"3.236.ved5e1b_cb_50b_2",
"org.jenkins-ci.modules",
Set.of(),
Set.of("org.jenkinsci.main.modules.sshd.SSHD")),
JAVAX_ACTIVATION_API(
"javax-activation-api",
"2.330",
"1.2.0-2",
"io.jenkins.plugins",
Set.of(),
Set.of("javax.activation.DataHandler")),
JAVAX_MAIL_API(
"javax-mail-api",
"2.330",
"1.6.2-5",
"io.jenkins.plugins",
Set.of(),
Set.of(
"jenkins.plugins.javax.activation.CommandMapInitializer",
"jenkins.plugins.javax.activation.FileTypeMapInitializer")),
INSTANCE_IDENTITY(
"instance-identity",
"2.356",
"3.1",
"org.jenkins-ci.modules",
Set.of("org.jenkinsci.main.modules.instance_identity"),
Set.of("org.jenkinsci.main.modules.instance_identity.InstanceIdentity")),
;

private final String pluginId;
private final String lastCoreRelease;
private final String impliedVersion;
private final String groupId;
private final Set<String> packageName;
private final Set<String> classNames;

DetachedPlugins(
String pluginId,
String lastCoreRelease,
String impliedVersion,
String groupId,
Set<String> packageName,
Set<String> classNames) {
this.pluginId = pluginId;
this.lastCoreRelease = lastCoreRelease;
this.impliedVersion = impliedVersion;
this.groupId = groupId;
this.packageName = packageName;
this.classNames = classNames;
}

public String getPluginId() {
return pluginId;
}

public String getLastCoreRelease() {
return lastCoreRelease;
}

public String getImpliedVersion() {
return impliedVersion;
}

public String getGroupId() {
return groupId;
}

public Set<String> getPackageName() {
return packageName;
}

public Set<String> getClassNames() {
return classNames;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package io.jenkins.tools.pluginmodernizer.core.recipes;

import io.jenkins.tools.pluginmodernizer.core.model.DetachedPlugins;
import java.util.HashSet;
import java.util.Set;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.ScanningRecipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.tree.J;
import org.openrewrite.maven.AddDependency;
import org.openrewrite.maven.MavenIsoVisitor;
import org.openrewrite.xml.tree.Xml;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AddDetachedPluginDependency extends ScanningRecipe<Set<String>> {

/**
* Logger
*/
private static final Logger LOG = LoggerFactory.getLogger(AddDetachedPluginDependency.class);

/**
* The jenkins version.
*/
@Option(displayName = "Version", description = "Jenkins version.", example = "2.440.3")
String jenkinsVersion;

/**
* Constructor.
* @param jenkinsVersion The Jenkins version.
*/
public AddDetachedPluginDependency(String jenkinsVersion) {
this.jenkinsVersion = jenkinsVersion;
}

@Override
public String getDisplayName() {
return "Add missing detached plugins as dependencies";
}

@Override
public String getDescription() {
return "Detects usage of detached plugins and automatically adds them to pom.xml.";
}

@Override
public Set<String> getInitialValue(ExecutionContext ctx) {
return new HashSet<>();
}

/**
* Detect usage of detached plugins.
*/
@Override
public TreeVisitor<?, ExecutionContext> getScanner(Set<String> acc) {
return new JavaIsoVisitor<>() {
@Override
public J.Import visitImport(J.Import importStmt, ExecutionContext ctx) {
String importedClass = importStmt.getTypeName();
String importedPackage = importedClass.substring(0, importedClass.lastIndexOf('.'));

LOG.info("Detected import: {}", importedClass);
for (DetachedPlugins plugin : DetachedPlugins.values()) {
if (plugin.getPackageName().contains(importedPackage)
|| plugin.getClassNames().contains(importedClass)) {
// Only add if jenkins version past lastCoreRelease
if (new ComparableVersion(jenkinsVersion)
.compareTo(new ComparableVersion(plugin.getLastCoreRelease()))
> 0) {
LOG.info("Detected usage of detached plugin: {}", plugin.getPluginId());
acc.add(plugin.getPluginId());
}
}
}
return super.visitImport(importStmt, ctx);
}
};
}

/**
* Add dependencies to pom.xml if they were detected.
*/
@Override
public TreeVisitor<?, ExecutionContext> getVisitor(Set<String> acc) {
return new MavenIsoVisitor<>() {
@Override
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
for (String pluginId : acc) {
DetachedPlugins plugin =
DetachedPlugins.valueOf(pluginId.toUpperCase().replace("-", "_"));
document = (Xml.Document) new AddDependency(
plugin.getGroupId(),
plugin.getPluginId(),
"RELEASE",
null,
null,
null,
null,
null,
null,
null,
null,
null)
.getVisitor()
.visitNonNull(document, ctx);
}
LOG.info("Document after adding dependency: {}", document.printAll());
return super.visitDocument(document, ctx);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,8 @@ recipeList:
minimumVersion: 2.346.3
- io.jenkins.tools.pluginmodernizer.core.recipes.UpgradeJenkinsTestHarnessVersion:
jenkinsVersion: 2.346.1
- io.jenkins.tools.pluginmodernizer.core.recipes.AddDetachedPluginDependency:
jenkinsVersion: 2.346.3
- io.jenkins.tools.pluginmodernizer.RemoveDependencyVersionOverride
- io.jenkins.tools.pluginmodernizer.core.recipes.code.ReplaceRemovedSSHLauncherConstructor
- io.jenkins.tools.pluginmodernizer.RemoveExtraMavenProperties
Expand Down
Loading

0 comments on commit 8817219

Please sign in to comment.