Skip to content

Commit

Permalink
Merge pull request #550 from jonesbusy/feature/remove-relative-path-p…
Browse files Browse the repository at this point in the history
…recondition

Remove relativePath precondition
  • Loading branch information
jonesbusy authored Jan 1, 2025
2 parents 14333ce + b5d2ef7 commit ca4cedd
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,38 +97,7 @@ public enum PreconditionError {
plugin.withoutErrors();
return true;
},
"Found older Java version in pom file preventing using recent Maven older than 3.9.x"),

/**
* If the plugin has missing relative path preventing modernization
*/
MISSING_RELATIVE_PATH(
(document, xpath) -> {
try {
Double parentRelativePath = (Double) xpath.evaluate(
"count(//*[local-name()='project']/*[local-name()='parent']/*[local-name()='relativePath'])",
document,
XPathConstants.NUMBER);
return parentRelativePath == null || parentRelativePath.equals(0.0);
} catch (Exception e) {
return false;
}
},
plugin -> {
try {
PomModifier pomModifier = new PomModifier(
plugin.getLocalRepository().resolve("pom.xml").toString());
pomModifier.addRelativePath();
pomModifier.savePom(
plugin.getLocalRepository().resolve("pom.xml").toString());
plugin.withoutErrors();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
},
"Missing relative path in pom file preventing parent download");
"Found older Java version in pom file preventing using recent Maven older than 3.9.x");

/**
* Predicate to check if the flag is applicable for the given Document and XPath
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.jenkins.tools.pluginmodernizer.core.recipes;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.maven.MavenIsoVisitor;
import org.openrewrite.xml.tree.Content;
import org.openrewrite.xml.tree.Xml;

/**
* Ensure the parent pom has a relativePath set to disable local resolution.
*/
public class EnsureRelativePath extends Recipe {
@Override
public String getDisplayName() {
return "Ensure the parent pom has a relativePath set to disable local resolution";
}

@Override
public String getDescription() {
return "Ensure the parent pom has a relativePath set to disable local resolution.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new MavenIsoVisitor<>() {
@Override
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
if (isParentTag()) {
Xml.Tag relativePathTag = Xml.Tag.build("<relativePath />");
if (tag.getContent() == null) {
return tag;
}
List<Content> contents = new ArrayList<>(tag.getContent());
// Skip if relativePath is already present
if (contents.stream()
.anyMatch(content -> content instanceof Xml.Tag
&& ((Xml.Tag) content).getName().equals("relativePath"))) {
return tag;
}
Optional<Xml.Tag> maybeChild = tag.getChild("artifactId");
if (maybeChild.isEmpty()) {
return tag;
}
relativePathTag =
relativePathTag.withPrefix(maybeChild.get().getPrefix());

contents.add(relativePathTag);
return tag.withContent(contents);
}
return super.visitTag(tag, ctx);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ displayName: Upgrade to the next major parent version (5.X) requiring Jenkins 2.
description: Upgrade to the next major parent version (5.X) requiring Jenkins 2.479 and Java 17.
tags: ['dependencies']
recipeList:
- io.jenkins.tools.pluginmodernizer.core.recipes.EnsureRelativePath
- org.openrewrite.maven.UpgradeParentVersion:
groupId: org.jenkins-ci.plugins
artifactId: plugin
Expand Down Expand Up @@ -376,6 +377,7 @@ displayName: Upgrade to latest recommended core version and ensure the bom is ma
description: Upgrade to latest recommended core version and ensure the bom is matching the core version.
tags: ['developer']
recipeList:
- io.jenkins.tools.pluginmodernizer.core.recipes.EnsureRelativePath
- io.jenkins.tools.pluginmodernizer.UpgradeParentVersion
- io.jenkins.tools.pluginmodernizer.core.recipes.UpgradeJenkinsVersion:
minimumVersion: 2.452.4
Expand All @@ -390,6 +392,7 @@ displayName: Upgrade to latest LTS core version supporting Java 11
description: Upgrade to latest LTS core version supporting Java 11.
tags: ['developer']
recipeList:
- io.jenkins.tools.pluginmodernizer.core.recipes.EnsureRelativePath
- io.jenkins.tools.pluginmodernizer.UpgradeParentVersion
- io.jenkins.tools.pluginmodernizer.core.recipes.UpgradeJenkinsVersion:
minimumVersion: 2.462.3
Expand All @@ -404,6 +407,7 @@ displayName: Upgrade to latest LTS core version supporting Java 8
description: Upgrade to latest LTS core version supporting Java 8.
tags: ['developer']
recipeList:
- io.jenkins.tools.pluginmodernizer.core.recipes.EnsureRelativePath
- org.openrewrite.maven.UpgradeParentVersion:
groupId: org.jenkins-ci.plugins
artifactId: plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,6 @@ void upgradeToRecommendCoreVersionTest() {
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.87</version>
<relativePath />
</parent>
<groupId>io.jenkins.plugins</groupId>
<artifactId>empty</artifactId>
Expand Down Expand Up @@ -561,7 +560,6 @@ void upgradeToRecommendCoreVersionTestWithBaseline() {
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.87</version>
<relativePath />
</parent>
<groupId>io.jenkins.plugins</groupId>
<artifactId>empty</artifactId>
Expand Down Expand Up @@ -799,7 +797,6 @@ void upgradeToUpgradeToLatestJava11CoreVersion() {
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.55</version>
<relativePath />
</parent>
<groupId>io.jenkins.plugins</groupId>
<artifactId>empty</artifactId>
Expand Down Expand Up @@ -1002,7 +999,6 @@ void upgradeNextMajorParentVersionTest() {
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.87</version>
<relativePath />
</parent>
<groupId>io.jenkins.plugins</groupId>
<artifactId>empty</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package io.jenkins.tools.pluginmodernizer.core.recipes;

import static org.openrewrite.maven.Assertions.pomXml;

import org.junit.jupiter.api.Test;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

public class EnsureRelativePathTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new EnsureRelativePath());
}

@Test
void addRelativePath() {
rewriteRun(
// language=xml
pomXml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.88</version>
</parent>
<artifactId>empty</artifactId>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
</project>
""",
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.88</version>
<relativePath />
</parent>
<artifactId>empty</artifactId>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
</project>
"""));
}

@Test
void keepRelativePath() {
rewriteRun(
// language=xml
pomXml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.88</version>
<relativePath/>
</parent>
<artifactId>empty</artifactId>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
</project>
"""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,26 +168,4 @@ public void testReplaceHttpWithHttps() throws Exception {
assertTrue(url.startsWith("https://"), "URL should start with https://");
}
}

/**
* Tests the addRelativePath method of PomModifier.
*
* @throws Exception if an error occurs during the test
*/
@Test
public void testAddRelativePath() throws Exception {
PomModifier pomModifier = new PomModifier(OUTPUT_POM_PATH);
pomModifier.addRelativePath();
pomModifier.savePom(OUTPUT_POM_PATH);

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new File(OUTPUT_POM_PATH));
doc.getDocumentElement().normalize();

NodeList relativePathList = doc.getElementsByTagName("relativePath");
assertEquals(1, relativePathList.getLength(), "There should be one relativePath element");
Node relativePathNode = relativePathList.item(0);
assertTrue(relativePathNode.getTextContent().isEmpty(), "The relativePath element should be self-closing");
}
}

0 comments on commit ca4cedd

Please sign in to comment.