integralzone

The ultimate guide to Creating custom rules to validate Bitbucket repository with IZ Analyzer- Part 1

Written by:
Published on March 10, 2022

The ultimate guide to Creating custom rules to validate Bitbucket repository with IZ Analyzer- Part 1

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:

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.

Custom Rule displayed in Fly Results

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Other Blog Posts

Other Blog Posts

Customer Success Stories: Why IZ Analyzer Is a Game Changer

IZ Analyzer, a dynamic code analysis tool, has garnered significant praise from a diverse range of clients, each with their own unique perspectives on what makes it stand out. Client Feedback: A Deep Dive Client from a Fortune 500 beverage manufacturing: Specialized MuleSoft Code Scanning For this global leader in the beverage industry, the specialization …

Read more

MuleSoft Runtime Code Scanning – Why Do You Need It?

One of the most frequently asked questions is if we have static code analysis and a well defined DevOps process, why would we need run time code analysis? In this article, let’s explore the differences between the two and why you might want to have runtime code analysis (and IZ Runtime Analyzer) even if you have …

Read more

Ensuring Software Quality in Healthcare: Leveraging IZ Analyzer for MuleSoft Code Scanning πŸ₯πŸ’»

Ensuring software quality in the healthcare industry is a top priority, with direct implications for patient safety, data security, and regulatory compliance. Healthcare software development requires adherence to specific rules and best practices to meet the unique challenges of the industry. In this blog post, we will explore essential software quality rules specific to healthcare …

Read more