Skip to content

Commit 24e51d2

Browse files
marc-jasper-sonarsourcesonartech
authored andcommitted
SONARPY-3808 Add telemetry key that checks if sonar.tests is set (#865)
GitOrigin-RevId: e3ef416f52432d4f59fbf45d83fde18ad6f09862
1 parent 192c080 commit 24e51d2

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ public void execute(SensorContext context) {
156156
scanner.execute(pythonFiles, context);
157157
Duration sensorTime = Duration.between(sensorStartTime, Instant.now());
158158

159+
updateSonarTestsTelemetry(context);
159160
updateDatabricksTelemetry(scanner);
160161
updateTypeInferenceTelemetry(scanner);
161162
updateTestFileTelemetry(scanner);
@@ -228,6 +229,14 @@ private void updateNamespacePackageTelemetry(PythonIndexer pythonIndexer) {
228229
}
229230
}
230231

232+
private void updateSonarTestsTelemetry(SensorContext context) {
233+
if (context.runtime().getProduct() == SonarProduct.SONARLINT) {
234+
return;
235+
}
236+
boolean sonarTestsSet = context.config().get("sonar.tests").isPresent();
237+
sensorTelemetryStorage.updateMetric(TelemetryMetricKey.PYTHON_SONAR_TESTS_SET, sonarTestsSet);
238+
}
239+
231240
private void updatePythonVersionTelemetry(SensorContext context, String[] pythonVersionParameter) {
232241
if (context.runtime().getProduct() == SonarProduct.SONARLINT) {
233242
return;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public enum TelemetryMetricKey {
5959
PYTHON_MAIN_FILES_MISCLASSIFIED_IMPORT_BASED_ONLY("python.files.main.misclassified_import_based_only"),
6060
PYTHON_MAIN_LINES_MISCLASSIFIED_IMPORT_BASED_ONLY("python.lines.main.misclassified_import_based_only"),
6161
PYTHON_MAIN_FILES_MISCLASSIFIED_PATH_BASED_ONLY("python.files.main.misclassified_path_based_only"),
62-
PYTHON_MAIN_LINES_MISCLASSIFIED_PATH_BASED_ONLY("python.lines.main.misclassified_path_based_only");
62+
PYTHON_MAIN_LINES_MISCLASSIFIED_PATH_BASED_ONLY("python.lines.main.misclassified_path_based_only"),
63+
PYTHON_SONAR_TESTS_SET("python.sonar_tests.set");
6364

6465
private final String key;
6566

python-commons/src/test/java/org/sonar/plugins/python/PythonSensorTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,35 @@ void send_telemetry_no_version() {
15311531
verify(contextSpy, times(1)).addTelemetryProperty(TelemetryMetricKey.PYTHON_VERSION_SET_KEY.key(), "0");
15321532
}
15331533

1534+
@Test
1535+
void send_telemetry_sonar_tests_set() {
1536+
activeRules = new ActiveRulesBuilder()
1537+
.addRule(new NewActiveRule.Builder()
1538+
.setRuleKey(RuleKey.of(PythonRuleRepository.REPOSITORY_KEY, "S930"))
1539+
.build())
1540+
.build();
1541+
1542+
context.setSettings(new MapSettings().setProperty("sonar.tests", "tests"));
1543+
var contextSpy = spy(context);
1544+
PythonSensor sensor = sensor();
1545+
sensor.execute(contextSpy);
1546+
verify(contextSpy, times(1)).addTelemetryProperty(TelemetryMetricKey.PYTHON_SONAR_TESTS_SET.key(), "1");
1547+
}
1548+
1549+
@Test
1550+
void send_telemetry_sonar_tests_not_set() {
1551+
activeRules = new ActiveRulesBuilder()
1552+
.addRule(new NewActiveRule.Builder()
1553+
.setRuleKey(RuleKey.of(PythonRuleRepository.REPOSITORY_KEY, "S930"))
1554+
.build())
1555+
.build();
1556+
1557+
PythonSensor sensor = sensor();
1558+
var contextSpy = spy(context);
1559+
sensor.execute(contextSpy);
1560+
verify(contextSpy, times(1)).addTelemetryProperty(TelemetryMetricKey.PYTHON_SONAR_TESTS_SET.key(), "0");
1561+
}
1562+
15341563
@Test
15351564
void detects_databricks() {
15361565
activeRules = new ActiveRulesBuilder()

0 commit comments

Comments
 (0)