Skip to content

Commit

Permalink
Updates SCM URLs in POM files from git:// to https:// protocol. (#560)
Browse files Browse the repository at this point in the history
* 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
nagu165 authored Jan 4, 2025
1 parent 22c6248 commit f1ccf6f
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 0 deletions.
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,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 plugin-modernizer-core/src/main/jte/pr-title-UpdateScmUrl.jte
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,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>
"""));
}
}

0 comments on commit f1ccf6f

Please sign in to comment.