Skip to content

Commit 545912a

Browse files
SONARPY-2379 Update the custom rules in the sonar-python repository to match LAYC format (#2188)
1 parent 2ef9e5c commit 545912a

File tree

27 files changed

+310
-156
lines changed

27 files changed

+310
-156
lines changed

docs/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<name>SonarQube Python :: Documentation</name>
1515

1616
<modules>
17-
<module>python-custom-rule-examples</module>
17+
<module>python-custom-rules-example</module>
1818
</modules>
1919

2020
</project>

docs/python-custom-rule-examples/src/main/java/org/sonar/samples/python/CustomPythonRuleRepository.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

docs/python-custom-rule-examples/src/test/java/org/sonar/samples/python/CustomPythonRuleRepositoryTest.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

docs/python-custom-rule-examples/pom.xml renamed to docs/python-custom-rules-example/pom.xml

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,51 @@
99
<version>4.25-SNAPSHOT</version>
1010
</parent>
1111

12-
<artifactId>python-custom-rule-examples</artifactId>
12+
<artifactId>python-custom-rules-example</artifactId>
1313
<packaging>sonar-plugin</packaging>
1414

1515
<name>SonarQube Python :: Documentation :: Custom Rules Example</name>
1616
<description>Python custom rule examples for SonarQube</description>
1717

1818
<properties>
19-
<sonar.python.version>3.15.0.9787</sonar.python.version>
19+
<sonar.python.version>4.24.0.18631</sonar.python.version>
2020
</properties>
2121
<dependencies>
2222
<dependency>
23-
<groupId>org.sonarsource.sonarqube</groupId>
23+
<groupId>org.sonarsource.api.plugin</groupId>
2424
<artifactId>sonar-plugin-api</artifactId>
25-
<version>7.9</version>
2625
<scope>provided</scope>
2726
</dependency>
27+
<dependency>
28+
<groupId>org.sonarsource.analyzer-commons</groupId>
29+
<artifactId>sonar-analyzer-commons</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.sonarsource.sonarqube</groupId>
33+
<artifactId>sonar-plugin-api-impl</artifactId>
34+
<scope>test</scope>
35+
</dependency>
2836
<dependency>
2937
<groupId>org.sonarsource.python</groupId>
3038
<artifactId>sonar-python-plugin</artifactId>
3139
<type>sonar-plugin</type>
32-
<version>${sonar.python.version}</version>
40+
<version>${project.version}</version>
3341
<scope>provided</scope>
3442
</dependency>
3543
<dependency>
3644
<groupId>org.sonarsource.python</groupId>
3745
<artifactId>python-checks-testkit</artifactId>
38-
<version>${sonar.python.version}</version>
46+
<version>${project.version}</version>
3947
<scope>test</scope>
4048
</dependency>
4149
<dependency>
42-
<groupId>junit</groupId>
43-
<artifactId>junit</artifactId>
44-
<version>4.13.1</version>
50+
<groupId>org.junit.jupiter</groupId>
51+
<artifactId>junit-jupiter</artifactId>
4552
<scope>test</scope>
4653
</dependency>
4754
<dependency>
48-
<groupId>org.mockito</groupId>
49-
<artifactId>mockito-core</artifactId>
55+
<groupId>org.junit.jupiter</groupId>
56+
<artifactId>junit-jupiter-api</artifactId>
5057
<scope>test</scope>
5158
</dependency>
5259
</dependencies>
@@ -56,22 +63,21 @@
5663
<plugin>
5764
<groupId>org.sonarsource.sonar-packaging-maven-plugin</groupId>
5865
<artifactId>sonar-packaging-maven-plugin</artifactId>
59-
<version>1.21.0.505</version>
6066
<extensions>true</extensions>
6167
<configuration>
6268
<pluginClass>org.sonar.samples.python.CustomPythonRulesPlugin</pluginClass>
63-
<requirePlugins>python:${sonar.python.version}</requirePlugins>
64-
<sonarLintSupported>true</sonarLintSupported>
65-
<skipDependenciesPackaging>true</skipDependenciesPackaging>
69+
<requirePlugins>python:${project.version}</requirePlugins>
70+
<pluginApiMinVersion>${pluginApiMinVersion}</pluginApiMinVersion>
71+
<requiredForLanguages>py,ipynb</requiredForLanguages>
6672
</configuration>
6773
</plugin>
6874

6975
<plugin>
7076
<artifactId>maven-compiler-plugin</artifactId>
7177
<version>3.7.0</version>
7278
<configuration>
73-
<source>1.8</source>
74-
<target>1.8</target>
79+
<source>17</source>
80+
<target>17</target>
7581
</configuration>
7682
</plugin>
7783
<plugin>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (C) 2011-2024 SonarSource SA - mailto:info AT sonarsource DOT com
3+
* This code is released under [MIT No Attribution](https://opensource.org/licenses/MIT-0) license.
4+
*/
5+
package org.sonar.samples.python;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import org.sonar.api.SonarRuntime;
10+
import org.sonar.api.server.rule.RulesDefinition;
11+
import org.sonar.plugins.python.api.PythonCustomRuleRepository;
12+
import org.sonarsource.analyzer.commons.RuleMetadataLoader;
13+
14+
public class CustomPythonRuleRepository implements RulesDefinition, PythonCustomRuleRepository {
15+
public static final String RESOURCE_BASE_PATH = "/org/sonar/l10n/python/rules/python";
16+
public static final String REPOSITORY_KEY = "python-custom-rules-example";
17+
public static final String REPOSITORY_NAME = "MyCompany Custom Repository";
18+
19+
private final SonarRuntime runtime;
20+
21+
public CustomPythonRuleRepository(SonarRuntime runtime) {
22+
this.runtime = runtime;
23+
}
24+
25+
@Override
26+
public void define(Context context) {
27+
NewRepository repository = context.createRepository(REPOSITORY_KEY, "py").setName(REPOSITORY_NAME);
28+
RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RESOURCE_BASE_PATH, runtime);
29+
ruleMetadataLoader.addRulesByAnnotatedClass(repository, new ArrayList<>(RulesList.getChecks()));
30+
repository.done();
31+
}
32+
33+
@Override
34+
public String repositoryKey() {
35+
return REPOSITORY_KEY;
36+
}
37+
38+
@Override
39+
public List<Class<?>> checkClasses() {
40+
return new ArrayList<>(RulesList.getChecks());
41+
}
42+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (C) 2011-2024 SonarSource SA - mailto:info AT sonarsource DOT com
3+
* This code is released under [MIT No Attribution](https://opensource.org/licenses/MIT-0) license.
4+
*/
5+
package org.sonar.samples.python;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.stream.Stream;
10+
import org.sonar.plugins.python.api.PythonCheck;
11+
import org.sonar.samples.python.checks.CustomPythonSubscriptionCheck;
12+
import org.sonar.samples.python.checks.CustomPythonVisitorCheck;
13+
14+
public final class RulesList {
15+
16+
private RulesList() {
17+
}
18+
19+
public static List<Class<? extends PythonCheck>> getChecks() {
20+
return new ArrayList<>(Stream.concat(
21+
getPythonChecks().stream(),
22+
getPythonTestChecks().stream()
23+
).toList());
24+
}
25+
26+
/**
27+
* These rules are going to target MAIN code only
28+
*/
29+
public static List<Class<? extends PythonCheck>> getPythonChecks() {
30+
return new ArrayList<>(List.of(
31+
CustomPythonSubscriptionCheck.class
32+
));
33+
}
34+
35+
/**
36+
* These rules are going to target TEST code only
37+
*/
38+
public static List<Class<? extends PythonCheck>> getPythonTestChecks() {
39+
return new ArrayList<>(List.of(
40+
CustomPythonVisitorCheck.class
41+
));
42+
}
43+
}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44
*/
55
package org.sonar.samples.python.checks;
66

7-
import org.sonar.check.Priority;
87
import org.sonar.check.Rule;
98
import org.sonar.plugins.python.api.PythonSubscriptionCheck;
109
import org.sonar.plugins.python.api.tree.ForStatement;
1110
import org.sonar.plugins.python.api.tree.Tree;
1211

13-
@Rule(
14-
key = CustomPythonSubscriptionCheck.RULE_KEY,
15-
priority = Priority.MINOR,
16-
name = "Python subscription visitor check",
17-
description = "desc")
12+
@Rule(key = CustomPythonSubscriptionCheck.RULE_KEY)
1813
public class CustomPythonSubscriptionCheck extends PythonSubscriptionCheck {
1914

2015
public static final String RULE_KEY = "subscription";
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@
44
*/
55
package org.sonar.samples.python.checks;
66

7-
import org.sonar.check.Priority;
87
import org.sonar.check.Rule;
98
import org.sonar.plugins.python.api.PythonVisitorCheck;
109
import org.sonar.plugins.python.api.tree.FunctionDef;
1110

12-
@Rule(
13-
key = CustomPythonVisitorCheck.RULE_KEY,
14-
priority = Priority.MINOR,
15-
name = "Python visitor check",
16-
description = "desc")
11+
@Rule(key = CustomPythonVisitorCheck.RULE_KEY)
1712
public class CustomPythonVisitorCheck extends PythonVisitorCheck {
1813

1914
public static final String RULE_KEY = "visitor";

0 commit comments

Comments
 (0)