def get(self, request, organization): """ Fetches actions that an alert rule can perform for an organization """ if not features.has( "organizations:incidents", organization, actor=request.user): raise ResourceDoesNotExist actions = [] integrations = get_available_action_integrations_for_org( organization).order_by("id") provider_integrations = defaultdict(list) for integration in integrations: provider_integrations[integration.provider].append(integration) registered_types = AlertRuleTriggerAction.get_registered_types() registered_types.sort(key=lambda x: x.slug) for registered_type in AlertRuleTriggerAction.get_registered_types(): if registered_type.integration_provider: for integration in provider_integrations[ registered_type.integration_provider]: actions.append( self.build_action_response(registered_type, integration)) else: actions.append(self.build_action_response(registered_type)) return Response(actions, status=status.HTTP_200_OK)
def get(self, request, organization): """ Fetches actions that an alert rule can perform for an organization """ if not features.has("organizations:incidents", organization, actor=request.user): raise ResourceDoesNotExist actions = [] # Cache Integration objects in this data structure to save DB calls. provider_integrations = defaultdict(list) for integration in get_available_action_integrations_for_org(organization): provider_integrations[integration.provider].append(integration) for registered_type in AlertRuleTriggerAction.get_registered_types(): # Used cached integrations for each `registered_type` instead of making N calls. if registered_type.integration_provider: actions += [ build_action_response( registered_type, integration=integration, organization=organization ) for integration in provider_integrations[registered_type.integration_provider] ] # Add all alertable SentryApps to the list. elif registered_type.type == AlertRuleTriggerAction.Type.SENTRY_APP: actions += [ build_action_response(registered_type, sentry_app=app) for app in get_alertable_sentry_apps(organization.id, with_metric_alerts=True) ] else: actions.append(build_action_response(registered_type)) return Response(actions, status=status.HTTP_200_OK)
def get_available_action_integrations_for_org(organization): """ Returns a list of integrations that the organization has installed. Integrations are filtered by the list of registered providers. :param organization: """ providers = [ registration.integration_provider for registration in AlertRuleTriggerAction.get_registered_types() if registration.integration_provider is not None ] return Integration.objects.filter(organizations=organization, provider__in=providers)
AlertRuleTriggerAction, ) from sentry.models.organizationmember import OrganizationMember from sentry.models.team import Team from sentry.models.user import User from sentry.snuba.dataset import Dataset from sentry.snuba.models import QueryDatasets from sentry.snuba.tasks import build_snuba_filter from sentry.utils.snuba import raw_query from sentry.utils.compat import zip logger = logging.getLogger(__name__) string_to_action_type = { registration.slug: registration.type for registration in AlertRuleTriggerAction.get_registered_types() } action_target_type_to_string = { AlertRuleTriggerAction.TargetType.USER: "******", AlertRuleTriggerAction.TargetType.TEAM: "team", AlertRuleTriggerAction.TargetType.SPECIFIC: "specific", } string_to_action_target_type = { v: k for (k, v) in action_target_type_to_string.items() } CRITICAL_TRIGGER_LABEL = "critical" WARNING_TRIGGER_LABEL = "warning"