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

Fix maven repositories http #345

Merged
merged 12 commits into from
Oct 18, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -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"),

Expand Down Expand Up @@ -126,6 +139,7 @@ public enum PreconditionError {

/**
* Constructor
*
* @param isApplicable Predicate to check if the flag is applicable for the given XML document
*/
PreconditionError(
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -155,6 +171,7 @@ public boolean remediate(Plugin plugin) {

/**
* Get the error message
*
* @return the error message
*/
public String getError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,29 @@ public void addBom(String groupId, String artifactId, String version) {
dependenciesElement.appendChild(dependencyElement);
}

/**
* Replaces 'http' with 'https' in repository URLs.
* <p>
* 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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://");
}
}
}
4 changes: 2 additions & 2 deletions plugin-modernizer-core/src/test/resources/test-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
<url>http://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
<url>http://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>

Expand Down
Loading