Skip to content

Commit

Permalink
SONARPY-2379 Update the custom rules in the sonar-python repository t…
Browse files Browse the repository at this point in the history
…o match LAYC format (#2188)
  • Loading branch information
maksim-grebeniuk-sonarsource authored and thomas-serre-sonarsource committed Nov 29, 2024
1 parent 2ef9e5c commit 545912a
Show file tree
Hide file tree
Showing 27 changed files with 310 additions and 156 deletions.
2 changes: 1 addition & 1 deletion docs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<name>SonarQube Python :: Documentation</name>

<modules>
<module>python-custom-rule-examples</module>
<module>python-custom-rules-example</module>
</modules>

</project>

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,51 @@
<version>4.25-SNAPSHOT</version>
</parent>

<artifactId>python-custom-rule-examples</artifactId>
<artifactId>python-custom-rules-example</artifactId>
<packaging>sonar-plugin</packaging>

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

<properties>
<sonar.python.version>3.15.0.9787</sonar.python.version>
<sonar.python.version>4.24.0.18631</sonar.python.version>
</properties>
<dependencies>
<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
<groupId>org.sonarsource.api.plugin</groupId>
<artifactId>sonar-plugin-api</artifactId>
<version>7.9</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.sonarsource.analyzer-commons</groupId>
<artifactId>sonar-analyzer-commons</artifactId>
</dependency>
<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
<artifactId>sonar-plugin-api-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.sonarsource.python</groupId>
<artifactId>sonar-python-plugin</artifactId>
<type>sonar-plugin</type>
<version>${sonar.python.version}</version>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.sonarsource.python</groupId>
<artifactId>python-checks-testkit</artifactId>
<version>${sonar.python.version}</version>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -56,22 +63,21 @@
<plugin>
<groupId>org.sonarsource.sonar-packaging-maven-plugin</groupId>
<artifactId>sonar-packaging-maven-plugin</artifactId>
<version>1.21.0.505</version>
<extensions>true</extensions>
<configuration>
<pluginClass>org.sonar.samples.python.CustomPythonRulesPlugin</pluginClass>
<requirePlugins>python:${sonar.python.version}</requirePlugins>
<sonarLintSupported>true</sonarLintSupported>
<skipDependenciesPackaging>true</skipDependenciesPackaging>
<requirePlugins>python:${project.version}</requirePlugins>
<pluginApiMinVersion>${pluginApiMinVersion}</pluginApiMinVersion>
<requiredForLanguages>py,ipynb</requiredForLanguages>
</configuration>
</plugin>

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
Expand Down
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());
}
}
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
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@
*/
package org.sonar.samples.python.checks;

import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.python.api.PythonSubscriptionCheck;
import org.sonar.plugins.python.api.tree.ForStatement;
import org.sonar.plugins.python.api.tree.Tree;

@Rule(
key = CustomPythonSubscriptionCheck.RULE_KEY,
priority = Priority.MINOR,
name = "Python subscription visitor check",
description = "desc")
@Rule(key = CustomPythonSubscriptionCheck.RULE_KEY)
public class CustomPythonSubscriptionCheck extends PythonSubscriptionCheck {

public static final String RULE_KEY = "subscription";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@
*/
package org.sonar.samples.python.checks;

import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.python.api.PythonVisitorCheck;
import org.sonar.plugins.python.api.tree.FunctionDef;

@Rule(
key = CustomPythonVisitorCheck.RULE_KEY,
priority = Priority.MINOR,
name = "Python visitor check",
description = "desc")
@Rule(key = CustomPythonVisitorCheck.RULE_KEY)
public class CustomPythonVisitorCheck extends PythonVisitorCheck {

public static final String RULE_KEY = "visitor";
Expand Down
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"
}
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"
}
Loading

0 comments on commit 545912a

Please sign in to comment.