use of com.contrastsecurity.sarif.Result in project bsl-language-server by 1c-syntax.
the class SarifReporter method createResult.
private static Result createResult(FileInfo fileInfo, Diagnostic diagnostic) {
var uri = Absolute.uri(fileInfo.getPath().toUri()).toString();
var message = new Message().withText(diagnostic.getMessage());
var ruleId = DiagnosticCode.getStringValue(diagnostic.getCode());
var level = severityToResultLevel.get(diagnostic.getSeverity());
var analysisTarget = new ArtifactLocation().withUri(uri);
var locations = List.of(createLocation(diagnostic.getMessage(), uri, diagnostic.getRange()));
var relatedLocations = Optional.ofNullable(diagnostic.getRelatedInformation()).stream().flatMap(Collection::stream).skip(1).map(relatedInformation -> createLocation(relatedInformation.getMessage(), relatedInformation.getLocation().getUri(), relatedInformation.getLocation().getRange())).collect(Collectors.toSet());
return new Result().withMessage(message).withRuleId(ruleId).withLevel(level).withAnalysisTarget(analysisTarget).withLocations(locations).withRelatedLocations(relatedLocations);
}
use of com.contrastsecurity.sarif.Result in project bsl-language-server by 1c-syntax.
the class SarifReporterTest method report.
@Test
void report() throws IOException {
// given
configuration.getDiagnosticsOptions().getParameters().put("Typo", Either.forLeft(false));
configuration.getDiagnosticsOptions().getParameters().put("test", Either.forLeft(true));
configuration.getDiagnosticsOptions().getParameters().put("some", Either.forRight(Map.of("test", 1)));
Diagnostic diagnostic = new Diagnostic(Ranges.create(0, 1, 2, 3), "message", DiagnosticSeverity.Error, "test-source", "test");
DocumentContext documentContext = TestUtils.getDocumentContext("");
String sourceDir = ".";
FileInfo fileInfo = new FileInfo(sourceDir, documentContext, Collections.singletonList(diagnostic));
AnalysisInfo analysisInfo = new AnalysisInfo(LocalDateTime.now(), Collections.singletonList(fileInfo), sourceDir);
// when
reporter.report(analysisInfo, Path.of(sourceDir));
// then
ObjectMapper mapper = new ObjectMapper();
var report = mapper.readValue(file, SarifSchema210.class);
assertThat(report).isNotNull();
var run = report.getRuns().get(0);
assertThat(run.getTool().getDriver().getName()).isEqualTo("BSL Language Server");
assertThat(run.getTool().getDriver().getRules()).hasSize(diagnosticInfos.size());
var invocation = run.getInvocations().get(0);
assertThat(invocation.getRuleConfigurationOverrides()).hasSizeGreaterThan(0).anyMatch(configurationOverride -> configurationOverride.getDescriptor().getId().equals("Typo") && !configurationOverride.getConfiguration().getEnabled()).anyMatch(configurationOverride -> configurationOverride.getDescriptor().getId().equals("test") && configurationOverride.getConfiguration().getEnabled()).anyMatch(configurationOverride -> configurationOverride.getDescriptor().getId().equals("some") && configurationOverride.getConfiguration().getParameters().getAdditionalProperties().get("test").equals(1));
assertThat(run.getResults()).hasSize(1).element(0).matches(result -> result.getRuleId().equals("test")).matches(result -> result.getLevel() == Result.Level.ERROR).matches(result -> result.getMessage().getText().equals("message")).matches(result -> result.getAnalysisTarget().getUri().equals(documentContext.getUri().toString())).extracting(Result::getLocations).extracting(locations -> locations.get(0)).extracting(Location::getPhysicalLocation).extracting(PhysicalLocation::getRegion).matches(region -> region.getStartLine().equals(diagnostic.getRange().getStart().getLine() + 1)).matches(region -> region.getStartColumn().equals(diagnostic.getRange().getStart().getCharacter() + 1)).matches(region -> region.getEndLine().equals(diagnostic.getRange().getEnd().getLine() + 1)).matches(region -> region.getEndColumn().equals(diagnostic.getRange().getEnd().getCharacter() + 1));
}
Aggregations