From 6677673df0a7c811cd563f93e2cef3065eb51ce3 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Wed, 10 May 2023 15:58:27 +0200 Subject: [PATCH] Provide a fallback if the serialization is broken. See https://github.com/jenkinsci/code-coverage-api-plugin/issues/647 --- .../metrics/steps/CoverageBuildAction.java | 46 +++++++++++++++---- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/plugin/src/main/java/io/jenkins/plugins/coverage/metrics/steps/CoverageBuildAction.java b/plugin/src/main/java/io/jenkins/plugins/coverage/metrics/steps/CoverageBuildAction.java index 0edee66a1..3d6374e94 100644 --- a/plugin/src/main/java/io/jenkins/plugins/coverage/metrics/steps/CoverageBuildAction.java +++ b/plugin/src/main/java/io/jenkins/plugins/coverage/metrics/steps/CoverageBuildAction.java @@ -9,6 +9,8 @@ import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; +import java.util.logging.Level; +import java.util.logging.Logger; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -24,6 +26,7 @@ import edu.hm.hafner.util.FilteredLog; import edu.hm.hafner.util.VisibleForTesting; import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import org.kohsuke.stapler.StaplerProxy; import hudson.Functions; @@ -73,28 +76,32 @@ public final class CoverageBuildAction extends BuildAction implements Stap private final List projectValues; /** The delta of this build's coverages with respect to the reference build. */ - private final NavigableMap difference; + private NavigableMap difference; /** The coverages filtered by modified lines of the associated change request. */ private final List modifiedLinesCoverage; - /** The delta of the coverages of the associated change request with respect to the reference build. */ - private final NavigableMap modifiedLinesCoverageDifference; + /** The coverage delta of the associated change request with respect to the reference build. */ + private NavigableMap modifiedLinesCoverageDifference; /** The coverage of the modified lines. */ private final List modifiedFilesCoverage; /** The coverage delta of the modified lines. */ - private final NavigableMap modifiedFilesCoverageDifference; + private NavigableMap modifiedFilesCoverageDifference; /** The indirect coverage changes of the associated change request with respect to the reference build. */ private final List indirectCoverageChanges; static { CoverageXmlStream.registerConverters(XSTREAM2); - XSTREAM2.registerLocalConverter(CoverageBuildAction.class, "difference", - new MetricFractionMapConverter()); - XSTREAM2.registerLocalConverter(CoverageBuildAction.class, "modifiedLinesCoverageDifference", + registerMapConverter("difference"); + registerMapConverter("modifiedLinesCoverageDifference"); + registerMapConverter("modifiedFilesCoverageDifference"); + } + + private static void registerMapConverter(final String difference) { + XSTREAM2.registerLocalConverter(CoverageBuildAction.class, difference, new MetricFractionMapConverter()); } @@ -110,7 +117,7 @@ public final class CoverageBuildAction extends BuildAction implements Stap * @param icon * name of the icon that should be used in actions and views * @param result - * the coverage tree as result to persist with this action + * the coverage tree as a result to persist with this action * @param qualityGateResult * status of the quality gates * @param log @@ -134,7 +141,7 @@ public CoverageBuildAction(final Run owner, final String id, final String * @param icon * name of the icon that should be used in actions and views * @param result - * the coverage tree as result to persist with this action + * the coverage tree as a result to persist with this action * @param qualityGateResult * status of the quality gates * @param log @@ -205,8 +212,27 @@ public CoverageBuildAction(final Run owner, final String id, final String } } + @Override @SuppressFBWarnings(value = "CRLF_INJECTION_LOGS", justification = "getOwner().toString() is under our control") + protected Object readResolve() { + super.readResolve(); + if (difference == null) { + difference = new TreeMap<>(); + Logger.getLogger(CoverageBuildAction.class.getName()).log(Level.FINE, "Difference serialization was null: " + getOwner().getDisplayName()); + } + if (modifiedLinesCoverageDifference == null) { + modifiedLinesCoverageDifference = new TreeMap<>(); + Logger.getLogger(CoverageBuildAction.class.getName()).log(Level.FINE, "Modified lines serialization was null: " + getOwner().getDisplayName()); + } + if (modifiedFilesCoverageDifference == null) { + modifiedFilesCoverageDifference = new TreeMap<>(); + Logger.getLogger(CoverageBuildAction.class.getName()).log(Level.FINE, "Modified files serialization was null: " + getOwner().getDisplayName()); + } + + return this; + } + /** - * Returns the actual name of the tool. If no user defined name is given, then the default name is returned. + * Returns the actual name of the tool. If no user-defined name is given, then the default name is returned. * * @return the name */