@@ -41,40 +41,42 @@ public class PythonProfile implements BuiltInQualityProfilesDefinition {
41
41
static final String PROFILE_LOCATION = RESOURCE_FOLDER + "/Sonar_way_profile.json" ;
42
42
static final String SECURITY_RULES_CLASS_NAME = "com.sonar.plugins.security.api.PythonRules" ;
43
43
static final String SECURITY_RULE_KEYS_METHOD_NAME = "getRuleKeys" ;
44
- static final String SECURITY_RULE_REPO_METHOD_NAME = "getRepositoryKey" ;
44
+ static final String DBD_RULES_CLASS_NAME = "com.sonarsource.plugins.dbd.api.PythonRules" ;
45
+ static final String DBD_RULE_KEYS_METHOD_NAME = "getDataflowBugDetectionRuleKeys" ;
46
+ static final String GET_REPOSITORY_KEY = "getRepositoryKey" ;
47
+
45
48
46
49
@ Override
47
50
public void define (Context context ) {
48
51
NewBuiltInQualityProfile profile = context .createBuiltInQualityProfile (PROFILE_NAME , Python .KEY );
49
52
BuiltInQualityProfileJsonLoader .load (profile , CheckList .REPOSITORY_KEY , PROFILE_LOCATION );
50
- getSecurityRuleKeys (SECURITY_RULES_CLASS_NAME , SECURITY_RULE_KEYS_METHOD_NAME , SECURITY_RULE_REPO_METHOD_NAME )
53
+ getSecurityRuleKeys ()
54
+ .forEach (key -> profile .activateRule (key .repository (), key .rule ()));
55
+ getDataflowBugDetectionRuleKeys ()
51
56
.forEach (key -> profile .activateRule (key .repository (), key .rule ()));
52
57
profile .done ();
53
58
}
54
59
55
- // Visible for testing
56
- static Set <RuleKey > getSecurityRuleKeys (String className , String ruleKeysMethodName , String ruleRepoMethodName ) {
57
- try {
60
+ static Set <RuleKey > getSecurityRuleKeys () {
61
+ return getExternalRuleKeys (SECURITY_RULES_CLASS_NAME , SECURITY_RULE_KEYS_METHOD_NAME , "security" );
62
+ }
63
+
64
+ static Set <RuleKey > getDataflowBugDetectionRuleKeys () {
65
+ return getExternalRuleKeys (DBD_RULES_CLASS_NAME , DBD_RULE_KEYS_METHOD_NAME , "dataflow bug detection" );
66
+ }
58
67
68
+ @ SuppressWarnings ("unchecked" )
69
+ static Set <RuleKey > getExternalRuleKeys (String className , String ruleKeysMethodName , String rulesCategory ) {
70
+ try {
59
71
Class <?> rulesClass = Class .forName (className );
60
72
Method getRuleKeysMethod = rulesClass .getMethod (ruleKeysMethodName );
61
73
Set <String > ruleKeys = (Set <String >) getRuleKeysMethod .invoke (null );
62
- Method getRepositoryKeyMethod = rulesClass .getMethod (ruleRepoMethodName );
74
+ Method getRepositoryKeyMethod = rulesClass .getMethod (GET_REPOSITORY_KEY );
63
75
String repositoryKey = (String ) getRepositoryKeyMethod .invoke (null );
64
76
return ruleKeys .stream ().map (k -> RuleKey .of (repositoryKey , k )).collect (Collectors .toSet ());
65
-
66
- } catch (ClassNotFoundException e ) {
67
- LOG .debug (className + " is not found, " + securityRuleMessage (e ));
68
- } catch (NoSuchMethodException e ) {
69
- LOG .debug ("Method not found on " + className +", " + securityRuleMessage (e ));
70
- } catch (IllegalAccessException | InvocationTargetException e ) {
71
- LOG .debug (e .getClass ().getSimpleName () + ": " + securityRuleMessage (e ));
77
+ } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e ) {
78
+ LOG .debug (String .format ("[%s], no %s rules added to Sonar way Python profile: %s" , e .getClass ().getSimpleName (), rulesCategory , e .getMessage ()));
72
79
}
73
-
74
80
return Collections .emptySet ();
75
81
}
76
-
77
- private static String securityRuleMessage (Exception e ) {
78
- return "no security rules added to Sonar way Python profile: " + e .getMessage ();
79
- }
80
82
}
0 commit comments