plugins - SonarQube how to create Profile and import new rules to it -


i'm developing plugin sonarqube in order import rules .xml file. far done , imported sonarqube instance , shown under "rules". although quality profile being created, imported rules aren't being added , can't understand why.

i don't want add them 1 one hand; i'm looking way add them directly created profile once imported .xml file. profile created with:

public class myprofile extends profiledefinition {     @override     public rulesprofile createprofile(validationmessages validation) {         return rulesprofile.create("qp", "java");     } } 

here code of methods suspect make happen:

public class myrules extends rulesdefinition {  public void define(context context) {     list<rulepack> rulepacks = this.rulepackparser.parse();     parsexml(context);                                                           parserulepacks(context, rulepacks);     (newrepository newrepository : newrepositories.values()) {         newrepository.done();     } }  private void parsexml(context context) {     (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) {     (rulepack rulepack : rulepacks) {         (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: described procedure add rules quality profile can considered workaround because @ time there open issue @ sonarqube's engine didn't allow access rules @ once in order add desired quality profile (this issue can viewed here). versions 5.6 can done this:

  • have implemented class extending profiledefinition;
  • override method public rulesprofile createprofile(validationmessages messages) , create profile object rulesprofile profile = rulesprofile.create();
  • on method, rules collection<rule> rules = rulefinder.findall(rulequery.create().withrepositorykey(key-of-the-repository-with-the-desired-rules)); (findall method broken)
  • activate rules on profile with:

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

  • finally possible set definitions, language profile or it's name. after return newly created profile object:

    profile.setlanguage("java"); profile.setname("my profile"); return profile;


so able solve problem different approach. in order add quality profile new rules, used sonarqube's rest web api docs.sonarqube.org/display/dev/web+service+api. client used sending/receiving requests to/from api postman. available commands documented in nemo.sonarqube.org.

after failures found out process appears have restrictions of use. working had to:

  • first, in plugin code gave in question, have load .xml file containing rules using class rulesdefinitionxmlloader method load this:

    xmlloader.load(repository, new bufferedreader(newinputstreamreader(rulesxml))); 
  • this process sweet , can have rules loaded easily. have make sure .xml file containing rules follow 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 repositorykey remaining listed fields mandatory. have make sure key used here same used add quality profile (which i'm going show next). key defined in class extending rulesdefinition (which important code provided in question) while creating repository.

if helps, can use request web api in order list repositories, can make sure key used right one: enter image description here

  • create .xml file previous 1 following information. file used rest web api create 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, have send request web api, using created second file. can use postman said (if have less or none knowledge using rest apis, do) or command prompt (in case needed install curl). in postman:

    1. set request "post" , add url (assuming sonarqube in local machine , listening default port): http://localhost:9000/api/qualityprofiles/restore

    2. set "authorization". default "admin"/"admin";

    3. at "body", set 1 param "key" = "backup" , "value" file (select down arrow) , select second file created (the 1 rules , tags profile).

request configuration

send request , if goes should able see in bottom window number of imported rules!


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -