Skip to content

Commit

Permalink
Add users custome attributes support
Browse files Browse the repository at this point in the history
  • Loading branch information
Gauvain Pocentek committed Nov 4, 2017
1 parent 32f7e17 commit 4fb2e43
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
18 changes: 18 additions & 0 deletions docs/gl_objects/users.py
Expand Up @@ -97,3 +97,21 @@
gl.auth()
current_user = gl.user
# end currentuser get

# ca list
attrs = user.customeattributes.list()
# end ca list

# ca get
attr = user.customeattributes.get(attr_key)
# end ca get

# ca set
attr = user.customeattributes.set(attr_key, attr_value)
# end ca set

# ca delete
attr.delete()
# or
user.customeattributes.delete(attr_key)
# end ca delete
36 changes: 36 additions & 0 deletions docs/gl_objects/users.rst
Expand Up @@ -70,6 +70,42 @@ Block/Unblock a user:
:start-after: # block
:end-before: # end block

User custom attributes
======================

References
----------

* v4 API:

+ :class:`gitlab.v4.objects.UserCustomAttribute`
+ :class:`gitlab.v4.objects.UserCustomAttributeManager`
+ :attr:`gitlab.v4.objects.User.customattributes`

List custom attributes for a user:

.. literalinclude:: users.py
:start-after: # ca list
:end-before: # end ca list

Get a custom attribute for a user:

.. literalinclude:: users.py
:start-after: # ca get
:end-before: # end ca get

Set (create or update) a custom attribute for a user:

.. literalinclude:: users.py
:start-after: # ca set
:end-before: # end ca set

Delete a custom attribute for a user:

.. literalinclude:: users.py
:start-after: # ca list
:end-before: # end ca list

Current User
============

Expand Down
4 changes: 4 additions & 0 deletions gitlab/exceptions.py
Expand Up @@ -77,6 +77,10 @@ class GitlabDeleteError(GitlabOperationError):
pass


class GitlabSetError(GitlabOperationError):
pass


class GitlabProtectError(GitlabOperationError):
pass

Expand Down
31 changes: 31 additions & 0 deletions gitlab/v4/objects.py
Expand Up @@ -112,6 +112,36 @@ def compound_metrics(self, **kwargs):
return self.gitlab.http_get('/sidekiq/compound_metrics', **kwargs)


class UserCustomAttribute(ObjectDeleteMixin, RESTObject):
_id_attr = 'key'


class UserCustomAttributeManager(RetrieveMixin, DeleteMixin, RESTManager):
_path = '/users/%(user_id)s/custom_attributes'
_obj_cls = UserCustomAttribute
_from_parent_attrs = {'user_id': 'id'}

def set(self, key, value, **kwargs):
"""Create or update a user attribute.
Args:
key (str): The attribute to update
value (str): The value to set
**kwargs: Extra options to send to the server (e.g. sudo)
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabSetError: If an error occured
Returns:
UserCustomAttribute: The created/updated user attribute
"""
path = '%s/%s' % (self.path, key.replace('/', '%2F'))
data = {'value': value}
server_data = self.gitlab.http_put(path, post_data=data, **kwargs)
return self._obj_cls(self, server_data)


class UserEmail(ObjectDeleteMixin, RESTObject):
_short_print_attr = 'email'

Expand Down Expand Up @@ -165,6 +195,7 @@ class UserProjectManager(CreateMixin, RESTManager):
class User(SaveMixin, ObjectDeleteMixin, RESTObject):
_short_print_attr = 'username'
_managers = (
('customattributes', 'UserCustomAttributeManager'),
('emails', 'UserEmailManager'),
('gpgkeys', 'UserGPGKeyManager'),
('keys', 'UserKeyManager'),
Expand Down
14 changes: 14 additions & 0 deletions tools/python_test_v4.py
Expand Up @@ -131,6 +131,20 @@
email.delete()
assert(len(new_user.emails.list()) == 0)

# custom attributes
attrs = new_user.customattributes.list()
assert(len(attrs) == 0)
attr = new_user.customattributes.set('key', 'value1')
assert(attr.key == 'key')
assert(attr.value == 'value1')
assert(len(new_user.customattributes.list()) == 1)
attr = new_user.customattributes.set('key', 'value2')
attr = new_user.customattributes.get('key')
assert(attr.value == 'value2')
assert(len(new_user.customattributes.list()) == 1)
attr.delete()
assert(len(new_user.customattributes.list()) == 0)

new_user.delete()
foobar_user.delete()
assert(len(gl.users.list()) == 3)
Expand Down

0 comments on commit 4fb2e43

Please sign in to comment.