Overview
IZ Analyzer is the most popular automated code quality analysis tool for MuleSoft and API (RAML and OAS) projects. It comes with over 200 pre-built rules aligned to industry best practices. In addition to the pre-built rules, project teams can define organization specific rules by using Custom Rules: one of our most exciting features. All it takes is a few lines of simple groovy script! Let us learn how to use IZ Analyzer to create a Custom Rule to identify whether a project repository is created in Bitbucket.
Prerequisites
In order to create custom rules, we need the following:
- IZ Analyzer Anypoint Studio plugin installed and configured.
- A valid license / trial license applied to the organization.
- An account in BitBucket
- Generate App password in BitBucket to invoke the REST APIs.
More information about BitBucket repository REST APIs can be found here
Setting Credentials in Global Settings
The custom rule that we are going to implement contains sensitive information (i.e. username and app password), so it is highly recommended that we set the values in the global settings and use the property reference in the custom rule.
- Login to your SonarQube instance where IZ Analyzer Mule plugin is deployed.
- Navigate to Administration -> Configuration -> Mule -> Project/Rule Settings.
- Add new property BIT_BUCKET_API_USER_NAME=<bitbucket username>
- Add new property BIT_BUCKET_API_APP_PASSWORD=<bitbucket app password>
- Save the settings.
Creating the Custom Rule
In this post lets look at creating a custom rule to verify if the Mule project has a repository created in BitBucket.
NOTE: Sync the metadata in Anypoint Studio to download all the settings from server by navigating to Window -> Preferences -> Sync Metadata
Lets use the Custom Rule Editor (Rules Playground) in Anypoint Studio to define and validate our new rule. Custom rules are created using simple Groovy scripts.
Following attributes needs to filled in the custom rule editor
Rule Name – Name for our new Rule. Eg: BitBucket Repository is NOT created
Rule Type – Type of rule Eg: Bug
Rule Severity – Severity Eg: Blocker
Rule Definition – Actual definition of the rule in Groovy.
def response = []
def pom = new groovy.xml.XmlSlurper().parse(new File(PROJECT_ROOT_DIR, 'pom.xml'))
def projectName = pom.artifactId
def httpGet = new org.apache.http.client.methods.HttpGet('https://api.bitbucket.org/2.0/repositories/xxxxx/'+projectName)
String auth = PROJECT_SETTINGS.BIT_BUCKET_API_USER_NAME + ':' + PROJECT_SETTINGS.BIT_BUCKET_API_APP_PASSWORD;
def encodedAuth = org.apache.commons.codec.binary.Base64.encodeBase64(auth.getBytes('UTF-8'))
String authHeader = 'Basic ' + new String(encodedAuth);
httpGet.setHeader('Authorization',authHeader)
def resp = HTTP_CLIENT.executeRequest(httpGet)
def statusCode = resp.getStatusLine().getStatusCode()
if(statusCode != 200) {
return 'PROJECT_HANDLE'
}

In this example, the rule definition validates if the repository is already created in BitBucket. The repository name is derived from the artifactId of project’s pom.xml.
As soon as we have keyed in the required attributes, we can use the ‘Evaluate’ button to validate the rule definitions, and then we can publish it to the server.
View Custom Rule in “On the Fly Results”
IZ Analyzer’s On The Fly Results table/view will display the issues related to the project that user is working on. Project is determined based on the current active file (i.e. the file that user is working on) in Anypoint Studio.
Use the Sync Rules from Server option to sync the rules from server to local Anypoint Studio instance. Once the process is complete, we should see the custom rule that we just defined, in the Fly Results table.



Endnotes
IZ Analyzer Custom Rules & Auto Fix definition can not only be used to fix errors in Mule configuration files, but also invoke external APIs and validate resources in external systems.
In this post we have looked at how to create a Rule to validate if the repository exists in BitBucket. In the next part of this blog series, we will explore more on Custom Auto Fix Rules to create a BitBucket repository.
Let us know what you think, feel free to drop a comment.
Start using IZ Analyzer for Free
Interested in trying out the most powerful MuleSoft code review tool in the Mule Ecosystem? Try now our free Online scanner and get your scan results instantly.
There’s more to it. If you would like to see the product in action, book an online demo.