Esempio n. 1
0
    def serialize(
        self,
        obj: Union[User, Team],
        attrs: Mapping[str, Iterable[Any]],
        user: User,
        **kwargs: Any,
    ) -> Mapping[str, Mapping[str, Mapping[int, Mapping[str, str]]]]:
        """
        Convert a user or team's NotificationSettings to a python object
        comprised of primitives. This will backfill all possible notification
        settings with the appropriate defaults.

        Example: {
            "workflow": {
                "project": {
                    1: {
                        "email": "always",
                        "slack": "always"
                    },
                    2: {
                        "email": "subscribe_only",
                        "slack": "subscribe_only"
                    }
                }
            }
        }

        :param obj: A user or team.
        :param attrs: The `obj` target's NotificationSettings
        :param user: The user who will be viewing the NotificationSettings.
        :param kwargs: The same `kwargs` as `get_attrs`.
        :returns A mapping. See example.
        """
        type_option: Optional[NotificationSettingTypes] = kwargs.get("type")
        types_to_serialize = {type_option} if type_option else set(
            VALID_VALUES_FOR_KEY.keys())
        user = obj if type(obj) == User else None

        project_ids = {_.id for _ in attrs["projects"]}
        organization_ids = {_.id for _ in attrs["organizations"]}

        data = get_fallback_settings(types_to_serialize, project_ids,
                                     organization_ids, user)

        # Forgive the variable name, I wanted the following lines to be legible.
        for n in attrs["settings"]:
            # Filter out invalid notification settings.
            if (n.scope_str == "project"
                    and n.scope_identifier not in project_ids) or (
                        n.scope_str == "organization"
                        and n.scope_identifier not in organization_ids):
                continue

            # Override the notification settings.
            data[n.type_str][n.scope_str][n.scope_identifier][
                n.provider_str] = n.value_str

        return data
Esempio n. 2
0
 def test_get_fallback_settings_projects(self):
     data = get_fallback_settings({NotificationSettingTypes.ISSUE_ALERTS}, {self.project.id}, {})
     assert data == {
         "alerts": {
             "project": {
                 self.project.id: {
                     "email": "default",
                     "slack": "default",
                 }
             }
         }
     }
Esempio n. 3
0
 def test_get_fallback_settings_user(self):
     data = get_fallback_settings({NotificationSettingTypes.ISSUE_ALERTS}, {}, {}, self.user)
     assert data == {
         "alerts": {
             "user": {
                 self.user.id: {
                     "email": "always",
                     "slack": "never",
                 }
             }
         }
     }
 def test_get_fallback_settings_feature_flag(self):
     data = get_fallback_settings(
         {NotificationSettingTypes.DEPLOY},
         {},
         {},
         self.user,
         should_use_slack_automatic=True,
     )
     assert data == {
         "deploy": {
             "user": {
                 self.user.id: {
                     "email": "committed_only",
                     "slack": "committed_only",
                 }
             }
         }
     }
Esempio n. 5
0
 def test_get_fallback_settings_minimal(self):
     assert get_fallback_settings({NotificationSettingTypes.ISSUE_ALERTS},
                                  {}, {}) == {}