Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates SCM URLs in POM files from git:// to https:// protocol. #560

Merged
merged 11 commits into from
Jan 4, 2025
Merged
Original file line number Diff line number Diff line change
@@ -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<?, ExecutionContext> getVisitor() {
return new UpdateScmUrlVisitor();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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<ExecutionContext> {

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<Content> 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 ("url".equals(childTag.getName()) && value.startsWith("git://")) {
Log.info("Updating SCM URL from 'git://' to 'https://'");
updatedValue = value.replace("git://", "https://");
} else 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;
}
}
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
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() {
jonesbusy marked this conversation as resolved.
Show resolved Hide resolved
rewriteRun(
spec -> spec.recipe(new UpdateScmUrl()),
// 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>
<groupId>io.jenkins.plugins</groupId>
<artifactId>empty</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>hpi</packaging>
<name>Empty Plugin</name>
<properties>
<jenkins.version>2.440.3</jenkins.version>
</properties>
<scm>
<url>git://github.com/example/repo.git</url>
jonesbusy marked this conversation as resolved.
Show resolved Hide resolved
<connection>scm:git:git://github.com/example/repo.git</connection>
</scm>
<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>
<groupId>io.jenkins.plugins</groupId>
<artifactId>empty</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>hpi</packaging>
<name>Empty Plugin</name>
<properties>
<jenkins.version>2.440.3</jenkins.version>
</properties>
<scm>
<url>https://github.com/example/repo.git</url>
<connection>scm:git:https://github.com/example/repo.git</connection>
</scm>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
</project>
"""));
}

@Test
void keepExistingHttpsUrls() {
rewriteRun(
spec -> spec.recipe(new UpdateScmUrl()),
// 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>
<groupId>io.jenkins.plugins</groupId>
<artifactId>empty</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>hpi</packaging>
<name>Empty Plugin</name>
<properties>
<jenkins.version>2.440.3</jenkins.version>
</properties>
<scm>
<url>https://github.com/example/repo.git</url>
<connection>scm:git:https://github.com/example/repo.git</connection>
</scm>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
</project>
"""));
}
}
Loading