-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONARPY-2379 Update the custom rules in the sonar-python repository t…
…o match LAYC format (#2188)
- Loading branch information
1 parent
2ef9e5c
commit 545912a
Showing
27 changed files
with
310 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 0 additions & 69 deletions
69
...stom-rule-examples/src/main/java/org/sonar/samples/python/CustomPythonRuleRepository.java
This file was deleted.
Oops, something went wrong.
46 changes: 0 additions & 46 deletions
46
...-rule-examples/src/test/java/org/sonar/samples/python/CustomPythonRuleRepositoryTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...stom-rules-example/src/main/java/org/sonar/samples/python/CustomPythonRuleRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright (C) 2011-2024 SonarSource SA - mailto:info AT sonarsource DOT com | ||
* This code is released under [MIT No Attribution](https://opensource.org/licenses/MIT-0) license. | ||
*/ | ||
package org.sonar.samples.python; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.sonar.api.SonarRuntime; | ||
import org.sonar.api.server.rule.RulesDefinition; | ||
import org.sonar.plugins.python.api.PythonCustomRuleRepository; | ||
import org.sonarsource.analyzer.commons.RuleMetadataLoader; | ||
|
||
public class CustomPythonRuleRepository implements RulesDefinition, PythonCustomRuleRepository { | ||
public static final String RESOURCE_BASE_PATH = "/org/sonar/l10n/python/rules/python"; | ||
public static final String REPOSITORY_KEY = "python-custom-rules-example"; | ||
public static final String REPOSITORY_NAME = "MyCompany Custom Repository"; | ||
|
||
private final SonarRuntime runtime; | ||
|
||
public CustomPythonRuleRepository(SonarRuntime runtime) { | ||
this.runtime = runtime; | ||
} | ||
|
||
@Override | ||
public void define(Context context) { | ||
NewRepository repository = context.createRepository(REPOSITORY_KEY, "py").setName(REPOSITORY_NAME); | ||
RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RESOURCE_BASE_PATH, runtime); | ||
ruleMetadataLoader.addRulesByAnnotatedClass(repository, new ArrayList<>(RulesList.getChecks())); | ||
repository.done(); | ||
} | ||
|
||
@Override | ||
public String repositoryKey() { | ||
return REPOSITORY_KEY; | ||
} | ||
|
||
@Override | ||
public List<Class<?>> checkClasses() { | ||
return new ArrayList<>(RulesList.getChecks()); | ||
} | ||
} |
File renamed without changes.
43 changes: 43 additions & 0 deletions
43
docs/python-custom-rules-example/src/main/java/org/sonar/samples/python/RulesList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) 2011-2024 SonarSource SA - mailto:info AT sonarsource DOT com | ||
* This code is released under [MIT No Attribution](https://opensource.org/licenses/MIT-0) license. | ||
*/ | ||
package org.sonar.samples.python; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
import org.sonar.plugins.python.api.PythonCheck; | ||
import org.sonar.samples.python.checks.CustomPythonSubscriptionCheck; | ||
import org.sonar.samples.python.checks.CustomPythonVisitorCheck; | ||
|
||
public final class RulesList { | ||
|
||
private RulesList() { | ||
} | ||
|
||
public static List<Class<? extends PythonCheck>> getChecks() { | ||
return new ArrayList<>(Stream.concat( | ||
getPythonChecks().stream(), | ||
getPythonTestChecks().stream() | ||
).toList()); | ||
} | ||
|
||
/** | ||
* These rules are going to target MAIN code only | ||
*/ | ||
public static List<Class<? extends PythonCheck>> getPythonChecks() { | ||
return new ArrayList<>(List.of( | ||
CustomPythonSubscriptionCheck.class | ||
)); | ||
} | ||
|
||
/** | ||
* These rules are going to target TEST code only | ||
*/ | ||
public static List<Class<? extends PythonCheck>> getPythonTestChecks() { | ||
return new ArrayList<>(List.of( | ||
CustomPythonVisitorCheck.class | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions
21
...tom-rules-example/src/main/resources/org/sonar/l10n/python/rules/python/subscription.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"title": "Python subscription visitor check", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "10min" | ||
}, | ||
"tags": [ | ||
"custom-python" | ||
], | ||
"code": { | ||
"impacts": { | ||
"MAINTAINABILITY": "LOW", | ||
"RELIABILITY": "LOW", | ||
"SECURITY": "LOW" | ||
}, | ||
"attribute": "CLEAR" | ||
}, | ||
"defaultSeverity": "Minor" | ||
} |
File renamed without changes.
21 changes: 21 additions & 0 deletions
21
...n-custom-rules-example/src/main/resources/org/sonar/l10n/python/rules/python/visitor.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"title": "Python visitor check", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"custom-python" | ||
], | ||
"code": { | ||
"impacts": { | ||
"MAINTAINABILITY": "LOW", | ||
"RELIABILITY": "LOW", | ||
"SECURITY": "LOW" | ||
}, | ||
"attribute": "CLEAR" | ||
}, | ||
"defaultSeverity": "Minor" | ||
} |
Oops, something went wrong.