Skip to content

Commit

Permalink
Add support for user/group/project filter by custom attribute
Browse files Browse the repository at this point in the history
Closes #367
  • Loading branch information
Gauvain Pocentek committed Jan 1, 2018
1 parent fa52024 commit 65c64eb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
18 changes: 16 additions & 2 deletions gitlab/__init__.py
Expand Up @@ -642,8 +642,22 @@ def sanitized_url(url):
return parsed._replace(path=new_path).geturl()

url = self._build_url(path)
params = query_data.copy()
params.update(kwargs)

def copy_dict(dest, src):
for k, v in src.items():
if isinstance(v, dict):
# Transform dict values in new attributes. For example:
# custom_attributes: {'foo', 'bar'} =>
# custom_attributes['foo']: 'bar'
for dict_k, dict_v in v.items():
dest['%s[%s]' % (k, dict_k)] = dict_v
else:
dest[k] = v

params = {}
copy_dict(params, query_data)
copy_dict(params, kwargs)

opts = self._get_session_opts(content_type='application/json')

# don't set the content-type header when uploading files
Expand Down
7 changes: 4 additions & 3 deletions gitlab/v4/objects.py
Expand Up @@ -253,7 +253,7 @@ class UserManager(CRUDMixin, RESTManager):
_obj_cls = User

_list_filters = ('active', 'blocked', 'username', 'extern_uid', 'provider',
'external', 'search')
'external', 'search', 'custom_attributes')
_create_attrs = (
tuple(),
('email', 'username', 'name', 'password', 'reset_password', 'skype',
Expand Down Expand Up @@ -656,7 +656,7 @@ class GroupManager(CRUDMixin, RESTManager):
_path = '/groups'
_obj_cls = Group
_list_filters = ('skip_groups', 'all_available', 'search', 'order_by',
'sort', 'statistics', 'owned')
'sort', 'statistics', 'owned', 'custom_attributes')
_create_attrs = (
('name', 'path'),
('description', 'visibility', 'parent_id', 'lfs_enabled',
Expand Down Expand Up @@ -2639,7 +2639,8 @@ class ProjectManager(CRUDMixin, RESTManager):
)
_list_filters = ('search', 'owned', 'starred', 'archived', 'visibility',
'order_by', 'sort', 'simple', 'membership', 'statistics',
'with_issues_enabled', 'with_merge_requests_enabled')
'with_issues_enabled', 'with_merge_requests_enabled',
'custom_attributes')


class Runner(SaveMixin, ObjectDeleteMixin, RESTObject):
Expand Down
3 changes: 3 additions & 0 deletions tools/python_test_v4.py
Expand Up @@ -129,6 +129,7 @@
attrs = new_user.customattributes.list()
assert(len(attrs) == 0)
attr = new_user.customattributes.set('key', 'value1')
assert(len(gl.users.list(custom_attributes={'key': 'value1'})) == 1)
assert(attr.key == 'key')
assert(attr.value == 'value1')
assert(len(new_user.customattributes.list()) == 1)
Expand Down Expand Up @@ -234,6 +235,7 @@
attrs = group2.customattributes.list()
assert(len(attrs) == 0)
attr = group2.customattributes.set('key', 'value1')
assert(len(gl.groups.list(custom_attributes={'key': 'value1'})) == 1)
assert(attr.key == 'key')
assert(attr.value == 'value1')
assert(len(group2.customattributes.list()) == 1)
Expand Down Expand Up @@ -303,6 +305,7 @@
attrs = admin_project.customattributes.list()
assert(len(attrs) == 0)
attr = admin_project.customattributes.set('key', 'value1')
assert(len(gl.projects.list(custom_attributes={'key': 'value1'})) == 1)
assert(attr.key == 'key')
assert(attr.value == 'value1')
assert(len(admin_project.customattributes.list()) == 1)
Expand Down

0 comments on commit 65c64eb

Please sign in to comment.