submitRules.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import {Client} from 'sentry/api';
  2. import {Applications, MethodType, PiiConfig, Rule, RuleType} from './types';
  3. function getSubmitFormatRule(rule: Rule): PiiConfig {
  4. if (rule.type === RuleType.PATTERN && rule.method === MethodType.REPLACE) {
  5. return {
  6. type: rule.type,
  7. pattern: rule.pattern,
  8. redaction: {
  9. method: rule.method,
  10. text: rule?.placeholder,
  11. },
  12. };
  13. }
  14. if (rule.type === RuleType.PATTERN) {
  15. return {
  16. type: rule.type,
  17. pattern: rule.pattern,
  18. redaction: {
  19. method: rule.method,
  20. },
  21. };
  22. }
  23. if (rule.method === MethodType.REPLACE) {
  24. return {
  25. type: rule.type,
  26. redaction: {
  27. method: rule.method,
  28. text: rule?.placeholder,
  29. },
  30. };
  31. }
  32. return {
  33. type: rule.type,
  34. redaction: {
  35. method: rule.method,
  36. },
  37. };
  38. }
  39. function submitRules(api: Client, endpoint: string, rules: Array<Rule>) {
  40. const applications: Applications = {};
  41. const submitFormatRules: Record<string, PiiConfig> = {};
  42. for (let i = 0; i < rules.length; i++) {
  43. const rule = rules[i];
  44. const ruleId = String(i);
  45. submitFormatRules[ruleId] = getSubmitFormatRule(rule);
  46. if (!applications[rule.source]) {
  47. applications[rule.source] = [];
  48. }
  49. if (!applications[rule.source].includes(ruleId)) {
  50. applications[rule.source].push(ruleId);
  51. }
  52. }
  53. const piiConfig = {rules: submitFormatRules, applications};
  54. return api.requestPromise(endpoint, {
  55. method: 'PUT',
  56. data: {relayPiiConfig: JSON.stringify(piiConfig)},
  57. });
  58. }
  59. export default submitRules;