Beispiel #1
0
 def test_registered(self):
     integration = Integration.objects.create(external_id="1",
                                              provider="slack")
     integration.add_organization(self.organization)
     assert list(
         get_available_action_integrations_for_org(
             self.organization)) == [integration]
    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)
Beispiel #3
0
    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)
Beispiel #4
0
 def test_none(self):
     assert list(
         get_available_action_integrations_for_org(
             self.organization)) == []