Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sonarqube 5.4 custom rule for C#

I'm using SonarQube 5.4 to analyse my own C# code, the analysis works as I expected. Now I have written some custom rules, one using StyleCop and another using FxCop to run on my code, but I don't find how to import theese custom rule in SonarQube. I underline that I use SonarQube 5.4 with C# plugin 5.1. In my installations the folder "rules" doesn't exists. Instead I can find:

sonar-fxcop-library-1.3.jar in /opt/sonarqube-5.4/data/web/deploy/plugins/csharp/META-INF/lib and sonar-stylecop-plugin-1.1 in /opt/sonarqube-5.4/extensions/plugins.

Anyone can help me to import my custom rules in SonarQube installation?

like image 414
grandeale83 Avatar asked May 09 '16 08:05

grandeale83


2 Answers

FxCop integration: extend the Template for custom FxCop rules in SonarQube ( fxcop:CustomRuleTemplate ) by specifying the CheckId of your custom FxCop rule. [edit] FxCop rules are now covered by the sonar-fxcop plugin.

StyleCop integration: deprecated as StyleCop doesn't rely on Roslyn.

like image 152
Nicolas B. Avatar answered Oct 25 '22 21:10

Nicolas B.


I am working on the same analysis tools for writing my own custom rules in C# for Sonar. In broad sense, we have to follow three steps to achieve our goal:

1.Use Roslyn to Write a Live Code Analyzer: I used Roslyn analyzer tool to write the custom rules for my analysis purpose. There are really some good tutorials on how to start writing rules. One which I used to start is Use Roslyn to Write a Live Code Analyzer for Your API. Building this project will generate a .nupkg file in the bin folder of the project.
Let's say building the project generated a package your_project_name.version, e.g. AnalyzerExample.1.0.6971.18074, something like that.

2.Use the SonarQube Roslyn SDK to generate a custom SonarQube plugin that wraps the Roslyn analyzer. There is some compatibility matrix we have to follow between the version of SDK and SonarQube. Either we can clone the repository or we can download the analysis tool. Running this tool will generate a jar file.

RoslynSonarQubePluginGenerator.exe /a:analyzerexample 

Running the tool will generate some xml file in the current working directory. We could configure the custom rule properties in the xml file and then generate jar file by following command:

RoslynSonarQubePluginGenerator.exe /a:analyzerexample /rules:rules.xml

After running this generator with suitable arguments we get a jar file as analyzerexample-plugin-1.0.6971.18074. This is the required plugin we are looking for. It would contain all the rules we have written in our project in step 1.

3.Use the generated jar file as plugin: Copy the plugin(jar file) generated in step 1 to the extensions folder of the SonarQube. Restart the Sonar server. Log in to the server and then you can see your added rule in the quality profile section, in the list of recent rules. Click on that rule to activate it, by adding it to some quality profile.

like image 41
Nitesh Avatar answered Oct 25 '22 23:10

Nitesh