Skip to content

Commit

Permalink
[JENKINS-49237] - Fix escaping of StatisticSummary
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-nenashev committed Jan 30, 2018
1 parent 4f43d8d commit 8309ec9
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 26 deletions.
12 changes: 6 additions & 6 deletions src/main/java/hudson/plugins/cppncss/AbstractBuildReport.java
Expand Up @@ -2,6 +2,7 @@

import hudson.model.AbstractBuild;
import hudson.model.HealthReportingAction;
import hudson.plugins.cppncss.parser.StatisticSummary;
import hudson.plugins.cppncss.parser.StatisticsResult;
import hudson.plugins.cppncss.parser.StatisticsTotalResult;
import hudson.plugins.helpers.AbstractBuildAction;
Expand Down Expand Up @@ -52,21 +53,20 @@ public Integer getFunctionNcssViolationThreshold(){
return functionNcssViolationThreshold;
}

/**
/**
* The summary of this build report for display on the build index page.
*
* @return
*/
public String getSummary() {
@Override
public StatisticSummary getStatisticSummary() {
AbstractBuild<?, ?> prevBuild = getBuild().getPreviousBuild();
while (prevBuild != null && prevBuild.getAction(getClass()) == null) {
prevBuild = prevBuild.getPreviousBuild();
}
if (prevBuild == null) {
return totals.toSummary();
return totals.getStatisticSummary();
} else {
AbstractBuildReport action = prevBuild.getAction(getClass());
return totals.toSummary(action.getTotals());
return totals.getStatisticSummary(action.getTotals());
}
}

Expand Down
@@ -0,0 +1,100 @@
/*
* The MIT License
*
* Copyright (c) 2018 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.plugins.cppncss.parser;

import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import javax.annotation.Nonnull;

/**
* @author Oleg Nenashev
* @since TODO
*/
@Restricted(NoExternalUse.class)
public final class FormattedStatisticSummary extends StatisticSummary {

private final long wasFunctions;
private final long wasNcss;
private final long wasCcn;
private final long functions;
private final long ncss;
private final long ccn;

public FormattedStatisticSummary(long wasCcn, long wasFunctions, long wasNcss, long ccn, long functions, long ncss) {
this.wasFunctions = wasFunctions;
this.wasNcss = wasNcss;
this.wasCcn = wasCcn;
this.functions = functions;
this.ncss = ncss;
this.ccn = ccn;
}

public FormattedStatisticSummary(long ccn, long functions, long ncss) {
this(0,0,0, ccn, functions, ncss);
}

public FormattedStatisticSummary(@Nonnull Statistic was, @Nonnull Statistic now) {
this(was.getCcn(), was.getFunctions(), was.getNcss(), now.getCcn(), now.getFunctions(), now.getNcss());
}

public long getCcn() {
return ccn;
}

public long getFunctions() {
return functions;
}

public long getNcss() {
return ncss;
}

private static String diff(long a, long b, String name) {
if (a == b) {
return "";
} else if (a < b) {
return "<li>" + name + " (+" + (b - a) + ")</li>";
} else { // if (a < b)
return "<li>" + name + " (-" + (a - b) + ")</li>";
}
}

/**
* Converts object to the HTML string
*/
@Override
public String getHtmlSummary() {
return "<ul>"
+ diff(wasCcn, ccn, "ccn")
+ diff(wasFunctions, functions, "functions")
+ diff(wasCcn, ncss, "ncss")
+ "</ul>";
}

@Override
public String toString() {
return String.format("cnn: %s, functions: %s, ncss: %s. Was: %s/%s/%s", ccn, functions, ncss, wasCcn, wasFunctions, wasNcss);
}
}
40 changes: 22 additions & 18 deletions src/main/java/hudson/plugins/cppncss/parser/Statistic.java
Expand Up @@ -345,30 +345,34 @@ public String toString() {
'}';
}

/**
* @deprecated use {@link #getStatisticSummary()}
*/
@Deprecated
public String toSummary() {
return "<ul>"
+ diff(0, ccn, "ccn")
+ diff(0, functions, "functions")
+ diff(0, ncss, "ncss")
+ "</ul>";
return getStatisticSummary().getHtmlSummary();
}

private static String diff(long a, long b, String name) {
if (a == b) {
return "";
} else if (a < b) {
return "<li>" + name + " (+" + (b - a) + ")</li>";
} else { // if (a < b)
return "<li>" + name + " (-" + (a - b) + ")</li>";
}
/**
* @since TODO
*/
public StatisticSummary getStatisticSummary() {
return new FormattedStatisticSummary(ccn, functions, ncss);
}

/**
* @deprecated Use {@link #getStatisticSummary(Statistic)}
*/
@Deprecated
public String toSummary(Statistic totals) {
return "<ul>"
+ diff(totals.ccn, ccn, "ccn")
+ diff(totals.functions, functions, "functions")
+ diff(totals.ncss, ncss, "ncss")
+ "</ul>";
return getStatisticSummary(totals).getHtmlSummary();
}

/**
* @since TODO
*/
public StatisticSummary getStatisticSummary(Statistic totals) {
return new FormattedStatisticSummary(totals, this);
}

public void set(Statistic that) {
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/hudson/plugins/cppncss/parser/StatisticSummary.java
@@ -0,0 +1,43 @@
/*
* The MIT License
*
* Copyright (c) 2018 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.plugins.cppncss.parser;

import javax.annotation.Nonnull;

/**
* Summary for {@link Statistic}.
* Implementations must have the {@code summary.jelly} file defined.
*
* @since TODO
*/
public abstract class StatisticSummary {

/**
* Gets raw HTML summary string
* @return Summary String
*/
@Nonnull
public abstract String getHtmlSummary();

}
Expand Up @@ -18,13 +18,36 @@ public void setFileTotal(Statistic fileTotal) {
public Statistic getFileTotal() {
return fileTotal;
}


/**
* @deprecated Use {@link #getStatisticSummary(StatisticsTotalResult)}
*/
@Deprecated
public String toSummary(StatisticsTotalResult statisticsTotalResult) {
return fileTotal.toSummary(statisticsTotalResult.fileTotal);
}

/**
* @deprecated Use {@link #getStatisticSummary()}
*/
@Deprecated
public String toSummary() {
return fileTotal.toSummary();
}

/**
* @since TODO
*/
public StatisticSummary getStatisticSummary(StatisticsTotalResult statisticsTotalResult) {
return fileTotal.getStatisticSummary(statisticsTotalResult.fileTotal);
}

/**
* @since TODO
*/
public StatisticSummary getStatisticSummary() {
return fileTotal.getStatisticSummary();
}

public void set(StatisticsTotalResult that) {
this.functionTotal = that.functionTotal;
Expand Down
@@ -0,0 +1,51 @@
/*
* The MIT License
*
* Copyright (c) 2018 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.plugins.cppncss.parser;

import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

/**
* @since TODO
* @deprecated Compatibility layer for external class usages.
*/
@Deprecated
@Restricted(NoExternalUse.class)
public class StringStatisticSummary extends StatisticSummary {

private final String summary;

public StringStatisticSummary(String summary) {
this.summary = summary;
}

public String getSummary() {
return summary;
}

@Override
public String getHtmlSummary() {
return summary;
}
}
16 changes: 16 additions & 0 deletions src/main/java/hudson/plugins/helpers/AbstractBuildAction.java
@@ -1,7 +1,10 @@
package hudson.plugins.helpers;

import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.HealthReportingAction;
import hudson.plugins.cppncss.parser.StatisticSummary;
import hudson.plugins.cppncss.parser.StringStatisticSummary;

import java.io.Serializable;

Expand Down Expand Up @@ -82,8 +85,21 @@ public String getGraphName() {
* Override to control the build summary detail.
*
* @return the summary string for the main build page.
* @deprecated Use {@link #getStatisticSummary()}
*/
@Deprecated
public String getSummary() {
if (Util.isOverridden(AbstractBuildAction.class, this.getClass(), "getStatisticSummary")) {
return getStatisticSummary().getHtmlSummary();
}
return "";
}

/**
* @since TODO
*/
public StatisticSummary getStatisticSummary() {
// default to the obsolete method
return new StringStatisticSummary(getSummary());
}
}
@@ -0,0 +1,5 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler">
<!-- The class is final, so we trust the implementation -->
<j:out value="${it.htmlSummary}"/>
</j:jelly>
@@ -0,0 +1,5 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler">
<!-- Legacy mode, strings may be escaped -->
${it.summary}
</j:jelly>
Expand Up @@ -3,7 +3,7 @@
xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:i="jelly:fmt">
<t:summary icon="${it.iconFileName}">
<a href="${it.urlName}">${it.displayName}</a>
${it.summary}
<st:include page="summary.jelly" it="${it.statisticSummary}"/>
</t:summary>
<j:if test="${it.floatingBoxActive}">
<div style="float:right">
Expand Down

0 comments on commit 8309ec9

Please sign in to comment.