I'am building custom rules in SonarQube 5.1.2 and I can't find out how to apply a rule to multiple file types.
I've seen that ant-style file pattern is only one pattern, not a list.
Specifically I want my rule to match **/*.wsdl and **/*.WSDL and eventually files with other extensions.
Is there a better way to do this than replicating the rule?
thanks.
Related
I wanted to configure my SonarQube [v8.9.6.50800] scan to ignore Specific rule e.g. squid:S1172 from classes which contains a specific string e.g. implements BillingApi.
To set this up I need to use some configuration?! that will accept a regular expression, let's say: .* implements [^{]*Api and rule.
I'm aware of the option to exclude specific rule for file path pattern as explained here: SonarQube define "Ignore Issues on Multiple Criteria" in maven build
Or the option to exclude all rules in a file as explained here: sonar project.properties to ignore files containing a regular expression
But I need to narrow the scope to exclude specific rule from file contains some regex.
Instead of excluding or ignore rules in sonar's property file, I'd like to have only a few certain rules for sonar to analyse, so I don't need to exclude a large number of rules out of 344 rules for c++. How can I do that? (I'm not adding customized rules)
I imageine the syntax would be: (in .properties file)
sonar.issue.include.multicriteria=***
sonar.issue.include.multicriteria.***.ruleKey=cpp:S984
....
EDIT:
1, I need to configure this in a CLI environment.
2, It's about one project, two rule sets. one rule sets for local use and the other one for CI/CD use.
You need to craft a Quality Profile that contains only your rules of interest, and then either make it the default profile for C++, or explicitly assign your project to it.
BTW, correctly setting exclusions in properties (versus through the UI) is quite tricky. I'm not sure about the correctness of the ruleKey field name, and you're probably missing another field in there, but your syntax seems to be on the right track.
I would like to use apache camel with regex pattern for txt files, but the problem is the correct pattern and how to use it in from() method. The documentation mentions only about the keyword include and exclude. Which is the easiest way to use a pattern in order to check if filename matches the regex pattern? Thank you in advance.
The easiest might be the one you've mentioned (include parameter on the file endpoint). Example (include every txt):
from("file://input/directory?include=.*\\.txt")
Other option is to implement a GenericFileFilter.
Since you're only checking for the file name, you can do something like this for files appearing in a specific folder and choose what to do with them using a predicate:
from("file://fooFileFolder/")
.choice()
.when(header("CamelFileNameOnly").regex("fooPattern")).to("mock:fooHere")
.otherwise().to("mock:fooThere")
.end();
It's up to you to use the regex suitable for the matching pattern in the currently read file name you're looking to apply the test to. You can also use regex with Camel's Simple dialect.
I have lot of logs and every record contains a url. And I have about 2000+ url patterns to filter the log. Some patterns are regular pattern with capturable group. I want to get url and the matched pattern and, if possible, the captured groupes. Is there a java lib can help me. Or any Algorithm which can solve my problem. Or anyting else which related to my problem. Thanks a lot.
Take a look at java regular expressions library (link).
You can construct a single large pattern by concatenating your original patterns with | between them (use () to specify that you don't want just 1 character).
The regular expression can be compiled into an efficient matching finite automata, that you can run over your data. Just make sure you compile it once and reuse it for every record.
It will handle extracting groups, but you need to handle the groups in a generic way (since any group can be matched). If it makes it easier consider using named groups to make handling simpler.
is there a possibility to duplicate existing checkstyle rules? I want Sonarqube to differ guideline rules.
Example:
The rule for method complexity should mark the method as [MINOR] when there is more than 10 lines of code per method and as [MAJOR] when there is more than 30 lines of code per method.
Maybe there is a particular plugin for this special case?
Thanks in advance
This is currently a limitation in SonarQube. However, it can be done partially.
For Checkstyle based rules, you can copy some rules (those that have a Copy Rule button; screenshot here under "Multiple activation of rules"). I've personally used this for the Comment Pattern Matcher and created a rule that matches TODO comments as INFO severity and another that matches FIXME comments as CRITICAL. As I understand your question, that is what you are looking for. I don't think this is possible for all parameterizable rules, though.
Also, for PMD based rules, there is this blog post which describes how to use the XPath rule template in order to create custom PMD rules. This could be a workaround for those PMD based rules that don't have a Copy Rule button.
The feature we would need is a Copy Rule button for every parameterizable rule. AFAIK, this does not exist yet.