Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace sarif4k usage with Groovy JSON from gradleApi() in detekt-gra…
…dle-plugin (#6117)
- Loading branch information
1 parent
e65ef3b
commit f3f0ce4
Showing
3 changed files
with
164 additions
and
151 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
23 changes: 19 additions & 4 deletions
23
detekt-gradle-plugin/src/main/kotlin/io/gitlab/arturbosch/detekt/report/SarifReportMerger.kt
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,19 +1,34 @@ | ||
package io.gitlab.arturbosch.detekt.report | ||
|
||
import io.github.detekt.sarif4k.SarifSerializer | ||
import groovy.json.JsonOutput | ||
import groovy.json.JsonSlurper | ||
import java.io.File | ||
|
||
private typealias JsonObject = MutableMap<String, Any?> | ||
|
||
/** | ||
* A naive implementation to merge SARIF assuming all inputs are written by detekt. | ||
*/ | ||
object SarifReportMerger { | ||
|
||
fun merge(inputs: Collection<File>, output: File) { | ||
val sarifs = inputs.filter { it.exists() }.map { | ||
SarifSerializer.fromJson(it.readText()) | ||
@Suppress("UNCHECKED_CAST") | ||
(JsonSlurper().parse(it) as JsonObject) | ||
} | ||
val mergedResults = sarifs.flatMap { it.runs.single().results.orEmpty() } | ||
val mergedSarif = sarifs[0].copy(runs = listOf(sarifs[0].runs.single().copy(results = mergedResults))) | ||
output.writeText(SarifSerializer.toJson(mergedSarif)) | ||
val mergedSarif = sarifs[0].apply { this.runs.single().results = mergedResults } | ||
output.writeText(JsonOutput.prettyPrint(JsonOutput.toJson(mergedSarif))) | ||
} | ||
} | ||
|
||
private val JsonObject.runs: List<JsonObject> | ||
@Suppress("UNCHECKED_CAST") | ||
get() = this["runs"] as List<JsonObject> | ||
|
||
private var JsonObject.results: List<JsonObject>? | ||
@Suppress("UNCHECKED_CAST") | ||
get() = this["results"] as List<JsonObject>? | ||
set(value) { | ||
this["results"] = value | ||
} |
289 changes: 145 additions & 144 deletions
289
detekt-gradle-plugin/src/test/resources/output.sarif.json
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,156 +1,157 @@ | ||
{ | ||
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json", | ||
"version": "2.1.0", | ||
"runs": [ | ||
{ | ||
"originalUriBaseIds": { | ||
"%SRCROOT%": { | ||
"uri": "file:///Users/tester/detekt/" | ||
} | ||
}, | ||
"results": [ | ||
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json", | ||
"version": "2.1.0", | ||
"runs": [ | ||
{ | ||
"level": "warning", | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "TestFile.kt", | ||
"uriBaseId": "%SRCROOT%" | ||
}, | ||
"region": { | ||
"startColumn": 1, | ||
"startLine": 1 | ||
"originalUriBaseIds": { | ||
"%SRCROOT%": { | ||
"uri": "file:///Users/tester/detekt/" | ||
} | ||
} | ||
} | ||
], | ||
"message": { | ||
"text": "TestMessage" | ||
}, | ||
"ruleId": "detekt.TestSmellA.TestSmellA" | ||
}, | ||
{ | ||
"level": "warning", | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "TestFile.kt", | ||
"uriBaseId": "%SRCROOT%" | ||
}, | ||
"results": [ | ||
{ | ||
"level": "warning", | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "TestFile.kt", | ||
"uriBaseId": "%SRCROOT%" | ||
}, | ||
"region": { | ||
"startColumn": 1, | ||
"startLine": 1 | ||
} | ||
} | ||
} | ||
], | ||
"message": { | ||
"text": "TestMessage" | ||
}, | ||
"ruleId": "detekt.TestSmellA.TestSmellA" | ||
}, | ||
"region": { | ||
"startColumn": 1, | ||
"startLine": 1 | ||
} | ||
} | ||
} | ||
], | ||
"message": { | ||
"text": "TestMessage" | ||
}, | ||
"ruleId": "detekt.TestSmellB.TestSmellB" | ||
}, | ||
{ | ||
"level": "warning", | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "TestFile.kt", | ||
"uriBaseId": "%SRCROOT%" | ||
{ | ||
"level": "warning", | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "TestFile.kt", | ||
"uriBaseId": "%SRCROOT%" | ||
}, | ||
"region": { | ||
"startColumn": 1, | ||
"startLine": 1 | ||
} | ||
} | ||
} | ||
], | ||
"message": { | ||
"text": "TestMessage" | ||
}, | ||
"ruleId": "detekt.TestSmellB.TestSmellB" | ||
}, | ||
"region": { | ||
"startColumn": 1, | ||
"startLine": 1 | ||
} | ||
} | ||
} | ||
], | ||
"message": { | ||
"text": "TestMessage" | ||
}, | ||
"ruleId": "detekt.TestSmellC.TestSmellC" | ||
}, | ||
{ | ||
"level": "warning", | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "TestFile.kt", | ||
"uriBaseId": "%SRCROOT%" | ||
{ | ||
"level": "warning", | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "TestFile.kt", | ||
"uriBaseId": "%SRCROOT%" | ||
}, | ||
"region": { | ||
"startColumn": 1, | ||
"startLine": 1 | ||
} | ||
} | ||
} | ||
], | ||
"message": { | ||
"text": "TestMessage" | ||
}, | ||
"ruleId": "detekt.TestSmellC.TestSmellC" | ||
}, | ||
"region": { | ||
"startColumn": 1, | ||
"startLine": 1 | ||
} | ||
} | ||
} | ||
], | ||
"message": { | ||
"text": "TestMessage" | ||
}, | ||
"ruleId": "detekt.TestSmellD.TestSmellD" | ||
}, | ||
{ | ||
"level": "warning", | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "TestFile.kt", | ||
"uriBaseId": "%SRCROOT%" | ||
{ | ||
"level": "warning", | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "TestFile.kt", | ||
"uriBaseId": "%SRCROOT%" | ||
}, | ||
"region": { | ||
"startColumn": 1, | ||
"startLine": 1 | ||
} | ||
} | ||
} | ||
], | ||
"message": { | ||
"text": "TestMessage" | ||
}, | ||
"ruleId": "detekt.TestSmellD.TestSmellD" | ||
}, | ||
"region": { | ||
"startColumn": 1, | ||
"startLine": 1 | ||
} | ||
} | ||
} | ||
], | ||
"message": { | ||
"text": "TestMessage" | ||
}, | ||
"ruleId": "detekt.TestSmellE.TestSmellE" | ||
}, | ||
{ | ||
"level": "warning", | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "TestFile.kt", | ||
"uriBaseId": "%SRCROOT%" | ||
{ | ||
"level": "warning", | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "TestFile.kt", | ||
"uriBaseId": "%SRCROOT%" | ||
}, | ||
"region": { | ||
"startColumn": 1, | ||
"startLine": 1 | ||
} | ||
} | ||
} | ||
], | ||
"message": { | ||
"text": "TestMessage" | ||
}, | ||
"ruleId": "detekt.TestSmellE.TestSmellE" | ||
}, | ||
"region": { | ||
"startColumn": 1, | ||
"startLine": 1 | ||
{ | ||
"level": "warning", | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "TestFile.kt", | ||
"uriBaseId": "%SRCROOT%" | ||
}, | ||
"region": { | ||
"startColumn": 1, | ||
"startLine": 1 | ||
} | ||
} | ||
} | ||
], | ||
"message": { | ||
"text": "TestMessage" | ||
}, | ||
"ruleId": "detekt.TestSmellF.TestSmellF" | ||
} | ||
], | ||
"tool": { | ||
"driver": { | ||
"downloadUri": "https://github.com/detekt/detekt/releases/download/v1.0.0/detekt", | ||
"fullName": "detekt", | ||
"guid": "022ca8c2-f6a2-4c95-b107-bb72c43263f3", | ||
"informationUri": "https://detekt.dev", | ||
"language": "en", | ||
"name": "detekt", | ||
"organization": "detekt", | ||
"rules": [ | ||
|
||
], | ||
"semanticVersion": "1.0.0", | ||
"version": "1.0.0" | ||
} | ||
} | ||
} | ||
], | ||
"message": { | ||
"text": "TestMessage" | ||
}, | ||
"ruleId": "detekt.TestSmellF.TestSmellF" | ||
} | ||
], | ||
"tool": { | ||
"driver": { | ||
"downloadUri": "https://github.com/detekt/detekt/releases/download/v1.0.0/detekt", | ||
"fullName": "detekt", | ||
"guid": "022ca8c2-f6a2-4c95-b107-bb72c43263f3", | ||
"informationUri": "https://detekt.dev", | ||
"language": "en", | ||
"name": "detekt", | ||
"organization": "detekt", | ||
"rules": [ | ||
], | ||
"semanticVersion": "1.0.0", | ||
"version": "1.0.0" | ||
} | ||
} | ||
} | ||
] | ||
] | ||
} |