Skip to content

Commit

Permalink
Merge pull request #566 from jonesbusy/feature/only-add-space-if-not-…
Browse files Browse the repository at this point in the history
…using-cd

Only add space on empty relativePath tag if still using maven release plugin
  • Loading branch information
jonesbusy authored Jan 3, 2025
2 parents 8fedf57 + 6444338 commit e2fa620
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package io.jenkins.tools.pluginmodernizer.core.recipes;

import io.jenkins.tools.pluginmodernizer.core.extractor.ArchetypeCommonFile;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.ScanningRecipe;
import org.openrewrite.SourceFile;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.maven.MavenIsoVisitor;
import org.openrewrite.xml.tree.Content;
Expand All @@ -13,43 +16,74 @@
/**
* Ensure the parent pom has a relativePath set to disable local resolution.
*/
public class EnsureRelativePath extends Recipe {
public class EnsureRelativePath extends ScanningRecipe<StringBuilder> {
@Override
public String getDisplayName() {
return "Ensure the parent pom has a relativePath set to disable local resolution";
}

@Override
public StringBuilder getInitialValue(ExecutionContext ctx) {
return new StringBuilder("<relativePath />");
}

@Override
public TreeVisitor<?, ExecutionContext> getScanner(StringBuilder tag) {
return new TreeVisitor<>() {

@Override
public Tree visit(Tree tree, ExecutionContext ctx) {
SourceFile sourceFile = (SourceFile) tree;
if (sourceFile.getSourcePath().equals(ArchetypeCommonFile.WORKFLOW_CD.getPath())) {
tag.replace(0, tag.length(), "<relativePath/>");
}
return tree;
}
};
}

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

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
public TreeVisitor<?, ExecutionContext> getVisitor(StringBuilder expectedTag) {
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());
// Replace correct relative path if present
Optional<Xml.Tag> maybeRelativePath = tag.getChild("relativePath");
Xml.Tag relativePathTag = Xml.Tag.build(expectedTag.toString())
.withPrefix(maybeChild.get().getPrefix());
if (maybeRelativePath.isPresent()) {
Xml.Tag existingRelativePath = maybeRelativePath.get();
// Skip if already correct
if (existingRelativePath
.getBeforeTagDelimiterPrefix()
.equals(relativePathTag.getBeforeTagDelimiterPrefix())) {
return tag;
}
contents.remove(maybeRelativePath.get());
contents.add(relativePathTag);
return tag.withContent(contents);
}

contents.add(relativePathTag);
return tag.withContent(contents);
// Add relative path
else {
contents.add(relativePathTag);
return tag.withContent(contents);
}
}
return super.visitTag(tag, ctx);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.jenkins.tools.pluginmodernizer.core.recipes;

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

import io.jenkins.tools.pluginmodernizer.core.extractor.ArchetypeCommonFile;
import org.junit.jupiter.api.Test;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand Down Expand Up @@ -64,6 +66,134 @@ void keepRelativePath() {
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>
"""));
}

@Test
void keepRelativePathWithCd() {
rewriteRun(
// language=yaml
yaml("""
---
""", sourceSpecs -> {
sourceSpecs.path(ArchetypeCommonFile.WORKFLOW_CD.getPath());
}),
// 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>
"""));
}

@Test
void fixRelativePath() {
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>
""",
"""
<?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 fixRelativePathIfCdEnabled() {
rewriteRun(
// language=yaml
yaml("""
---
""", sourceSpecs -> {
sourceSpecs.path(ArchetypeCommonFile.WORKFLOW_CD.getPath());
}),
// 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>
""",
"""
<?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>
Expand Down

0 comments on commit e2fa620

Please sign in to comment.