Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import should work with rule templates #2

Open
romani opened this issue Apr 26, 2016 · 27 comments
Open

import should work with rule templates #2

romani opened this issue Apr 26, 2016 · 27 comments
Labels

Comments

@romani
Copy link
Member

romani commented Apr 26, 2016

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
Copy link
Member Author

romani commented Dec 16, 2016

the same as #34

@romani
Copy link
Member Author

romani commented Dec 18, 2016

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
Copy link
Member Author

romani commented Jan 7, 2017

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
Copy link

ozlmulg commented Jan 7, 2017

+1

3 similar comments
@millems
Copy link

millems commented Feb 10, 2017

+1

@dorogush
Copy link

+1

@proferit
Copy link

+1

@romani
Copy link
Member Author

romani commented Apr 13, 2017

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
Copy link

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
Copy link
Member Author

romani commented Apr 27, 2017

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

@diegolovison
Copy link

+1

@diegolovison
Copy link

Hello, are there any workaround?

@romani
Copy link
Member Author

romani commented Oct 28, 2017

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
Copy link

+1

1 similar comment
@zinnchen
Copy link

+1

@romani
Copy link
Member Author

romani commented Jan 10, 2018

if smb have a time, please send PR.

@romani
Copy link
Member Author

romani commented Jun 3, 2018

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

@roeeduv
Copy link

roeeduv commented Oct 3, 2018

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
Copy link
Member Author

romani commented Oct 4, 2018

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

@roeeduv
Copy link

roeeduv commented Oct 5, 2018

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
Copy link
Member

rnveach commented Oct 5, 2018

@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
Copy link

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
Copy link

roeeduv commented Dec 20, 2018

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
Copy link

SPICYJO commented May 23, 2021

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
Copy link

drriguz commented Dec 1, 2021

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

@muhlba91
Copy link
Collaborator

muhlba91 commented Dec 2, 2021

@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
Copy link

SPICYJO commented Dec 31, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests