forked from detekt/detekt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Results.kt
65 lines (57 loc) · 2.32 KB
/
Results.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package io.github.detekt.report.sarif
import io.github.detekt.psi.toUnifiedString
import io.github.detekt.sarif4k.ArtifactLocation
import io.github.detekt.sarif4k.Level
import io.github.detekt.sarif4k.Message
import io.github.detekt.sarif4k.PhysicalLocation
import io.github.detekt.sarif4k.Region
import io.gitlab.arturbosch.detekt.api.Detektion
import io.gitlab.arturbosch.detekt.api.Finding
import io.gitlab.arturbosch.detekt.api.Location
import io.gitlab.arturbosch.detekt.api.RuleSetId
import io.gitlab.arturbosch.detekt.api.SeverityLevel
internal fun toResults(detektion: Detektion): List<io.github.detekt.sarif4k.Result> =
detektion.findings.flatMap { (ruleSetId, findings) ->
findings.map { it.toResult(ruleSetId) }
}
private fun SeverityLevel.toResultLevel() = when (this) {
SeverityLevel.ERROR -> Level.Error
SeverityLevel.WARNING -> Level.Warning
SeverityLevel.INFO -> Level.Note
}
private fun Finding.toResult(ruleSetId: RuleSetId): io.github.detekt.sarif4k.Result {
val code = entity.ktElement?.containingFile?.text
return io.github.detekt.sarif4k.Result(
ruleID = "detekt.$ruleSetId.$id",
level = severity.toResultLevel(),
locations = (listOf(location) + references.map { it.location }).map { it.toLocation(code) }.distinct().toList(),
message = Message(text = messageOrDescription())
)
}
private fun Location.toLocation(code: String?): io.github.detekt.sarif4k.Location {
var endLine: Long? = null
var endColumn: Long? = null
if (code != null) {
val snippet = code.take(text.end).split('\n')
endLine = snippet.size.toLong()
endColumn = snippet.last().length.toLong() + 1
}
return io.github.detekt.sarif4k.Location(
physicalLocation = PhysicalLocation(
region = Region(
startLine = source.line.toLong(),
startColumn = source.column.toLong(),
endLine = endLine,
endColumn = endColumn
),
artifactLocation = if (filePath.relativePath != null) {
ArtifactLocation(
uri = filePath.relativePath?.toUnifiedString(),
uriBaseID = SRCROOT
)
} else {
ArtifactLocation(uri = filePath.absolutePath.toUnifiedString())
}
)
)
}