Skip to content

Commit

Permalink
Merge pull request #1866 from python-gitlab/jlvillal/arrays_2
Browse files Browse the repository at this point in the history
chore: create new ArrayAttribute class
  • Loading branch information
nejch committed Jan 31, 2022
2 parents 7a13b9b + a57334f commit 7646360
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 49 deletions.
15 changes: 14 additions & 1 deletion gitlab/types.py
Expand Up @@ -32,7 +32,9 @@ def get_for_api(self) -> Any:
return self._value


class CommaSeparatedListAttribute(GitlabAttribute):
class _ListArrayAttribute(GitlabAttribute):
"""Helper class to support `list` / `array` types."""

def set_from_cli(self, cli_value: str) -> None:
if not cli_value.strip():
self._value = []
Expand All @@ -49,6 +51,17 @@ def get_for_api(self) -> str:
return ",".join([str(x) for x in self._value])


class ArrayAttribute(_ListArrayAttribute):
"""To support `array` types as documented in
https://docs.gitlab.com/ee/api/#array"""


class CommaSeparatedListAttribute(_ListArrayAttribute):
"""For values which are sent to the server as a Comma Separated Values
(CSV) string. We allow them to be specified as a list and we convert it
into a CSV"""


class LowercaseStringAttribute(GitlabAttribute):
def get_for_api(self) -> str:
return str(self._value).lower()
Expand Down
7 changes: 2 additions & 5 deletions gitlab/v4/objects/groups.py
Expand Up @@ -314,10 +314,7 @@ class GroupManager(CRUDMixin, RESTManager):
"shared_runners_setting",
),
)
_types = {
"avatar": types.ImageAttribute,
"skip_groups": types.CommaSeparatedListAttribute,
}
_types = {"avatar": types.ImageAttribute, "skip_groups": types.ArrayAttribute}

def get(self, id: Union[str, int], lazy: bool = False, **kwargs: Any) -> Group:
return cast(Group, super().get(id=id, lazy=lazy, **kwargs))
Expand Down Expand Up @@ -377,7 +374,7 @@ class GroupSubgroupManager(ListMixin, RESTManager):
"with_custom_attributes",
"min_access_level",
)
_types = {"skip_groups": types.CommaSeparatedListAttribute}
_types = {"skip_groups": types.ArrayAttribute}


class GroupDescendantGroup(RESTObject):
Expand Down
15 changes: 3 additions & 12 deletions gitlab/v4/objects/issues.py
Expand Up @@ -65,10 +65,7 @@ class IssueManager(RetrieveMixin, RESTManager):
"updated_after",
"updated_before",
)
_types = {
"iids": types.CommaSeparatedListAttribute,
"labels": types.CommaSeparatedListAttribute,
}
_types = {"iids": types.ArrayAttribute, "labels": types.CommaSeparatedListAttribute}

def get(self, id: Union[str, int], lazy: bool = False, **kwargs: Any) -> Issue:
return cast(Issue, super().get(id=id, lazy=lazy, **kwargs))
Expand Down Expand Up @@ -98,10 +95,7 @@ class GroupIssueManager(ListMixin, RESTManager):
"updated_after",
"updated_before",
)
_types = {
"iids": types.CommaSeparatedListAttribute,
"labels": types.CommaSeparatedListAttribute,
}
_types = {"iids": types.ArrayAttribute, "labels": types.CommaSeparatedListAttribute}


class ProjectIssue(
Expand Down Expand Up @@ -239,10 +233,7 @@ class ProjectIssueManager(CRUDMixin, RESTManager):
"discussion_locked",
),
)
_types = {
"iids": types.CommaSeparatedListAttribute,
"labels": types.CommaSeparatedListAttribute,
}
_types = {"iids": types.ArrayAttribute, "labels": types.CommaSeparatedListAttribute}

def get(
self, id: Union[str, int], lazy: bool = False, **kwargs: Any
Expand Down
4 changes: 2 additions & 2 deletions gitlab/v4/objects/members.py
Expand Up @@ -41,7 +41,7 @@ class GroupMemberManager(CRUDMixin, RESTManager):
_update_attrs = RequiredOptional(
required=("access_level",), optional=("expires_at",)
)
_types = {"user_ids": types.CommaSeparatedListAttribute}
_types = {"user_ids": types.ArrayAttribute}

def get(
self, id: Union[str, int], lazy: bool = False, **kwargs: Any
Expand Down Expand Up @@ -101,7 +101,7 @@ class ProjectMemberManager(CRUDMixin, RESTManager):
_update_attrs = RequiredOptional(
required=("access_level",), optional=("expires_at",)
)
_types = {"user_ids": types.CommaSeparatedListAttribute}
_types = {"user_ids": types.ArrayAttribute}

def get(
self, id: Union[str, int], lazy: bool = False, **kwargs: Any
Expand Down
14 changes: 7 additions & 7 deletions gitlab/v4/objects/merge_requests.py
Expand Up @@ -95,8 +95,8 @@ class MergeRequestManager(ListMixin, RESTManager):
"deployed_after",
)
_types = {
"approver_ids": types.CommaSeparatedListAttribute,
"approved_by_ids": types.CommaSeparatedListAttribute,
"approver_ids": types.ArrayAttribute,
"approved_by_ids": types.ArrayAttribute,
"in": types.CommaSeparatedListAttribute,
"labels": types.CommaSeparatedListAttribute,
}
Expand Down Expand Up @@ -133,8 +133,8 @@ class GroupMergeRequestManager(ListMixin, RESTManager):
"wip",
)
_types = {
"approver_ids": types.CommaSeparatedListAttribute,
"approved_by_ids": types.CommaSeparatedListAttribute,
"approver_ids": types.ArrayAttribute,
"approved_by_ids": types.ArrayAttribute,
"labels": types.CommaSeparatedListAttribute,
}

Expand Down Expand Up @@ -455,9 +455,9 @@ class ProjectMergeRequestManager(CRUDMixin, RESTManager):
"wip",
)
_types = {
"approver_ids": types.CommaSeparatedListAttribute,
"approved_by_ids": types.CommaSeparatedListAttribute,
"iids": types.CommaSeparatedListAttribute,
"approver_ids": types.ArrayAttribute,
"approved_by_ids": types.ArrayAttribute,
"iids": types.ArrayAttribute,
"labels": types.CommaSeparatedListAttribute,
}

Expand Down
4 changes: 2 additions & 2 deletions gitlab/v4/objects/milestones.py
Expand Up @@ -93,7 +93,7 @@ class GroupMilestoneManager(CRUDMixin, RESTManager):
optional=("title", "description", "due_date", "start_date", "state_event"),
)
_list_filters = ("iids", "state", "search")
_types = {"iids": types.CommaSeparatedListAttribute}
_types = {"iids": types.ArrayAttribute}

def get(
self, id: Union[str, int], lazy: bool = False, **kwargs: Any
Expand Down Expand Up @@ -177,7 +177,7 @@ class ProjectMilestoneManager(CRUDMixin, RESTManager):
optional=("title", "description", "due_date", "start_date", "state_event"),
)
_list_filters = ("iids", "state", "search")
_types = {"iids": types.CommaSeparatedListAttribute}
_types = {"iids": types.ArrayAttribute}

def get(
self, id: Union[str, int], lazy: bool = False, **kwargs: Any
Expand Down
2 changes: 1 addition & 1 deletion gitlab/v4/objects/projects.py
Expand Up @@ -125,7 +125,7 @@ class ProjectGroupManager(ListMixin, RESTManager):
"shared_min_access_level",
"shared_visible_only",
)
_types = {"skip_groups": types.CommaSeparatedListAttribute}
_types = {"skip_groups": types.ArrayAttribute}


class Project(RefreshMixin, SaveMixin, ObjectDeleteMixin, RepositoryMixin, RESTObject):
Expand Down
12 changes: 6 additions & 6 deletions gitlab/v4/objects/settings.py
Expand Up @@ -80,12 +80,12 @@ class ApplicationSettingsManager(GetWithoutIdMixin, UpdateMixin, RESTManager):
),
)
_types = {
"asset_proxy_allowlist": types.CommaSeparatedListAttribute,
"disabled_oauth_sign_in_sources": types.CommaSeparatedListAttribute,
"domain_allowlist": types.CommaSeparatedListAttribute,
"domain_denylist": types.CommaSeparatedListAttribute,
"import_sources": types.CommaSeparatedListAttribute,
"restricted_visibility_levels": types.CommaSeparatedListAttribute,
"asset_proxy_allowlist": types.ArrayAttribute,
"disabled_oauth_sign_in_sources": types.ArrayAttribute,
"domain_allowlist": types.ArrayAttribute,
"domain_denylist": types.ArrayAttribute,
"import_sources": types.ArrayAttribute,
"restricted_visibility_levels": types.ArrayAttribute,
}

@exc.on_http_error(exc.GitlabUpdateError)
Expand Down
2 changes: 1 addition & 1 deletion gitlab/v4/objects/users.py
Expand Up @@ -369,7 +369,7 @@ class ProjectUserManager(ListMixin, RESTManager):
_obj_cls = ProjectUser
_from_parent_attrs = {"project_id": "id"}
_list_filters = ("search", "skip_users")
_types = {"skip_users": types.CommaSeparatedListAttribute}
_types = {"skip_users": types.ArrayAttribute}


class UserEmail(ObjectDeleteMixin, RESTObject):
Expand Down
42 changes: 30 additions & 12 deletions tests/unit/test_types.py
Expand Up @@ -30,45 +30,63 @@ def test_gitlab_attribute_get():
assert o._value is None


def test_csv_list_attribute_input():
o = types.CommaSeparatedListAttribute()
def test_array_attribute_input():
o = types.ArrayAttribute()
o.set_from_cli("foo,bar,baz")
assert o.get() == ["foo", "bar", "baz"]

o.set_from_cli("foo")
assert o.get() == ["foo"]


def test_csv_list_attribute_empty_input():
o = types.CommaSeparatedListAttribute()
def test_array_attribute_empty_input():
o = types.ArrayAttribute()
o.set_from_cli("")
assert o.get() == []

o.set_from_cli(" ")
assert o.get() == []


def test_csv_list_attribute_get_for_api_from_cli():
o = types.CommaSeparatedListAttribute()
def test_array_attribute_get_for_api_from_cli():
o = types.ArrayAttribute()
o.set_from_cli("foo,bar,baz")
assert o.get_for_api() == "foo,bar,baz"


def test_csv_list_attribute_get_for_api_from_list():
o = types.CommaSeparatedListAttribute(["foo", "bar", "baz"])
def test_array_attribute_get_for_api_from_list():
o = types.ArrayAttribute(["foo", "bar", "baz"])
assert o.get_for_api() == "foo,bar,baz"


def test_csv_list_attribute_get_for_api_from_int_list():
o = types.CommaSeparatedListAttribute([1, 9, 7])
def test_array_attribute_get_for_api_from_int_list():
o = types.ArrayAttribute([1, 9, 7])
assert o.get_for_api() == "1,9,7"


def test_csv_list_attribute_does_not_split_string():
o = types.CommaSeparatedListAttribute("foo")
def test_array_attribute_does_not_split_string():
o = types.ArrayAttribute("foo")
assert o.get_for_api() == "foo"


# CommaSeparatedListAttribute tests
def test_csv_string_attribute_get_for_api_from_cli():
o = types.CommaSeparatedListAttribute()
o.set_from_cli("foo,bar,baz")
assert o.get_for_api() == "foo,bar,baz"


def test_csv_string_attribute_get_for_api_from_list():
o = types.CommaSeparatedListAttribute(["foo", "bar", "baz"])
assert o.get_for_api() == "foo,bar,baz"


def test_csv_string_attribute_get_for_api_from_int_list():
o = types.CommaSeparatedListAttribute([1, 9, 7])
assert o.get_for_api() == "1,9,7"


# LowercaseStringAttribute tests
def test_lowercase_string_attribute_get_for_api():
o = types.LowercaseStringAttribute("FOO")
assert o.get_for_api() == "foo"

0 comments on commit 7646360

Please sign in to comment.