Skip to content

Commit

Permalink
Removes version logic from patch
Browse files Browse the repository at this point in the history
  • Loading branch information
spong committed Mar 22, 2023
1 parent 31257f2 commit d48bec2
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -390,27 +390,6 @@ export const patchTypeSpecificSnakeToCamel = (
}
};

const versionExcludedKeys = ['enabled', 'id', 'rule_id'];
const incrementVersion = (nextParams: PatchRuleRequestBody, existingRule: RuleParams) => {
// The the version from nextParams if it's provided
if (nextParams.version) {
return nextParams.version;
}

// If the rule is immutable, keep the current version
if (existingRule.immutable) {
return existingRule.version;
}

// For custom rules, check modified params to deicide whether version increment is needed
for (const key in nextParams) {
if (!versionExcludedKeys.includes(key)) {
return existingRule.version + 1;
}
}
return existingRule.version;
};

// eslint-disable-next-line complexity
export const convertPatchAPIToInternalSchema = (
nextParams: PatchRuleRequestBody & {
Expand Down Expand Up @@ -456,9 +435,7 @@ export const convertPatchAPIToInternalSchema = (
references: nextParams.references ?? existingParams.references,
namespace: nextParams.namespace ?? existingParams.namespace,
note: nextParams.note ?? existingParams.note,
// Always use the version from the request if specified. If it isn't specified, leave immutable rules alone and
// increment the version of mutable rules by 1.
version: incrementVersion(nextParams, existingParams),
version: nextParams.version ?? existingParams.version,
exceptionsList: nextParams.exceptions_list ?? existingParams.exceptionsList,
...typeSpecificParams,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export default ({ getService }: FtrProviderContext) => {

const outputRule = getSimpleRuleOutput();
outputRule.name = 'some other name';
outputRule.version = 2;
outputRule.revision = 1;
const bodyToCompare = removeServerGeneratedProperties(body);
expect(bodyToCompare).to.eql(outputRule);
Expand Down Expand Up @@ -86,7 +85,6 @@ export default ({ getService }: FtrProviderContext) => {

const outputRule = getSimpleMlRuleOutput();
outputRule.name = 'some other name';
outputRule.version = 2;
outputRule.revision = 1;
const bodyToCompare = removeServerGeneratedProperties(body);
expect(bodyToCompare).to.eql(outputRule);
Expand All @@ -106,7 +104,6 @@ export default ({ getService }: FtrProviderContext) => {

const outputRule = getSimpleRuleOutputWithoutRuleId();
outputRule.name = 'some other name';
outputRule.version = 2;
outputRule.revision = 1;
const bodyToCompare = removeServerGeneratedPropertiesIncludingRuleId(body);
expect(bodyToCompare).to.eql(outputRule);
Expand All @@ -124,13 +121,12 @@ export default ({ getService }: FtrProviderContext) => {

const outputRule = getSimpleRuleOutput();
outputRule.name = 'some other name';
outputRule.version = 2;
outputRule.revision = 1;
const bodyToCompare = removeServerGeneratedProperties(body);
expect(bodyToCompare).to.eql(outputRule);
});

it('should not change the version of a rule when it patches only enabled', async () => {
it('should not change the revision of a rule when it patches only enabled', async () => {
await createRule(supertest, log, getSimpleRule('rule-1'));

// patch a simple rule's enabled to false
Expand All @@ -147,7 +143,7 @@ export default ({ getService }: FtrProviderContext) => {
expect(bodyToCompare).to.eql(outputRule);
});

it('should change the version of a rule when it patches enabled and another property', async () => {
it('should change the revision of a rule when it patches enabled and another property', async () => {
await createRule(supertest, log, getSimpleRule('rule-1'));

// patch a simple rule's enabled to false and another property
Expand All @@ -160,7 +156,6 @@ export default ({ getService }: FtrProviderContext) => {
const outputRule = getSimpleRuleOutput();
outputRule.enabled = false;
outputRule.severity = 'low';
outputRule.version = 2;
outputRule.revision = 1;

const bodyToCompare = removeServerGeneratedProperties(body);
Expand Down Expand Up @@ -188,7 +183,6 @@ export default ({ getService }: FtrProviderContext) => {
outputRule.name = 'some other name';
outputRule.timeline_title = 'some title';
outputRule.timeline_id = 'some id';
outputRule.version = 3;
outputRule.revision = 2;

const bodyToCompare = removeServerGeneratedProperties(body);
Expand Down

0 comments on commit d48bec2

Please sign in to comment.