Browse Source

ref: Refactor DataScrubbingRelayPiiConfig fixture to be TS (#55669)

<!-- Describe your PR here. -->
Ryan Albrecht 1 year ago
parent
commit
756bda4419

+ 0 - 14
fixtures/js-stubs/dataScrubbingRelayPiiConfig.js

@@ -1,14 +0,0 @@
-export function DataScrubbingRelayPiiConfig() {
-  return {
-    rules: {
-      0: {type: 'password', redaction: {method: 'replace', text: 'Scrubbed'}},
-      1: {type: 'creditcard', redaction: {method: 'mask'}},
-      2: {
-        type: 'pattern',
-        pattern: '[a-zA-Z0-9]+',
-        redaction: {method: 'replace', text: 'Placeholder'},
-      },
-    },
-    applications: {password: ['0'], $message: ['1', '2']},
-  };
-}

+ 27 - 0
fixtures/js-stubs/dataScrubbingRelayPiiConfig.ts

@@ -0,0 +1,27 @@
+import {
+  Applications,
+  MethodType,
+  PiiConfig,
+  RuleType,
+} from 'sentry/views/settings/components/dataScrubbing/types';
+
+export function DataScrubbingRelayPiiConfig(): {
+  applications: Applications;
+  rules: Record<string, PiiConfig>;
+} {
+  return {
+    rules: {
+      0: {
+        type: RuleType.PASSWORD,
+        redaction: {method: MethodType.REPLACE, text: 'Scrubbed'},
+      },
+      1: {type: RuleType.CREDITCARD, redaction: {method: MethodType.MASK}},
+      2: {
+        type: RuleType.PATTERN,
+        pattern: '[a-zA-Z0-9]+',
+        redaction: {method: MethodType.REPLACE, text: 'Placeholder'},
+      },
+    },
+    applications: {password: ['0'], $message: ['1', '2']},
+  };
+}

+ 23 - 28
static/app/views/settings/components/dataScrubbing/convertRelayPiiConfig.tsx

@@ -9,7 +9,7 @@ import {Applications, MethodType, PiiConfig, Rule, RuleDefault, RuleType} from '
 
 export function convertRelayPiiConfig(relayPiiConfig?: string | null): Rule[] {
   const piiConfig = relayPiiConfig ? JSON.parse(relayPiiConfig) : {};
-  const rules: PiiConfig = piiConfig.rules || {};
+  const rules: Record<string, PiiConfig> = piiConfig.rules || {};
   const applications: Applications = piiConfig.applications || {};
   const convertedRules: Array<Rule> = [];
 
@@ -46,32 +46,28 @@ export function convertRelayPiiConfig(relayPiiConfig?: string | null): Rule[] {
       }
 
       const {type, redaction} = resolvedRule;
-      const method = redaction.method as MethodType;
-
-      if (method === MethodType.REPLACE && resolvedRule.type === RuleType.PATTERN) {
-        convertedRules.push({
-          id,
-          method: MethodType.REPLACE,
-          type: RuleType.PATTERN,
-          source,
-          placeholder: redaction?.text,
-          pattern: resolvedRule.pattern,
-        });
-        continue;
-      }
+      const method = redaction.method;
 
       if (method === MethodType.REPLACE) {
-        convertedRules.push({
-          id,
-          method: MethodType.REPLACE,
-          type,
-          source,
-          placeholder: redaction?.text,
-        });
-        continue;
-      }
-
-      if (resolvedRule.type === RuleType.PATTERN) {
+        if (type === RuleType.PATTERN) {
+          convertedRules.push({
+            id,
+            method: MethodType.REPLACE,
+            type: RuleType.PATTERN,
+            source,
+            placeholder: redaction?.text,
+            pattern: resolvedRule.pattern,
+          });
+        } else {
+          convertedRules.push({
+            id,
+            method: MethodType.REPLACE,
+            type,
+            source,
+            placeholder: redaction?.text,
+          });
+        }
+      } else if (type === RuleType.PATTERN) {
         convertedRules.push({
           id,
           method,
@@ -79,10 +75,9 @@ export function convertRelayPiiConfig(relayPiiConfig?: string | null): Rule[] {
           source,
           pattern: resolvedRule.pattern,
         });
-        continue;
+      } else {
+        convertedRules.push({id, method, type, source});
       }
-
-      convertedRules.push({id, method, type, source});
     }
   }
 

+ 1 - 2
static/app/views/settings/components/dataScrubbing/modals/add.spec.tsx

@@ -17,8 +17,7 @@ import {
 const relayPiiConfig = TestStubs.DataScrubbingRelayPiiConfig();
 const stringRelayPiiConfig = JSON.stringify(relayPiiConfig);
 const organizationSlug = 'sentry';
-const convertedRules = convertRelayPiiConfig(stringRelayPiiConfig);
-const rules = convertedRules;
+const rules = convertRelayPiiConfig(stringRelayPiiConfig);
 const successfullySaved = jest.fn();
 const projectId = 'foo';
 const endpoint = `/projects/${organizationSlug}/${projectId}/`;