Search in sources :

Example 1 with Result

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);
}
Also used : ArtifactLocation(com.contrastsecurity.sarif.ArtifactLocation) ToolComponent(com.contrastsecurity.sarif.ToolComponent) SneakyThrows(lombok.SneakyThrows) ServerInfo(org.eclipse.lsp4j.ServerInfo) FileInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) ReportingDescriptor(com.contrastsecurity.sarif.ReportingDescriptor) Message(com.contrastsecurity.sarif.Message) Absolute(com.github._1c_syntax.utils.Absolute) ArrayList(java.util.ArrayList) Region(com.contrastsecurity.sarif.Region) HashSet(java.util.HashSet) AnalysisInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.AnalysisInfo) LanguageServerConfiguration(com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration) Map(java.util.Map) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) ConfigurationOverride(com.contrastsecurity.sarif.ConfigurationOverride) Run(com.contrastsecurity.sarif.Run) URI(java.net.URI) Invocation(com.contrastsecurity.sarif.Invocation) Path(java.nio.file.Path) DiagnosticInfo(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) Location(com.contrastsecurity.sarif.Location) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) Collectors(java.util.stream.Collectors) File(java.io.File) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) ReportingDescriptorReference(com.contrastsecurity.sarif.ReportingDescriptorReference) PropertyBag(com.contrastsecurity.sarif.PropertyBag) Tool(com.contrastsecurity.sarif.Tool) Optional(java.util.Optional) ReportingConfiguration(com.contrastsecurity.sarif.ReportingConfiguration) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) MultiformatMessageString(com.contrastsecurity.sarif.MultiformatMessageString) PhysicalLocation(com.contrastsecurity.sarif.PhysicalLocation) DiagnosticCode(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode) Result(com.contrastsecurity.sarif.Result) SarifSchema210(com.contrastsecurity.sarif.SarifSchema210) Message(com.contrastsecurity.sarif.Message) ArtifactLocation(com.contrastsecurity.sarif.ArtifactLocation) Result(com.contrastsecurity.sarif.Result)

Example 2 with Result

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));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FileInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo) LocalDateTime(java.time.LocalDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) Diagnostic(org.eclipse.lsp4j.Diagnostic) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) AnalysisInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.AnalysisInfo) LanguageServerConfiguration(com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration) Ranges(com.github._1c_syntax.bsl.languageserver.utils.Ranges) Map(java.util.Map) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) TestUtils(com.github._1c_syntax.bsl.languageserver.util.TestUtils) Path(java.nio.file.Path) DiagnosticInfo(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) Location(com.contrastsecurity.sarif.Location) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) File(java.io.File) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) CleanupContextBeforeClassAndAfterClass(com.github._1c_syntax.bsl.languageserver.util.CleanupContextBeforeClassAndAfterClass) Collections(java.util.Collections) PhysicalLocation(com.contrastsecurity.sarif.PhysicalLocation) Result(com.contrastsecurity.sarif.Result) SarifSchema210(com.contrastsecurity.sarif.SarifSchema210) FileInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo) Diagnostic(org.eclipse.lsp4j.Diagnostic) AnalysisInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.AnalysisInfo) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PhysicalLocation(com.contrastsecurity.sarif.PhysicalLocation) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

Location (com.contrastsecurity.sarif.Location)2 PhysicalLocation (com.contrastsecurity.sarif.PhysicalLocation)2 Result (com.contrastsecurity.sarif.Result)2 SarifSchema210 (com.contrastsecurity.sarif.SarifSchema210)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 LanguageServerConfiguration (com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration)2 DiagnosticInfo (com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo)2 AnalysisInfo (com.github._1c_syntax.bsl.languageserver.reporters.data.AnalysisInfo)2 FileInfo (com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo)2 File (java.io.File)2 Path (java.nio.file.Path)2 Collection (java.util.Collection)2 Map (java.util.Map)2 Diagnostic (org.eclipse.lsp4j.Diagnostic)2 ArtifactLocation (com.contrastsecurity.sarif.ArtifactLocation)1 ConfigurationOverride (com.contrastsecurity.sarif.ConfigurationOverride)1 Invocation (com.contrastsecurity.sarif.Invocation)1 Message (com.contrastsecurity.sarif.Message)1 MultiformatMessageString (com.contrastsecurity.sarif.MultiformatMessageString)1 PropertyBag (com.contrastsecurity.sarif.PropertyBag)1