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]

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.



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



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.