Rule creation for max activities inside a mircroflow
Hi all,
I am currently trying to create a rule that checks the number of activities inside a microflow. The goal is to trigger a warning once the number reaches, for example, 20 or more.
I have tried two approaches, but neither has produced any usable results.
The first one was based on 002_0001_number_of_entities.rego, which I customized accordingly.
# METADATA
# scope: package
# title: Microflow activity count too high
# description: Microflows should not contain more than 20 executable activities
# authors:
# - Sinar Can Yatar <[email protected]>
# custom:
# category: microflows
# rulename: MaxMicroflowActivities
# severity: MEDIUM
# rulenumber: 005_0002
# remediation: Refactor or split microflow to reduce complexity
# input: "**/*$Microflow.yaml"
package app.mendix.microflows.max_mircroflow_activities
import rego.v1
annotation := rego.metadata.chain()[1].annotations
default allow := false
allow if count(errors) == 0
max_activities := 0
errors contains error if {
count_activities := count(input.ObjectCollection.Objects)
count_activities > max_activities
error := sprintf("[%v, %v, %v] There are %v activities which is more than %v",
[
annotation.custom.severity,
annotation.custom.category,
annotation.custom.rulenumber,
count_activities,
max_activities,
]
)
}
second approach with a different logic:
errors contains error if {
activity_count := count([a | a := input.ObjectCollection.Objects[_]; a["$Type"] != "Microflows$Annotation"])
activity_count > 0
error := sprintf("[%v, %v, %v] %v (found: %v activities)",
[
annotation.custom.severity,
annotation.custom.category,
annotation.custom.rulenumber,
annotation.title,
activity_count,
]
)
}
The max_activities value is set to zero to trigger the rule every time for testing purposes. I have a strong feeling that the issue is related to the input configuration somehow. Do you have any suggestions on how to fix this?
Hi Sinar,
Bart has created a similar rule at https://github.com/mxlint/mxlint-rules/blob/main/rules/005_microflows/005_0003_number_of_elements_in_microflow.rego
It is not perfect as it assumes there is always a start and end element and therefore can subtract 2 from the count before comparing to your limit.
Some ways to try:
-
use LLM to assist creating the rule. The key here is supply the AI enough context: example input document or test scenario, the rule to be modified and the prompt to filter based on activity types etc.
-
we just released new mxlint-cli with support for rules in javascript format. Check the files in resources/rules/*
Hi Steven,
thank you for your suggestions.
I was able to get the rules running, but I am still facing limitations with the input – both in Rego and JS. The problem I’m still facing is not reaching all microflows that are structured in different levels of folder hierarchies.
For example:
The microflows I was trying to lint are located under this path: modelsource\MyFirstModuleRename\Microflows.
If I now lint with this input: input: "**/*$Microflow.yaml" which is used in the rule by Bart, the rule does not catch the YAML files of the microflows inside my dedicated folder.
If I change the input to input: "*/*/*$Microflow.yaml", I reach the right hierarchy inside my dedicated microflow folder.
I tried a lot, but I could not write a generic input statement that reaches all hierarchies inside the modelsource folder. Mendix projects will have microflows at different levels of hierarchy, and I’m afraid I cannot reach them all at once. Imagine subfolders of microflows or, generally, different kinds of folder structures in different projects from different developers.
Am I under a misconception here and doing something wrong, or is this really a limitation I’m facing that can only be resolved with more effort?
I was thinking of creating a for-loop over the rule, iterating over the hierarchies. Another solution we considered is changing the model export so that everything is structured at only one level from the beginning, not following the actual Mendix project hierarchy.
I hope I was able to express myself clearly and understandably for you :)
Hi Sinar,
It's possible you have run into a bug in how the input expression is used by mxlint-cli. For now I suggest:
- keep using //*$Microflow.yaml as input expression for now
- file a bug in mxlint-cli for this
with https://github.com/mxlint/mxlint-cli/releases/tag/v3.6.0 the input expression bug has been fixed. The new format is a regular expression:
.*/*\\$Microflow\\.yaml
Also we now support javascript format to write rules. That should be a lot easier.