-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates SCM URLs in POM files from git:// to https:// protocol. (#560)
* Updates scm urls in POM files from git:// to https:// protocol as git:// protocol is deprecated by GitHub * Updated the logic in UpdateScmUrlVisitor.java to allow visitor to directly manipulating the connection and scmConnection. * minor formatting * fixed errors in the UpdateScmUrlTest.java file * Fix pom file formatting issues * Modified the UpdateScmUrl recipe to apply only to 'connection'
- Loading branch information
Showing
5 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
...nizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/recipes/UpdateScmUrl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...re/src/main/java/io/jenkins/tools/pluginmodernizer/core/visitors/UpdateScmUrlVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<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 ("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; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
plugin-modernizer-core/src/main/jte/pr-title-UpdateScmUrl.jte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...r-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/UpdateScmUrlTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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( | ||
""" | ||
<?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>org.jenkins-ci.plugins</groupId> | ||
<artifactId>plugin</artifactId> | ||
<version>4.88</version> | ||
<packaging>hpi</packaging> | ||
<name>Empty Plugin</name> | ||
<properties> | ||
<jenkins.version>2.440.3</jenkins.version> | ||
</properties> | ||
<scm> | ||
<connection>scm:git:git://github.com/jenkinsci/your-plugin.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>org.jenkins-ci.plugins</groupId> | ||
<artifactId>plugin</artifactId> | ||
<version>4.88</version> | ||
<packaging>hpi</packaging> | ||
<name>Empty Plugin</name> | ||
<properties> | ||
<jenkins.version>2.440.3</jenkins.version> | ||
</properties> | ||
<scm> | ||
<connection>scm:git:https://github.com/jenkinsci/your-plugin.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>org.jenkins-ci.plugins</groupId> | ||
<artifactId>plugin</artifactId> | ||
<version>4.88</version> | ||
<packaging>hpi</packaging> | ||
<name>Empty Plugin</name> | ||
<properties> | ||
<jenkins.version>2.440.3</jenkins.version> | ||
</properties> | ||
<scm> | ||
<connection>scm:git:https://github.com/jenkinsci/your-plugin.git</connection> | ||
</scm> | ||
<repositories> | ||
<repository> | ||
<id>repo.jenkins-ci.org</id> | ||
<url>https://repo.jenkins-ci.org/public/</url> | ||
</repository> | ||
</repositories> | ||
</project> | ||
""")); | ||
} | ||
} |