Skip to main content

Adding new rules

Custom rules let you enforce conventions specific to your codebase. They are packaged as a plugin that works both in SonarQube, next to the ZPA plugin, and in the ZPA CLI.

Start from the template project​

Download the template project and open it in your IDE. It builds with Maven or Gradle and contains a working rule, its test and the plugin entry point.

Adjust the build file to your organization:

In pom.xml, change groupId, artifactId, version, name and description. The sonar-packaging-maven-plugin configuration defines the plugin: keep pluginClass pointing to your plugin class and change pluginKey to a unique key.

The sonar-plugin-api dependency sets the minimum SonarQube version your plugin supports. sonar-zpa-plugin provides the ZPA API, and zpa-checks-testkit provides the test helpers. Use the same ZPA version as the one where the plugin will run.

Name the rule repository​

SonarQube groups rules in repositories. Set the repository key and name in the PlSqlCustomRulesDefinition class, which also lists the rule classes of the plugin.

Write a rule​

A rule is a subclass of PlSqlCheck. Its metadata (name, description, key, severity) comes from annotations. Most rules override two methods:

  • init(): subscribes to the grammar elements the rule needs.
  • visitNode(AstNode): inspects each matching node and reports issues.

This rule from the template reports DML statements on the USER table:

import org.sonar.plugins.plsqlopen.api.DmlGrammar;
import org.sonar.plugins.plsqlopen.api.annotations.ActivatedByDefault;
import org.sonar.plugins.plsqlopen.api.annotations.ConstantRemediation;
import org.sonar.plugins.plsqlopen.api.annotations.Priority;
import org.sonar.plugins.plsqlopen.api.annotations.Rule;
import org.sonar.plugins.plsqlopen.api.checks.PlSqlCheck;
import org.sonar.plugins.plsqlopen.api.sslr.AstNode;

@Rule(
name = "Avoid DML on table USER",
description = "You should use the functions from the USER_WRAPPER package.",
key = "ForbiddenDmlCheck",
priority = Priority.MAJOR
)
@ConstantRemediation("10min")
@ActivatedByDefault
public class ForbiddenDmlCheck extends PlSqlCheck {

@Override
public void init() {
subscribeTo(DmlGrammar.DML_TABLE_EXPRESSION_CLAUSE);
}

@Override
public void visitNode(AstNode node) {
AstNode table = node.getFirstChildOrNull(DmlGrammar.TABLE_REFERENCE);

if (table != null && table.getTokenOriginalValue().equalsIgnoreCase("user")) {
addIssue(table, "Replace this query by a function of the USER_WRAPPER package.");
}
}

}

To find which grammar elements to subscribe to, open a sample file in the ZPA Toolkit and inspect its syntax tree:

ZPA Toolkit

Add each new rule class to the list in PlSqlCustomRulesDefinition.

Since ZPA 4.3.0, @Rule also accepts a category (RuleCategory.LOGIC_ERROR, RuleCategory.READABILITY, RuleCategory.PERFORMANCE…, from org.sonar.plugins.plsqlopen.api.annotations.RuleCategory). SonarQube shows it as the rule's clean code attribute, and the ZPA CLI includes it in the SonarQube report. Without it, the rule has none.

Test the rule​

A rule test needs a test class and a PL/SQL file with the expected issues:

import com.felipebz.zpa.checks.verifier.PlSqlCheckVerifier;
import org.junit.jupiter.api.Test;

public class ForbiddenDmlCheckTest {

@Test
public void test() {
PlSqlCheckVerifier.verify("src/test/resources/forbidden-dml.sql", new ForbiddenDmlCheck());
}

}
select *
from user u; -- Noncompliant {{Replace this query by a function of the USER_WRAPPER package.}}
-- ^^^^

Each -- Noncompliant comment marks a line where an issue is expected; the optional text in double braces is the expected message, and the optional ^ characters on the next line mark the expected position. The test fails if an issue is missing, unexpected or different.

To ask ZPA what the code means (which declaration a name refers to, whether a variable is used, the type of a value) or to look at more than one file, see Semantic API.

Deploy the plugin​

Build the plugin (mvn package creates it in target/, gradle build in build/libs/) and copy the .jar:

  • SonarQube: to SONARQUBE_HOME/extensions/plugins, then restart SonarQube. The rules appear in a repository with the name you chose; add them to your quality profile.
  • ZPA CLI: to the plugins folder of the CLI installation. Rules annotated with @ActivatedByDefault run without a configuration file; others can be enabled with --config.