checkstyle / sonar-checkstyle

Support on Checkstyle in SonarQube. Officially transfered from https://github.com/SonarQubeCommunity/sonar-checkstyle
GNU Lesser General Public License v3.0
174 stars 70 forks source link

import should work with rule templates #2

Closed romani closed 2 weeks ago

romani commented 8 years ago

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 commented 7 years ago

the same as #34

romani commented 7 years ago

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 commented 7 years ago

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.

ozlmulg commented 7 years ago

+1

millems commented 7 years ago

+1

dorogush commented 7 years ago

+1

proferit commented 7 years ago

+1

romani commented 7 years ago

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.

proferit commented 7 years ago

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.

romani commented 7 years ago

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

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.

diegolovison commented 7 years ago

+1

diegolovison commented 7 years ago

Hello, are there any workaround?

romani commented 7 years ago

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.

arnabcse28 commented 7 years ago

+1

zinnchen commented 6 years ago

+1

romani commented 6 years ago

if smb have a time, please send PR.

romani commented 6 years ago

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

roeeduv commented 6 years ago

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.

romani commented 6 years ago

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

roeeduv commented 6 years ago

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

rnveach commented 6 years ago

@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.

ddelizia commented 5 years ago

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.

roeeduv commented 5 years ago

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

SPICYJO commented 3 years ago

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,

drriguz commented 2 years ago

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

muhlba91 commented 2 years ago

@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.

SPICYJO commented 2 years ago

@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.

muhlba91 commented 2 weeks ago

SQ deprecated import/export functionalities, and there won't be a replacement. hence, closing this issue.