submitRules.tsx 1.6 KB

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