mulesoft-catalyst / mule-sonarqube-plugin

The Mule SonarQube Plugin provides the capability to do code inspecting and taking project metrics from a mule project using SonarQube.
The Unlicense
46 stars 68 forks source link
code-quality code-quality-analyzer mule sonarqube static-code-analysis

Table of Contents

Mule SonarQube Plugin

A plugin has been created to validate Mule applications code (Configuration Files) using SonarQube. This plugin contains a set of rules and metrics that are going to used and calculated every time a project is being inspected. For more information, about SonarQube please refer to: https://www.sonarqube.org/

This is an UNLICENSED software, please review the considerations. If you need assistance for extending this, contact MuleSoft Professional Services

SonarQube Concepts

Issues

Is the way to identify and classify different aspects or exceptions of the code. Currently, there are three type of issues:

And each issue could have different severities:

For more information, please refer to https://docs.sonarqube.org/latest/user-guide/issues/

Quality Profiles

Quality profiles are collections of rules defined for each language that is going to be executed during the inspection of the code. The plugin defines one Language, Mule, and two profiles, one for each version of mule. Each one of these profiles defines a set of rules that can be updated easily.

qualityprofiles

Measures

Depending on the language, different metrics are being calculated. Issues are one type of metric, but you will have more information about the project, information such as, for example, Size, Complexity, and Coverage.

For more information, please refer to https://docs.sonarqube.org/latest/user-guide/metric-definitions/

Quality Gates

With them, you could set up your quality control and enforce it on all your projects. You could say that you will not deploy an app with less than 60% of coverage or with more than 3 Code Smell. This quality control could be easily added to your CI/CD process to, for example, allow or not the deployment of your app.

For more information, please refer to https://docs.sonarqube.org/latest/user-guide/quality-gates/

Plugin Aspects

Rules

They are created based on a file, named rule store. In this rule store, you could define: -rulesets: A way to group the rules by a category. The category itself could be anything, for example, categories like application, configuration, api, or batch could be used. When inspecting a particular project, you could specify which categories (rules) you want to inspect by running for example, mvn sonar:sonar -Dsonar.mule.ruleset.categories=flows,configuration,api -rules: the rule itself, with these attributes:

Currently, there are two files (rule stores), one per each mule runtime version (3|4). For example, the rule store (rules-4.xml) has three rulesets (categories):

Rule Store Example

<rulestore type="mule4">
    <ruleset category="application">
        <rule id="1"
            name="Application should have used APIKit to auto-generate the implementation interface"
            description="Application should have used APIKit to auto-generate the implementation interface"
            severity="MAJOR" applies="application" type="code_smell">
            count(//mule:mule/apikit:config)>0
        </rule>
  </ruleset>
    <ruleset category="flows">
        <rule id="1"
            name="Configuration files should not have so many flows"
            description="Configuration files should not have so many flows"
            severity="MAJOR" type="code_smell">
            not(count(//mule:mule/mule:flow)>=10)
        </rule>
    </ruleset>
    <ruleset category="configuration">
        <rule id="1"
            name="Data Transformations should be stored in external DWL Files - Payload"
            description="Data Transformations should be stored in external DWL Files - Payload"
            severity="MINOR" type="code_smell">
            count(//mule:mule/mule:flow/ee:transform/ee:message/ee:set-payload)=0
            or
            matches(//mule:mule/mule:flow/ee:transform/ee:message/ee:set-payload/@resource,'^.*dwl$')
        </rule>
        <rule id="2"
            name="Mule Credentials Vault should not use a hardcoded encryption key"
            description="Mule Credentials Vault should not use a hardcoded encryption key"
            severity="MAJOR" type="bug">
            count(//mule:mule/secure-properties:config)=0
            or
            is-configurable(//mule:mule/secure-properties:config/@key)
        </rule>
    </ruleset>
</rulestore>

Measures

The plugin handles different types of metrics, such as:

Configuration

Server

As the plugin inspects xml files and SonarQube already comes with an XML plugin, you have to modify this behavior so only one plugin inspects xml files. For that reason, you have to remove the xml extension from it. To do that you have to, as administrator, go to Administration-> Configuration->General Settings->XML and delete the .xml extension from it.

serverconfig

Plugin

  1. Plugin Generation

    • Download the module source code.
    • Open a terminal window and browse to module root folder.
    • Build the mule plugin for Mule rules running mvn clean package sonar-packaging:sonar-plugin -Dlanguage=mule.
    • Copy the generated file, mule-validation-sonarqube-plugin-{version}-mule.jar to sonar-home/extensions/plugins
  2. Copy rules Mule 3 Rules and Mule 4 Rules to sonar-home/extensions/plugins The jar file of the plugin has to be placed in the following folder /extensions/plugins/

Project

Quality Profile

By default, the mule 4 quality profile is going to be used. In case you are analyzing a mule 3 you need to change it, to do that, as an administrator, go to the project -> Administration -> Quality Profiles and change the profile for the Mule Language.

quality-profiles-conf

Quality Gate

If you have created a custom Mule Quality Gate, to enforce it on a project, you will have to go to the project -> Administration -> Quality Gates and change the gate previously selected.

quality-gate

Execution

Analyzing

To analyze a project, you could run the maven command: mvn sonar:sonar. For that, you need to specify the server you are going to use and the directories you want to inspect when you are running the command like this:

mvn sonar:sonar -Dsonar.host.url=http://mysonarqubeserver:9000 -Dsonar.sources=src/

And alternative to this, could be:

Results

Once you run the command, you will see the project and the information about it like this:

Overview

project-overview

Issues

project-issues

Measures

project-measures

Try it out

docker pull fperezpa/mulesonarqube:7.7.3
docker run -d --name sonarqube -p 9000:9000 -p 9092:9092 fperezpa/mulesonarqube:7.7.3

Disclaimer The docker image is based on the official SonarQube Image, sonarqube:7.7-community. For more information please visit, https://hub.docker.com/_/sonarqube/

Final Notes

Enjoy and provide feedback / contribute :)