Skip to content

Commit

Permalink
Implement user_agent_detail for snippets
Browse files Browse the repository at this point in the history
Add a new UserAgentDetail mixin to avoid code duplication.
  • Loading branch information
Gauvain Pocentek committed May 27, 2018
1 parent 590ea0d commit 7025743
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 19 deletions.
4 changes: 4 additions & 0 deletions docs/gl_objects/projects.rst
Expand Up @@ -430,6 +430,10 @@ Delete a snippet::
# or
snippet.delete()

Get user agent detail (admin only)::

detail = snippet.user_agent_detail()

Notes
=====

Expand Down
4 changes: 4 additions & 0 deletions docs/gl_objects/snippets.rst
Expand Up @@ -52,3 +52,7 @@ Delete a snippet::
gl.snippets.delete(snippet_id)
# or
snippet.delete()

Get user agent detail (admin only)::

detail = snippet.user_agent_detail()
17 changes: 17 additions & 0 deletions gitlab/mixins.py
Expand Up @@ -371,6 +371,23 @@ def delete(self, **kwargs):
self.manager.delete(self.get_id())


class UserAgentDetailMixin(object):
@cli.register_custom_action(('Snippet', 'ProjectSnippet', 'ProjectIssue'))
@exc.on_http_error(exc.GitlabGetError)
def user_agent_detail(self, **kwargs):
"""Get the user agent detail.
Args:
**kwargs: Extra options to send to the server (e.g. sudo)
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabGetError: If the server cannot perform the request
"""
path = '%s/%s/user_agent_detail' % (self.manager.path, self.get_id())
return self.manager.gitlab.http_get(path, **kwargs)


class AccessRequestMixin(object):
@cli.register_custom_action(('ProjectAccessRequest', 'GroupAccessRequest'),
tuple(), ('access_level', ))
Expand Down
7 changes: 7 additions & 0 deletions gitlab/tests/test_mixins.py
Expand Up @@ -73,6 +73,13 @@ class O(SetMixin):
obj = O()
self.assertTrue(hasattr(obj, 'set'))

def test_user_agent_detail_mixin(self):
class O(UserAgentDetailMixin):
pass

obj = O()
self.assertTrue(hasattr(obj, 'user_agent_detail'))


class TestMetaMixins(unittest.TestCase):
def test_retrieve_mixin(self):
Expand Down
25 changes: 6 additions & 19 deletions gitlab/v4/objects.py
Expand Up @@ -786,7 +786,7 @@ class LicenseManager(RetrieveMixin, RESTManager):
_optional_get_attrs = ('project', 'fullname')


class Snippet(SaveMixin, ObjectDeleteMixin, RESTObject):
class Snippet(UserAgentDetailMixin, SaveMixin, ObjectDeleteMixin, RESTObject):
_short_print_attr = 'title'

@cli.register_custom_action('Snippet')
Expand Down Expand Up @@ -1386,8 +1386,9 @@ class ProjectIssueDiscussionManager(RetrieveMixin, CreateMixin, RESTManager):
_create_attrs = (('body',), ('created_at',))


class ProjectIssue(SubscribableMixin, TodoMixin, TimeTrackingMixin, SaveMixin,
ObjectDeleteMixin, RESTObject):
class ProjectIssue(UserAgentDetailMixin, SubscribableMixin, TodoMixin,
TimeTrackingMixin, SaveMixin, ObjectDeleteMixin,
RESTObject):
_short_print_attr = 'title'
_id_attr = 'iid'
_managers = (
Expand All @@ -1396,21 +1397,6 @@ class ProjectIssue(SubscribableMixin, TodoMixin, TimeTrackingMixin, SaveMixin,
('notes', 'ProjectIssueNoteManager'),
)

@cli.register_custom_action('ProjectIssue')
@exc.on_http_error(exc.GitlabUpdateError)
def user_agent_detail(self, **kwargs):
"""Get user agent detail.
Args:
**kwargs: Extra options to send to the server (e.g. sudo)
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabGetError: If the detail could not be retrieved
"""
path = '%s/%s/user_agent_detail' % (self.manager.path, self.get_id())
return self.manager.gitlab.http_get(path, **kwargs)

@cli.register_custom_action('ProjectIssue', ('to_project_id',))
@exc.on_http_error(exc.GitlabUpdateError)
def move(self, to_project_id, **kwargs):
Expand Down Expand Up @@ -2291,7 +2277,8 @@ class ProjectSnippetDiscussionManager(RetrieveMixin, CreateMixin, RESTManager):
_create_attrs = (('body',), ('created_at',))


class ProjectSnippet(SaveMixin, ObjectDeleteMixin, RESTObject):
class ProjectSnippet(UserAgentDetailMixin, SaveMixin, ObjectDeleteMixin,
RESTObject):
_url = '/projects/%(project_id)s/snippets'
_short_print_attr = 'title'
_managers = (
Expand Down
6 changes: 6 additions & 0 deletions tools/python_test_v4.py
Expand Up @@ -506,6 +506,8 @@
assert(len(issue1.notes.list()) == 0)
assert(isinstance(issue1.user_agent_detail(), dict))

assert(issue1.user_agent_detail()['user_agent'])

discussion = issue1.discussions.create({'body': 'Discussion body'})
assert(len(issue1.discussions.list()) == 1)
d_note = discussion.notes.create({'body': 'first note'})
Expand Down Expand Up @@ -534,6 +536,8 @@
'visibility': gitlab.v4.objects.VISIBILITY_PRIVATE}
)

assert(snippet.user_agent_detail()['user_agent'])

discussion = snippet.discussions.create({'body': 'Discussion body'})
assert(len(snippet.discussions.list()) == 1)
d_note = discussion.notes.create({'body': 'first note'})
Expand Down Expand Up @@ -699,6 +703,8 @@
content = snippet.content()
assert(content == 'import gitlab')

assert(snippet.user_agent_detail()['user_agent'])

snippet.delete()
snippets = gl.snippets.list(all=True)
assert(len(snippets) == 0)
Expand Down

0 comments on commit 7025743

Please sign in to comment.