diff --git a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/model/PreconditionError.java b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/model/PreconditionError.java index 6ec7f7b2..7f2edd37 100644 --- a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/model/PreconditionError.java +++ b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/model/PreconditionError.java @@ -43,9 +43,22 @@ public enum PreconditionError { } }, plugin -> { - // TODO: Implement remediation function (See - // https://github.com/jenkinsci/plugin-modernizer-tool/pull/307) - return false; + PomModifier pomModifier = new PomModifier( + plugin.getLocalRepository().resolve("pom.xml").toString()); + try { + boolean changed = pomModifier.replaceHttpWithHttps(); + if (changed) { + pomModifier.savePom( + plugin.getLocalRepository().resolve("pom.xml").toString()); + plugin.withoutErrors(); + return true; + } else { + return false; + } + } catch (Exception e) { + e.printStackTrace(); + return false; + } }, "Found non-https repository URL in pom file preventing maven older than 3.8.1"), @@ -126,6 +139,7 @@ public enum PreconditionError { /** * Constructor + * * @param isApplicable Predicate to check if the flag is applicable for the given XML document */ PreconditionError( @@ -137,8 +151,9 @@ public enum PreconditionError { /** * Check if the flag is applicable for the given Document and XPath + * * @param Document the XML document - * @param xpath the XPath object + * @param xpath the XPath object * @return true if the flag is applicable, false otherwise */ public boolean isApplicable(Document Document, XPath xpath) { @@ -147,6 +162,7 @@ public boolean isApplicable(Document Document, XPath xpath) { /** * Remediate the error for the given plugin + * * @param plugin the plugin to remediate */ public boolean remediate(Plugin plugin) { @@ -155,6 +171,7 @@ public boolean remediate(Plugin plugin) { /** * Get the error message + * * @return the error message */ public String getError() { diff --git a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/utils/PomModifier.java b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/utils/PomModifier.java index a628b082..ec0af107 100644 --- a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/utils/PomModifier.java +++ b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/utils/PomModifier.java @@ -245,6 +245,29 @@ public void addBom(String groupId, String artifactId, String version) { dependenciesElement.appendChild(dependencyElement); } + /** + * Replaces 'http' with 'https' in repository URLs. + *
+ * This method iterates through all the url elements in the POM file and replaces
+ * any URLs that start with 'http://' with 'https://'. This is useful for ensuring
+ * that all repository URLs use a secure connection.
+ *
+ * @return false Always returns false, indicating that no specific condition is met.
+ */
+ public boolean replaceHttpWithHttps() {
+ boolean changedAtLeastOneUrl = false;
+ NodeList repositoryUrls = document.getElementsByTagName("url");
+ for (int i = 0; i < repositoryUrls.getLength(); i++) {
+ Node urlNode = repositoryUrls.item(i);
+ String url = urlNode.getTextContent();
+ if (url.startsWith("http://")) {
+ urlNode.setTextContent(url.replace("http://", "https://"));
+ changedAtLeastOneUrl = true;
+ }
+ }
+ return changedAtLeastOneUrl;
+ }
+
/**
* Saves the modified POM file to the specified output path.
*
diff --git a/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/utils/PomModifierTest.java b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/utils/PomModifierTest.java
index b0c50bcc..705c3276 100644
--- a/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/utils/PomModifierTest.java
+++ b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/utils/PomModifierTest.java
@@ -16,6 +16,8 @@
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
/**
* Test class for PomModifier.
@@ -142,4 +144,28 @@ public void testUpdateJenkinsMinimalVersion() throws Exception {
logger.info("Jenkins version found: " + jenkinsVersion);
assertEquals("2.462.2", jenkinsVersion);
}
+
+ /**
+ * Tests the replaceHttpWithHttps method of PomModifier.
+ *
+ * @throws Exception if an error occurs during the test
+ */
+ @Test
+ public void testReplaceHttpWithHttps() throws Exception {
+ PomModifier pomModifier = new PomModifier(OUTPUT_POM_PATH);
+ pomModifier.replaceHttpWithHttps();
+ pomModifier.savePom(OUTPUT_POM_PATH);
+
+ DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
+ Document doc = dBuilder.parse(new File(OUTPUT_POM_PATH));
+ doc.getDocumentElement().normalize();
+
+ NodeList repositoryUrls = doc.getElementsByTagName("url");
+ for (int i = 0; i < repositoryUrls.getLength(); i++) {
+ Node urlNode = repositoryUrls.item(i);
+ String url = urlNode.getTextContent();
+ assertTrue(url.startsWith("https://"), "URL should start with https://");
+ }
+ }
}
diff --git a/plugin-modernizer-core/src/test/resources/test-pom.xml b/plugin-modernizer-core/src/test/resources/test-pom.xml
index 48f97bf1..1914d4c1 100644
--- a/plugin-modernizer-core/src/test/resources/test-pom.xml
+++ b/plugin-modernizer-core/src/test/resources/test-pom.xml
@@ -48,14 +48,14 @@