-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Development: Add SARIF parser (#9609)
- Loading branch information
Showing
33 changed files
with
1,383 additions
and
286 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 6 additions & 73 deletions
79
src/main/java/de/tum/cit/aet/artemis/programming/service/localci/scaparser/ReportParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,27 @@ | ||
package de.tum.cit.aet.artemis.programming.service.localci.scaparser; | ||
|
||
import static de.tum.cit.aet.artemis.programming.service.localci.scaparser.utils.ReportUtils.createErrorReport; | ||
import static de.tum.cit.aet.artemis.programming.service.localci.scaparser.utils.ReportUtils.createFileTooLargeReport; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import de.tum.cit.aet.artemis.programming.dto.StaticCodeAnalysisReportDTO; | ||
import de.tum.cit.aet.artemis.programming.service.localci.scaparser.exception.ParserException; | ||
import de.tum.cit.aet.artemis.programming.service.localci.scaparser.exception.UnsupportedToolException; | ||
import de.tum.cit.aet.artemis.programming.service.localci.scaparser.strategy.ParserPolicy; | ||
import de.tum.cit.aet.artemis.programming.service.localci.scaparser.strategy.ParserStrategy; | ||
import de.tum.cit.aet.artemis.programming.service.localci.scaparser.utils.FileUtils; | ||
|
||
/** | ||
* Public API for parsing of static code analysis reports | ||
*/ | ||
public class ReportParser { | ||
|
||
private final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
// Reports that are bigger then the threshold will not be parsed | ||
// and an issue will be generated. The unit is in megabytes. | ||
private static final int STATIC_CODE_ANALYSIS_REPORT_FILESIZE_LIMIT_IN_MB = 1; | ||
|
||
/** | ||
* Transform a given static code analysis report into a JSON representation. | ||
* All supported tools share the same JSON format. | ||
* | ||
* @param file Reference to the static code analysis report | ||
* @return Static code analysis report represented as a JSON String | ||
* @throws ParserException - If an exception occurs that is not already handled by the parser itself, e.g. caused by the json-parsing | ||
*/ | ||
public String transformToJSONReport(File file) throws ParserException { | ||
try { | ||
StaticCodeAnalysisReportDTO report = transformToReport(file); | ||
return mapper.writeValueAsString(report); | ||
} | ||
catch (Exception e) { | ||
throw new ParserException(e.getMessage(), e); | ||
} | ||
} | ||
|
||
/** | ||
* Transform a given static code analysis report given as a file into a plain Java object. | ||
* | ||
* @param file Reference to the static code analysis report | ||
* @return Static code analysis report represented as a plain Java object | ||
*/ | ||
public StaticCodeAnalysisReportDTO transformToReport(File file) { | ||
if (file == null) { | ||
throw new IllegalArgumentException("File must not be null"); | ||
} | ||
|
||
// The static code analysis parser only supports xml files. | ||
if (!FileUtils.getExtension(file).equals("xml")) { | ||
throw new IllegalArgumentException("File must be xml format"); | ||
} | ||
try { | ||
// Reject any file larger than the given threshold | ||
if (FileUtils.isFilesizeGreaterThan(file, STATIC_CODE_ANALYSIS_REPORT_FILESIZE_LIMIT_IN_MB)) { | ||
return createFileTooLargeReport(file.getName()); | ||
} | ||
|
||
return getReport(file); | ||
} | ||
catch (Exception e) { | ||
return createErrorReport(file.getName(), e); | ||
} | ||
} | ||
private static final ParserPolicy parserPolicy = new ParserPolicy(); | ||
|
||
/** | ||
* Builds the document using the provided file and parses it to a Report object using ObjectMapper. | ||
* Builds the document using the provided string and parses it to a Report object. | ||
* | ||
* @param file File referencing the static code analysis report | ||
* @param reportContent String containing the static code analysis report | ||
* @param fileName filename of the report used for configuring a parser | ||
* @return Report containing the static code analysis issues | ||
* @throws UnsupportedToolException if the static code analysis tool which created the report is not supported | ||
* @throws IOException if the file could not be read | ||
*/ | ||
public static StaticCodeAnalysisReportDTO getReport(File file) throws IOException { | ||
String xmlContent = Files.readString(file.toPath()); | ||
return getReport(xmlContent, file.getName()); | ||
} | ||
|
||
public static StaticCodeAnalysisReportDTO getReport(String xmlContent, String fileName) { | ||
ParserPolicy parserPolicy = new ParserPolicy(); | ||
public static StaticCodeAnalysisReportDTO getReport(String reportContent, String fileName) { | ||
ParserStrategy parserStrategy = parserPolicy.configure(fileName); | ||
return parserStrategy.parse(xmlContent); | ||
return parserStrategy.parse(reportContent); | ||
} | ||
} |
16 changes: 0 additions & 16 deletions
16
.../tum/cit/aet/artemis/programming/service/localci/scaparser/exception/ParserException.java
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
.../cit/aet/artemis/programming/service/localci/scaparser/format/sarif/ArtifactLocation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package de.tum.cit.aet.artemis.programming.service.localci.scaparser.format.sarif; | ||
|
||
import java.util.Optional; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
||
/** | ||
* Specifies the location of an artifact. | ||
* | ||
* @param uri A string containing a valid relative or absolute URI. | ||
*/ | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public record ArtifactLocation(String uri) { | ||
|
||
/** | ||
* A string containing a valid relative or absolute URI. | ||
*/ | ||
public Optional<String> getOptionalUri() { | ||
return Optional.ofNullable(uri); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
.../aet/artemis/programming/service/localci/scaparser/format/sarif/GlobalMessageStrings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package de.tum.cit.aet.artemis.programming.service.localci.scaparser.format.sarif; | ||
|
||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAnySetter; | ||
|
||
/** | ||
* A dictionary, each of whose keys is a resource identifier and each of whose values is a multiformatMessageString object, which holds message strings in plain text and | ||
* (optionally) Markdown format. The strings can include placeholders, which can be used to construct a message in combination with an arbitrary number of additional string | ||
* arguments. | ||
*/ | ||
public record GlobalMessageStrings(@JsonAnySetter Map<String, MultiformatMessageString> additionalProperties) { | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...a/de/tum/cit/aet/artemis/programming/service/localci/scaparser/format/sarif/Location.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package de.tum.cit.aet.artemis.programming.service.localci.scaparser.format.sarif; | ||
|
||
import java.util.Optional; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
||
/** | ||
* A location within a programming artifact. | ||
* | ||
* @param physicalLocation A physical location relevant to a result. Specifies a reference to a programming artifact together with a range of bytes or characters within that | ||
* artifact. | ||
*/ | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public record Location(PhysicalLocation physicalLocation) { | ||
|
||
/** | ||
* A physical location relevant to a result. Specifies a reference to a programming artifact together with a range of bytes or characters within that artifact. | ||
*/ | ||
public Optional<PhysicalLocation> getOptionalPhysicalLocation() { | ||
return Optional.ofNullable(physicalLocation); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...va/de/tum/cit/aet/artemis/programming/service/localci/scaparser/format/sarif/Message.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package de.tum.cit.aet.artemis.programming.service.localci.scaparser.format.sarif; | ||
|
||
import java.util.Optional; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
||
/** | ||
* Encapsulates a message intended to be read by the end user. | ||
* | ||
* @param text A plain text message string. | ||
* @param id The identifier for this message. | ||
*/ | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public record Message(String text, String id) { | ||
|
||
/** | ||
* A plain text message string. | ||
*/ | ||
public Optional<String> getOptionalText() { | ||
return Optional.ofNullable(text); | ||
} | ||
|
||
/** | ||
* The identifier for this message. | ||
*/ | ||
public Optional<String> getOptionalId() { | ||
return Optional.ofNullable(id); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...um/cit/aet/artemis/programming/service/localci/scaparser/format/sarif/MessageStrings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package de.tum.cit.aet.artemis.programming.service.localci.scaparser.format.sarif; | ||
|
||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAnySetter; | ||
|
||
/** | ||
* A set of name/value pairs with arbitrary names. Each value is a multiformatMessageString object, which holds message strings in plain text and (optionally) Markdown format. The | ||
* strings can include placeholders, which can be used to construct a message in combination with an arbitrary number of additional string arguments. | ||
*/ | ||
public record MessageStrings(@JsonAnySetter Map<String, MultiformatMessageString> additionalProperties) { | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
.../artemis/programming/service/localci/scaparser/format/sarif/MultiformatMessageString.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package de.tum.cit.aet.artemis.programming.service.localci.scaparser.format.sarif; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
||
/** | ||
* A message string or message format string rendered in multiple formats. | ||
* | ||
* @param text A plain text message string or format string. | ||
* (Required) | ||
*/ | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public record MultiformatMessageString(String text) { | ||
} |
30 changes: 30 additions & 0 deletions
30
.../cit/aet/artemis/programming/service/localci/scaparser/format/sarif/PhysicalLocation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package de.tum.cit.aet.artemis.programming.service.localci.scaparser.format.sarif; | ||
|
||
import java.util.Optional; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
||
/** | ||
* A physical location relevant to a result. Specifies a reference to a programming artifact together with a range of bytes or characters within that artifact. | ||
* | ||
* @param artifactLocation Specifies the location of an artifact. | ||
* @param region A region within an artifact where a result was detected. | ||
*/ | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public record PhysicalLocation(ArtifactLocation artifactLocation, Region region) { | ||
|
||
/** | ||
* Specifies the location of an artifact. | ||
*/ | ||
public Optional<ArtifactLocation> getOptionalArtifactLocation() { | ||
return Optional.ofNullable(artifactLocation); | ||
} | ||
|
||
/** | ||
* A region within an artifact where a result was detected. | ||
*/ | ||
public Optional<Region> getOptionalRegion() { | ||
return Optional.ofNullable(region); | ||
} | ||
|
||
} |
Oops, something went wrong.