Skip to content

Commit 04dc981

Browse files
guillaume-dequennesonartech
authored andcommitted
SONARPY-3982 Add Security/DBD/Architecture rules to AI quality profile (#1015)
GitOrigin-RevId: ac8c6fff6a312746aa42beb649ed69d01b6ba762
1 parent dce39f8 commit 04dc981

File tree

6 files changed

+61
-6
lines changed

6 files changed

+61
-6
lines changed

python-checks/src/main/java/org/sonar/python/checks/OpenSourceCheckList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public class OpenSourceCheckList {
111111

112112
public static final String RESOURCE_FOLDER = "org/sonar/l10n/py/rules/python";
113113
public static final String SONAR_WAY_PROFILE_LOCATION = RESOURCE_FOLDER + "/Sonar_way_profile.json";
114-
public static final String AI_QUALITY_PROFILE_LOCATION = RESOURCE_FOLDER + "/AI_quality_profile.json";
114+
public static final String AI_QUALITY_PROFILE_LOCATION = RESOURCE_FOLDER + "/Sonar_agentic_AI_profile.json";
115115

116116
public Stream<Class<?>> getChecks() {
117117
return Stream.of(

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/AI_quality_profile.json renamed to python-checks/src/main/resources/org/sonar/l10n/py/rules/python/Sonar_agentic_AI_profile.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "AI quality profile",
2+
"name": "Sonar agentic AI",
33
"ruleKeys": [
44
"S100",
55
"S101",

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
public class AIQualityProfile implements BuiltInQualityProfilesDefinition {
2525

26-
static final String PROFILE_NAME = "AI quality profile";
26+
static final String PROFILE_NAME = "Sonar agentic AI";
2727

2828
private final RepositoryInfoProvider[] editionMetadataProviders;
2929

@@ -39,6 +39,12 @@ public void define(Context context) {
3939
registerRulesForEdition(repositoryInfoProvider, profile);
4040
}
4141

42+
PythonProfile.getSecurityRuleKeys()
43+
.forEach(key -> profile.activateRule(key.repository(), key.rule()));
44+
PythonProfile.getDataflowBugDetectionRuleKeys()
45+
.forEach(key -> profile.activateRule(key.repository(), key.rule()));
46+
PythonProfile.getArchitectureRuleKeys()
47+
.forEach(key -> profile.activateRule(key.repository(), key.rule()));
4248
profile.done();
4349
}
4450

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
public class IPynbAIQualityProfile implements BuiltInQualityProfilesDefinition {
2525

26-
static final String PROFILE_NAME = "AI quality profile";
26+
static final String PROFILE_NAME = "Sonar agentic AI";
2727

2828
private final RepositoryInfoProvider[] editionMetadataProviders;
2929

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

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717
package org.sonar.plugins.python;
1818

19+
import com.sonar.plugins.security.api.PythonRules;
20+
import com.sonarsource.plugins.architecturepythonfrontend.api.ArchitecturePythonRules;
21+
import org.junit.jupiter.api.BeforeEach;
1922
import org.junit.jupiter.api.Test;
2023
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
2124
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInActiveRule;
@@ -26,10 +29,20 @@
2629

2730
class AIQualityProfileTest {
2831

32+
@BeforeEach
33+
void setUp() {
34+
PythonRules.throwOnCall = false;
35+
PythonRules.getRuleKeys().clear();
36+
com.sonarsource.plugins.dbd.api.PythonRules.throwOnCall = false;
37+
com.sonarsource.plugins.dbd.api.PythonRules.getDataflowBugDetectionRuleKeys().clear();
38+
ArchitecturePythonRules.throwOnCall = false;
39+
ArchitecturePythonRules.getRuleKeys().clear();
40+
}
41+
2942
public BuiltInQualityProfilesDefinition.BuiltInQualityProfile getProfile() {
3043
BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
3144
new AIQualityProfile(new RepositoryInfoProvider[]{new OpenSourceRepositoryInfoProvider()}).define(context);
32-
return context.profile("py", "AI quality profile");
45+
return context.profile("py", "Sonar agentic AI");
3346
}
3447

3548
@Test
@@ -39,4 +52,40 @@ void profile() {
3952
assertThat(profile.rules()).hasSizeGreaterThan(25);
4053
assertThat(profile.rules()).extracting(BuiltInActiveRule::ruleKey).contains("S100");
4154
}
55+
56+
@Test
57+
void should_contain_security_rules_when_available() {
58+
PythonRules.getRuleKeys().add("S3649");
59+
try {
60+
BuiltInQualityProfilesDefinition.BuiltInQualityProfile profile = getProfile();
61+
assertThat(profile.rules()).extracting("repoKey").contains("pythonsecurity");
62+
assertThat(profile.rules()).extracting(BuiltInActiveRule::ruleKey).contains("S3649");
63+
} finally {
64+
PythonRules.getRuleKeys().clear();
65+
}
66+
}
67+
68+
@Test
69+
void should_contain_dataflow_bug_detection_rules_when_available() {
70+
com.sonarsource.plugins.dbd.api.PythonRules.getDataflowBugDetectionRuleKeys().add("S2259");
71+
try {
72+
BuiltInQualityProfilesDefinition.BuiltInQualityProfile profile = getProfile();
73+
assertThat(profile.rules()).extracting("repoKey").contains("dbd-repo-key");
74+
assertThat(profile.rules()).extracting(BuiltInActiveRule::ruleKey).contains("S2259");
75+
} finally {
76+
com.sonarsource.plugins.dbd.api.PythonRules.getDataflowBugDetectionRuleKeys().clear();
77+
}
78+
}
79+
80+
@Test
81+
void should_contain_architecture_rules_when_available() {
82+
ArchitecturePythonRules.getRuleKeys().add("S7134");
83+
try {
84+
BuiltInQualityProfilesDefinition.BuiltInQualityProfile profile = getProfile();
85+
assertThat(profile.rules()).extracting("repoKey").contains("pythonarchitecture");
86+
assertThat(profile.rules()).extracting(BuiltInActiveRule::ruleKey).contains("S7134");
87+
} finally {
88+
ArchitecturePythonRules.getRuleKeys().clear();
89+
}
90+
}
4291
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class IPynbAIQualityProfileTest {
2929
public BuiltInQualityProfilesDefinition.BuiltInQualityProfile getProfile() {
3030
BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
3131
new IPynbAIQualityProfile(new RepositoryInfoProvider[]{new OpenSourceRepositoryInfoProvider()}).define(context);
32-
return context.profile("ipynb", "AI quality profile");
32+
return context.profile("ipynb", "Sonar agentic AI");
3333
}
3434

3535
@Test

0 commit comments

Comments
 (0)