Skip to content

Commit

Permalink
More descriptive parameter name
Browse files Browse the repository at this point in the history
This boolean indicates whether the rule in question is identified as a
prebuilt rule, more specifically than just the rule_id existing.
  • Loading branch information
rylnd committed Oct 9, 2024
1 parent f17fb84 commit b236c84
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('calculateRuleSourceForImport', () => {
const result = calculateRuleSourceForImport({
rule: getRulesSchemaMock(),
prebuiltRuleAssetsByRuleId: {},
ruleIdExists: false,
isKnownPrebuiltRule: false,
});

expect(result).toEqual({
Expand All @@ -32,7 +32,7 @@ describe('calculateRuleSourceForImport', () => {
const result = calculateRuleSourceForImport({
rule,
prebuiltRuleAssetsByRuleId: {},
ruleIdExists: true,
isKnownPrebuiltRule: true,
});

expect(result).toEqual({
Expand All @@ -52,7 +52,7 @@ describe('calculateRuleSourceForImport', () => {
const result = calculateRuleSourceForImport({
rule,
prebuiltRuleAssetsByRuleId,
ruleIdExists: true,
isKnownPrebuiltRule: true,
});

expect(result).toEqual({
Expand All @@ -72,7 +72,7 @@ describe('calculateRuleSourceForImport', () => {
const result = calculateRuleSourceForImport({
rule,
prebuiltRuleAssetsByRuleId,
ruleIdExists: true,
isKnownPrebuiltRule: true,
});

expect(result).toEqual({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ import { calculateRuleSourceFromAsset } from './calculate_rule_source_from_asset
* @param rule The rule to be imported
* @param prebuiltRuleAssets A list of prebuilt rule assets, which may include
* the installed version of the specified prebuilt rule.
* @param ruleIdExists {boolean} Whether the rule's rule_id is available as a
* @param isKnownPrebuiltRule {boolean} Whether the rule's rule_id is available as a
* prebuilt asset (independent of the specified version).
*
* @returns The calculated rule_source and immutable fields for the rule
*/
export const calculateRuleSourceForImport = ({
rule,
prebuiltRuleAssetsByRuleId,
ruleIdExists,
isKnownPrebuiltRule,
}: {
rule: ValidatedRuleToImport;
prebuiltRuleAssetsByRuleId: Record<string, PrebuiltRuleAsset>;
ruleIdExists: boolean;
isKnownPrebuiltRule: boolean;
}): { ruleSource: RuleSource; immutable: boolean } => {
const assetWithMatchingVersion = prebuiltRuleAssetsByRuleId[rule.rule_id];
// We convert here so that RuleSource calculation can
Expand All @@ -42,7 +42,7 @@ export const calculateRuleSourceForImport = ({
const ruleSource = calculateRuleSourceFromAsset({
rule: ruleResponseForImport,
assetWithMatchingVersion,
ruleIdExists,
isKnownPrebuiltRule,
});

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('calculateRuleSourceFromAsset', () => {
const result = calculateRuleSourceFromAsset({
rule: getRulesSchemaMock(),
assetWithMatchingVersion: undefined,
ruleIdExists: false,
isKnownPrebuiltRule: false,
});

expect(result).toEqual({
Expand All @@ -27,7 +27,7 @@ describe('calculateRuleSourceFromAsset', () => {
const result = calculateRuleSourceFromAsset({
rule: ruleToImport,
assetWithMatchingVersion: undefined,
ruleIdExists: true,
isKnownPrebuiltRule: true,
});

expect(result).toEqual({
Expand All @@ -46,7 +46,7 @@ describe('calculateRuleSourceFromAsset', () => {
version: 1, // version 1 (same version as imported rule)
// no other overwrites -> no differences
}),
ruleIdExists: true,
isKnownPrebuiltRule: true,
});

expect(result).toEqual({
Expand All @@ -64,7 +64,7 @@ describe('calculateRuleSourceFromAsset', () => {
version: 1, // version 1 (same version as imported rule)
name: 'Customized name', // mock a customization
}),
ruleIdExists: true,
isKnownPrebuiltRule: true,
});

expect(result).toEqual({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ import { calculateIsCustomized } from '../detection_rules_client/mergers/rule_so
*
* @param rule The rule for which rule_source is being calculated
* @param assetWithMatchingVersion The prebuilt rule asset that matches the specified rule_id and version
* @param ruleIdExists Whether a prebuilt rule with the specified rule_id is currently installed
* @param isKnownPrebuiltRule Whether a prebuilt rule with the specified rule_id is currently installed
*
* @returns The calculated rule_source
*/
export const calculateRuleSourceFromAsset = ({
rule,
assetWithMatchingVersion,
ruleIdExists,
isKnownPrebuiltRule,
}: {
rule: RuleResponse;
assetWithMatchingVersion: PrebuiltRuleAsset | undefined;
ruleIdExists: boolean;
isKnownPrebuiltRule: boolean;
}): RuleSource => {
if (!ruleIdExists) {
if (!isKnownPrebuiltRule) {
return {
type: 'internal',
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('ruleSourceImporter', () => {
expect(calculatorSpy).toHaveBeenCalledWith({
rule,
prebuiltRuleAssetsByRuleId: { 'rule-1': expect.objectContaining({ rule_id: 'rule-1' }) },
ruleIdExists: true,
isKnownPrebuiltRule: true,
});
});

Expand Down Expand Up @@ -167,7 +167,7 @@ describe('ruleSourceImporter', () => {
expect(calculatorSpy).toHaveBeenCalledWith({
rule,
prebuiltRuleAssetsByRuleId: { 'rule-1': expect.objectContaining({ rule_id: 'rule-1' }) },
ruleIdExists: true,
isKnownPrebuiltRule: true,
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class RuleSourceImporter implements IRuleSourceImporter {
return calculateRuleSourceForImport({
rule,
prebuiltRuleAssetsByRuleId: this.matchingAssetsByRuleId,
ruleIdExists: this.availableRuleAssetIds.has(rule.rule_id),
isKnownPrebuiltRule: this.availableRuleAssetIds.has(rule.rule_id),
});
}

Expand Down

0 comments on commit b236c84

Please sign in to comment.