Skip to content

Commit 69b23ee

Browse files
authored
Add logic for fixes from SARIF into FixPlugin (#474)
### What's done: * Enable sarif library * Refactor methods - split into little one * Add test for sarif mode in FixPlugin
1 parent bc2f039 commit 69b23ee

9 files changed

Lines changed: 323 additions & 59 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
3+
"version": "2.1.0",
4+
"runs": [
5+
{
6+
"originalUriBaseIds": {
7+
"%SRCROOT%": {
8+
"uri": "."
9+
}
10+
},
11+
"results": [
12+
{
13+
"fixes": [
14+
{
15+
"artifactChanges": [
16+
{
17+
"artifactLocation": {
18+
"uri": "src/main/kotlin/com/saveourtool/save/Example1Test.kt"
19+
},
20+
"replacements": [
21+
{
22+
"deletedRegion": {
23+
"endColumn": 19,
24+
"startColumn": 9,
25+
"startLine": 1
26+
},
27+
"insertedContent": {
28+
"text": "com.saveourtool.save"
29+
}
30+
}
31+
]
32+
}
33+
],
34+
"description": {
35+
"text": "Package name is incorrect"
36+
}
37+
}
38+
],
39+
"locations": [
40+
{
41+
"physicalLocation": {
42+
"artifactLocation": {
43+
"uri": "src/main/kotlin/com/saveourtool/save/Example1Test.kt",
44+
"uriBaseId": "%SRCROOT%"
45+
},
46+
"region": {
47+
"snippet": {
48+
"text": "test.smoke"
49+
},
50+
"endColumn": 19,
51+
"startColumn": 9,
52+
"startLine": 1
53+
}
54+
}
55+
}
56+
],
57+
"message": {
58+
"text": "Package name is incorrect"
59+
},
60+
"ruleId": "diktat-ruleset:identifier-naming"
61+
},
62+
{
63+
"fixes": [
64+
{
65+
"artifactChanges": [
66+
{
67+
"artifactLocation": {
68+
"uri": "src/main/kotlin/com/saveourtool/save/Example1Test.kt"
69+
},
70+
"replacements": [
71+
{
72+
"deletedRegion": {
73+
"endColumn": 8,
74+
"startColumn": 7,
75+
"startLine": 3
76+
},
77+
"insertedContent": {
78+
"text": "E"
79+
}
80+
}
81+
]
82+
}
83+
],
84+
"description": {
85+
"text": "Class name should be in PascalCase"
86+
}
87+
}
88+
],
89+
"locations": [
90+
{
91+
"physicalLocation": {
92+
"artifactLocation": {
93+
"uri": "src/main/kotlin/com/saveourtool/save/Example1Test.kt",
94+
"uriBaseId": "%SRCROOT%"
95+
},
96+
"region": {
97+
"snippet": {
98+
"text": "e"
99+
},
100+
"endColumn": 8,
101+
"startColumn": 7,
102+
"startLine": 3
103+
}
104+
}
105+
}
106+
],
107+
"message": {
108+
"text": "Class name should be in PascalCase"
109+
},
110+
"ruleId": "diktat-ruleset:identifier-naming"
111+
}
112+
],
113+
"tool": {
114+
"driver": {
115+
"downloadUri": "https://github.com/pinterest/ktlint/releases/tag/0.42.0",
116+
"fullName": "ktlint",
117+
"informationUri": "https://github.com/pinterest/ktlint/",
118+
"language": "en",
119+
"name": "ktlint",
120+
"organization": "pinterest",
121+
"rules": [
122+
],
123+
"semanticVersion": "0.42.0",
124+
"version": "0.42.0"
125+
}
126+
}
127+
}
128+
]
129+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[general]
2+
tags = ["smoke"]
3+
description = "SARIF fix objects: Smoke tests for diktat"
4+
suiteName = "SARIF fix objects - Autofix"
5+
6+
[fix]
7+
actualFixFormat = "SARIF"
8+
actualFixSarifFileName = "save-fixes.sarif"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.saveourtool.save
2+
3+
class Example {
4+
val isValid = true
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package test.smoke
2+
3+
class example {
4+
val isValid = true
5+
}

gradle/libs.versions.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ multiplatform-diff = "0.4.0"
1212
kotlinpoet = "1.12.0"
1313
kotest = "5.5.4"
1414
sarif4k = "0.3.0"
15+
sarif-utils = "0.1.0"
1516

1617
[plugins]
1718
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
@@ -37,3 +38,5 @@ square-kotlinpoet = { module = "com.squareup:kotlinpoet", version.ref = "kotlinp
3738
diktat-gradle-plugin = { module = "org.cqfn.diktat:diktat-gradle-plugin", version.ref = "diktat" }
3839
sarif4k = { module = "io.github.detekt.sarif4k:sarif4k", version.ref = "sarif4k" }
3940
kotest-assertions-core = { module = "io.kotest:kotest-assertions-core", version.ref = "kotest" }
41+
sarif-utils = { module = "com.saveourtool.sarifutils:fixpatches", version.ref = "sarif-utils" }
42+
sarif-utils-jvm = { module = "com.saveourtool.sarifutils:fixpatches-jvm", version.ref = "sarif-utils" }

save-core/src/commonNonJsTest/kotlin/com/saveourtool/save/core/integration/ClassicFixTest.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ClassicFixTest {
1919
runTestsWithDiktat(
2020
listOf(
2121
"fix/save.toml"
22-
), 5
22+
), 6
2323
)
2424
}
2525

@@ -67,4 +67,13 @@ class ClassicFixTest {
6767
), 1
6868
)
6969
}
70+
71+
@Test
72+
fun `execute fix plugin in sarif mode`() {
73+
runTestsWithDiktat(
74+
listOf(
75+
"fix/sarif/src/main/kotlin/com/saveourtool/save"
76+
), 1
77+
)
78+
}
7079
}

save-plugins/fix-plugin/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ kotlin {
1111
implementation(projects.saveCommon)
1212
implementation(libs.multiplatform.diff)
1313
implementation(libs.kotlinx.serialization.core)
14+
implementation(libs.sarif.utils)
1415
}
1516
}
1617
}

0 commit comments

Comments
 (0)