使用 RPG 在 SonarQube 中添加我自己的规则

Add my own rules in SonarQube with RPG

我想为 RPG 语言创建自己的 SonarQube 插件。我有以下问题。

我首先创建了扩展到 AbstractLanguage 的 RpgLanguage class。在这个class中,我定义了我的新语言"Rpg"。你可以在下面的代码中看到我的 class :

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;
      }
}

之后,我创建了实现 RulesDefinition 的 RpgRulesDefinition class。在这个 class 中,我为语言 RPG 创建了一个新的存储库,我想在这个存储库中添加一个规则(空规则)。代码如下:

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();
    }

我的 class RpgFileCheckRegistrar 调用我的规则:

 /**
       * 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[] {};
      }

我的规则class(还是空的):

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

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

}

以及调用所有这些扩展的 class SonarPlugin :

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
        );
      }
}

当我想启动服务器声纳时的问题,我得到这个错误堆栈:

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]

我尝试了不同的方法,但我不明白为什么会出现这些错误。 当然,我希望我的存储库 "rpg_repository_mkoza" 显示在 SonarQube 的 RPG 存储库中,规则如下:RulesExampleCheck.

我的sonar-plugin-version是3.7.1

我发现了我的问题。 @Rule中需要添加字段'description'.

例如:

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