Skip to content

Commit

Permalink
Merge pull request #57 from openrewrite/bugfix/bom-property-order
Browse files Browse the repository at this point in the history
Make AddPluginsBom Order Independent
  • Loading branch information
sghill authored Apr 19, 2024
2 parents c02ceeb + a9b94e9 commit 17cdebb
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 28 deletions.
77 changes: 49 additions & 28 deletions src/main/java/org/openrewrite/jenkins/AddPluginsBom.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,22 @@
import org.openrewrite.xml.RemoveContentVisitor;
import org.openrewrite.xml.tree.Xml;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static java.util.Collections.emptyList;

@Value
@EqualsAndHashCode(callSuper = false)
public class AddPluginsBom extends Recipe {
private static final BomLookup LOOKUP = new BomLookup();
private static final String PLUGINS_BOM_GROUP_ID = "io.jenkins.tools.bom";
private static final String LATEST_RELEASE = "latest.release";
private static final String VERSION_METADATA_PATTERN = "\\.v[a-f0-9_]+";
private static final String PLUGIN_BOMS_KEY = "pluginBoms";
private static final String PLUGIN_BOM_NAME_KEY = "pluginBomName";

@Override
public String getDisplayName() {
Expand All @@ -57,16 +62,15 @@ public String getDescription() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new MavenIsoVisitor<ExecutionContext>() {
private boolean alreadyChanged = false;
private String bomName = "";

@Override
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
Xml.Document d = super.visitDocument(document, ctx);
Markers m = document.getMarkers();
Optional<MavenResolutionResult> maybeMavenResult = m.findFirst(MavenResolutionResult.class);
if (!maybeMavenResult.isPresent()) {
return d;
return document;
}
if (Jenkins.isJenkinsPluginPom(document) == null) {
return document;
}
MavenResolutionResult result = maybeMavenResult.get();
ResolvedPom resolvedPom = result.getPom();
Expand Down Expand Up @@ -97,6 +101,11 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
).getVisitor());
}
}
Xml.Document d = super.visitDocument(document, ctx);
String bomName = getCursor().getMessage(PLUGIN_BOM_NAME_KEY);
if (bomName == null) {
throw new IllegalStateException("Could not find jenkins.version property");
}
if (!bomFound && hasDependencyInBom) {
return (Xml.Document) new AddManagedDependency(
PLUGINS_BOM_GROUP_ID,
Expand All @@ -110,8 +119,37 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
null,
null
).getVisitor().visitNonNull(d, ctx, getCursor().getParentOrThrow());
} else if (bomFound) {
Xml.Tag exact = null;
Xml.Tag change = null;
List<Xml.Tag> pluginBoms = getCursor().getMessage(PLUGIN_BOMS_KEY, emptyList());
for (Xml.Tag bom : pluginBoms) {
String artifactId = bom.getChildValue("artifactId")
.orElseThrow(() -> new IllegalStateException("No artifactId found on bom"));
if (artifactId.equals(bomName) && exact == null) {
exact = bom;
} else if (change == null) {
change = bom;
} else {
doAfterVisit(new RemoveContentVisitor<>(bom, true));
}
}
if (exact != null && change != null) {
doAfterVisit(new RemoveContentVisitor<>(change, true));
} else if (change != null) {
String artifactId = change.getChildValue("artifactId")
.orElseThrow(() -> new IllegalStateException("No artifactId found on bom"));
doAfterVisit(new ChangeManagedDependencyGroupIdAndArtifactId(
PLUGINS_BOM_GROUP_ID,
artifactId,
PLUGINS_BOM_GROUP_ID,
bomName,
LATEST_RELEASE,
VERSION_METADATA_PATTERN
).getVisitor());
}
}
return document;
return d;
}

@Override
Expand All @@ -121,35 +159,18 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
String groupId = tag.getChildValue("groupId").orElse("");
String artifactId = tag.getChildValue("artifactId").orElse("");
if (PLUGINS_BOM_GROUP_ID.equals(groupId) && !artifactId.isEmpty()) {
if (alreadyChanged) {
doAfterVisit(new RemoveContentVisitor<>(t, true));
} else {
alreadyChanged = true;
if (!Objects.equals(bomName, artifactId)) {
doAfterVisit(new ChangeManagedDependencyGroupIdAndArtifactId(
groupId,
artifactId,
groupId,
bomName,
LATEST_RELEASE,
VERSION_METADATA_PATTERN
).getVisitor());
}
}
List<Xml.Tag> pluginBoms = getCursor().getNearestMessage(PLUGIN_BOMS_KEY, new LinkedList<>());
pluginBoms.add(t);
getCursor().putMessageOnFirstEnclosing(Xml.Document.class, PLUGIN_BOMS_KEY, pluginBoms);
}
} else if (isPropertyTag() && Objects.equals("jenkins.version", t.getName())) {
String jenkinsVersion = t.getValue().orElseThrow(() ->
new IllegalStateException("No value found for jenkins.version property tag"));
bomName = Jenkins.bomNameForJenkinsVersion(jenkinsVersion);
String bomName = Jenkins.bomNameForJenkinsVersion(jenkinsVersion);
getCursor().putMessageOnFirstEnclosing(Xml.Document.class, PLUGIN_BOM_NAME_KEY, bomName);
}
return t;
}
};
}

@Value
static class Artifact {
String groupId;
String artifactId;
}
}
79 changes: 79 additions & 0 deletions src/test/java/org/openrewrite/jenkins/AddPluginsBomTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,85 @@ void shouldFixOutdatedPluginsBom() {
));
}

@Test
void shouldFixOutdatedPluginsBomPropertiesBelowManagedDependencies() {
// language=xml
rewriteRun(pomXml(
"""
<project>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.70</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.346.x</artifactId>
<version>1706.vc166d5f429f8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>ant</artifactId>
</dependency>
</dependencies>
<properties>
<jenkins.version>2.361.4</jenkins.version>
</properties>
</project>
""".stripIndent(),
"""
<project>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.70</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.361.x</artifactId>
<version>2102.v854b_fec19c92</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>ant</artifactId>
</dependency>
</dependencies>
<properties>
<jenkins.version>2.361.4</jenkins.version>
</properties>
</project>
""".stripIndent()
));
}

@Test
void shouldFixOutdatedPluginsBomEvenIfUnused() {
// language=xml
Expand Down

0 comments on commit 17cdebb

Please sign in to comment.