Name
x_snc_codesanity.GetExecutionRulesForCheckByRules
Description
Returns the rules that are applicable to the given check based on the given execution rules.
Script
function GetExecutionRulesForCheckByRules(grCheck,rules)
{
var IsValidRecord = x_snc_devtools.IsValidRecord;
var SetValue = x_snc_devtools.SetValue;
var GetValue = x_snc_devtools.GetValue;
var GetArrayValue = x_snc_devtools.GetArrayValue;
var ArrayPushUnique = x_snc_devtools.ArrayPushUnique;
var GetStringValue = x_snc_devtools.GetStringValue;
var ArrayRemoveValue = x_snc_devtools.ArrayRemoveValue;
var GetKeys = x_snc_devtools.GetKeys;
var IsEqual = x_snc_devtools.IsEqual;
var GetBoolValue = x_snc_devtools.GetBoolValue;
if (IsValidRecord(grCheck) == false)
{
return {};
}
var rulesForCheck = {};
// BLACKLIST - prevent specific checks from running
var aBlacklistRules = GetArrayValue(GetValue(rules,'blacklist'));
for (var nRule = 0; nRule < aBlacklistRules.length; nRule++)
{
var rule = aBlacklistRules[nRule];
var aChecks = GetArrayValue(GetValue(rule,'checks'));
var bAppliesToCheck = false;
if (aChecks.length == 0) // no checks are specified in the rule: the rule applies to all checks
{
bAppliesToCheck = true;
}
else
{
for (var nCheck = 0; nCheck < aChecks.length; nCheck++)
{
var strCheck = aChecks[nCheck];
if (strCheck == grCheck.getValue('name') || strCheck == GetStringValue(grCheck.sys_id))
{
bAppliesToCheck = true;
}
}
}
if (bAppliesToCheck == true)
{
var bAll = GetBoolValue(GetValue(rule,'all'));
if (bAll == true)
{
SetValue(true,rulesForCheck,'blacklist','all');
}
else
{
var aScopes = GetArrayValue(GetValue(rule,'scopes'));
for (var nScope = 0; nScope < aScopes.length; nScope++)
{
var aScopesInRule = GetArrayValue(GetValue(rulesForCheck,'blacklist','scopes'));
aScopesInRule = ArrayPushUnique(aScopesInRule,aScopes[nScope]);
SetValue(aScopesInRule,rulesForCheck,'blacklist','scopes');
}
}
}
}
// WHITELIST - only run specific checks if conditions match
var aWhitelistRules = GetArrayValue(GetValue(rules,'whitelist'));
for (var nRule = 0; nRule < aWhitelistRules.length; nRule++)
{
var rule = aWhitelistRules[nRule];
var aChecks = GetArrayValue(GetValue(rule,'checks'));
var bAppliesToCheck = false;
for (var nCheck = 0; nCheck < aChecks.length; nCheck++)
{
var strCheck = aChecks[nCheck];
if (strCheck == grCheck.getValue('name') || strCheck == GetStringValue(grCheck.sys_id))
{
bAppliesToCheck = true;
}
}
if (bAppliesToCheck == true)
{
var aScopes = GetArrayValue(GetValue(rule,'scopes'));
if (aScopes.length == 0)
{
var aScopesInRule = GetValue(rulesForCheck,'blacklist','scopes');
if (aScopesInRule == null)
{
SetValue([],rulesForCheck,'whitelist','scopes');
}
}
else
{
for (var nScope = 0; nScope < aScopes.length; nScope++)
{
var aScopesInRule = GetArrayValue(GetValue(rulesForCheck,'whitelist','scopes'));
aScopesInRule = ArrayPushUnique(aScopesInRule,aScopes[nScope]);
SetValue(aScopesInRule,rulesForCheck,'whitelist','scopes');
}
}
}
}
// Cleanup blacklist based on contradicting whitelist
var aBlacklistScopes = GetValue(rulesForCheck,'blacklist','scopes');
var aWhitelistScopes = GetValue(rulesForCheck,'whitelist','scopes');
if (aBlacklistScopes != null && aWhitelistScopes != null)
{
if (aBlacklistScopes.length > 0 && aWhitelistScopes.length > 0)
{
for (var nWhitelistScope = 0; nWhitelistScope < aWhitelistScopes.length; nWhitelistScope++)
{
aBlacklistScopes = ArrayRemoveValue(aBlacklistScopes,aWhitelistScopes[nWhitelistScope]);
}
if (aBlacklistScopes.length == 0)
{
delete rulesForCheck.blacklist.scopes;
if (IsEqual(GetKeys(rulesForCheck.blacklist),[]) == true)
{
delete rulesForCheck.blacklist;
}
//SetValue(aScopesInRule,rulesForCheck,'blacklist',null);
}
else
{
SetValue(aBlacklistScopes,rulesForCheck,'blacklist','scopes');
}
}
}
return rulesForCheck;
}
Sys ID
205d9894db999150dcbdd03cd39619a9