From 5dfd8ff2961f409920eadf814a3bb75aa1967c9c Mon Sep 17 00:00:00 2001 From: Marcel Gaupp Date: Sun, 10 Nov 2024 03:19:28 +0100 Subject: [PATCH] Extract location processing method --- .../scaparser/strategy/sarif/SarifParser.java | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/tum/cit/aet/artemis/programming/service/localci/scaparser/strategy/sarif/SarifParser.java b/src/main/java/de/tum/cit/aet/artemis/programming/service/localci/scaparser/strategy/sarif/SarifParser.java index d3d721ba8db9..06252cb8b6d3 100644 --- a/src/main/java/de/tum/cit/aet/artemis/programming/service/localci/scaparser/strategy/sarif/SarifParser.java +++ b/src/main/java/de/tum/cit/aet/artemis/programming/service/localci/scaparser/strategy/sarif/SarifParser.java @@ -54,6 +54,9 @@ private InformationMissingException(String message) { } } + private record FileLocation(String path, int startLine, int endLine, int startColumn, int endColumn) { + } + private final ObjectMapper objectMapper = new ObjectMapper(); private final StaticCodeAnalysisTool tool; @@ -104,18 +107,9 @@ private StaticCodeAnalysisIssue tryProcessResult(Result result, ToolComponent dr } private StaticCodeAnalysisIssue processResult(Result result, ToolComponent driver, Map ruleOfId) throws SarifFormatException { - PhysicalLocation location = result.getLocations().flatMap(locations -> locations.stream().findFirst()).flatMap(Location::getPhysicalLocation) + FileLocation fileLocation = result.getLocations().flatMap(locations -> locations.stream().findFirst()).flatMap(Location::getPhysicalLocation).map(this::extractLocation) .orElseThrow(() -> new InformationMissingException("Location needed")); - URI uri = URI.create(location.getArtifactLocation().flatMap(ArtifactLocation::getUri).orElseThrow(() -> new InformationMissingException("File path needed"))); - String path = uri.getPath(); - - Region region = location.getRegion().orElseThrow(() -> new SarifFormatException("Region must be present")); - int startLine = region.getStartLine().orElseThrow(() -> new InformationMissingException("Text region needed")); - int startColumn = region.getStartColumn().orElse(1); - int endLine = region.getEndLine().orElse(startLine); - int endColumn = region.getEndColumn().orElse(startColumn + 1); - String ruleId = getRuleId(result); Optional ruleIndex = getRuleIndex(result); @@ -130,7 +124,21 @@ private StaticCodeAnalysisIssue processResult(Result result, ToolComponent drive String message = findMessage(result, driver, rule); - return new StaticCodeAnalysisIssue(path, startLine, endLine, startColumn, endColumn, ruleId, category, message, level.toString(), null); + return new StaticCodeAnalysisIssue(fileLocation.path(), fileLocation.startLine(), fileLocation.endLine(), fileLocation.startColumn(), fileLocation.endColumn(), ruleId, + category, message, level.toString(), null); + } + + private FileLocation extractLocation(PhysicalLocation location) { + URI uri = URI.create(location.getArtifactLocation().flatMap(ArtifactLocation::getUri).orElseThrow(() -> new InformationMissingException("File path needed"))); + + Region region = location.getRegion().orElseThrow(() -> new SarifFormatException("Region must be present")); + + int startLine = region.getStartLine().orElseThrow(() -> new InformationMissingException("Text region needed")); + int startColumn = region.getStartColumn().orElse(1); + int endLine = region.getEndLine().orElse(startLine); + int endColumn = region.getEndColumn().orElse(startColumn + 1); + + return new FileLocation(uri.getPath(), startLine, endLine, startColumn, endColumn); } private static String getRuleId(Result result) throws SarifFormatException {