Skip to content

Commit

Permalink
Remove deprecated code and refactor some classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Jan 10, 2023
1 parent a4c74b9 commit b5fb988
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 306 deletions.
10 changes: 1 addition & 9 deletions plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<job-dsl.version>1.81</job-dsl.version>
<echarts-api.version>5.4.0-1</echarts-api.version>
<xmlunit.version>2.9.0</xmlunit.version>
<plugin-util-api.version>3.0.0-rc673.91d72411f430</plugin-util-api.version>
<plugin-util-api.version>3.0.0-rc674.f33a_a_ee40321</plugin-util-api.version>
</properties>

<developers>
Expand Down Expand Up @@ -365,14 +365,6 @@
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,13 @@ public boolean hasDelta(final Baseline baseline, final Metric metric) {
public String formatDelta(final Baseline baseline, final Metric metric) {
if (baseline == Baseline.PROJECT) {
if (hasDelta(baseline, metric)) {
return FORMATTER.formatDelta(metric, difference.get(metric),
return FORMATTER.formatDelta(difference.get(metric), metric,
Functions.getCurrentLocale());
}
}
if (baseline == Baseline.MODIFIED_LINES) {
if (hasDelta(baseline, metric)) {
return FORMATTER.formatDelta(metric, changeCoverageDifference.get(metric),
return FORMATTER.formatDelta(changeCoverageDifference.get(metric), metric,
Functions.getCurrentLocale());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public Optional<? extends Value> getCoverageValue(final Job<?, ?> job) {
*/
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public DisplayColors getDisplayColors(final Job<?, ?> job, final Optional<? extends Value> coverage) {
if (hasCoverageAction(job) && coverage.isPresent()) {
if (coverage.isPresent() && hasCoverageAction(job)) {
if (coverage.get() instanceof Coverage) {
return baseline.getDisplayColors(((Coverage)coverage.get()).getCoveredPercentage().doubleValue() * 100.0, ColorProviderFactory.createDefaultColorProvider());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@
import io.jenkins.plugins.prism.SourceCodeDirectory;
import io.jenkins.plugins.prism.SourceCodeRetention;
import io.jenkins.plugins.util.AgentFileVisitor.FileVisitorResult;
import io.jenkins.plugins.util.CharsetValidation;
import io.jenkins.plugins.util.EnvironmentResolver;
import io.jenkins.plugins.util.JenkinsFacade;
import io.jenkins.plugins.util.LogHandler;
import io.jenkins.plugins.util.RunResultHandler;
import io.jenkins.plugins.util.StageResultHandler;
import io.jenkins.plugins.util.ValidationUtilities;

/**
* A pipeline {@code Step} or Freestyle or Maven {@link Recorder} that reads and parses coverage results in a build and
Expand All @@ -65,6 +65,7 @@
@SuppressWarnings("checkstyle:ClassFanOutComplexity")
public class CoverageRecorder extends Recorder {
static final String DEFAULT_ID = "coverage";
private static final ValidationUtilities VALIDATION_UTILITIES = new ValidationUtilities();

private List<CoverageTool> tools = new ArrayList<>();
private List<QualityGate> qualityGates = new ArrayList<>();
Expand Down Expand Up @@ -132,7 +133,7 @@ public List<QualityGate> getQualityGates() {
*/
@DataBoundSetter
public void setId(final String id) {
new ModelValidation().ensureValidId(id);
VALIDATION_UTILITIES.ensureValidId(id);

this.id = id;
}
Expand Down Expand Up @@ -332,7 +333,7 @@ void perform(final Run<?, ?> run, final FilePath workspace, final TaskListener t
FilteredLog log = new FilteredLog("Errors while recording code coverage:");
log.logInfo("Recording coverage results");

var validation = new ModelValidation().validateId(getId());
var validation = VALIDATION_UTILITIES.validateId(getId());
if (validation.kind != Kind.OK) {
failStage(resultHandler, logHandler, log, validation.getLocalizedMessage());
}
Expand Down Expand Up @@ -432,8 +433,6 @@ public Descriptor getDescriptor() {
@Symbol("recordCoverage")
public static class Descriptor extends BuildStepDescriptor<Publisher> {
private static final JenkinsFacade JENKINS = new JenkinsFacade();
private static final CharsetValidation CHARSET_VALIDATION = new CharsetValidation();
private static final ModelValidation MODEL_VALIDATION = new ModelValidation();

@NonNull
@Override
Expand Down Expand Up @@ -483,7 +482,7 @@ private void add(final ListBoxModel options, final SourceCodeRetention sourceCod
@SuppressWarnings("unused") // used by Stapler view data binding
public ComboBoxModel doFillSourceCodeEncodingItems(@AncestorInPath final AbstractProject<?, ?> project) {
if (JENKINS.hasPermission(Item.CONFIGURE, project)) {
return CHARSET_VALIDATION.getAllCharsets();
return VALIDATION_UTILITIES.getAllCharsets();
}
return new ComboBoxModel();
}
Expand All @@ -506,7 +505,7 @@ public FormValidation doCheckSourceCodeEncoding(@AncestorInPath final AbstractPr
return FormValidation.ok();
}

return CHARSET_VALIDATION.validateCharset(sourceCodeEncoding);
return VALIDATION_UTILITIES.validateCharset(sourceCodeEncoding);
}

/**
Expand All @@ -526,7 +525,7 @@ public FormValidation doCheckId(@AncestorInPath final AbstractProject<?, ?> proj
return FormValidation.ok();
}

return MODEL_VALIDATION.validateId(id);
return VALIDATION_UTILITIES.validateId(id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class CoverageStatistics {
final List<? extends Value> changeValueMapping,
final NavigableMap<Metric, Fraction> changeDelta,
final List<? extends Value> fileValueMapping,
final NavigableMap<Metric, Fraction> fileDelta) {
final NavigableMap<Metric, Fraction> fileDelta) {
this.projectValueMapping = List.copyOf(projectValueMapping);
this.changeValueMapping = List.copyOf(changeValueMapping);
this.fileValueMapping = List.copyOf(fileValueMapping);
Expand All @@ -47,6 +47,16 @@ private static TreeMap<Metric, Value> asValueMap(final NavigableMap<Metric, Frac
TreeMap::new));
}

/**
* Returns the value for the specified baseline and metric.
*
* @param baseline
* the baseline of the value
* @param metric
* the metric of the value
*
* @return the value, if available
*/
public Optional<Value> getValue(final Baseline baseline, final Metric metric) {
if (baseline == Baseline.PROJECT) {
return Value.findValue(metric, projectValueMapping);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import org.apache.commons.lang3.StringUtils;

import edu.hm.hafner.metric.Node;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
Expand All @@ -31,7 +30,6 @@
import hudson.model.Item;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.Recorder;
import hudson.tools.ToolDescriptor;
import hudson.util.ComboBoxModel;
import hudson.util.FormValidation;
Expand All @@ -40,21 +38,19 @@
import io.jenkins.plugins.prism.SourceCodeDirectory;
import io.jenkins.plugins.prism.SourceCodeRetention;
import io.jenkins.plugins.util.AbstractExecution;
import io.jenkins.plugins.util.CharsetValidation;
import io.jenkins.plugins.util.JenkinsFacade;
import io.jenkins.plugins.util.ValidationUtilities;

/**
* A pipeline {@code Step} or Freestyle or Maven {@link Recorder} that reads and parses coverage results in a build and
* adds the results to the persisted build results.
* <p>
* Stores the created issues in a {@link Node}. This result is then attached to the {@link Run build} by registering a
* {@link CoverageBuildAction}.
* </p>
* A pipeline {@code Step} that reads and parses coverage results in a build and adds the results to the persisted build
* results. This step only provides the entry point for pipelines, the actual computation is delegated to an associated
* Freestyle {@link CoverageRecorder} instance.
*
* @author Ullrich Hafner
*/
public class CoverageStep extends Step implements Serializable {
private static final long serialVersionUID = 34386077204781270L;
private static final ValidationUtilities VALIDATION_UTILITIES = new ValidationUtilities();

private List<CoverageTool> tools = new ArrayList<>();
private List<QualityGate> qualityGates = new ArrayList<>();
Expand Down Expand Up @@ -126,7 +122,7 @@ public List<QualityGate> getQualityGates() {
*/
@DataBoundSetter
public void setId(final String id) {
new ModelValidation().ensureValidId(id);
VALIDATION_UTILITIES.ensureValidId(id);

this.id = id;
}
Expand Down Expand Up @@ -296,7 +292,8 @@ static class Execution extends AbstractExecution<Void> {
this.step = step;
}

@Override @CheckForNull
@Override
@CheckForNull
protected Void run() throws IOException, InterruptedException {
var recorder = new CoverageRecorder();
recorder.setTools(step.getTools());
Expand Down Expand Up @@ -324,15 +321,14 @@ protected Void run() throws IOException, InterruptedException {
@SuppressWarnings("unused") // most methods are used by the corresponding jelly view
public static class Descriptor extends StepDescriptor {
private static final JenkinsFacade JENKINS = new JenkinsFacade();
private static final CharsetValidation CHARSET_VALIDATION = new CharsetValidation();
private static final ModelValidation MODEL_VALIDATION = new ModelValidation();

@Override
public String getFunctionName() {
return "recordCoverage";
}

@NonNull @Override
@NonNull
@Override
public String getDisplayName() {
return Messages.Recorder_Name();
}
Expand Down Expand Up @@ -388,7 +384,7 @@ private void add(final ListBoxModel options, final SourceCodeRetention sourceCod
@SuppressWarnings("unused") // used by Stapler view data binding
public ComboBoxModel doFillSourceCodeEncodingItems(@AncestorInPath final AbstractProject<?, ?> project) {
if (JENKINS.hasPermission(Item.CONFIGURE, project)) {
return CHARSET_VALIDATION.getAllCharsets();
return VALIDATION_UTILITIES.getAllCharsets();
}
return new ComboBoxModel();
}
Expand All @@ -411,7 +407,7 @@ public FormValidation doCheckSourceCodeEncoding(@AncestorInPath final AbstractPr
return FormValidation.ok();
}

return CHARSET_VALIDATION.validateCharset(sourceCodeEncoding);
return VALIDATION_UTILITIES.validateCharset(sourceCodeEncoding);
}

/**
Expand All @@ -431,7 +427,7 @@ public FormValidation doCheckId(@AncestorInPath final AbstractProject<?, ?> proj
return FormValidation.ok();
}

return MODEL_VALIDATION.validateId(id);
return VALIDATION_UTILITIES.validateId(id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,6 @@ class CoverageTableModel extends TableModel {
private final RowRenderer renderer;
private final String id;

/**
* Creates an indirect coverage changes table model.
*
* @param id
* The ID of the table
* @param root
* The root of the coverage tree
* @param renderer
* the renderer to use for the file names
* @param colors
* The {@link ColorProvider} which provides the used colors
*/
CoverageTableModel(final String id, final Node root, final RowRenderer renderer, final ColorProvider colors) {
super();

Expand Down Expand Up @@ -177,7 +165,8 @@ static class CoverageRow {
private final RowRenderer renderer;
private final ColorProvider colorProvider;

CoverageRow(final FileNode file, final Locale browserLocale, final RowRenderer renderer, final ColorProvider colors) {
CoverageRow(final FileNode file, final Locale browserLocale, final RowRenderer renderer,
final ColorProvider colors) {
this.file = file;
this.browserLocale = browserLocale;
this.renderer = renderer;
Expand Down Expand Up @@ -225,6 +214,7 @@ public int getLoc() {
*
* @param coverage
* the coverage of the element
*
* @return the new {@link DetailedCell}
*/
protected DetailedCell<?> createColoredCoverageColumn(final Coverage coverage) {
Expand All @@ -247,6 +237,8 @@ protected DetailedCell<?> createColoredCoverageColumn(final Coverage coverage) {
/**
* Creates a table cell which colorizes the tendency of the shown coverage delta.
*
* @param metric
* the metric to use
* @param delta
* The coverage delta as percentage
*
Expand All @@ -259,7 +251,7 @@ protected DetailedCell<?> createColoredCoverageDeltaColumn(final Metric metric,
div().withClasses(COVERAGE_COLUMN_INNER)
.withStyle(String.format("background-color:%s;", colors.getFillColorAsRGBAHex(
TABLE_COVERAGE_COLOR_ALPHA)))
.withText(FORMATTER.formatDelta(metric, delta, browserLocale)))
.withText(FORMATTER.formatDelta(delta, metric, browserLocale)))
.render();
return new DetailedCell<>(cell, percentage);
}
Expand All @@ -273,10 +265,9 @@ protected FileNode getFile() {
* {@link Metric}.
*
* @param metric
* The coverage metric
* the coverage metric
*
* @return the created {@link DetailedCell}
* @since 3.0.0
*/
private DetailedCell<?> createColoredFileCoverageDeltaColumn(final Metric metric) {
if (file.hasChangeCoverage(metric)) {
Expand Down

0 comments on commit b5fb988

Please sign in to comment.