简体   繁体   English

SonarQube 如何创建配置文件并向其导入新规则

[英]SonarQube how to create Profile and import new rules to it

I'm developing a plugin to SonarQube in order to import rules from a .xml file.我正在为 SonarQube 开发一个插件,以便从 .xml 文件中导入规则。 So far this is done and they really are imported into the SonarQube instance and shown under "Rules".到目前为止,这已经完成,它们确实被导入到 SonarQube 实例中并显示在“规则”下。 Although the Quality Profile is being created, all the imported rules aren't being added to it and I can't understand why.尽管正在创建质量配置文件,但并未将所有导入的规则添加到其中,我不明白为什么。

I don't want to add them one by one by hand;不想一一添加; I'm looking for a way to add them directly to the created profile once they are imported from the .xml file.我正在寻找一种方法,将它们从 .xml 文件导入后直接添加到创建的配置文件中。 The profile is simply created with:配置文件只是通过以下方式创建的:

public class MyProfile extends ProfileDefinition {
    @Override
    public RulesProfile createProfile(ValidationMessages validation) {
        return RulesProfile.create("QP", "Java");
    }
}

Here is some code of the methods that I suspect would make that happen:这是我怀疑会发生这种情况的一些方法代码:

public class MyRules extends RulesDefinition {

public void define(Context context) {
    List<RulePack> rulePacks = this.rulePackParser.parse();
    parseXml(context);                                                      
    parseRulePacks(context, rulePacks);
    for (NewRepository newRepository : newRepositories.values()) {
        newRepository.done();
    }
}

private void parseXml(Context context) {
    for (Language supportedLanguage : languages.all()) {
        InputStream rulesXml = this.getClass().getResourceAsStream("/rules-TESTE.xml");
        if (rulesXml != null) {
            NewRepository repository = getRepository(context, supportedLanguage.getKey());
            xmlLoader.load(repository, new BufferedReader(new InputStreamReader(rulesXml)));
            repository.done();
        }
    }
}

private void parseRulePacks(Context context, List<RulePack> rulePacks) {
    for (RulePack rulePack : rulePacks) {
        for (AppScanRule rule : rulePack.getRules()) {
            String sqLanguageKey = convertToSQ(rulePack.getRuleLanguage(rule));
            if (this.languages.get(sqLanguageKey) != null && isAnInterestingRule(rule)) {
                processRule(context, rulePack, rule, sqLanguageKey);
            }
        }
    }
}
}

Thanks in advance.提前致谢。

Edit note: the described procedure for actually add the rules to the quality profile can be considered as a workaround because at the time there was an open issue at SonarQube's engine that didn't allow to access all rules at once in order to easily add to the desired quality profile (this issue can be viewed here ).编辑说明:实际将规则添加到质量配置文件的描述过程可以被视为一种解决方法,因为当时 SonarQube 的引擎存在一个未解决的问题,不允许一次访问所有规则以便轻松添加到所需的质量配置文件(可以在此处查看此问题)。 So to versions from 5.6 it can be done like this:所以对于 5.6 的版本,它可以像这样完成:

  • Have yourself implemented a class extending ProfileDefinition ;自己实现了一个扩展ProfileDefinition的类;
  • Override the method public RulesProfile createProfile(ValidationMessages messages) and create the profile object RulesProfile profile = RulesProfile.create();覆盖方法public RulesProfile createProfile(ValidationMessages messages)并创建配置文件对象RulesProfile profile = RulesProfile.create();
  • On that method, get all rules by Collection<Rule> rules = ruleFinder.findAll(RuleQuery.create().withRepositoryKey(key-of-the-repository-with-the-desired-rules));在该方法中,通过Collection<Rule> rules = ruleFinder.findAll(RuleQuery.create().withRepositoryKey(key-of-the-repository-with-the-desired-rules));获取所有规则Collection<Rule> rules = ruleFinder.findAll(RuleQuery.create().withRepositoryKey(key-of-the-repository-with-the-desired-rules)); (findAll is the method that was broken) (findAll 是被破坏的方法)
  • Activate the rules on the profile with:使用以下命令激活配置文件上的规则:

    for(Rule rule : rules) { profile.activateRule(rule, null); }

  • Finally it is possible to set some definitions, like the language for the profile or it's name.最后,可以设置一些定义,例如配置文件的语言或其名称。 After that return the newly created profile object:之后返回新创建的配置文件对象:

    profile.setLanguage("Java"); profile.setName("My Profile"); return profile;


So I was able to solve this problem but with a very different approach.所以我能够解决这个问题,但采用了一种非常不同的方法。 In order to add the quality profile with my new rules, I used SonarQube's REST Web API docs.sonarqube.org/display/DEV/Web+Service+API .为了用我的新规则添加质量配置文件,我使用了 SonarQube 的 REST Web API docs.sonarqube.org/display/DEV/Web+Service+API The client that I used for sending/receiving requests to/from the API was Postman .我用于向/从 API 发送/接收请求的客户端是Postman The available commands are also documented in nemo.sonarqube.org . nemo.sonarqube.org中也记录了可用的命令。

After some failures I found out that this process appears to have some restrictions of use.经过一些失败,我发现这个过程似乎有一些使用限制。 To get this working I had to:为了让这个工作,我必须:

  • First, in the plugin code I gave in the question, I have to load the .XML file containing the rules by using the class RulesDefinitionXmlLoader with the method load like this:首先,在我在问题中给出的插件代码中,我必须使用类RulesDefinitionXmlLoader和方法load 加载包含规则的 .XML 文件,如下所示:

     xmlLoader.load(repository, new BufferedReader(newInputStreamReader(rulesXml)));
  • This process I actually very sweet and you can have your rules loaded very easily.这个过程我真的很甜蜜,你可以很容易地加载你的规则。 All you have to do is to make sure that the .XML file containing the rules follow this standard template:您所要做的就是确保包含规则的 .XML 文件遵循以下标准模板:

     <rules> <rule> <repositoryKey>java-key</repositoryKey> <key>1</key> <internalKey> da-rule-name-1</internalKey> <name> da-rule-name </name> <description>da-description </description> </rule> </rules>

The main concern here is the repositoryKey as the remaining listed fields are mandatory.这里主要关注的是repositoryKey,因为其余列出的字段是强制性的。 You have to make sure that the key used here is the same used to add the quality profile (which I'm going to show next).您必须确保此处使用的密钥用于添加质量配置文件的密钥相同(我接下来将展示)。 This key is defined in the class extending RulesDefinition (which most important code I provided in the question) while creating a repository .这个键是在创建存储库时扩展 RulesDefinition (我在问题中提供的最重要的代码)的类中定义的。

If it helps, you can also use a request to the Web API in order to list all the repositories, so you can make sure the key used is the right one:如果有帮助,您还可以使用对 Web API 的请求来列出所有存储库,这样您就可以确保使用的密钥是正确的: 在此处输入图片说明

  • Create a .XML file like the previous one but with the following information.创建一个与上一个类似的 .XML 文件,但包含以下信息。 This is the file that will be used by the REST Web API to create the quality profile:这是 REST Web API 将用于创建质量配置文件的文件:

     <profile> <name>da-profile-name</name> <language>java</language> <rules> <rule> <repositoryKey>java-key</repositoryKey> <key>1</key> <internalKey> da-rule-name-1</internalKey> <name> da-rule-name </name> <description>da-description </description> </rule> </rules> </profile>
  • At last, all you have to do is to send the request to the Web API, using this created second file.最后,您所要做的就是使用创建的第二个文件将请求发送到 Web API。 To do this you can use Postman like I said (if you have less or none knowledge using REST APIs, like I do) or the command prompt (in this case it is needed to install Curl ).为此,您可以像我说的那样使用 Postman(如果您对使用 REST API 的知识较少或没有知识,就像我一样)或命令提示符(在这种情况下需要安装 Curl )。 In Postman:在邮递员中:

    1. Set request as "Post" and add the URL (assuming SonarQube is in the local machine and listening to default port): http://localhost:9000/api/qualityprofiles/restore将请求设置为“发布”并添加 URL(假设 SonarQube 在本地机器中并侦听默认端口): http://localhost:9000/api/qualityprofiles/restore

    2. Set "Authorization".设置“授权”。 Default is "admin"/"admin" ;默认为"admin"/"admin" ;

    3. At "Body", set one param with "key" = "backup" and the "value" as a file (select the down arrow) and select the second file created (the one with rules and extra tags for profile) .在“正文”中,将一个参数设置为“key”=“backup”,将“value”设置为一个文件(选择向下箭头),然后选择创建的第二个文件(带有规则和额外标签的文件)

请求配置

Send the request and if all goes well you should be able to see in the bottom window the number of successfully imported rules!发送请求,如果一切顺利,您应该能够在底部窗口中看到成功导入规则的数量!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM