Java RulesDefinition not working

Hi, I try to add custom Rule but it didn’t work
I follow sample https://github.com/SonarSource/sonar-custom-rules-examples/blob/master/java-custom-rules/src/main/java/org/sonar/samples/java/MyJavaRulesDefinition.java

What I do

Create an implementation of RulesDefinition

code I got from RulesDefinition interface comments

Then I add an extension to my plugin

Now I suppose I had new rule with id x12

I try to bundle an issue which I will register via sensor with this new custom rule

But it’s didn’t bundle.
Could you provide some sample code how I could register a new custom rule and then create an issue for that rule?

NewAdHocRule I didn’t consider as it has a limited amount of properties

Thanks in advance

Hi,

You are mixing multiple concepts. Custom rules are extra rules that you develop, based on SonarSource analyzers. Basically we provide an API to simplify the coding of new rules (providing an AST, so that you d’ont have to handle the parsing/inference yourself). The repo https://github.com/SonarSource/sonar-custom-rules-examples is about that. For custom rules, you should not implement a Sensor, but instead the extension point provided by the custom rule API. In the case of SonarJava, the entry point is CheckRegistrar that returns a list of Checks.

Now if you want to develop your own rules, without relying on SonarJava API, you have two options:

  1. If you know in advance the list of possible rules, and want to configure the rules activation/severity/params from SonarQube UI, you should use “normal” rules. Implement RulesDefinition extension point (similar to your exemple above), and then in your Sensor use the method newIssue to create an issue for a specific rule key. Caution: the rule should be activated in the project quality profile to have it ultimately reported in SonarQube.

  2. If you don’t know the list of rules, or more generically you simply want to quickly import third party report of issues, where the rules configuration is managed outside SonarQube, then you can use newExternalIssue, with or without newAdHocRule to give more details about the rule. Note that to import external issues, you could also not develop any Java plugin, but simply feed the scanner with the issue report converted to a specific XML format.

1 Like

Hi @Julien_HENRY, thanks for providing healthy feedback
I managed how to add rules via


and bound them with custom JavaCheck

I still need each issue to have own rich details (text describing reasons why issue happens and some link to point how it could be solved)
I see that each issue had comments and I want to use them to reach my goal.
Could you provide some hint on how I could add comments to new issues I created

I noted there is PostJobIssue so I assume there is some post-process where all issues are created and I could write some my extension to add comments.
I see its possible to add comment https://docs.sonarqube.org/pages/viewpage.action?pageId=2392181

Hi Julien,

I’m Tamir, the Product Manager that works with Vitalii.
What we’re essentially trying to accomplish is to link between an individual exception error to the OverOps root cause. The problem is that each exception has a unique link to our OverOps Automated Root Cause screen (tale.html), and we’re not sure what’s the best way to develop that - whether it’s with Custom Rules and/or Templates.

Please let me know if that makes sense, and if there’s any more product/technical context you need.

Thanks,
Tamir

@Julien_HENRY I just want to say thanks, you pointed me in right direction

How am supposed to activate it without use of the UI

I want this qualityProfile activate via my custome plugin only ( through code )

I already have ruleDefinition and QualityProfile created using BuilinQualityProfile which is showing me perfectly inside of SonarQube. However I have to manually set that profile to project ( I want this to be automatically activated for new project)

If you are contributing rules for an existing language, this is not possible. For a language, there will be a default built in quality profile (Sonar Way) that the owner of the language defined.
We don’t want to have third party plugins contributing extra rules to this default profile.

So if you are contributing java rules, you’ll have to tell your users to activate your rules in their quality profile.