|
| 1 | +package io.jenkins.tools.pluginmodernizer.core.visitors; |
| 2 | + |
| 3 | +import io.jenkins.tools.pluginmodernizer.core.recipes.AddProperty; |
| 4 | +import java.util.Optional; |
| 5 | +import java.util.regex.Matcher; |
| 6 | +import java.util.regex.Pattern; |
| 7 | +import org.openrewrite.ExecutionContext; |
| 8 | +import org.openrewrite.maven.MavenIsoVisitor; |
| 9 | +import org.openrewrite.xml.ChangeTagValueVisitor; |
| 10 | +import org.openrewrite.xml.tree.Xml; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | + |
| 14 | +/** |
| 15 | + * Visitor to modify POM for incrementals support. |
| 16 | + * Transforms version structure and adds required properties. |
| 17 | + */ |
| 18 | +public class AddIncrementalsVisitor extends MavenIsoVisitor<ExecutionContext> { |
| 19 | + |
| 20 | + private static final Logger LOG = LoggerFactory.getLogger(AddIncrementalsVisitor.class); |
| 21 | + private static final Pattern VERSION_PATTERN = Pattern.compile("^(\\d+(?:\\.\\d+)*)(.*|$)"); |
| 22 | + private static final Pattern GITHUB_PATTERN = |
| 23 | + Pattern.compile("github\\.com[:/]([^/]+/[^/]+?)(?:\\.git)?$", Pattern.CASE_INSENSITIVE); |
| 24 | + |
| 25 | + @Override |
| 26 | + public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { |
| 27 | + document = super.visitDocument(document, ctx); |
| 28 | + |
| 29 | + Xml.Tag root = document.getRoot(); |
| 30 | + |
| 31 | + // Check if properties section exists |
| 32 | + Optional<Xml.Tag> propertiesTag = root.getChild("properties"); |
| 33 | + if (propertiesTag.isEmpty()) { |
| 34 | + LOG.warn("POM lacks a properties section. Cannot add incrementals properties. Skipping transformation."); |
| 35 | + return document; |
| 36 | + } |
| 37 | + |
| 38 | + // Check if already using incrementals format |
| 39 | + Optional<Xml.Tag> versionTag = root.getChild("version"); |
| 40 | + if (versionTag.isPresent()) { |
| 41 | + String currentVersion = versionTag.get().getValue().orElse(""); |
| 42 | + if (currentVersion.contains("${revision}") || currentVersion.contains("${changelist}")) { |
| 43 | + LOG.info("POM already uses incrementals version format. Skipping transformation."); |
| 44 | + return document; |
| 45 | + } |
| 46 | + |
| 47 | + // Extract revision from current version |
| 48 | + Matcher versionMatcher = VERSION_PATTERN.matcher(currentVersion); |
| 49 | + String revision = "1"; |
| 50 | + if (versionMatcher.find()) { |
| 51 | + revision = versionMatcher.group(1); |
| 52 | + } |
| 53 | + |
| 54 | + // Update version to ${revision}${changelist} format |
| 55 | + LOG.debug("Transforming version from {} to ${{revision}}${{changelist}}", currentVersion); |
| 56 | + document = (Xml.Document) new ChangeTagValueVisitor<>(versionTag.get(), "${revision}${changelist}") |
| 57 | + .visitNonNull(document, ctx); |
| 58 | + |
| 59 | + // Add properties if they don't exist |
| 60 | + document = (Xml.Document) |
| 61 | + new AddProperty("revision", revision).getVisitor().visitNonNull(document, ctx); |
| 62 | + document = (Xml.Document) |
| 63 | + new AddProperty("changelist", "-SNAPSHOT").getVisitor().visitNonNull(document, ctx); |
| 64 | + |
| 65 | + // Extract and add GitHub repo from SCM |
| 66 | + Optional<Xml.Tag> scmTag = root.getChild("scm"); |
| 67 | + String gitHubRepo = null; |
| 68 | + if (scmTag.isPresent()) { |
| 69 | + gitHubRepo = extractGitHubRepo(scmTag.get()); |
| 70 | + if (gitHubRepo != null) { |
| 71 | + LOG.debug("Adding gitHubRepo property: {}", gitHubRepo); |
| 72 | + document = (Xml.Document) new AddProperty("gitHubRepo", gitHubRepo) |
| 73 | + .getVisitor() |
| 74 | + .visitNonNull(document, ctx); |
| 75 | + |
| 76 | + // Update SCM URLs to use ${gitHubRepo} property |
| 77 | + document = updateScmUrls(document, gitHubRepo, ctx); |
| 78 | + } |
| 79 | + |
| 80 | + // Add scmTag property |
| 81 | + document = (Xml.Document) |
| 82 | + new AddProperty("scmTag", "HEAD").getVisitor().visitNonNull(document, ctx); |
| 83 | + |
| 84 | + // Refresh root tag reference to avoid stale references after document transformations |
| 85 | + root = document.getRoot(); |
| 86 | + scmTag = root.getChild("scm"); |
| 87 | + |
| 88 | + // Update SCM tag to use ${scmTag} |
| 89 | + Optional<Xml.Tag> tagTag = scmTag.isPresent() ? scmTag.get().getChild("tag") : Optional.empty(); |
| 90 | + if (tagTag.isPresent()) { |
| 91 | + document = (Xml.Document) |
| 92 | + new ChangeTagValueVisitor<>(tagTag.get(), "${scmTag}").visitNonNull(document, ctx); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Only rewrite the URL if the gitHubRepo property was successfully added |
| 97 | + if (gitHubRepo != null) { |
| 98 | + Optional<Xml.Tag> urlTag = root.getChild("url"); |
| 99 | + if (urlTag.isPresent()) { |
| 100 | + String url = urlTag.get().getValue().orElse(""); |
| 101 | + Matcher urlMatcher = GITHUB_PATTERN.matcher(url); |
| 102 | + if (urlMatcher.find()) { |
| 103 | + document = (Xml.Document) |
| 104 | + new ChangeTagValueVisitor<>(urlTag.get(), "https://github.com/${gitHubRepo}") |
| 105 | + .visitNonNull(document, ctx); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return document; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Extract GitHub repository from SCM tag |
| 116 | + */ |
| 117 | + private String extractGitHubRepo(Xml.Tag scmTag) { |
| 118 | + // Try connection tag first |
| 119 | + Optional<Xml.Tag> connectionTag = scmTag.getChild("connection"); |
| 120 | + if (connectionTag.isPresent()) { |
| 121 | + String connection = connectionTag.get().getValue().orElse(""); |
| 122 | + Matcher matcher = GITHUB_PATTERN.matcher(connection); |
| 123 | + if (matcher.find()) { |
| 124 | + return matcher.group(1); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // Fall back to developerConnection tag |
| 129 | + Optional<Xml.Tag> developerConnectionTag = scmTag.getChild("developerConnection"); |
| 130 | + if (developerConnectionTag.isPresent()) { |
| 131 | + String developerConnection = developerConnectionTag.get().getValue().orElse(""); |
| 132 | + Matcher matcher = GITHUB_PATTERN.matcher(developerConnection); |
| 133 | + if (matcher.find()) { |
| 134 | + return matcher.group(1); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // Fall back to url tag |
| 139 | + Optional<Xml.Tag> urlTag = scmTag.getChild("url"); |
| 140 | + if (urlTag.isPresent()) { |
| 141 | + String url = urlTag.get().getValue().orElse(""); |
| 142 | + Matcher matcher = GITHUB_PATTERN.matcher(url); |
| 143 | + if (matcher.find()) { |
| 144 | + return matcher.group(1); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return null; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Update SCM URLs to use ${gitHubRepo} property |
| 153 | + */ |
| 154 | + private Xml.Document updateScmUrls(Xml.Document document, String gitHubRepo, ExecutionContext ctx) { |
| 155 | + Xml.Tag root = document.getRoot(); |
| 156 | + Optional<Xml.Tag> scmTag = root.getChild("scm"); |
| 157 | + if (scmTag.isEmpty()) { |
| 158 | + return document; |
| 159 | + } |
| 160 | + |
| 161 | + // Update connection |
| 162 | + Optional<Xml.Tag> connectionTag = scmTag.get().getChild("connection"); |
| 163 | + if (connectionTag.isPresent()) { |
| 164 | + document = (Xml.Document) |
| 165 | + new ChangeTagValueVisitor<>(connectionTag.get(), "scm:git:https://github.com/${gitHubRepo}.git") |
| 166 | + .visitNonNull(document, ctx); |
| 167 | + // Refresh references after document transformation |
| 168 | + root = document.getRoot(); |
| 169 | + scmTag = root.getChild("scm"); |
| 170 | + } |
| 171 | + |
| 172 | + // Update developerConnection |
| 173 | + Optional<Xml.Tag> devConnectionTag = |
| 174 | + scmTag.isPresent() ? scmTag.get().getChild("developerConnection") : Optional.empty(); |
| 175 | + if (devConnectionTag.isPresent()) { |
| 176 | + document = (Xml.Document) |
| 177 | + new ChangeTagValueVisitor<>(devConnectionTag.get(), "scm:git:git@github.com:${gitHubRepo}.git") |
| 178 | + .visitNonNull(document, ctx); |
| 179 | + // Refresh references after document transformation |
| 180 | + root = document.getRoot(); |
| 181 | + scmTag = root.getChild("scm"); |
| 182 | + } |
| 183 | + |
| 184 | + // Update url |
| 185 | + Optional<Xml.Tag> scmUrlTag = scmTag.isPresent() ? scmTag.get().getChild("url") : Optional.empty(); |
| 186 | + if (scmUrlTag.isPresent()) { |
| 187 | + document = (Xml.Document) new ChangeTagValueVisitor<>(scmUrlTag.get(), "https://github.com/${gitHubRepo}") |
| 188 | + .visitNonNull(document, ctx); |
| 189 | + } |
| 190 | + |
| 191 | + return document; |
| 192 | + } |
| 193 | +} |
0 commit comments