utils.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import IdBadge from 'sentry/components/idBadge';
  2. import {t} from 'sentry/locale';
  3. import {Organization, Project} from 'sentry/types';
  4. import {IssueAlertRule, RuleActionsCategories} from 'sentry/types/alerts';
  5. import {isActiveSuperuser} from 'sentry/utils/isActiveSuperuser';
  6. import {MetricRule} from 'sentry/views/alerts/rules/metric/types';
  7. export function getProjectOptions({
  8. organization,
  9. projects,
  10. isFormDisabled,
  11. }: {
  12. isFormDisabled: boolean;
  13. organization: Organization;
  14. projects: Project[];
  15. }) {
  16. const hasOrgAlertWrite = organization.access.includes('alerts:write');
  17. const hasOrgWrite = organization.access.includes('org:write');
  18. const hasOpenMembership = organization.features.includes('open-membership');
  19. // If form is enabled, we want to limit to the subset of projects which the
  20. // user can create/edit alerts.
  21. const projectWithWrite =
  22. isFormDisabled || hasOrgAlertWrite
  23. ? projects
  24. : projects.filter(project => project.access.includes('alerts:write'));
  25. const myProjects = projectWithWrite.filter(project => project.isMember);
  26. const allProjects = projectWithWrite.filter(project => !project.isMember);
  27. const myProjectOptions = myProjects.map(myProject => ({
  28. value: myProject.id,
  29. label: myProject.slug,
  30. leadingItems: renderIdBadge(myProject),
  31. }));
  32. const openMembershipProjects = [
  33. {
  34. label: t('My Projects'),
  35. options: myProjectOptions,
  36. },
  37. {
  38. label: t('All Projects'),
  39. options: allProjects.map(allProject => ({
  40. value: allProject.id,
  41. label: allProject.slug,
  42. leadingItems: renderIdBadge(allProject),
  43. })),
  44. },
  45. ];
  46. return hasOpenMembership || hasOrgWrite || isActiveSuperuser()
  47. ? openMembershipProjects
  48. : myProjectOptions;
  49. }
  50. function renderIdBadge(project: Project) {
  51. return (
  52. <IdBadge
  53. project={project}
  54. avatarProps={{consistentWidth: true}}
  55. avatarSize={18}
  56. disableLink
  57. hideName
  58. />
  59. );
  60. }
  61. export function getRuleActionCategory(rule: IssueAlertRule) {
  62. const numDefaultActions = rule.actions.filter(
  63. action => action.id === 'sentry.mail.actions.NotifyEmailAction'
  64. ).length;
  65. switch (numDefaultActions) {
  66. // Are all actions default actions?
  67. case rule.actions.length:
  68. return RuleActionsCategories.ALL_DEFAULT;
  69. // Are none of the actions default actions?
  70. case 0:
  71. return RuleActionsCategories.NO_DEFAULT;
  72. default:
  73. return RuleActionsCategories.SOME_DEFAULT;
  74. }
  75. }
  76. export function getAlertRuleActionCategory(rule: MetricRule) {
  77. const actions = rule.triggers.map(trigger => trigger.actions).flat();
  78. const numDefaultActions = actions.filter(action => action.type === 'email').length;
  79. switch (numDefaultActions) {
  80. // Are all actions default actions?
  81. case actions.length:
  82. return RuleActionsCategories.ALL_DEFAULT;
  83. // Are none of the actions default actions?
  84. case 0:
  85. return RuleActionsCategories.NO_DEFAULT;
  86. default:
  87. return RuleActionsCategories.SOME_DEFAULT;
  88. }
  89. }