Search code examples
pluginssonarquberpg

Add my own rules in SonarQube with RPG


I want to create my own SonarQube Plugin for the RPG language. I have the following problem.

I start by created the RpgLanguage class that extends to AbstractLanguage. In this class, I defined my new language "Rpg". You can see my class in the following code :

public class RpgLanguage extends AbstractLanguage{


     public static final String KEY = "rpg";

      private Settings settings;

      public RpgLanguage(Settings settings) {
        super(KEY, "Rpg");
        this.settings = settings;
      }

      public String[] getFileSuffixes() {
        String[] suffixes = settings.getStringArray("");
        if (suffixes == null || suffixes.length == 0) {
          suffixes = StringUtils.split(".RPG", ",");
        }
        return suffixes;
      }
}

After, I have created my RpgRulesDefinition class that implements RulesDefinition. In this class, I create a new repository for the language RPG and I want to add a rule in this repository (empty rules). The code is like below :

public static final String REPOSITORY_KEY = "rpg_repository_mkoza";

    public void define(Context context) {

        NewRepository repo = context.createRepository(REPOSITORY_KEY, "rpg");
        repo.setName("Mkoza Analyser rules RPG");

        // We could use a XML or JSON file to load all rule metadata, but
        // we prefer use annotations in order to have all information in a single place
        RulesDefinitionAnnotationLoader annotationLoader = new RulesDefinitionAnnotationLoader();
        annotationLoader.load(repo, RpgFileCheckRegistrar.checkClasses());

        repo.done();
    }

My class RpgFileCheckRegistrar that call my Rules :

 /**
       * Register the classes that will be used to instantiate checks during analysis.
       */
    public void register(RegistrarContext registrarContext) {
        // Call to registerClassesForRepository to associate the classes with the correct repository key
        registrarContext.registerClassesForRepository(RpgRulesDefinition.REPOSITORY_KEY, Arrays.asList(checkClasses()), Arrays.asList(testCheckClasses()));

    }

      /**
       * Lists all the checks provided by the plugin
       */
      public static Class<? extends JavaCheck>[] checkClasses() {
        return new Class[] {
          RulesExampleCheck.class
          };
      }

      /**
       * Lists all the test checks provided by the plugin
       */
      public static Class<? extends JavaCheck>[] testCheckClasses() {
        return new Class[] {};
      }

My Rule class (still empty):

 @Rule(
      key = "Rule1",
      name = "Rule that make nothing",
      priority = Priority.MAJOR,
      tags = {"example"}
      )
public class RulesExampleCheck extends BaseTreeVisitor{

    /**
     * Right in java code your rule
     */

}

And the class SonarPlugin that call all these extensions :

public final class RpgSonarPlugin extends SonarPlugin
{
     // This is where you're going to declare all your Sonar extensions
      public List getExtensions() {
       return Arrays.asList(
            RpgLanguage.class,
            RpgRulesDefinition.class,
            RpgFileCheckRegistrar.class
        );
      }
}

The problem when I want to start the server sonar, I obtain this error stack :

Exception sending context initialized event to listener instance of class org.sonar.server.platform.PlatformServletContextListener
java.lang.IllegalStateException: One of HTML description or Markdown description must be defined for rule [repository=rpg_repository_mkoza, key=Rule1]

I try different things but I don't understand why there are these error. Of course I want that my repository "rpg_repository_mkoza" is display in the RPG's repository in SonarQube with the Rules : RulesExampleCheck.

My sonar-plugin-version is the 3.7.1


Solution

  • I find my problem. There are need to add the field 'description' in @Rule.

    For example :

    @Rule(
      key = "Rule1",
      name = "RuleExampleCheck",
      description = "This rule do nothing",
      priority = Priority.INFO,
      tags = {"try"}
      )