diff --git a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/recipes/UpdateScmUrl.java b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/recipes/UpdateScmUrl.java new file mode 100644 index 00000000..9577a0cd --- /dev/null +++ b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/recipes/UpdateScmUrl.java @@ -0,0 +1,27 @@ +package io.jenkins.tools.pluginmodernizer.core.recipes; + +import io.jenkins.tools.pluginmodernizer.core.visitors.UpdateScmUrlVisitor; +import org.openrewrite.ExecutionContext; +import org.openrewrite.Recipe; +import org.openrewrite.TreeVisitor; + +/** + * A recipe that updates the SCM URL from git:// to https://. + */ +public class UpdateScmUrl extends Recipe { + + @Override + public String getDisplayName() { + return "Update SCM URLs from git:// to https://"; + } + + @Override + public String getDescription() { + return "Update the SCM URL in the SCM section of the POM file."; + } + + @Override + public TreeVisitor getVisitor() { + return new UpdateScmUrlVisitor(); + } +} diff --git a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/visitors/UpdateScmUrlVisitor.java b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/visitors/UpdateScmUrlVisitor.java new file mode 100644 index 00000000..1d42d612 --- /dev/null +++ b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/visitors/UpdateScmUrlVisitor.java @@ -0,0 +1,51 @@ +package io.jenkins.tools.pluginmodernizer.core.visitors; + +import java.util.ArrayList; +import java.util.List; +import org.openrewrite.ExecutionContext; +import org.openrewrite.maven.MavenIsoVisitor; +import org.openrewrite.xml.tree.Content; +import org.openrewrite.xml.tree.Xml; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class UpdateScmUrlVisitor extends MavenIsoVisitor { + + private static final Logger Log = LoggerFactory.getLogger(UpdateScmUrlVisitor.class); + + @Override + public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { + tag = super.visitTag(tag, ctx); + + if (!"scm".equals(tag.getName())) { + return tag; + } + + boolean changed = false; + List contents = tag.getContent() != null ? new ArrayList<>(tag.getContent()) : new ArrayList<>(); + + for (int i = 0; i < contents.size(); i++) { + if (!(contents.get(i) instanceof Xml.Tag)) { + continue; + } + + Xml.Tag childTag = (Xml.Tag) contents.get(i); + String updatedValue = null; + + if (childTag.getValue().isPresent()) { + String value = childTag.getValue().get(); + if ("connection".equals(childTag.getName()) && value.startsWith("scm:git:git://")) { + Log.info("Updating SCM connection from 'scm:git:git:' to 'scm:git:https:'"); + updatedValue = value.replace("scm:git:git://", "scm:git:https://"); + } + } + + if (updatedValue != null) { + contents.set(i, childTag.withValue(updatedValue)); + changed = true; + } + } + + return changed ? tag.withContent(contents) : tag; + } +} diff --git a/plugin-modernizer-core/src/main/jte/pr-title-UpdateScmUrl.jte b/plugin-modernizer-core/src/main/jte/pr-title-UpdateScmUrl.jte new file mode 100644 index 00000000..13157d5c --- /dev/null +++ b/plugin-modernizer-core/src/main/jte/pr-title-UpdateScmUrl.jte @@ -0,0 +1,5 @@ +@import io.jenkins.tools.pluginmodernizer.core.model.Plugin +@import io.jenkins.tools.pluginmodernizer.core.model.Recipe +@param Plugin plugin +@param Recipe recipe +Updates SCM URLs in POM files from git:// to https:// protocol. \ No newline at end of file diff --git a/plugin-modernizer-core/src/main/resources/META-INF/rewrite/recipes.yml b/plugin-modernizer-core/src/main/resources/META-INF/rewrite/recipes.yml index 8a862326..5051418f 100644 --- a/plugin-modernizer-core/src/main/resources/META-INF/rewrite/recipes.yml +++ b/plugin-modernizer-core/src/main/resources/META-INF/rewrite/recipes.yml @@ -8,6 +8,14 @@ recipeList: - io.jenkins.tools.pluginmodernizer.core.recipes.FetchMetadata --- type: specs.openrewrite.org/v1beta/recipe +name: io.jenkins.tools.pluginmodernizer.UpdateScmUrl +displayName: Update scm urls from git:// to https:// +description: Updates scm urls in POM files from git:// to https:// protocol as git:// protocol is deprecated by GitHub +tags: ['chore'] +recipeList: + - io.jenkins.tools.pluginmodernizer.core.recipes.UpdateScmUrl +--- +type: specs.openrewrite.org/v1beta/recipe name: io.jenkins.tools.pluginmodernizer.SetupJenkinsfile displayName: Setup the Jenkinsfile description: Add a missing Jenkinsfile to the Jenkins plugin. diff --git a/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/UpdateScmUrlTest.java b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/UpdateScmUrlTest.java new file mode 100644 index 00000000..f1ac534c --- /dev/null +++ b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/UpdateScmUrlTest.java @@ -0,0 +1,97 @@ +package io.jenkins.tools.pluginmodernizer.core.recipes; + +import static org.openrewrite.maven.Assertions.pomXml; + +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RewriteTest; + +/** + * Test for {@link UpdateScmUrl}. + */ +public class UpdateScmUrlTest implements RewriteTest { + + @Test + void updateScmUrls() { + rewriteRun( + spec -> spec.recipe(new UpdateScmUrl()), + // language=xml + pomXml( + """ + + + 4.0.0 + org.jenkins-ci.plugins + plugin + 4.88 + hpi + Empty Plugin + + 2.440.3 + + + scm:git:git://github.com/jenkinsci/your-plugin.git + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + """, + """ + + + 4.0.0 + org.jenkins-ci.plugins + plugin + 4.88 + hpi + Empty Plugin + + 2.440.3 + + + scm:git:https://github.com/jenkinsci/your-plugin.git + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + """)); + } + + @Test + void keepExistingHttpsUrls() { + rewriteRun( + spec -> spec.recipe(new UpdateScmUrl()), + // language=xml + pomXml( + """ + + + 4.0.0 + org.jenkins-ci.plugins + plugin + 4.88 + hpi + Empty Plugin + + 2.440.3 + + + scm:git:https://github.com/jenkinsci/your-plugin.git + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + """)); + } +}