Skip to content

Commit

Permalink
Release 1.0.4 (#6)
Browse files Browse the repository at this point in the history
* CLOUD-571, CLOUD-568 - Adding flowId to errors as well as adding timing attribute to track length of run for each rule and the aggregate rulePack

* CLOUD-568
  • Loading branch information
tsigle authored Dec 4, 2024
1 parent f2b5b58 commit 8e66f21
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 28 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"davinci",
"dvlint",
"ramac",
"ruleid",
"sprintf",
"subflows"
]
Expand Down
28 changes: 17 additions & 11 deletions lib/LintResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,22 @@ class LintResult {
this.errors = [];
}

setFlowId(flowId) {
this.flowId = flowId;
}

/**
* Get the Message Object based on the lintCodes table. Also, substitutes the
* message and recommendation arguments into the message and recommendation
* strings based on the percent (%) character.
*/
getMessageObj(code, messageArgs, recommendationArgs, nodeId) {
#getMessageObj(code, messageArgs, recommendationArgs, nodeId, flowId) {
const messageObj = {
code: code.code,
message: sprintf(code?.message, ...messageArgs),
type: code?.type || "unknown-type",
recommendation: sprintf(code?.recommendation, ...recommendationArgs),
flowId: flowId || this.flowId,
};
if (nodeId) {
messageObj.nodeId = nodeId;
Expand All @@ -39,26 +44,26 @@ class LintResult {
addError(code, props = {}) {
if (code) {
this.errors.push(
this.getMessageObj(
this.#getMessageObj(
code,
props.messageArgs || [],
props.recommendationArgs || [],
props.nodeId,
props.flowId,
),
);
} else {
this.errors.push(
this.getMessageObj(
this.#getMessageObj(
{
code: "generic-error",
description:
"An unknown error has occurred running the rule. Please report this error to owner of rule-pack.",
message: "Unknown Error: [%]",
type: "error",
code: `ruleId-${this.ruleId}`,
message: `Unknown Error [ruleId:${this.ruleId}]: %`,
type: "internal-error",
recommendation:
"Resolve error, exclude or ignore this rule for the flow.",
flowId: this.flowId,
},
props.messageArgs,
props.messageArgs || [],
[],
),
);
Expand All @@ -70,10 +75,11 @@ class LintResult {

addUnknownCodeError(codeId) {
this.errors.push({
code: "unknown-code",
message: `Unknown Error Code: ${codeId}`,
code: `ruleId-${this.ruleId}-unknown-code`,
message: `Unknown Error Code [ruleId:${this.ruleId}]: ${codeId}`,
type: "internal-error",
recommendation: "Correct rule to include valid code.",
flowId: this.flowId,
});

this.errorCount++;
Expand Down
1 change: 1 addition & 0 deletions lib/LintRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class LintRule {
this.dvUtil.setFlow(mainFlow);
this.mainFlow = mainFlow;
this.allFlows = allFlows;
this.result.setFlowId(mainFlow.flowId);
}

/**
Expand Down
26 changes: 23 additions & 3 deletions lib/LintRulePack.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ class LintRulePack {
errorCount: 0,
lintResults: [],
clean: false,
timing: 0.0,
};

this.cleanFlow = props.cleanFlow || false;
Expand Down Expand Up @@ -393,15 +394,29 @@ class LintRulePack {
this.rules.forEach((rule) => {
// If the rule is not in the list of rules to run, then skip it
if (runRules.includes(rule.id)) {
rule.setClean(this.cleanFlow);
rule.setFlows(mainFlow, allFlows);

try {
// Clear the rule results before running the rule
rule.clear();

// Set the indicator for cleaning the flow
rule.setClean(this.cleanFlow);

// Set the flows for the rule.
// allFlows is an array of all the flows in the bundle, depicting subflows.
rule.setFlows(mainFlow, allFlows);

// Run the rule
const ruleStartTime = performance.now();
rule.runRule();

const response = rule.getResults();

// Add the timing to the response
response.timing = parseFloat(
(performance.now() - ruleStartTime).toFixed(3),
);

// Keep track of the rules that were applied
ruleResponse.rulesApplied.push(rule.id);

if (ignore && ignore.includes(rule.id)) {
Expand Down Expand Up @@ -429,6 +444,11 @@ class LintRulePack {
// Aggregate clean
rulePackResponse.clean = rulePackResponse.clean || response.clean;

// Aggregate timings
rulePackResponse.timing = parseFloat(
(rulePackResponse.timing + response.timing).toFixed(3),
);

if (!response.ruleIgnored) {
rulePackResponse.rulesIgnored = true;
rulePackResponse.errorCount += response.errorCount;
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ping-identity/dvlint",
"version": "1.0.3",
"version": "1.0.4",
"description": "PingOne DaVinci Flow Linter",
"homepage": "https://library.pingidentity.com/page/davinci-linter-cli",
"main": "lib/index.js",
Expand Down
7 changes: 6 additions & 1 deletion test/rules/example-rule/ExampleRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ class ExampleRule extends LintRule {
nodeId: "n1234",
});
}

// Test an undefined error
this.addError(undefined, { messageArgs: ["Testing undefined errors"] });
// Create an unknown code error
this.addError("unknown-code");
} catch (err) {
this.addError("generic-error", { messageArgs: [`${err}`] });
this.addError(undefined, { messageArgs: [`${err}`] });
}
}
}
Expand Down
36 changes: 32 additions & 4 deletions test/rules/example-rule/tests/Example.expect.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
{
"name": "PingOne DaVinci Linter",
"pass": false,
"errorCount": 1,
"errorCount": 3,
"rulePackResults": [
{
"name": "@ping-identity/dvlint",
"pass": false,
"errorCount": 1,
"errorCount": 3,
"lintResults": [
{
"flowId": "2291498df43a63457abad5d2d1fa4df9",
"flowName": "dv-rule-general-001",
"pass": false,
"errorCount": 1,
"errorCount": 3,
"errors": [
{
"code": "example-error",
"message": "Example Rule of flow '2291498df43a63457abad5d2d1fa4df9'",
"nodeId": "n1234",
"recommendation": "We recommend this example.",
"type": "best-practice"
},
{
"code": "ruleId-example-rule",
"message": "Unknown Error [ruleId:example-rule]: Testing undefined errors",
"type": "internal-error",
"recommendation": "Resolve error, exclude or ignore this rule for the flow.",
"flowId": "2291498df43a63457abad5d2d1fa4df9"
},
{
"code": "ruleId-example-rule-unknown-code",
"message": "Unknown Error Code [ruleId:example-rule]: unknown-code",
"type": "internal-error",
"recommendation": "Correct rule to include valid code.",
"flowId": "2291498df43a63457abad5d2d1fa4df9"
}
],
"rulesApplied": [
Expand All @@ -30,14 +44,28 @@
"ruleId": "example-rule",
"ruleDescription": "Example Rule",
"pass": false,
"errorCount": 1,
"errorCount": 3,
"errors": [
{
"code": "example-error",
"message": "Example Rule of flow '2291498df43a63457abad5d2d1fa4df9'",
"nodeId": "n1234",
"recommendation": "We recommend this example.",
"type": "best-practice"
},
{
"code": "ruleId-example-rule",
"message": "Unknown Error [ruleId:example-rule]: Testing undefined errors",
"type": "internal-error",
"recommendation": "Resolve error, exclude or ignore this rule for the flow.",
"flowId": "2291498df43a63457abad5d2d1fa4df9"
},
{
"code": "ruleId-example-rule-unknown-code",
"message": "Unknown Error Code [ruleId:example-rule]: unknown-code",
"type": "internal-error",
"recommendation": "Correct rule to include valid code.",
"flowId": "2291498df43a63457abad5d2d1fa4df9"
}
]
}
Expand Down

0 comments on commit 8e66f21

Please sign in to comment.