The way how to bulk-activate rules in Web API

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
  • what are you trying to achieve
  • what have you tried so far to achieve this

*versions

  1. Sonarqube 7.9.1
  2. Scanner 4.6.2

*I tried “api/qualityprofiles/activate_rules?targetKey=‘MyQualityProfileKey’”.
It works if I want to activate all of the rules.
But I just wanted to activate specific rules(like csharpsquid:S2757, csharpsquid:S1698, …) on my custom quality profile.
So I added rule_key parameter and set the value to ‘csharpsquid:S2757’. then It activates only one rule. If I set the value to ‘csharpsquid:S2757,csharpsquid:S1698’ or ‘csharpsquid:S2757;csharpsquid:S1698’, It doesn’t work.
How can I set bulk-activate in Web API?

Hi,

Your best bet here is to bulk-activate some rules via the UI and use your browser’s developer tools to eavesdrop on the web service that’s called.

 
HTH,
Ann

I really appreciate your answer.
Eventually, You mean that there is no way to bulk-activate/deactivate rules on Web API, right?



As you can see those pictures, I tried by using Web API instead of UI.
I can’t activate more than one ruleset at a time.

Hi,

You can bulk activate/deactivate rules from the UI. All the actions in the UI are backed / supported / provided by the API. If you can do it in the UI you can do it in the API (altho to be fair, some actions are supported by “internal” API).

So I am not saying that you can’t bulk activate via the API. I’m saying use the UI to figure out how to do it.

 
HTH,
Ann

What I want to know is that I just wanted to bulk-activate/deactivate on Web API. not from the UI.

Hello, did you manage to bulk-activate/deactivate on Web API?
Thanks

You can use “api/qualityprofiles/activate_rules” and “api/qualityprofiles/deactivate_rules”, check the documentation on your SonarQube instance (http://your_server/web_api/api/qualityprofiles).

1 Like

I also have similar requiremnt, saw the sonarqube docs and API’s.
is there no way to activate/deactivate multiple rules based on rule-key’s ?

Hi Guys,
This is a bit old topic, but recently I had similar task, and this is how I do it.

I’ve used api/qualityprofiles/activate_rule to send requests to, and used Postman Pre-request/Tests scripts in order to loop through all rule keys that I need to activate for specific profile.

  1. Create collection in Postman and set collection variables rules and rule with empty values.
  2. Pre-request script:
let rules = pm.collectionVariables.get("rules");

if(!rules || rules.length == 0){
    rules = [
        "java:S923",
        "java:S1452",
        "java:S881",
        "java:S2972",
        "java:S2694",
        "java:S4738",
        "java:S3626",
        "java:S5785",
        "java:S5786",
        "java:S5612"
        ];
}

let currentRule = rules.shift();
console.log(currentRule);
pm.collectionVariables.set("rule", currentRule);
pm.collectionVariables.set("rules", rules);
  1. Tests script:
let rules = pm.collectionVariables.get("rules");

if(rules && rules.length > 0){
    postman.setNextRequest('<name_of_your_request>');
} else {
    postman.setNextRequest(null);
}

pm.test("Status code is 204", () => {
    pm.response.to.have.status(204);
})

Than you can use collection runner in order to loop till there is no more rule keys left in the array.

Hope that this will help someone who struggles with same task.

Best Regards!

2 Likes