diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2024-07-23 15:22:13 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-07-24 20:02:47 +0000 |
commit | e4313bc4793eaf2a265f55f4bfa867e4c76942cd (patch) | |
tree | af29396ed8f5140d6bee4fe8ba6360cd1ce43ba9 /sonar-scanner-engine/src/main | |
parent | 012bf4a5f40dfa9034e534f81941ce3cdbd5ac13 (diff) | |
download | sonarqube-e4313bc4793eaf2a265f55f4bfa867e4c76942cd.tar.gz sonarqube-e4313bc4793eaf2a265f55f4bfa867e4c76942cd.zip |
SONAR-22603 Use generated code for the SARIF parser
Diffstat (limited to 'sonar-scanner-engine/src/main')
9 files changed, 72 insertions, 70 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/DefaultSarif210Importer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/DefaultSarif210Importer.java index 8b4154ce197..cf70f351b6a 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/DefaultSarif210Importer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/DefaultSarif210Importer.java @@ -26,8 +26,8 @@ import org.slf4j.LoggerFactory; import org.sonar.api.batch.sensor.issue.NewExternalIssue; import org.sonar.api.batch.sensor.rule.NewAdHocRule; import org.sonar.api.scanner.ScannerSide; -import org.sonar.core.sarif.Run; -import org.sonar.core.sarif.Sarif210; +import org.sonar.sarif.pojo.Run; +import org.sonar.sarif.pojo.SarifSchema210; import org.sonar.scanner.externalissue.sarif.RunMapper.RunMapperResult; import static java.util.Objects.requireNonNull; @@ -43,12 +43,12 @@ public class DefaultSarif210Importer implements Sarif210Importer { } @Override - public SarifImportResults importSarif(Sarif210 sarif210) { + public SarifImportResults importSarif(SarifSchema210 sarif210) { int successFullyImportedIssues = 0; int successFullyImportedRuns = 0; int failedRuns = 0; - Set<Run> runs = requireNonNull(sarif210.getRuns(), "The runs section of the Sarif report is null"); + List<Run> runs = requireNonNull(sarif210.getRuns(), "The runs section of the Sarif report is null"); for (Run run : runs) { RunMapperResult runMapperResult = tryMapRun(run); if (runMapperResult.isSuccess()) { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/LocationMapper.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/LocationMapper.java index a3cb87de11d..df816a466d7 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/LocationMapper.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/LocationMapper.java @@ -38,10 +38,10 @@ import org.sonar.api.batch.fs.internal.predicates.AbstractFilePredicate; import org.sonar.api.batch.sensor.SensorContext; import org.sonar.api.batch.sensor.issue.NewIssueLocation; import org.sonar.api.scanner.ScannerSide; -import org.sonar.core.sarif.ArtifactLocation; -import org.sonar.core.sarif.Location; -import org.sonar.core.sarif.PhysicalLocation; -import org.sonar.core.sarif.Result; +import org.sonar.sarif.pojo.ArtifactLocation; +import org.sonar.sarif.pojo.Location; +import org.sonar.sarif.pojo.PhysicalLocation; +import org.sonar.sarif.pojo.Result; import static java.util.Objects.requireNonNull; import static org.sonar.api.utils.Preconditions.checkArgument; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RegionMapper.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RegionMapper.java index 48a1982c1da..076952c92d9 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RegionMapper.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RegionMapper.java @@ -25,7 +25,7 @@ import javax.annotation.Nullable; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.TextRange; import org.sonar.api.scanner.ScannerSide; -import org.sonar.core.sarif.Region; +import org.sonar.sarif.pojo.Region; @ScannerSide public class RegionMapper { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/ResultMapper.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/ResultMapper.java index ae8790c2848..c81b1e33313 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/ResultMapper.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/ResultMapper.java @@ -20,9 +20,9 @@ package org.sonar.scanner.externalissue.sarif; import com.google.common.collect.ImmutableMap; +import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.Set; import javax.annotation.Nullable; import org.sonar.api.batch.rule.Severity; import org.sonar.api.batch.sensor.SensorContext; @@ -32,8 +32,8 @@ import org.sonar.api.issue.impact.SoftwareQuality; import org.sonar.api.rules.CleanCodeAttribute; import org.sonar.api.rules.RuleType; import org.sonar.api.scanner.ScannerSide; -import org.sonar.core.sarif.Location; -import org.sonar.core.sarif.Result; +import org.sonar.sarif.pojo.Location; +import org.sonar.sarif.pojo.Result; import static java.util.Objects.requireNonNull; import static org.sonar.api.issue.impact.Severity.HIGH; @@ -45,18 +45,18 @@ import static org.sonar.api.rules.CleanCodeAttribute.CONVENTIONAL; @ScannerSide public class ResultMapper { - private static final Map<String, Severity> SEVERITY_MAPPING = ImmutableMap.<String, Severity>builder() - .put("error", Severity.CRITICAL) - .put("warning", Severity.MAJOR) - .put("note", Severity.MINOR) - .put("none", Severity.INFO) + private static final Map<Result.Level, Severity> SEVERITY_MAPPING = ImmutableMap.<Result.Level, Severity>builder() + .put(Result.Level.ERROR, Severity.CRITICAL) + .put(Result.Level.WARNING, Severity.MAJOR) + .put(Result.Level.NOTE, Severity.MINOR) + .put(Result.Level.NONE, Severity.INFO) .build(); - private static final Map<String, org.sonar.api.issue.impact.Severity> IMPACT_SEVERITY_MAPPING = ImmutableMap.<String, org.sonar.api.issue.impact.Severity>builder() - .put("error", HIGH) - .put("warning", MEDIUM) - .put("note", LOW) - .put("none", LOW) + private static final Map<Result.Level, org.sonar.api.issue.impact.Severity> IMPACT_SEVERITY_MAPPING = ImmutableMap.<Result.Level, org.sonar.api.issue.impact.Severity>builder() + .put(Result.Level.ERROR, HIGH) + .put(Result.Level.WARNING, MEDIUM) + .put(Result.Level.NOTE, LOW) + .put(Result.Level.NONE, LOW) .build(); public static final Severity DEFAULT_SEVERITY = Severity.MAJOR; @@ -73,7 +73,7 @@ public class ResultMapper { this.locationMapper = locationMapper; } - NewExternalIssue mapResult(String driverName, @Nullable String ruleSeverity, @Nullable String ruleSeverityForNewTaxonomy, Result result) { + NewExternalIssue mapResult(String driverName, @Nullable Result.Level ruleSeverity, @Nullable Result.Level ruleSeverityForNewTaxonomy, Result result) { NewExternalIssue newExternalIssue = sensorContext.newExternalIssue(); newExternalIssue.type(DEFAULT_TYPE); newExternalIssue.engineId(driverName); @@ -86,17 +86,17 @@ public class ResultMapper { return newExternalIssue; } - protected static org.sonar.api.issue.impact.Severity toSonarQubeImpactSeverity(@Nullable String ruleSeverity) { + protected static org.sonar.api.issue.impact.Severity toSonarQubeImpactSeverity(@Nullable Result.Level ruleSeverity) { return IMPACT_SEVERITY_MAPPING.getOrDefault(ruleSeverity, DEFAULT_IMPACT_SEVERITY); } - protected static Severity toSonarQubeSeverity(@Nullable String ruleSeverity) { + protected static Severity toSonarQubeSeverity(@Nullable Result.Level ruleSeverity) { return SEVERITY_MAPPING.getOrDefault(ruleSeverity, DEFAULT_SEVERITY); } private void mapLocations(Result result, NewExternalIssue newExternalIssue) { NewIssueLocation newIssueLocation = newExternalIssue.newLocation(); - Set<Location> locations = result.getLocations(); + List<Location> locations = result.getLocations(); if (locations == null || locations.isEmpty()) { newExternalIssue.at(locationMapper.fillIssueInProjectLocation(result, newIssueLocation)); } else { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RuleMapper.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RuleMapper.java index 71219332d4a..5638088fd11 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RuleMapper.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RuleMapper.java @@ -23,7 +23,8 @@ import javax.annotation.Nullable; import org.sonar.api.batch.sensor.SensorContext; import org.sonar.api.batch.sensor.rule.NewAdHocRule; import org.sonar.api.scanner.ScannerSide; -import org.sonar.core.sarif.Rule; +import org.sonar.sarif.pojo.ReportingDescriptor; +import org.sonar.sarif.pojo.Result; import static java.lang.String.join; @@ -36,7 +37,7 @@ public class RuleMapper { this.sensorContext = sensorContext; } - NewAdHocRule mapRule(Rule rule, String driverName, @Nullable String ruleSeverity, @Nullable String ruleSeverityForNewTaxonomy) { + NewAdHocRule mapRule(ReportingDescriptor rule, String driverName, @Nullable Result.Level ruleSeverity, @Nullable Result.Level ruleSeverityForNewTaxonomy) { return sensorContext.newAdHocRule() .severity(ResultMapper.toSonarQubeSeverity(ruleSeverity)) .type(ResultMapper.DEFAULT_TYPE) diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RulesSeverityDetector.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RulesSeverityDetector.java index 35183ccaa3b..d7223ae273f 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RulesSeverityDetector.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RulesSeverityDetector.java @@ -28,12 +28,12 @@ import java.util.function.Predicate; import javax.annotation.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.sonar.core.sarif.DefaultConfiguration; -import org.sonar.core.sarif.Extension; -import org.sonar.core.sarif.Result; -import org.sonar.core.sarif.Rule; -import org.sonar.core.sarif.Run; -import org.sonar.core.sarif.Tool; +import org.sonar.sarif.pojo.ReportingConfiguration; +import org.sonar.sarif.pojo.ReportingDescriptor; +import org.sonar.sarif.pojo.Result; +import org.sonar.sarif.pojo.Run; +import org.sonar.sarif.pojo.Tool; +import org.sonar.sarif.pojo.ToolComponent; import static java.util.Collections.emptyMap; import static java.util.Collections.emptySet; @@ -47,20 +47,20 @@ public class RulesSeverityDetector { private RulesSeverityDetector() {} - public static Map<String, String> detectRulesSeverities(Run run, String driverName) { - Map<String, String> resultDefinedRuleSeverities = getResultDefinedRuleSeverities(run); + public static Map<String, Result.Level> detectRulesSeverities(Run run, String driverName) { + Map<String, Result.Level> resultDefinedRuleSeverities = getResultDefinedRuleSeverities(run); if (!resultDefinedRuleSeverities.isEmpty()) { return resultDefinedRuleSeverities; } - Map<String, String> driverDefinedRuleSeverities = getDriverDefinedRuleSeverities(run); + Map<String, Result.Level> driverDefinedRuleSeverities = getDriverDefinedRuleSeverities(run); if (!driverDefinedRuleSeverities.isEmpty()) { return driverDefinedRuleSeverities; } - Map<String, String> extensionDefinedRuleSeverities = getExtensionsDefinedRuleSeverities(run); + Map<String, Result.Level> extensionDefinedRuleSeverities = getExtensionsDefinedRuleSeverities(run); if (!extensionDefinedRuleSeverities.isEmpty()) { return extensionDefinedRuleSeverities; @@ -70,14 +70,14 @@ public class RulesSeverityDetector { return emptyMap(); } - public static Map<String, String> detectRulesSeveritiesForNewTaxonomy(Run run, String driverName) { - Map<String, String> driverDefinedRuleSeverities = getDriverDefinedRuleSeverities(run); + public static Map<String, Result.Level> detectRulesSeveritiesForNewTaxonomy(Run run, String driverName) { + Map<String, Result.Level> driverDefinedRuleSeverities = getDriverDefinedRuleSeverities(run); if (!driverDefinedRuleSeverities.isEmpty()) { return driverDefinedRuleSeverities; } - Map<String, String> extensionDefinedRuleSeverities = getExtensionsDefinedRuleSeverities(run); + Map<String, Result.Level> extensionDefinedRuleSeverities = getExtensionsDefinedRuleSeverities(run); if (!extensionDefinedRuleSeverities.isEmpty()) { return extensionDefinedRuleSeverities; @@ -87,7 +87,7 @@ public class RulesSeverityDetector { return emptyMap(); } - private static Map<String, String> getResultDefinedRuleSeverities(Run run) { + private static Map<String, Result.Level> getResultDefinedRuleSeverities(Run run) { Predicate<Result> hasResultDefinedLevel = result -> Optional.ofNullable(result).map(Result::getLevel).isPresent(); return run.getResults() @@ -96,34 +96,34 @@ public class RulesSeverityDetector { .collect(toMap(Result::getRuleId, Result::getLevel, (x, y) -> y)); } - private static Map<String, String> getDriverDefinedRuleSeverities(Run run) { + private static Map<String, Result.Level> getDriverDefinedRuleSeverities(Run run) { return run.getTool().getDriver().getRules() .stream() .filter(RulesSeverityDetector::hasRuleDefinedLevel) - .collect(toMap(Rule::getId, x -> x.getDefaultConfiguration().getLevel())); + .collect(toMap(ReportingDescriptor::getId, x -> Result.Level.valueOf(x.getDefaultConfiguration().getLevel().name()))); } - private static Map<String, String> getExtensionsDefinedRuleSeverities(Run run) { + private static Map<String, Result.Level> getExtensionsDefinedRuleSeverities(Run run) { return getExtensions(run) .stream() - .map(Extension::getRules) + .map(ToolComponent::getRules) .filter(Objects::nonNull) .flatMap(Collection::stream) .filter(RulesSeverityDetector::hasRuleDefinedLevel) - .collect(toMap(Rule::getId, rule -> rule.getDefaultConfiguration().getLevel())); + .collect(toMap(ReportingDescriptor::getId, rule -> Result.Level.valueOf(rule.getDefaultConfiguration().getLevel().name()))); } - private static Set<Extension> getExtensions(Run run) { + private static Set<ToolComponent> getExtensions(Run run) { return Optional.of(run) .map(Run::getTool) .map(Tool::getExtensions) .orElse(emptySet()); } - private static boolean hasRuleDefinedLevel(@Nullable Rule rule) { + private static boolean hasRuleDefinedLevel(@Nullable ReportingDescriptor rule) { return Optional.ofNullable(rule) - .map(Rule::getDefaultConfiguration) - .map(DefaultConfiguration::getLevel) + .map(ReportingDescriptor::getDefaultConfiguration) + .map(ReportingConfiguration::getLevel) .isPresent(); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RunMapper.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RunMapper.java index f94549c5ade..90787d379f2 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RunMapper.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RunMapper.java @@ -30,12 +30,11 @@ import org.slf4j.LoggerFactory; import org.sonar.api.batch.sensor.issue.NewExternalIssue; import org.sonar.api.batch.sensor.rule.NewAdHocRule; import org.sonar.api.scanner.ScannerSide; -import org.sonar.core.sarif.Driver; -import org.sonar.core.sarif.Extension; -import org.sonar.core.sarif.Result; -import org.sonar.core.sarif.Rule; -import org.sonar.core.sarif.Run; -import org.sonar.core.sarif.Tool; +import org.sonar.sarif.pojo.ReportingDescriptor; +import org.sonar.sarif.pojo.Result; +import org.sonar.sarif.pojo.Run; +import org.sonar.sarif.pojo.Tool; +import org.sonar.sarif.pojo.ToolComponent; import static java.util.Collections.emptyList; import static java.util.stream.Collectors.toSet; @@ -61,8 +60,8 @@ public class RunMapper { } String driverName = getToolDriverName(run); - Map<String, String> ruleSeveritiesByRuleId = detectRulesSeverities(run, driverName); - Map<String, String> ruleSeveritiesByRuleIdForNewCCT = detectRulesSeveritiesForNewTaxonomy(run, driverName); + Map<String, Result.Level> ruleSeveritiesByRuleId = detectRulesSeverities(run, driverName); + Map<String, Result.Level> ruleSeveritiesByRuleIdForNewCCT = detectRulesSeveritiesForNewTaxonomy(run, driverName); return new RunMapperResult() .newAdHocRules(toNewAdHocRules(run, driverName, ruleSeveritiesByRuleId, ruleSeveritiesByRuleIdForNewCCT)) @@ -78,13 +77,14 @@ public class RunMapper { return Optional.ofNullable(run) .map(Run::getTool) .map(Tool::getDriver) - .map(Driver::getName) + .map(ToolComponent::getName) .isPresent(); } - private List<NewAdHocRule> toNewAdHocRules(Run run, String driverName, Map<String, String> ruleSeveritiesByRuleId, Map<String, String> ruleSeveritiesByRuleIdForNewCCT) { - Set<Rule> driverRules = run.getTool().getDriver().getRules(); - Set<Rule> extensionRules = hasExtensions(run.getTool()) + private List<NewAdHocRule> toNewAdHocRules(Run run, String driverName, + Map<String, Result.Level> ruleSeveritiesByRuleId, Map<String, Result.Level> ruleSeveritiesByRuleIdForNewCCT) { + Set<ReportingDescriptor> driverRules = run.getTool().getDriver().getRules(); + Set<ReportingDescriptor> extensionRules = hasExtensions(run.getTool()) ? run.getTool().getExtensions().stream().filter(RunMapper::hasRules).flatMap(extension -> extension.getRules().stream()).collect(toSet()) : Set.of(); return Stream.concat(driverRules.stream(), extensionRules.stream()) @@ -97,11 +97,12 @@ public class RunMapper { return tool.getExtensions() != null && !tool.getExtensions().isEmpty(); } - private static boolean hasRules(Extension extension) { + private static boolean hasRules(ToolComponent extension) { return extension.getRules() != null && !extension.getRules().isEmpty(); } - private List<NewExternalIssue> toNewExternalIssues(Run run, String driverName, Map<String, String> ruleSeveritiesByRuleId, Map<String, String> ruleSeveritiesByRuleIdForNewCCT) { + private List<NewExternalIssue> toNewExternalIssues(Run run, String driverName, Map<String, Result.Level> ruleSeveritiesByRuleId, + Map<String, Result.Level> ruleSeveritiesByRuleIdForNewCCT) { return run.getResults() .stream() .map(result -> toNewExternalIssue(driverName, ruleSeveritiesByRuleId.get(result.getRuleId()), ruleSeveritiesByRuleIdForNewCCT.get(result.getRuleId()), result)) @@ -110,7 +111,7 @@ public class RunMapper { .toList(); } - private Optional<NewExternalIssue> toNewExternalIssue(String driverName, @Nullable String ruleSeverity, @Nullable String ruleSeverityForNewTaxonomy, Result result) { + private Optional<NewExternalIssue> toNewExternalIssue(String driverName, @Nullable Result.Level ruleSeverity, @Nullable Result.Level ruleSeverityForNewTaxonomy, Result result) { try { return Optional.of(resultMapper.mapResult(driverName, ruleSeverity, ruleSeverityForNewTaxonomy, result)); } catch (Exception exception) { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/Sarif210Importer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/Sarif210Importer.java index af293a3dc11..9491ed63ed8 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/Sarif210Importer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/Sarif210Importer.java @@ -19,7 +19,7 @@ */ package org.sonar.scanner.externalissue.sarif; -import org.sonar.core.sarif.Sarif210; +import org.sonar.sarif.pojo.SarifSchema210; public interface Sarif210Importer { @@ -28,5 +28,5 @@ public interface Sarif210Importer { * @param sarif210 the deserialized sarif report * @return the number of issues imported */ - SarifImportResults importSarif(Sarif210 sarif210); + SarifImportResults importSarif(SarifSchema210 sarif210); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/SarifIssuesImportSensor.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/SarifIssuesImportSensor.java index 8d9d1218687..ce333db5216 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/SarifIssuesImportSensor.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/SarifIssuesImportSensor.java @@ -39,8 +39,8 @@ import org.sonar.api.resources.Qualifiers; import org.sonar.api.scanner.ScannerSide; import org.sonar.api.scanner.sensor.ProjectSensor; import org.sonar.api.utils.MessageException; -import org.sonar.core.sarif.Sarif210; import org.sonar.core.sarif.SarifSerializer; +import org.sonar.sarif.pojo.SarifSchema210; import static java.lang.String.format; @@ -101,7 +101,7 @@ public class SarifIssuesImportSensor implements ProjectSensor { private SarifImportResults processReport(SensorContext context, String reportPath) throws NoSuchFileException { LOG.debug("Importing SARIF issues from '{}'", reportPath); Path reportFilePath = context.fileSystem().resolvePath(reportPath).toPath(); - Sarif210 sarifReport = sarifSerializer.deserialize(reportFilePath); + SarifSchema210 sarifReport = sarifSerializer.deserialize(reportFilePath); return sarifImporter.importSarif(sarifReport); } |