Skip to content

Commit

Permalink
Merge pull request #310 from jonesbusy/feature/remediation
Browse files Browse the repository at this point in the history
Add (empty) function to remediate a precondition error of a plugin
  • Loading branch information
jonesbusy authored Oct 9, 2024
2 parents 30240d9 + 50e621f commit 76c6b20
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.jenkins.tools.pluginmodernizer.core.model.PreconditionError;
import java.io.Serializable;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -116,7 +117,7 @@ public boolean hasFlag(MetadataFlag flag) {
}

public Set<PreconditionError> getErrors() {
return errors;
return Collections.unmodifiableSet(errors);
}

public void setErrors(Set<PreconditionError> errors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,29 @@ private void process(Plugin plugin) {
LOG.debug("Metadata already computed for plugin {}. Using cached metadata.", plugin.getName());
}

// Abort here if we have errors
// Try to remediate precondition errors
if (plugin.hasPreconditionErrors()) {
plugin.getPreconditionErrors().forEach(preconditionError -> {
if (preconditionError.remediate(plugin)) {
plugin.removePreconditionError(preconditionError);
LOG.info(
"Precondition error {} was remediated for plugin {}",
preconditionError,
plugin.getName());
} else {
LOG.info(
"Precondition error {} was not remediated for plugin {}",
preconditionError,
plugin.getName());
}
});

// Retry to collect metadata after remediation to get up-to-date results
plugin.collectMetadata(mavenInvoker);
plugin.moveMetadata(cacheManager);
}

// Check if we still have errors and abort if not remediation is possible
if (plugin.hasErrors() || plugin.hasPreconditionErrors()) {
plugin.addPreconditionErrors(plugin.getMetadata());
LOG.info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.net.URI;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -241,6 +242,14 @@ public boolean hasPreconditionErrors() {
&& !metadata.getErrors().isEmpty();
}

/**
* Get the precondition errors of the plugin
* @return Set of precondition errors
*/
public Set<PreconditionError> getPreconditionErrors() {
return Collections.unmodifiableSet(metadata.getErrors());
}

/**
* Add precondition errors to the plugin errors
*/
Expand All @@ -251,12 +260,25 @@ public void addPreconditionErrors(PluginMetadata metadata) {
metadata.getErrors().forEach(error -> addError(error.getError()));
}

/**
* Remove a precondition error from the metadata errors
* @param preconditionError Precondition error to remove
*/
public void removePreconditionError(PreconditionError preconditionError) {
if (metadata == null) {
return;
}
metadata.setErrors(metadata.getErrors().stream()
.filter(error -> !error.equals(preconditionError))
.collect(Collectors.toSet()));
}

/**
* Get the errors of the plugin
* @return List of errors
*/
public List<PluginProcessingException> getErrors() {
return errors;
return Collections.unmodifiableList(errors);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.jenkins.tools.pluginmodernizer.core.model;

import java.util.function.BiFunction;
import java.util.function.Function;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import org.w3c.dom.Document;
Expand All @@ -20,6 +21,7 @@ public enum PreconditionError {
(document, xpath) -> {
return document == null;
},
plugin -> false, // No remediation function available if pom is missing
"No pom file found"),

/**
Expand All @@ -40,6 +42,11 @@ public enum PreconditionError {
return false;
}
},
plugin -> {
// TODO: Implement remediation function (See

Check warning on line 46 in plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/model/PreconditionError.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: Implement remediation function (See
// https://github.com/jenkinsci/plugin-modernizer-tool/pull/307)
return false;
},
"Found non-https repository URL in pom file preventing maven older than 3.8.1"),

/**
Expand All @@ -61,6 +68,11 @@ public enum PreconditionError {
return false;
}
},
plugin -> {
// TODO: Implement remediation function (See

Check warning on line 72 in plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/model/PreconditionError.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: Implement remediation function (See
// https://github.com/jenkinsci/plugin-modernizer-tool/pull/307)
return false;
},
"Found older Java version in pom file preventing using recent Maven older than 3.9.x"),

/**
Expand All @@ -78,13 +90,24 @@ public enum PreconditionError {
return false;
}
},
plugin -> {
// TODO: Implement remediation function (See

Check warning on line 94 in plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/model/PreconditionError.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: Implement remediation function (See
// https://github.com/jenkinsci/plugin-modernizer-tool/pull/307)
return false;
},
"Missing relative path in pom file preventing parent download");

/**
* Predicate to check if the flag is applicable for the given Document and XPath
*/
private final BiFunction<Document, XPath, Boolean> isApplicable;

/**
* Remediation function to fix the error transforming plugin before OpenRewrite
* This function should return true if the remediation was successful, false otherwise
*/
private final Function<Plugin, Boolean> remediation;

/**
* Error message
*/
Expand All @@ -94,8 +117,10 @@ public enum PreconditionError {
* Constructor
* @param isApplicable Predicate to check if the flag is applicable for the given XML document
*/
PreconditionError(BiFunction<Document, XPath, Boolean> isApplicable, String error) {
PreconditionError(
BiFunction<Document, XPath, Boolean> isApplicable, Function<Plugin, Boolean> remediation, String error) {
this.isApplicable = isApplicable;
this.remediation = remediation;
this.error = error;
}

Expand All @@ -109,6 +134,14 @@ public boolean isApplicable(Document Document, XPath xpath) {
return isApplicable.apply(Document, xpath);
}

/**
* Remediate the error for the given plugin
* @param plugin the plugin to remediate
*/
public boolean remediate(Plugin plugin) {
return remediation.apply(plugin);
}

/**
* Get the error message
* @return the error message
Expand Down

0 comments on commit 76c6b20

Please sign in to comment.