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

Add icon parameter for tools. #391

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/edu/hm/hafner/grading/AggregatedScore.java
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ public void gradeAnalysis(final AnalysisReportFactory factory) {
.withConfiguration(analysisConfiguration)
.withName(StringUtils.defaultIfBlank(tool.getName(), report.getName()))
.withId(tool.getId())
.withIcon(tool.getIcon())
.withReport(report)
.build();
scores.add(score);
Expand Down Expand Up @@ -461,6 +462,7 @@ public void gradeCoverage(final CoverageReportFactory factory) {
.withConfiguration(coverageConfiguration)
.withName(StringUtils.defaultIfBlank(tool.getName(), report.getName()))
.withId(tool.getId())
.withIcon(tool.getIcon())
.withReport(report, Metric.fromTag(tool.getMetric()))
.build();
scores.add(score);
Expand Down Expand Up @@ -495,6 +497,7 @@ public void gradeTests(final CoverageReportFactory factory) {
var score = new TestScoreBuilder()
.withConfiguration(testConfiguration)
.withId(tool.getId())
.withIcon(tool.getIcon())
.withName(StringUtils.defaultIfBlank(tool.getName(), report.getName()))
.withReport(report)
.build();
Expand Down Expand Up @@ -530,6 +533,7 @@ public void gradeMetrics(final CoverageReportFactory factory) {
var score = new MetricScoreBuilder()
.withConfiguration(metricConfiguration)
.withId(tool.getId())
.withIcon(tool.getIcon())
.withName(StringUtils.defaultIfBlank(tool.getName(), report.getName()))
.withReport(report, Metric.fromName(tool.getMetric()))
.build();
Expand Down
26 changes: 8 additions & 18 deletions src/main/java/edu/hm/hafner/grading/AnalysisMarkdown.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,14 @@ private int sum(final AnalysisScore score, final Function<AnalysisScore, Integer
}

@Override
protected List<String> createSummary(final AnalysisScore score) {
return score.getSubScores().stream()
.map(s -> SPACE + SPACE + getIconAndName(s) + ": " + s.createSummary()).toList();
}

private String getIconAndName(final AnalysisScore analysisScore) {
return format(" %s &nbsp; %s", extractParserIcon(analysisScore), analysisScore.getName())
+ createScoreTitle(analysisScore);
}

private String extractParserIcon(final AnalysisScore analysisScore) {
var descriptor = REGISTRY.get(analysisScore.getId());
if (descriptor.getIconUrl().isEmpty()) {
return getIcon(analysisScore);
}
else {
return format("<img src=\"%s\" alt=\"%s\" height=\"%d\" width=\"%d\">",
descriptor.getIconUrl(), analysisScore.getName(), ICON_SIZE, ICON_SIZE);
protected String getToolIcon(final AnalysisScore score) {
if (REGISTRY.contains(score.getId())) {
var descriptor = REGISTRY.get(score.getId());
if (!descriptor.getIconUrl().isBlank()) {
return format("<img src=\"%s\" alt=\"%s\" height=\"%d\" width=\"%d\">",
descriptor.getIconUrl(), score.getName(), ICON_SIZE, ICON_SIZE);
}
}
return getDefaultIcon(score);
}
}
31 changes: 25 additions & 6 deletions src/main/java/edu/hm/hafner/grading/AnalysisScore.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public final class AnalysisScore extends Score<AnalysisScore, AnalysisConfigurat

private transient Report report; // do not persist the issues

private AnalysisScore(final String id, final String name, final AnalysisConfiguration configuration,
private AnalysisScore(final String id, final String name, final String icon, final AnalysisConfiguration configuration,
final List<AnalysisScore> scores) {
super(id, name, configuration, scores.toArray(new AnalysisScore[0]));
super(id, name, icon, configuration, scores.toArray(new AnalysisScore[0]));

this.errorSize = scores.stream().reduce(0, (sum, score) -> sum + score.getErrorSize(), Integer::sum);
this.highSeveritySize = scores.stream().reduce(0, (sum, score) -> sum + score.getHighSeveritySize(), Integer::sum);
Expand All @@ -57,9 +57,9 @@ private AnalysisScore(final String id, final String name, final AnalysisConfigur
scores.stream().map(AnalysisScore::getReport).forEach(report::addAll);
}

private AnalysisScore(final String id, final String name, final AnalysisConfiguration configuration,
private AnalysisScore(final String id, final String name, final String icon, final AnalysisConfiguration configuration,
final Report report) {
super(id, name, configuration);
super(id, name, icon, configuration);

this.errorSize = report.getSizeOf(ERROR);
this.highSeveritySize = report.getSizeOf(WARNING_HIGH);
Expand Down Expand Up @@ -226,6 +226,7 @@ public static class AnalysisScoreBuilder {
private String id;
@CheckForNull
private String name;
private String icon = StringUtils.EMPTY;
@CheckForNull
private AnalysisConfiguration configuration;

Expand Down Expand Up @@ -269,6 +270,24 @@ private String getName() {
return StringUtils.defaultIfBlank(name, getConfiguration().getName());
}

/**
* Sets the icon of the analysis score.
*
* @param icon
* the icon to show
*
* @return this
*/
@CanIgnoreReturnValue
public AnalysisScoreBuilder withIcon(final String icon) {
this.icon = icon;
return this;
}

private String getIcon() {
return StringUtils.defaultString(icon);
}

/**
* Sets the grading configuration.
*
Expand Down Expand Up @@ -328,9 +347,9 @@ public AnalysisScore build() {
"You must either specify an analysis report or provide a list of sub-scores.");

if (report == null) {
return new AnalysisScore(getId(), getName(), getConfiguration(), scores);
return new AnalysisScore(getId(), getName(), getIcon(), getConfiguration(), scores);
}
return new AnalysisScore(getId(), getName(), getConfiguration(), Objects.requireNonNull(report));
return new AnalysisScore(getId(), getName(), getIcon(), getConfiguration(), Objects.requireNonNull(report));
}
}
}
5 changes: 0 additions & 5 deletions src/main/java/edu/hm/hafner/grading/CodeCoverageMarkdown.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,4 @@ public CodeCoverageMarkdown() {
protected List<CoverageScore> createScores(final AggregatedScore aggregation) {
return aggregation.getCodeCoverageScores();
}

@Override
protected String getIcon(final CoverageScore score) {
return ":%s:".formatted(score.getIcon());
}
}
12 changes: 9 additions & 3 deletions src/main/java/edu/hm/hafner/grading/CoverageMarkdown.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,14 @@
}

@Override
protected List<String> createSummary(final CoverageScore score) {
return score.getSubScores().stream()
.map(s -> SPACE + SPACE + getTitle(s, 0) + ": " + s.createSummary()).toList();
protected String getToolIcon(final CoverageScore score) {
return switch (score.getMetric()) {
case BRANCH -> emoji("curly_loop");
case LINE -> emoji("wavy_dash");
case CYCLOMATIC_COMPLEXITY -> emoji("part_alternation_mark");
case LOC -> emoji("pencil2");

Check warning on line 82 in src/main/java/edu/hm/hafner/grading/CoverageMarkdown.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/edu/hm/hafner/grading/CoverageMarkdown.java#L81-L82

Added lines #L81 - L82 were not covered by tests
case TEST_STRENGTH -> emoji("muscle");
default -> getDefaultIcon(score);
};
}
}
97 changes: 35 additions & 62 deletions src/main/java/edu/hm/hafner/grading/CoverageScore.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@

private final int coveredPercentage;
private final Metric metric;
private final String icon;
private final int missedItems;
private transient Node report; // do not persist the coverage tree

private CoverageScore(final String id, final String name, final CoverageConfiguration configuration,
private CoverageScore(final String id, final String name, final String icon, final CoverageConfiguration configuration,
final List<CoverageScore> scores) {
super(id, name, configuration, scores.toArray(new CoverageScore[0]));
super(id, name, icon, configuration, scores.toArray(new CoverageScore[0]));

this.coveredPercentage = scores.stream()
.reduce(0, (sum, score) -> sum + score.getCoveredPercentage(), Integer::sum)
Expand All @@ -58,19 +57,17 @@
else {
this.metric = metrics.iterator().next();
}
this.icon = selectIcon();

this.report = new ContainerNode(name);
scores.stream().map(CoverageScore::getReport).forEach(report::addChild);
}

private CoverageScore(final String id, final String name, final CoverageConfiguration configuration,
private CoverageScore(final String id, final String name, final String icon, final CoverageConfiguration configuration,
final Node report, final Metric metric) {
super(id, name, configuration);
super(id, name, icon, configuration);

this.report = report;
this.metric = metric;
this.icon = selectIcon();

var value = report.getValue(metric);
if (value.isPresent() && value.get() instanceof Coverage) {
Expand All @@ -87,33 +84,6 @@
return missedItems;
}

public String getIcon() {
return icon;
}

private String selectIcon() {
switch (metric) {
case BRANCH -> {
return "curly_loop";
}
case LINE -> {
return "wavy_dash";
}
case CYCLOMATIC_COMPLEXITY -> {
return "part_alternation_mark";
}
case LOC -> {
return "pencil2";
}
case TEST_STRENGTH -> {
return "muscle";
}
default -> {
return "footprints";
}
}
}

/**
* Restore an empty report after de-serialization.
*
Expand Down Expand Up @@ -166,32 +136,16 @@
}

private String getItemName() {
switch (metric) {
case MUTATION -> {
return "survived mutations";
}
case TEST_STRENGTH -> {
return "survived mutations in tested code";
}
case BRANCH -> {
return "missed branches";
}
case LINE -> {
return "missed lines";
}
case CYCLOMATIC_COMPLEXITY -> {
return "complexity";
}
case LOC -> {
return "lines of code";
}
case CONTAINER -> {
return "missed items";
}
default -> {
return "items";
}
}
return switch (metric) {
case MUTATION -> "survived mutations";
case TEST_STRENGTH -> "survived mutations in tested code";
case BRANCH -> "missed branches";
case LINE -> "missed lines";
case CYCLOMATIC_COMPLEXITY -> "complexity";
case LOC -> "lines of code";

Check warning on line 145 in src/main/java/edu/hm/hafner/grading/CoverageScore.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/edu/hm/hafner/grading/CoverageScore.java#L144-L145

Added lines #L144 - L145 were not covered by tests
case CONTAINER -> "missed items";
default -> "items";

Check warning on line 147 in src/main/java/edu/hm/hafner/grading/CoverageScore.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/edu/hm/hafner/grading/CoverageScore.java#L147

Added line #L147 was not covered by tests
};
}

@Override
Expand Down Expand Up @@ -225,6 +179,7 @@
private String id;
@CheckForNull
private String name;
private String icon = StringUtils.EMPTY;
@CheckForNull
private CoverageConfiguration configuration;

Expand Down Expand Up @@ -270,6 +225,24 @@
return StringUtils.defaultIfBlank(name, getConfiguration().getName());
}

/**
* Sets the icon of the coverage score.
*
* @param icon
* the icon to show
*
* @return this
*/
@CanIgnoreReturnValue
public CoverageScoreBuilder withIcon(final String icon) {
this.icon = icon;
return this;
}

private String getIcon() {
return StringUtils.defaultString(icon);
}

/**
* Sets the grading configuration.
*
Expand Down Expand Up @@ -334,12 +307,12 @@
"You must either specify a coverage report or provide a list of sub-scores.");

if (scores.isEmpty() && report != null && metric != null) {
return new CoverageScore(getId(), getName(), getConfiguration(),
return new CoverageScore(getId(), getName(), getIcon(), getConfiguration(),
Objects.requireNonNull(report),
Objects.requireNonNull(metric));
}
else {
return new CoverageScore(getId(), getName(), getConfiguration(), scores);
return new CoverageScore(getId(), getName(), getIcon(), getConfiguration(), scores);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package edu.hm.hafner.grading;

import org.apache.commons.lang3.StringUtils;

import edu.hm.hafner.coverage.Metric;
import edu.hm.hafner.coverage.Node;
import edu.hm.hafner.coverage.Value;
Expand All @@ -16,10 +14,8 @@
public final class FileSystemMetricReportFactory implements CoverageReportFactory {
@Override
public Node create(final ToolConfiguration tool, final FilteredLog log) {
var name = StringUtils.defaultIfBlank(tool.getName(), "Software Metrics");

var delegate = new FileSystemCoverageReportFactory();
var report = delegate.create(new ToolConfiguration("metrics", name, tool.getPattern(), "", "Metric"), log);
var report = delegate.create(tool, log);

Check warning on line 18 in src/main/java/edu/hm/hafner/grading/FileSystemMetricReportFactory.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/edu/hm/hafner/grading/FileSystemMetricReportFactory.java#L18

Added line #L18 was not covered by tests

var metricName = delegate.extractMetric(tool, report);
var metric = Metric.fromTag(metricName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public Node create(final ToolConfiguration tool, final FilteredLog log) {
var name = StringUtils.defaultIfBlank(tool.getName(), "Tests");

var delegate = new FileSystemCoverageReportFactory();
var report = delegate.create(new ToolConfiguration("junit", name, tool.getPattern(), "", Metric.TESTS.name()), log);
var report = delegate.create(new ToolConfiguration("junit", name, tool.getPattern(),
tool.getSourcePath(), Metric.TESTS.name(), tool.getIcon()), log);

log.logInfo("-> %s Total: %s tests",
name, report.getValue(Metric.TESTS).orElse(Value.nullObject(Metric.TESTS)));
Expand Down
12 changes: 3 additions & 9 deletions src/main/java/edu/hm/hafner/grading/MetricMarkdown.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,13 @@ protected void createSpecificDetails(final AggregatedScore aggregation, final Li
}

@Override
protected List<String> createSummary(final MetricScore score) {
return score.getSubScores().stream()
.map(s -> SPACE + SPACE + getTitle(s, 0) + ": " + s.createSummary()).toList();
}

@Override
protected String getIcon(final MetricScore score) {
protected String getToolIcon(final MetricScore score) {
return switch (score.getMetric()) {
case CYCLOMATIC_COMPLEXITY -> ":cyclone:";
case NCSS -> ":memo:";
case COGNITIVE_COMPLEXITY -> ":bulb:";
case COGNITIVE_COMPLEXITY -> ":brain:";
case NPATH_COMPLEXITY -> ":loop:";
default -> super.getIcon(score);
default -> getDefaultIcon(score);
};
}
}
Loading