{{announcement.body}}
{{announcement.title}}

Mulesoft Code Review Automation Using SonarQube

DZone 's Guide to

Mulesoft Code Review Automation Using SonarQube

The code review process is very critical for project success and below are some pointers on why one should include a code review process.

· Integration Zone ·
Free Resource

The code review process is very critical for project success and below are some pointers on why one should include a code review process:

  1. Consistent design and implementation
  2. Minimizing your mistakes and their impact
  3. Ensuring project quality and meeting requirements
  4. Improving code performance
  5. Sharing new techniques

SonarQube

SonarQube is an open-source code review tool for continuous inspection of code quality to perform automatic reviews with static analysis of code to detect bugs, code smells, and security vulnerabilities on various programming languages.

Each language plugin comes with a predefined, built-in profile (called “Sonar way”) having a set of predefined rules and it will be used as a default profile during analysis for projects. We can create our Profile and add rules to it.

Sonar Installation

  1. Install the latest SonarQube distribution, use SonarQube 7.7.

  2. Start the SonarQube server: StartSonar.bat

  3. Log in to http://localhost:9000 with administrator credentials (admin/admin).

Configuration

Setting Rules

SonarQube analyzes the code against the rules added to the active profile.

As MuleSoft codes are written in XML, the focus in this article will be on XML rules and Xpath expressions.

To add a rule follow the below steps:

  • Log in as a Quality Profile Administrator and go to the rule page
  • Select XML Language to create the XPath rule
  • Select Template criterion and click on "Show Templates Only"
  • Add XPath rules in “Track breaches of an XPath rule” template:sonarqube
  • Create a custom rule:create rule
  •  Write the XPath query in the expression tab (it should comply to XPath 1.0)
  • Activate the recently added rule by clicking on XML (Sonar way) in the Quality Profile and run an analysis.sonarqube
  • Click on the activate more button to activate specific rules.

XML

bulk change

Quality Gates

A quality gate is a set of conditions and a set of projects to be checked against these conditions. It can be defined as a set of threshold measures set on your project like Code Coverage, Technical Debt Measure, Number of Blockers/Critical issues, Security Rating/ Unit Test Pass Rate, and more.

sonarqube

Based on your projects, you can set up the metrics in your Quality Gate to explicitly throw a Warning or Error when the code crosses a threshold.

Mule 4 Application Rules

The below Xpath rules will give a brief idea of how one can write rules for a Mule 4 application:

  1. Validate re-connection strategy for HTTP Requester
    Expression:

//*[local-name()='request-connection' and 

namespace-uri()='http://www.mulesoft.org/schema/mule/http']

[not(*[local-name()='reconnection'])] 

Below is the highlighted element used with local-name() function:

HTTP
 




x


 
1
<http:request-connection host="${https.requestor.host}" port="${https.requestor.port}" connectionIdleTimeout="${https.requestor.timeout.connection}" protocol="HTTPS" usePersistentConnections="false">
2
 
          
3
<reconnection>
4
 
          
5
<reconnect frequency="${https.requestor.reconnection.frequency}" count="${https.requestor.reconnection.attempts}" />
6
 
          
7
</reconnection>
8
 
          
9
</http:request-connection>


  1. No Exception Strategy set

Expression:

filePattern: **/*error.xml

Expression : not(/*/error-handler)

Below is the highlighted element used:

<error-handler name="errorError_Handler" 

doc:id="68f0db07-a3ea-4f1e-a4a5-83a45c22ee28"/> 

Once you create all rules, you can add them to the SonarQube as explained in the “Setting Rules” step.

Mule Project Code Review Automation

Add below profile in settings.xml in maven/conf/settings.xml

HTTP
 




xxxxxxxxxx
1
25


 
1
<profile>
2
 
          
3
<id>sonar</id>
4
 
          
5
<activation>
6
 
          
7
<activeByDefault>true</activeByDefault>
8
 
          
9
</activation>
10
 
          
11
<properties>
12
 
          
13
<sonar.host.url>http://localhost:9000</sonar.host.url>
14
 
          
15
<sonar.sources>src/main/mule,src/main/resources,pom.xml</sonar.sources>
16
 
          
17
</properties>
18
 
          
19
</profile>
20
 
          
21
<activeProfiles>
22
 
          
23
<activeProfile>sonar</activeProfile>
24
 
          
25
</activeProfiles>



Run the below command to generate a sonar report

mvn clean sonar:sonar -Dsonar.login=username

-Dsonar.password=password 

(default username:password is admin:admin)

mvn clean sonar

Report Generation

  • Once the analysis is complete, you can log in to the SonarQube GUI to check the project snapshot as shown in the image below.

test passed

  • Click on the project name to go to the Project overview page

test master

  • Click on the Issues tab to look at all the code violations in the project(s) scanned. As shown in the image below, each issue has description with a recommendation to fix, followed by Activity/Path where the issue occurred along with the configuration name/value where the issue occurred. testing

Conclusion

Automating the code review process reduces the delivery time and human error and SonarQube is a good fit for the Mulesoft Automated Code Review tool as it sets a collection of rules to analyze your source code at compile time to identify potential vulnerabilities, bugs, anti-patterns, refactoring, and poor coding practices.

Topics:
automation tool, codereview, integration, mule 4, mulesoft, sonarqube, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}