-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add recipe for index.jelly and to replace a test constructor on outdated plugin #571
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
...r-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/recipes/EnsureIndexJelly.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package io.jenkins.tools.pluginmodernizer.core.recipes; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import io.jenkins.tools.pluginmodernizer.core.extractor.ArchetypeCommonFile; | ||
import io.jenkins.tools.pluginmodernizer.core.extractor.PluginMetadata; | ||
import io.jenkins.tools.pluginmodernizer.core.extractor.PomResolutionVisitor; | ||
import java.nio.file.Path; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.intellij.lang.annotations.Language; | ||
import org.openrewrite.Cursor; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.ScanningRecipe; | ||
import org.openrewrite.SourceFile; | ||
import org.openrewrite.Tree; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.maven.MavenIsoVisitor; | ||
import org.openrewrite.text.PlainText; | ||
import org.openrewrite.xml.tree.Xml; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Ensure `index.jelly` exists | ||
*/ | ||
public class EnsureIndexJelly extends ScanningRecipe<EnsureIndexJelly.ShouldCreate> { | ||
|
||
/** | ||
* Jelly file | ||
*/ | ||
@Language("xml") | ||
public static final String JELLY_FILE = | ||
""" | ||
<?jelly escape-by-default='true'?> | ||
<div> | ||
DESCRIPTION | ||
</div> | ||
"""; | ||
|
||
/** | ||
* LOGGER. | ||
*/ | ||
private static final Logger LOG = LoggerFactory.getLogger(EnsureIndexJelly.class); | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Create `index.jelly` if it doesn't exist"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Jenkins tooling [requires](https://github.com/jenkinsci/maven-hpi-plugin/pull/302) " | ||
+ "`src/main/resources/index.jelly` exists with a description."; | ||
} | ||
|
||
@Override | ||
public ShouldCreate getInitialValue(ExecutionContext ctx) { | ||
return new ShouldCreate(); | ||
} | ||
|
||
@Override | ||
@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE") | ||
public TreeVisitor<?, ExecutionContext> getScanner(ShouldCreate shouldCreate) { | ||
PluginMetadata metadata = new PluginMetadata(); | ||
return new TreeVisitor<>() { | ||
@Override | ||
public Tree visit(Tree tree, ExecutionContext ctx) { | ||
SourceFile sourceFile = (SourceFile) tree; | ||
// We visit a jelly | ||
if (sourceFile.getSourcePath().endsWith(ArchetypeCommonFile.INDEX_JELLY.getPath())) { | ||
LOG.info("Found 1 index.jelly a Will not replace it"); | ||
shouldCreate.jelliesPath.add(sourceFile.getSourcePath()); | ||
return tree; | ||
} | ||
// We visit a pom | ||
if (sourceFile.getSourcePath().endsWith(ArchetypeCommonFile.POM.getPath())) { | ||
new PomResolutionVisitor().reduce(sourceFile, metadata); | ||
if (metadata.getJenkinsVersion() == null) { | ||
LOG.info("Skipping pom {} as it is not a Jenkins plugin", sourceFile.getSourcePath()); | ||
return tree; | ||
} | ||
Path jellyPath = sourceFile | ||
.getSourcePath() | ||
.resolve("..") | ||
.resolve(ArchetypeCommonFile.INDEX_JELLY.getPath()) | ||
.normalize(); | ||
Xml.Document pom = (Xml.Document) sourceFile; | ||
DescriptionVisitor descriptionVisitor = new DescriptionVisitor(); | ||
descriptionVisitor.visitNonNull(pom, ctx); | ||
if (!descriptionVisitor.description.isEmpty()) { | ||
shouldCreate.plugins.put(jellyPath, descriptionVisitor.description); | ||
} else if (!descriptionVisitor.artifactId.isEmpty()) { | ||
shouldCreate.plugins.put(jellyPath, descriptionVisitor.artifactId); | ||
} | ||
LOG.debug(shouldCreate.toString()); | ||
} | ||
return tree; | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Visitor to extract the metadata from the POM file. | ||
*/ | ||
private static class DescriptionVisitor extends MavenIsoVisitor<ExecutionContext> { | ||
private String artifactId = ""; | ||
private String description = ""; | ||
|
||
@Override | ||
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { | ||
Cursor parent = getCursor().getParentOrThrow(); | ||
if (!(parent.getValue() instanceof Xml.Tag)) { | ||
return super.visitTag(tag, ctx); | ||
} | ||
Xml.Tag parentTag = parent.getValue(); | ||
if (!parentTag.getName().equals("project")) { | ||
return super.visitTag(tag, ctx); | ||
} | ||
if ("description".equals(tag.getName())) { | ||
description = tag.getValue().orElse(""); | ||
} else if ("artifactId".equals(tag.getName()) && !isManagedDependencyTag() && !isDependencyTag()) { | ||
artifactId = | ||
tag.getValue().orElseThrow(() -> new IllegalStateException("Expected to find an artifact id")); | ||
} | ||
return super.visitTag(tag, ctx); | ||
} | ||
} | ||
|
||
/** | ||
* Accumulator to know if the file should be created or not and with which description. | ||
*/ | ||
public static class ShouldCreate { | ||
private Map<Path, String> plugins = new HashMap<>(); | ||
private List<Path> jelliesPath = new LinkedList<>(); | ||
} | ||
|
||
@Override | ||
public Collection<SourceFile> generate(ShouldCreate shouldCreate, ExecutionContext ctx) { | ||
if (shouldCreate.plugins.isEmpty()) { | ||
return Collections.emptyList(); | ||
} | ||
List<SourceFile> generated = new LinkedList<>(); | ||
for (Map.Entry<Path, String> plugin : shouldCreate.plugins.entrySet()) { | ||
if (shouldCreate.jelliesPath.contains(plugin.getKey())) { | ||
continue; | ||
} | ||
LOG.info("Creating index.jelly at " + plugin.getKey() + " with description " + plugin.getValue()); | ||
generated.add(PlainText.builder() | ||
.sourcePath(plugin.getKey()) | ||
.text(JELLY_FILE.replace("DESCRIPTION", plugin.getValue())) | ||
.build()); | ||
} | ||
return generated; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...enkins/tools/pluginmodernizer/core/recipes/code/ReplaceRemovedSSHLauncherConstructor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package io.jenkins.tools.pluginmodernizer.core.recipes.code; | ||
|
||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.NlsRewrite; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.java.JavaTemplate; | ||
import org.openrewrite.java.search.IsLikelyTest; | ||
import org.openrewrite.java.tree.J; | ||
|
||
/** | ||
* A recipe that update the bom version to latest available. | ||
*/ | ||
public class ReplaceRemovedSSHLauncherConstructor extends Recipe { | ||
|
||
@Override | ||
public @NlsRewrite.DisplayName String getDisplayName() { | ||
return "Replace a remove SSHLauncher constructor"; | ||
} | ||
|
||
@Override | ||
public @NlsRewrite.Description String getDescription() { | ||
return "Replace a remove SSHLauncher constructor."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
// Only run on test files | ||
return Preconditions.check(new IsLikelyTest(), new UseDataBoundConstructor()); | ||
} | ||
|
||
/** | ||
* Visitor that replace the removed SSHLauncher removed constructor by the new one. | ||
*/ | ||
public static class UseDataBoundConstructor extends JavaIsoVisitor<ExecutionContext> { | ||
|
||
// We will replace by the @DataBoundConstructor | ||
JavaTemplate newConstructorTemplate = JavaTemplate.builder( | ||
"new SSHLauncher(#{any(java.lang.String)}, #{any(int)}, null)") | ||
.imports("hudson.plugins.sshslaves.SSHLauncher") | ||
.javaParser(JavaParser.fromJavaVersion().classpath("ssh-slaves")) | ||
.build(); | ||
|
||
@Override | ||
public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) { | ||
newClass = super.visitNewClass(newClass, ctx); | ||
if (newClass.getConstructorType() == null) { | ||
return newClass; | ||
} | ||
if (!newClass.getConstructorType() | ||
.getDeclaringType() | ||
.getFullyQualifiedName() | ||
.equals("hudson.plugins.sshslaves.SSHLauncher")) { | ||
return newClass; | ||
} | ||
// Replace removed 6 arguments constructor with 3 arguments constructor | ||
// See https://github.com/jenkinsci/ssh-agents-plugin/commit/f540572d7819bec840605227636de319a192bc84 | ||
if (newClass.getArguments().size() == 6) { | ||
return newConstructorTemplate.apply( | ||
updateCursor(newClass), | ||
newClass.getCoordinates().replace(), | ||
newClass.getArguments().get(0), | ||
newClass.getArguments().get(1)); | ||
} | ||
return newClass; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does that differentiate from https://github.com/openrewrite/rewrite-jenkins/blob/main/src/main/java/org/openrewrite/jenkins/CreateIndexJelly.java