SonarQube how to create Profile and import new rules to it

前端 未结 1 1423
不思量自难忘°
不思量自难忘° 2021-01-23 14:49

I\'m developing a plugin to SonarQube in order to import rules from a .xml file. So far this is done and they really are imported into the SonarQube instance and shown under \"R

相关标签:
1条回答
  • 2021-01-23 15:40

    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). So to versions from 5.6 it can be done like this:

    • Have yourself implemented a class extending ProfileDefinition;
    • Override the method public RulesProfile createProfile(ValidationMessages messages) and create the profile object 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)); (findAll is the method that was broken)
    • 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. The client that I used for sending/receiving requests to/from the API was Postman. The available commands are also documented in 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:

      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:

      <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. 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.

    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:

    • Create a .XML file like the previous one but with the following information. This is the file that will be used by the REST Web API to create the quality profile:

      <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. 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). 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

      2. Set "Authorization". Default is "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).

    Send the request and if all goes well you should be able to see in the bottom window the number of successfully imported rules!

    0 讨论(0)
提交回复
热议问题