Skip to content

Commit 286c605

Browse files
thomas-serre-sonarsourcesonartech
authored andcommitted
SONARPY-3086 Send rule IDs that are suppressed with NOSONAR through telemetry (#382)
GitOrigin-RevId: 1ab10f987c10c9145f28a8d6efa52e3c87f7f6ca
1 parent 370ebf2 commit 286c605

5 files changed

Lines changed: 31 additions & 12 deletions

File tree

python-commons/src/main/java/org/sonar/plugins/python/IPynbSensor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public void execute(SensorContext context) {
108108
} else {
109109
processNotebooksFiles(pythonFiles, context);
110110
}
111+
sensorTelemetryStorage.updateMetric(TelemetryMetricKey.NOSONAR_RULE_ID_KEYS, noSonarLineInfoCollector.getSuppressedRuleIds());
111112
sensorTelemetryStorage.send(context);
112113
}
113114

python-commons/src/main/java/org/sonar/plugins/python/PythonSensor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void execute(SensorContext context) {
141141
scanner.execute(pythonFiles, context);
142142

143143
updateDatabricksTelemetry(scanner);
144-
144+
sensorTelemetryStorage.updateMetric(TelemetryMetricKey.NOSONAR_RULE_ID_KEYS, noSonarLineInfoCollector.getSuppressedRuleIds());
145145
sensorTelemetryStorage.send(context);
146146
durationReport.stop();
147147
}

python-commons/src/main/java/org/sonar/plugins/python/TelemetryMetricKey.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public enum TelemetryMetricKey {
2626
PYTHON_DATABRICKS_FOUND("python.notebook.databricks.python"),
2727
IPYNB_DATABRICKS_FOUND("python.notebook.databricks.ipynb"),
2828
PYTHON_DEPENDENCIES("python.dependencies"),
29-
PYTHON_DEPENDENCIES_FORMAT_VERSION("python.dependencies.format_version");
29+
PYTHON_DEPENDENCIES_FORMAT_VERSION("python.dependencies.format_version"),
30+
NOSONAR_RULE_ID_KEYS("python.nosonar.rule_ids");
3031

3132
private final String key;
3233

python-commons/src/main/java/org/sonar/plugins/python/nosonar/NoSonarLineInfoCollector.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,12 @@ private Set<String> parseNoSonarRules(String noSonarCommentLine) {
160160
return rules;
161161
}
162162

163+
public String getSuppressedRuleIds(){
164+
return componentKeyToNoSonarLineInfoMap.values().stream()
165+
.flatMap(map -> map.values().stream())
166+
.flatMap(noSonarLineInfo -> noSonarLineInfo.suppressedRuleKeys().stream())
167+
.distinct()
168+
.sorted()
169+
.collect(Collectors.joining(","));
170+
}
163171
}

python-commons/src/test/java/org/sonar/plugins/python/nosonar/NoSonarLineInfoCollectorTest.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class NoSonarLineInfoCollectorTest {
3030

3131
@ParameterizedTest
3232
@MethodSource("provideCollectorParameters")
33-
void collector_test(String code, Map<Integer, NoSonarLineInfo> expectedLineInfos, Set<Integer> expectedEmptyNoSonarLines) {
33+
void collector_test(String code, Map<Integer, NoSonarLineInfo> expectedLineInfos, Set<Integer> expectedEmptyNoSonarLines, String expectedSuppressedRuleIds) {
3434
var astNode = PythonParser.create().parse(code);
3535

3636
var fileInput = new PythonTreeMaker().fileInput(astNode);
@@ -48,45 +48,53 @@ private static Stream<Arguments> provideCollectorParameters() {
4848
a = 1 # NOSONAR(something)
4949
""",
5050
Map.of(1, new NoSonarLineInfo(1, Set.of("something"))),
51-
Set.of()
51+
Set.of(),
52+
"something"
5253
),
5354
Arguments.of("""
5455
a = 1 # NOSONAR(one, two) some text
5556
""",
5657
Map.of(1, new NoSonarLineInfo(1, Set.of("one", "two"))),
57-
Set.of()
58+
Set.of(),
59+
"one,two"
5860
),
5961
Arguments.of("""
6062
a = 1 # NOSONAR()
6163
""",
6264
Map.of(1, new NoSonarLineInfo(1, Set.of())),
63-
Set.of(1)
65+
Set.of(1),
66+
""
6467
),
6568
Arguments.of("""
6669
a = 1 # NOSONAR(something,)
6770
""",
6871
Map.of(1, new NoSonarLineInfo(1, Set.of("something"))),
69-
Set.of()
72+
Set.of(),
73+
"something"
7074
),
7175
Arguments.of("""
7276
a = 1 # NOSONAR
7377
""",
7478
Map.of(1, new NoSonarLineInfo(1, Set.of())),
75-
Set.of(1)
79+
Set.of(1),
80+
""
7681
),
7782
Arguments.of("""
7883
a = 1 # NOSONAR some text
7984
""",
8085
Map.of(1, new NoSonarLineInfo(1, Set.of())),
81-
Set.of(1)
86+
Set.of(1),
87+
""
8288
),
8389
Arguments.of("a = 1 # NOSONAR some text",
8490
Map.of(1, new NoSonarLineInfo(1, Set.of())),
85-
Set.of(1)
91+
Set.of(1),
92+
""
8693
),
8794
Arguments.of("# NOSONAR some text",
8895
Map.of(1, new NoSonarLineInfo(1, Set.of())),
89-
Set.of(1)
96+
Set.of(1),
97+
""
9098
),
9199
Arguments.of("""
92100
""\"
@@ -102,7 +110,8 @@ private static Stream<Arguments> provideCollectorParameters() {
102110
4, new NoSonarLineInfo(4, Set.of()),
103111
5, new NoSonarLineInfo(5, Set.of())
104112
),
105-
Set.of(1, 2, 3, 4, 5)
113+
Set.of(1, 2, 3, 4, 5),
114+
""
106115
)
107116
);
108117
}

0 commit comments

Comments
 (0)