integralzone

The Ultimate guide to Creating custom rules to validate Bitbucket repository with IZ Analyzer- Part 2

Written by:
Published on March 16, 2022

The Ultimate guide to Creating custom rules to validate Bitbucket repository with IZ Analyzer- Part 2

In the previous blog The Ultimate guide to Creating custom rules to validate Bitbucket repository with IZ Analyzer- Part1, we have already created a custom rule to validate if the repository is created in BitBucket.

In this post, let us look at creating a custom AutoFix definition to create a new repository in BitBucket if it does not exist.

Prerequisite

  • Create a custom rule to validate if a repository is created in BitBucket: refer to the first part of this blog

Defining the Custom Auto Fix Code

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.

NOTE: In the previous blog, we have already created a rule to validate the BitBucket repo. IZ Analyzer can also update the existing rule by providing the rule key in Custom Rule Editor or create a new one based on the requirement.

Add the below auto fix definition in Custom Rule Editor including the values for other fields as discussed in previous blog.

AutoFix definition of the rule in Groovy script is given below

def code = """
	def pom = new groovy.xml.XmlSlurper().parse(new File(PROJECT_ROOT_DIR, 'pom.xml'))
	def projectName = pom.artifactId

	def httpPost = new org.apache.http.client.methods.HttpPost('https://api.bitbucket.org/2.0/repositories/<organiaztion>/'+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);
	httpPost.setHeader('Authorization',authHeader)

	String req = '{"scm": "git", "project": { "key": "PROJ" } }'

	def entity = new org.apache.http.entity.StringEntity(req)
	httpPost.setEntity(entity)
	httpPost.setHeader("Content-type", "application/json")

	HTTP_CLIENT.executeRequest(httpPost)
	return projectName
"""
def fix = Fix.create('custom_script', code)
fix.addLog(""" DATA ? 'Created repo in BitBucket with name '+DATA : 'Create a new repository in BitBucket' """)

return [fix]
Defining the rule in Custom Rule Editor

In this example, the rule definition validates whether the repository is already created in BitBucket. The repository name is derived from the artifactId of project’s pom.xml.

Once the required attributes are given, we can use the ‘Evaluate‘ button to validate the rule definitions and finally publish the rule to the server.

View Custom Rule in “On the Fly Results”

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.

New custom rule displayed in “Fly Results table

Use the Preview option to view the action that will be performed once the AutoFix is applied. In this case, we need to check the repository to be created in BitBucket if its not already created.

Auto Fix the Issue

Lets use the Fix button to fix the issue, which in our case should go ahead and create a new repository in BitBucket.

Once the AutoFix is complete, we should see a new repository created in the BitBucket account as shown below and the issue will no longer be displayed in Fly Results table

New repository created in BitBucket

Auto Fix – Logs

We can always get a log of all the actions performed by auto fix in the Auto Fix Logs View.

Following attributes will be available in the log –

  • Fix Date – Date on which the fix was applied
  • Rule Key – Key of the rule which as fixed.
  • Description – Rule description
  • Severity – Rule severity
  • Type – Rule type
  • Fix Log – The action performed to fix the issue. (Creating a repo in BitBucket in this case)

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 create resources in external systems.

In this post we have looked at how to create a Rule to validate if the repository exists in BitBucket and a Auto Fix definition to create one if it does not exist.

IZ Analyzer is one of the most powerful tools used by MuleSoft teams to ensure Code review practices are in tandem with industry best practices.

IZ Analyzer’s Anypoint Studio plugin, specially with its latest Preview and AutoFix features simply takes MuleSoft CQA to the next level. Making it easy, faster and simply hassle free. In addition, the 200+ predefined rules IZ Analyzer comes bundled with for MuleSoft ensures that best practices are never compromised, plus the ability to create additional custom rules!

Let us know what you think, feel free to drop a comment.

Start using IZ Analyzer for Free

If you are reading this blog, you should definitely try IZ Analyzer. Simply write to sales@integralzone.com for a free

  • Product Walkthrough or
  • a 14-day Trial Licence

Try now our free Online scanner and get your scan results instantly.

Leave a Reply

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

Other Blog Posts

Other Blog Posts

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

Mule OWASAP API Security Top 10 – Broken Object Level Authorization

In Mule, Object-Level Authorization refers to the process of controlling access to specific objects or resources within an application based on the permissions of the authenticated user. It ensures that users can only perform operations on objects for which they have appropriate authorization. To demonstrate a broken Object-Level Authorization example in Mule, let’s consider a …

Read more

How KongZap Revolutionises Kong Gateway Deployment

In a rapidly evolving digital landscape, businesses face numerous challenges. Faster time to market is the only option business can choose. When it comes end to end Kong Gateway life cycle from deploying to managing Kong Gateway, every one of these challenges is applicable. However, KongZap, a groundbreaking solution is a game-changer by addressing some …

Read more