Skip to content

Commit

Permalink
fix: avoiding duplicated fingerprints in CodeClimate report (refs #181)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Sep 17, 2023
1 parent f789cd2 commit da54923
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static se.bjurr.violations.lib.util.Utils.checkNotNull;
import static se.bjurr.violations.lib.util.Utils.emptyToNull;
import static se.bjurr.violations.lib.util.Utils.firstNonNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -11,6 +13,7 @@ public class CodeClimate {
private final String description;
private final String fingerprint;
private final CodeClimateLocation location;
private final List<CodeClimateLocation> other_locations;
private final CodeClimateSeverity severity;
private final String type;
private final List<String> categories;
Expand All @@ -26,10 +29,12 @@ public CodeClimate(
final CodeClimateSeverity severity,
final String check_name,
final String engine_name,
final List<CodeClimateCategory> categories) {
final List<CodeClimateCategory> categories,
final List<CodeClimateLocation> other_locations) {
this.description = checkNotNull(emptyToNull(description), "description");
this.fingerprint = checkNotNull(emptyToNull(fingerprint), "fingerprint");
this.location = checkNotNull(location, "location");
this.other_locations = firstNonNull(other_locations, new ArrayList<>());
this.severity = severity;
this.type = "issue";
this.check_name = check_name;
Expand Down Expand Up @@ -71,6 +76,10 @@ public CodeClimateLocation getLocation() {
return this.location;
}

public List<CodeClimateLocation> getOther_locations() {
return this.other_locations;
}

@Override
public String toString() {
return "CodeClimate [description="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;
import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.model.Violation;
Expand All @@ -16,12 +18,24 @@
public class CodeClimateTransformer {
public static List<CodeClimate> fromViolations(final Set<Violation> from) {
final List<Path> allFiles = ViolationUtils.getAllFiles();
return from.stream()
.map(
violation -> {
return toCodeClimate(allFiles, violation);
})
.collect(Collectors.toList());
final List<CodeClimate> codeClimates =
from.stream()
.map(
violation -> {
return toCodeClimate(allFiles, violation);
})
.collect(Collectors.toList());

final Map<String, CodeClimate> codeClimatesPerFingerprint = new TreeMap<>();
for (final CodeClimate candidate : codeClimates) {
if (codeClimatesPerFingerprint.containsKey(candidate.getFingerprint())) {
final CodeClimate existing = codeClimatesPerFingerprint.get(candidate.getFingerprint());
existing.getOther_locations().add(candidate.getLocation());
} else {
codeClimatesPerFingerprint.put(candidate.getFingerprint(), candidate);
}
}
return new ArrayList<CodeClimate>(codeClimatesPerFingerprint.values());
}

private static CodeClimate toCodeClimate(final List<Path> allFiles, final Violation v) {
Expand All @@ -36,7 +50,14 @@ private static CodeClimate toCodeClimate(final List<Path> allFiles, final Violat
final List<CodeClimateCategory> categories = new ArrayList<>();
categories.add(CodeClimateCategory.BUGRISK);
return new CodeClimate(
description, fingerprint, location, severity, check_name, engine_name, categories);
description,
fingerprint,
location,
severity,
check_name,
engine_name,
categories,
new ArrayList<CodeClimateLocation>());
}

private static CodeClimateSeverity toSeverity(final SEVERITY severity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static se.bjurr.violations.lib.model.Violation.violationBuilder;

import com.google.gson.GsonBuilder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -60,7 +61,44 @@ public void testThatViolationsCanBeTransformed() {
severity,
check_name,
engine_name,
categories)));
categories,
new ArrayList<CodeClimateLocation>())));
}

@Test
public void testThatViolationsAreGroupedWithOtherLocations() {
final Set<Violation> violationSet = new TreeSet<>();

violationSet.add(
violationBuilder() //
.setFile("whatever/path.c") //
.setMessage("asdasd") //
.setParser(Parser.CHECKSTYLE) //
.setRule("Cyclomatic complexity") //
.setSeverity(SEVERITY.ERROR) //
.setStartLine(123) //
.build());

List<CodeClimate> transformed = CodeClimateTransformer.fromViolations(violationSet);

assertThat(transformed).hasSize(1);
assertThat(transformed.get(0).getOther_locations()).hasSize(0);

violationSet.add(
violationBuilder() //
.setFile("whatever/path.c") //
.setMessage("asdasd") //
.setParser(Parser.CHECKSTYLE) //
.setRule("Cyclomatic complexity") //
.setSeverity(SEVERITY.ERROR) //
.setStartLine(124) //
.build());

transformed = CodeClimateTransformer.fromViolations(violationSet);

assertThat(transformed).hasSize(1);
assertThat(transformed.get(0).getOther_locations()).hasSize(1);
assertThat(transformed.get(0).getOther_locations().get(0).getLines().getBegin()).isEqualTo(123);
}

private String toJson(final Object o) {
Expand Down

0 comments on commit da54923

Please sign in to comment.