sonar-checkstyle icon indicating copy to clipboard operation
sonar-checkstyle copied to clipboard

import should work with rule templates

Open romani opened this issue 8 years ago • 27 comments

With SonarQube 4.5.1 and Checkstyle plugin 2.2, importing the following file triggers an error: "Rule template can't be activated on a Quality profile: checkstyle:com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck"

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC
    "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
    "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">

<module name="Checker">
  <module name="TreeWalker">
    <module name="RegexpSinglelineJava">
      <!-- . matches any character, so we need to
         escape it and use \. to match dots. -->
      <property name="format" value="System\.out\.println"/>
      <property name="ignoreComments" value="true"/>
    </module>
  </module>
</module>

romani avatar Apr 26 '16 13:04 romani

the same as #34

romani avatar Dec 16 '16 23:12 romani

from http://stackoverflow.com/a/26977345/1015848

Those Checkstyle rules (like "ConstantNameCheck") are defined with a multiple cardinality in the SonarQube Checkstyle plugin. This means that it should be possible to activate several "instances" of those rules with different values for its parameters. In SonarQube 4.4+, it is no more possible to have multiple activations of those rules. They are considered as "rule templates", which means that you must create custom rules (with explicit parameters) out of them in order to be able to activate them on quality profiles. Everything is explained on the Rules documentation page (http://docs.sonarqube.org/display/SONAR/Rules).

romani avatar Dec 18 '16 02:12 romani

import functionality is hosted in plugin - https://github.com/checkstyle/sonar-checkstyle/blob/master/checkstyle-sonar-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporter.java So it should be possible to generate Rule with custom ID during import.

romani avatar Jan 07 '17 15:01 romani

+1

ozlmulg avatar Jan 07 '17 23:01 ozlmulg

+1

millems avatar Feb 10 '17 21:02 millems

+1

dorogush avatar Apr 12 '17 12:04 dorogush

+1

proferit avatar Apr 13 '17 10:04 proferit

If smb have time to fix please be welcome with PR. Currently whole checkstyle team is focused on main project due to GSoC activity. We will not have time till September almost for sure.

romani avatar Apr 13 '17 12:04 romani

It seems not to be possible to create a new rule from a template by the CheckstyleProfileImporter. Please give me a hint how to achieve this.

proferit avatar Apr 27 '17 09:04 proferit

@proferit , I do not know exactly how to make it. But there are two types of Rules already in plugin:

  • withtout properties
  • with properties <cardinality>MULTIPLE</cardinality>

without properties - https://github.com/checkstyle/sonar-checkstyle/blob/master/checkstyle-sonar-plugin/src/main/resources/org/sonar/plugins/checkstyle/rules.xml#L97 they are created as rules , not as templates. So solutions is somewhere there. If still not clear - we need to ask Sonar on their maillist on how to make it.

romani avatar Apr 27 '17 13:04 romani

+1

diegolovison avatar Oct 04 '17 11:10 diegolovison

Hello, are there any workaround?

diegolovison avatar Oct 27 '17 10:10 diegolovison

workaround:

  1. remove all problematic module configurations
  2. import automatically what is possible
  3. manually create rule(s) from template(s) in Sonar UI
  4. manually put all properties for Rule/Check to UI from config.

romani avatar Oct 28 '17 12:10 romani

+1

arnabcse28 avatar Nov 14 '17 02:11 arnabcse28

+1

zinnchen avatar Jan 10 '18 10:01 zinnchen

if smb have a time, please send PR.

romani avatar Jan 10 '18 19:01 romani

I placed 15$ bounty on this issue (from general donations budget), does any body wants to contribute and have a beer after this :) ?

romani avatar Jun 03 '18 18:06 romani

I believe i created a working google checkstyle xml file just import it when you create Quality Profile for Java

google checkstyle for sonar.zip

Note: 12 rules that caused errors were removed and have to be added manually as templates. Also this fix is only for a specific release and won't have changes for future releases.

roeeduv avatar Oct 03 '18 07:10 roeeduv

@roeeduv , can you explain how you overcome problem of several instances of the same Check in original config ?

romani avatar Oct 04 '18 14:10 romani

I didn't use Checkstyle rules. I took Google's coding style XML and deleted all the rules with errors (about 12 rules) and I added them later from Checkstyle templates

roeeduv avatar Oct 05 '18 13:10 roeeduv

@roeeduv I added your comment to the zip link. This means it isn't a complete workaround by itself, and is also disconnected from the updated configuration checkstyle provides every release for Google. It is a temporary fix until the real problem is solved.

rnveach avatar Oct 05 '18 13:10 rnveach

I am getting the following error when I load your checkstyle in sonar:

checkstyle:com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck

I believe i created a working google checkstyle xml file just import it when you create Quality Profile for Java

google checkstyle for sonar.zip

Note: 12 rules that caused errors were removed and have to be added manually as templates. Also this fix is only for a specific release and won't have changes for future releases.

ddelizia avatar Dec 18 '18 11:12 ddelizia

I am getting the following error when I load your checkstyle in sonar:

checkstyle:com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck

I believe i created a working google checkstyle xml file just import it when you create Quality Profile for Java google checkstyle for sonar.zip Note: 12 rules that caused errors were removed and have to be added manually as templates. Also this fix is only for a specific release and won't have changes for future releases.

try removing "EmptyLineSeparatorCheck" rule from the xml file and add it manually from the templates

roeeduv avatar Dec 20 '18 08:12 roeeduv

Hi,

I spent few days to investigate this issue and I figured out that both sonarqube code and sonar-checkstyle code has to be modified in order to resolve this issue. I managed to make importProfile to work properly by modifying both source code, but it wouldn't work unless both code are modified. I will create a discussion thread for this issue in Sonarqube community.

As for now, I created a script which works around this issue via Sonarqube Web API. If anybody wants to save themselves from manual creation/activation of checkstyle rules, please check my workaround script written in Python.

Problems (why importProfile does not work)

  1. Sonarqube server does not support defining custom rule from template rule when importing a profile Referring to relevent code in Sonarqube, it only supports activation of pre-defined rules and does not support definition of custom rule. Since template rules are not supposed to activated by themselves, custom rules should be newly defined from a template rule. But defining new custom rules during importProfile is not supported by Sonarqube referring to relevent code.

  2. Defining custom rule from template in plugin-side is also not supported As far as I investigated, there is no way to define a new custom rule from template rule in plugin-side referring to plugin api docs. So it is not possible to make new custom rule neither in plugin-side.

  3. Plugin does not allow whitespaces around comma When I tried to import google_checks.xml, it failed because of whitespaces around comma(i.e. LITERAL_TRY, LITERAL_FINALLY, LITERAL_IF fails because of whitespaces). I am not sure whether it is a supposed behavior or not, but I think it would be better to allow whitespaces around commas. (Maybe this(sonar-checkstyle#107) is relevent issue?)

Workaround

You can try my workaround script written in Python which use web API to import profile. I have tested on my computer and it worked properly.

I appreciate everyone for maintaining this useful and awesome plugin. :) Please feel free to correct me if I misunderstood something or made a mistake. Sincerely,

SPICYJO avatar May 23 '21 13:05 SPICYJO

Hi there, it's already end of 2021 and it seems that the problem still haven't beed solved.

drriguz avatar Dec 01 '21 09:12 drriguz

@SPICYJO I will create a discussion thread for this issue in Sonarqube community. - any updates or a link on this?

@drriguz unfortunately, no. as @SPICYJO figured out SQ does only allow activation of rules and not creating custom instances. hence, we need to wait for an upstream fix (if it happens) unless one has a better idea. currently, there's also not enough bandwidth from my side to investigate if there's a different workaround that might work.

muhlba91 avatar Dec 02 '21 10:12 muhlba91

@muhlba91

Here is a link for the thread. https://community.sonarsource.com/t/is-there-any-way-to-define-a-new-custom-rule-from-template-rule-while-importing-profile/43655

But there is no updates yet.

SPICYJO avatar Dec 31 '21 06:12 SPICYJO