Skip to content

Commit

Permalink
SLCORE-1023 Parsing impact severity with INFO and BLOCKER should work (
Browse files Browse the repository at this point in the history
  • Loading branch information
nquinquenel authored Nov 11, 2024
1 parent c9c318b commit 9dfee8d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,15 @@ public enum ImpactSeverity {
LOW,
MEDIUM,
HIGH,
BLOCKER
BLOCKER;

public static ImpactSeverity mapSeverity(String severity) {
if ("BLOCKER".equals(severity) || "ImpactSeverity_BLOCKER".equals(severity)) {
return ImpactSeverity.BLOCKER;
} else if ("INFO".equals(severity) || "ImpactSeverity_INFO".equals(severity)) {
return ImpactSeverity.INFO;
} else {
return ImpactSeverity.valueOf(severity);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,23 +163,13 @@ public static Map<SoftwareQuality, ImpactSeverity> mergeImpacts(Map<SoftwareQual

for (ImpactPayload impact : overriddenImpacts) {
var quality = SoftwareQuality.valueOf(impact.getSoftwareQuality());
var severity = mapSeverity(impact.getSeverity());
var severity = ImpactSeverity.mapSeverity(impact.getSeverity());
mergedImpacts.computeIfPresent(quality, (k, v) -> severity);
}

return Collections.unmodifiableMap(mergedImpacts);
}

private static ImpactSeverity mapSeverity(String severity) {
if ("BLOCKER".equals(severity) || "ImpactSeverity_BLOCKER".equals(severity)) {
return ImpactSeverity.BLOCKER;
} else if ("INFO".equals(severity) || "ImpactSeverity_INFO".equals(severity)) {
return ImpactSeverity.INFO;
} else {
return ImpactSeverity.valueOf(severity);
}
}

public static RuleDetails merging(RuleDetails serverActiveRuleDetails, RaisedFindingDto raisedFindingDto) {
var isMQRMode = raisedFindingDto.getSeverityMode().isRight();
var softwareImpacts = new EnumMap<SoftwareQuality, ImpactSeverity>(SoftwareQuality.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public Optional<ServerRule> getRule(String ruleKey, SonarLintCancelMonitor cance
var cleanCodeAttribute = Enums.getIfPresent(CleanCodeAttribute.class, rule.getCleanCodeAttribute().name()).orNull();
var impacts = rule.getImpacts().getImpactsList().stream().collect(toMap(
impact -> SoftwareQuality.valueOf(impact.getSoftwareQuality().name()),
impact -> ImpactSeverity.valueOf(impact.getSeverity().name())));
impact -> ImpactSeverity.mapSeverity(impact.getSeverity().name())));
return Optional.of(new ServerRule(rule.getName(), IssueSeverity.valueOf(rule.getSeverity()), RuleType.valueOf(rule.getType().name()), rule.getLang(), rule.getHtmlDesc(),
convertDescriptionSections(rule),
rule.getHtmlNote(), Set.copyOf(rule.getEducationPrinciples().getEducationPrinciplesList()), cleanCodeAttribute, impacts));
Expand Down Expand Up @@ -123,7 +123,7 @@ public Collection<ServerActiveRule> getAllActiveRules(String qualityProfileKey,
ar.getParamsList().stream().collect(toMap(Rules.Active.Param::getKey, Rules.Active.Param::getValue)),
ruleTemplatesByRuleKey.get(ruleKey),
ar.getImpacts().getImpactsList().stream()
.map(impact -> new ImpactPayload(impact.getSoftwareQuality().toString(), impact.getSeverity().name()))
.map(impact -> new ImpactPayload(impact.getSoftwareQuality().toString(), ImpactSeverity.mapSeverity(impact.getSeverity().name()).name()))
.collect(Collectors.toList())));

},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ static ImpactSeverity parseProtoImpactSeverity(Common.Impact protoImpact) {
if (!protoImpact.hasSeverity() || protoImpact.getSeverity() == Common.ImpactSeverity.UNKNOWN_IMPACT_SEVERITY) {
throw new IllegalArgumentException("Unknown or missing impact severity");
}
return ImpactSeverity.valueOf(protoImpact.getSeverity().name());
return ImpactSeverity.mapSeverity(protoImpact.getSeverity().name());
}

private static List<ServerTaintIssue.Flow> convertFlows(SourceApi sourceApi, List<Flow> flowsList, Map<String, Path> componentPathsByKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,45 +350,43 @@ void parse_clean_code_attribute_from_lite_stream_unknown() {
assertThat(cleanCodeAttribute).isNull();
}

void parse_software_quality() {
var impact = Common.Impact.newBuilder().setSoftwareQuality(Common.SoftwareQuality.SECURITY).build();
@Test
void parse_software_quality_and_impact_severity() {
var impact = Common.Impact.newBuilder().setSoftwareQuality(Common.SoftwareQuality.SECURITY).setSeverity(Common.ImpactSeverity.MEDIUM).build();
assertThat(parseProtoSoftwareQuality(impact)).isEqualTo(SoftwareQuality.SECURITY);
assertThat(parseProtoImpactSeverity(impact)).isEqualTo(ImpactSeverity.MEDIUM);
}

void parse_software_quality_missing() {
var impact = Common.Impact.newBuilder().build();
assertThatThrownBy(() -> parseProtoSoftwareQuality(impact))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Unknown or missing software quality");
@Test
void parse_software_quality_and_impact_severity_info() {
var impact = Common.Impact.newBuilder().setSoftwareQuality(Common.SoftwareQuality.SECURITY).setSeverity(Common.ImpactSeverity.ImpactSeverity_INFO).build();
assertThat(parseProtoSoftwareQuality(impact)).isEqualTo(SoftwareQuality.SECURITY);
assertThat(parseProtoImpactSeverity(impact)).isEqualTo(ImpactSeverity.INFO);
}

@Test
void parse_software_quality_and_impact_severity_blocker() {
var impact = Common.Impact.newBuilder().setSoftwareQuality(Common.SoftwareQuality.SECURITY).setSeverity(Common.ImpactSeverity.ImpactSeverity_BLOCKER).build();
assertThat(parseProtoSoftwareQuality(impact)).isEqualTo(SoftwareQuality.SECURITY);
assertThat(parseProtoImpactSeverity(impact)).isEqualTo(ImpactSeverity.BLOCKER);
}

@Test
void parse_software_quality_unknown() {
var impact = Common.Impact.newBuilder().setSoftwareQuality(Common.SoftwareQuality.UNKNOWN_IMPACT_QUALITY).build();
var impact = Common.Impact.newBuilder().setSoftwareQuality(Common.SoftwareQuality.UNKNOWN_IMPACT_QUALITY).setSeverity(Common.ImpactSeverity.HIGH).build();
assertThatThrownBy(() -> parseProtoSoftwareQuality(impact))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Unknown or missing software quality");
}

void parse_impact_severity() {
var impact = Common.Impact.newBuilder().setSeverity(Common.ImpactSeverity.LOW).build();
assertThat(parseProtoImpactSeverity(impact)).isEqualTo(ImpactSeverity.LOW);
}

void parse_impact_severity_missing() {
var impact = Common.Impact.newBuilder().build();
assertThatThrownBy(() -> parseProtoImpactSeverity(impact))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Unknown or missing impact severity");
}

@Test
void parse_impact_severity_unknown() {
var impact = Common.Impact.newBuilder().setSeverity(Common.ImpactSeverity.UNKNOWN_IMPACT_SEVERITY).build();
var impact = Common.Impact.newBuilder().setSeverity(Common.ImpactSeverity.UNKNOWN_IMPACT_SEVERITY).setSoftwareQuality(Common.SoftwareQuality.MAINTAINABILITY).build();
assertThatThrownBy(() -> parseProtoImpactSeverity(impact))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Unknown or missing impact severity");
}


private static void assertTextRange(@Nullable TextRangeWithHash textRangeWithHash, int startLine, int startLineOffset,
int endLine, int endLineOffset, String hash) {
assertThat(textRangeWithHash).isNotNull();
Expand Down

0 comments on commit 9dfee8d

Please sign in to comment.