예제 #1
0
def build_action_response(registered_type, integration=None, organization=None, sentry_app=None):
    """
    Build the "available action" objects for the API. Each one can have different fields.

    :param registered_type: One of the registered AlertRuleTriggerAction types.
    :param integration: Optional. The Integration if this action uses a one.
    :param organization: Optional. If this is a PagerDuty action, we need the organization to look up services.
    :param sentry_app: Optional. The SentryApp if this action uses a one.
    :return: The available action object.
    """

    action_response = {
        "type": registered_type.slug,
        "allowedTargetTypes": [
            action_target_type_to_string.get(target_type)
            for target_type in registered_type.supported_target_types
        ],
    }

    if integration:
        action_response["integrationName"] = integration.name
        action_response["integrationId"] = integration.id

        if registered_type.type == AlertRuleTriggerAction.Type.PAGERDUTY:
            action_response["options"] = [
                {"value": service["id"], "label": service["service_name"]}
                for service in get_pagerduty_services(organization, integration.id)
            ]

    elif sentry_app:
        action_response["sentryAppName"] = sentry_app.name
        action_response["sentryAppId"] = sentry_app.id
        action_response["status"] = SentryAppStatus.as_str(sentry_app.status)

    return action_response
예제 #2
0
    def test_creates_internal_integration(self):
        self.create_project(organization=self.org)
        self.login_as(self.user)

        response = self._post(isInternal=True)

        assert response.data['slug'] == 'myapp'
        assert response.data['status'] == SentryAppStatus.as_str(
            SentryAppStatus.INTERNAL)
예제 #3
0
    def test_creates_internal_integration(self):
        self.create_project(organization=self.org)
        self.login_as(self.user)

        response = self._post(isInternal=True)

        assert re.match(r'myapp\-[0-9a-zA-Z]+', response.data['slug'])
        assert response.data['status'] == SentryAppStatus.as_str(
            SentryAppStatus.INTERNAL)
        assert not response.data['verifyInstall']
예제 #4
0
def build_action_response(registered_type,
                          integration=None,
                          organization=None,
                          sentry_app_installation=None):
    """
    Build the "available action" objects for the API. Each one can have different fields.

    :param registered_type: One of the registered AlertRuleTriggerAction types.
    :param integration: Optional. The Integration if this action uses a one.
    :param organization: Optional. If this is a PagerDuty action, we need the organization to look up services.
    :param sentry_app: Optional. The SentryApp if this action uses a one.
    :return: The available action object.
    """

    action_response = {
        "type":
        registered_type.slug,
        "allowedTargetTypes": [
            ACTION_TARGET_TYPE_TO_STRING.get(target_type)
            for target_type in registered_type.supported_target_types
        ],
    }

    if integration:
        action_response["integrationName"] = integration.name
        action_response["integrationId"] = integration.id

        if registered_type.type == AlertRuleTriggerAction.Type.PAGERDUTY:
            action_response["options"] = [{
                "value": service["id"],
                "label": service["service_name"]
            } for service in get_pagerduty_services(organization,
                                                    integration.id)]

    elif sentry_app_installation:
        action_response[
            "sentryAppName"] = sentry_app_installation.sentry_app.name
        action_response["sentryAppId"] = sentry_app_installation.sentry_app_id
        action_response[
            "sentryAppInstallationUuid"] = sentry_app_installation.uuid
        action_response["status"] = SentryAppStatus.as_str(
            sentry_app_installation.sentry_app.status)

        # Sentry Apps can be alertable but not have an Alert Rule UI Component
        component = sentry_app_installation.prepare_sentry_app_components(
            "alert-rule-action")
        if component:
            action_response["settings"] = component.schema.get("settings", {})

    return action_response
예제 #5
0
    def test_creates_internal_integration(self):
        self.create_project(organization=self.org)
        self.login_as(self.user)

        response = self._post(isInternal=True)

        assert re.match(r'myapp\-[0-9a-zA-Z]+', response.data['slug'])
        assert response.data['status'] == SentryAppStatus.as_str(SentryAppStatus.INTERNAL)
        assert not response.data['verifyInstall']

        # verify tokens are created properly
        sentry_app = SentryApp.objects.get(slug=response.data['slug'])
        sentry_app_installation = SentryAppInstallation.objects.get(sentry_app=sentry_app)

        sentry_app_installation_token = SentryAppInstallationToken.objects.get(
            sentry_app_installation=sentry_app_installation)

        # Below line will fail once we stop assigning api_token on the sentry_app_installation
        assert sentry_app_installation_token.api_token == sentry_app_installation.api_token
예제 #6
0
    def test_creates_internal_integration(self):
        self.create_project(organization=self.organization)

        response = self.get_success_response(**self.get_data(isInternal=True))
        assert re.match(r"myapp\-[0-9a-zA-Z]+", response.data["slug"])
        assert response.data["status"] == SentryAppStatus.as_str(
            SentryAppStatus.INTERNAL)
        assert not response.data["verifyInstall"]

        # Verify tokens are created properly.
        sentry_app = SentryApp.objects.get(slug=response.data["slug"])
        sentry_app_installation = SentryAppInstallation.objects.get(
            sentry_app=sentry_app)

        sentry_app_installation_token = SentryAppInstallationToken.objects.get(
            sentry_app_installation=sentry_app_installation)

        # Below line will fail once we stop assigning api_token on the sentry_app_installation.
        assert sentry_app_installation_token.api_token == sentry_app_installation.api_token