Пример #1
0
def is_valid_provider(
        provider: str,
        available_providers: Optional[Set[ExternalProviders]] = None) -> bool:
    try:
        validate_provider(provider, available_providers)
    except ParameterValidationError:
        return False
    return True
Пример #2
0
 def get_additional_attachment(
     self,
     integration: Integration,
     organization: Organization,
 ) -> SlackAttachment | None:
     # look up the generator by the provider but only accepting slack for now
     provider = validate_provider(integration.provider,
                                  [ExternalProviders.SLACK])
     attachment_generator = self.attachment_generators.get(provider)
     if attachment_generator is None:
         return None
     return attachment_generator(integration, organization)
Пример #3
0
def validate(
    data: Mapping[str, Mapping[str, Mapping[int, Mapping[str, str]]]],
    user: Optional[Any] = None,
    team: Optional[Any] = None,
) -> Iterable[Tuple[ExternalProviders, NotificationSettingTypes,
                    NotificationScopeType, int,
                    NotificationSettingOptionValues, ], ]:
    """
    Validate some serialized notification settings. If invalid, raise an
    exception. Otherwise, return them as a list of tuples.
    """

    if not data:
        raise ParameterValidationError("Payload required")

    parent_context = ["notification_settings"]
    context = parent_context
    notification_settings_to_update: Dict[
        Tuple[NotificationSettingTypes, NotificationScopeType, int,
              ExternalProviders, ], NotificationSettingOptionValues, ] = {}
    project_ids_to_look_up: Set[int] = set()
    organization_ids_to_look_up: Set[int] = set()
    for type_key, notifications_by_type in get_valid_items(data, context):
        type = validate_type(type_key, context)
        context = parent_context + [type_key]

        for scope_type_key, notifications_by_scope_type in get_valid_items(
                notifications_by_type, context):
            scope_type = validate_scope_type(scope_type_key, context)
            context = parent_context + [type_key, scope_type_key]
            for scope_id, notifications_by_scope_id in get_valid_items(
                    notifications_by_scope_type, context):
                scope_id = validate_scope(scope_id, scope_type, user, context)

                if scope_type == NotificationScopeType.PROJECT:
                    project_ids_to_look_up.add(scope_id)
                elif scope_type == NotificationScopeType.ORGANIZATION:
                    organization_ids_to_look_up.add(scope_id)

                context = parent_context + [
                    type_key, scope_type_key,
                    str(scope_id)
                ]
                for provider_key, value_key in get_valid_items(
                        notifications_by_scope_id, context):
                    provider = validate_provider(provider_key, context=context)
                    value = validate_value(type, value_key, context)

                    notification_settings_to_update[(type, scope_type,
                                                     scope_id,
                                                     provider)] = value

    validate_projects(project_ids_to_look_up, user=user, team=team)
    validate_organizations(organization_ids_to_look_up, user=user, team=team)

    return {(provider, type, scope_type, scope_id, value)
            for (
                type,
                scope_type,
                scope_id,
                provider,
            ), value in notification_settings_to_update.items()}
Пример #4
0
 def validate_provider(self, provider_name_option: str) -> int:
     provider = validate_provider(provider_name_option,
                                  available_providers=AVAILABLE_PROVIDERS)
     return int(provider.value)
Пример #5
0
 def get_provider_id(self, validated_data: MutableMapping[str, Any]) -> int:
     provider_name_option = validated_data.pop("provider", None)
     provider = validate_provider(provider_name_option, available_providers=AVAILABLE_PROVIDERS)
     return int(provider.value)