Skip to content

Commit

Permalink
Replace sarif4k usage with Groovy JSON from gradleApi() in detekt-gra…
Browse files Browse the repository at this point in the history
…dle-plugin (#6117)
  • Loading branch information
TWiStErRob committed May 22, 2023
1 parent e65ef3b commit f3f0ce4
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 151 deletions.
3 changes: 0 additions & 3 deletions detekt-gradle-plugin/build.gradle.kts
Expand Up @@ -70,9 +70,6 @@ dependencies {
compileOnly(libs.kotlin.gradle)
compileOnly(libs.kotlin.gradlePluginApi)
testFixturesCompileOnly("org.jetbrains:annotations:24.0.1")
implementation(libs.sarif4k) {
exclude("org.jetbrains.kotlin")
}
compileOnly("io.gitlab.arturbosch.detekt:detekt-cli:1.22.0")

testKitRuntimeOnly(libs.kotlin.gradle)
Expand Down
@@ -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 detekt-gradle-plugin/src/test/resources/output.sarif.json
@@ -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"
}
}
}
]
]
}

0 comments on commit f3f0ce4

Please sign in to comment.