Customise the Rules in SonarQube

There are a few rules in SonarQube I find a bit special. I understand their reasoning but believe that the resulting errors aren’t correct or helpful. Should you reach the same conclusion you can follow along to create your own custom set of rules.

This post is part of the SonarQube series. You can find the other parts here:

 

Pick a profile to start

I like the default profile for C# that comes with SonarQube and I only want to get rid of a few rules. Instead of creating a profile from scratch I copy the existing one and throw away what I don’t need. Log in as admin and open the Quality Profiles tab. There you find all the profiles and a little menu for each one. Click on Copy (1) to clone the current profile:

Your new profile needs a name:

A final click on Copy will create your own profile.

 

Use your custom Profile

Open your newly created profile to see how many different rules are enabled. You can activate or deactivate all existing rules.

If all your projects should use this profile you can set it as the default one. That can be done on the Action button by clicking on “Set as Default”:

You don’t need to make the new profile the default one. You can open your project, select the Administration menu and change the quality profile to your newly created one:

 

Customizing your Profile

A simple way to find the rules you don’t want is to go through the list of bugs. When you find a rule you don’t like you can open the explanation of the rule by clicking on the … next to the name:

You find the unique id of this rule in the top right corner. Copy this id, switch to the rules tab on the main navigation list and search for it. The result panel has a Deactivate button to get rid of this rule in your current profile:

You can repeat this until you got rid of all the unwanted rules. Run the SonarQube scanner again to remove all the reported errors for those now deselected rules.

 

What did you change in your Profile?

By modifying your own profile and not touching the default one, we are now able to compare our custom rules with the default one. Open your quality profile and select the “Compare” menu attached to the Action button. You can now compare your profile with the one named “Sonar way”:

The rules that are only in Sonar way are the ones you deactivated. If rules are only in your profile, then you activated rules that aren’t checked by the default profile.

This nice comparison helps you to keep track of the changes. And should you want to activate a rule you currently don’t include, you simply click on the name of the rule and activate this rule for your profile.

 

Next

Do you like SonarQube? So far we have a setup that is good enough to make the first steps with SonarQube. But to use it seriously we need to make some adjustments. The next post will explain what you should do to get SonarQube ready for production.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.