Auto Fix - In Scanner
Autofix in scanner is a feature, where static code analysis issues can be fixed automatically by enabling few properties while scanning the projects.
Make sure you have:
|
Enable Auto Fix
-
Autofix can be enabled by passing
analyzer.<plugin>.auto.fix.all
system property while running the sonarscanner/mvn command to analyze the project. For more information refer Code Analysis In Server-
Ex: -Danalyzer.mule.auto.fix.all=true
To enable auto fix for Mule plugin -
Ex: -Danalyzer.api.auto.fix.all=true
To enable auto fix for API plugin
-
-
Preview mode can be enabled to get the list of changes that would be performed by Auto Fix without updating any of the files.
-
Can be enabled using
-Danalyzer.<plugin>.auto.fix.preview
system property
-
-
Default log location is
target/autofix_log.csv
relative to the project from which scanner is invoked. This property can be customized usinganalyzer.<plugin>.auto.fix.log.location
system property. -
Fix can also be applied only on certain issues by passing various filters. Every rule will be associated with a
Type
,Severity
andTag
, any of these properties can be used to specify the filters-
Ex: -Danalyzer.<plugin>.auto.fix.rule.severities can be set to MAJOR,MINOR to fix only major and minor issues
-
Ex: -Danalyzer.<plugin>.auto.fix.rule.types can be set to CODE_SMELL to fix only Code Smells
-
Ex: -Danalyzer.<plugin>.auto.fix.rule.tags can be set to any custom tags attached to the rule. Only rules with specified tags will be auto fixed in this case.
-
Mule Scanner Properties
Following parameters can be passed as system properties to Mule scanner.
Property Name | Description | Possible Values | Default |
---|---|---|---|
|
Enable auto fix for the Mule scanner |
true / false |
false |
|
Location of the Auto Fix log. Logs will be generated in .csv format |
Any location where the .csv should be generated |
target/autofix_log.csv |
|
Enable or disable the Auto Fix preview mode |
true / false |
false |
|
Rule types to be auto fixed. Only those issues with specified rule types will be fixed. Multiple rule types can be specified by using comma(,) as a delimiter. |
BUG / VULNERABILITY / CODE_SMELL / SECURITY_HOTSPOT |
By default all the issues will be fixed |
|
Rule severities to be auto fixed. Only those issues with specified rule severities will be fixed. Multiple rule severities can be specified by using comma(,) as a delimiter. |
BLOCKER / CRITICAL / MAJOR / MINOR / INFO |
By default all the issues will be fixed |
|
Rule tags to be auto fixed. Only those issues with specified rule tags will be fixed. Multiple rule tags can be specified by using comma(,) as a delimiter. |
Any tags associated with rule |
By default all the issues will be fixed |
API Scanner Properties
Following parameters can be passed as system properties to API scanner.
Property Name | Description | Possible Values | Default |
---|---|---|---|
|
Enable auto fix for the API scanner |
true / false |
false |
|
Location of the Auto Fix log. Logs will be generated in .csv format |
Any location where the .csv should be generated |
target/autofix_log.csv |
|
Enable or disable the Auto Fix preview mode |
true / false |
false |
|
Rule types to be auto fixed. Only those issues with specified rule types will be fixed. Multiple rule types can be specified by using comma(,) as a delimiter. |
BUG / VULNERABILITY / CODE_SMELL / SECURITY_HOTSPOT |
By default all the issues will be fixed |
|
Rule severities to be auto fixed. Only those issues with specified rule severities will be fixed. Multiple rule severities can be specified by using comma(,) as a delimiter. |
BLOCKER / CRITICAL / MAJOR / MINOR / INFO |
By default all the issues will be fixed |
|
Rule tags to be auto fixed. Only those issues with specified rule tags will be fixed. Multiple rule tags can be specified by using comma(,) as a delimiter. |
Any tags associated with rule |
By default all the issues will be fixed |
Multiple Plugins Scanning the same Project
While scanning the Mule projects, by default Mule and API Analyzer plugins would report the static code analysis issues.
If there are any other open source plugins scanning files in the same project, then the Background Task
might fail in the server with an error _ Source of file has less/more lines than expected_ .
As a resolution the scanner command should be executed again without analyzer.<plugin>.auto.fix.all
system property.
-
Scan 1 - To auto Fix issues _sonar-scanner -Dsonar.projectKey=<key> -Dsonar.sources=. -Dsonar.host.url=<host> -Dsonar.login=<token> -Dsonar.exclusions=target/** -Danalyzer.api.auto.fix.all=true -Danalyzer.mule.auto.fix.all=true _
-
Scan 2 - To upload results to the server _sonar-scanner -Dsonar.projectKey=<key> -Dsonar.sources=. -Dsonar.host.url=<host> -Dsonar.login=<token> -Dsonar.exclusions=target/** _