Skip to content

Commit 7025743

Browse files
author
Gauvain Pocentek
committed
Implement user_agent_detail for snippets
Add a new UserAgentDetail mixin to avoid code duplication.
1 parent 590ea0d commit 7025743

File tree

6 files changed

+44
-19
lines changed

6 files changed

+44
-19
lines changed

docs/gl_objects/projects.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ Delete a snippet::
430430
# or
431431
snippet.delete()
432432

433+
Get user agent detail (admin only)::
434+
435+
detail = snippet.user_agent_detail()
436+
433437
Notes
434438
=====
435439

docs/gl_objects/snippets.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,7 @@ Delete a snippet::
5252
gl.snippets.delete(snippet_id)
5353
# or
5454
snippet.delete()
55+
56+
Get user agent detail (admin only)::
57+
58+
detail = snippet.user_agent_detail()

gitlab/mixins.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,23 @@ def delete(self, **kwargs):
371371
self.manager.delete(self.get_id())
372372

373373

374+
class UserAgentDetailMixin(object):
375+
@cli.register_custom_action(('Snippet', 'ProjectSnippet', 'ProjectIssue'))
376+
@exc.on_http_error(exc.GitlabGetError)
377+
def user_agent_detail(self, **kwargs):
378+
"""Get the user agent detail.
379+
380+
Args:
381+
**kwargs: Extra options to send to the server (e.g. sudo)
382+
383+
Raises:
384+
GitlabAuthenticationError: If authentication is not correct
385+
GitlabGetError: If the server cannot perform the request
386+
"""
387+
path = '%s/%s/user_agent_detail' % (self.manager.path, self.get_id())
388+
return self.manager.gitlab.http_get(path, **kwargs)
389+
390+
374391
class AccessRequestMixin(object):
375392
@cli.register_custom_action(('ProjectAccessRequest', 'GroupAccessRequest'),
376393
tuple(), ('access_level', ))

gitlab/tests/test_mixins.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ class O(SetMixin):
7373
obj = O()
7474
self.assertTrue(hasattr(obj, 'set'))
7575

76+
def test_user_agent_detail_mixin(self):
77+
class O(UserAgentDetailMixin):
78+
pass
79+
80+
obj = O()
81+
self.assertTrue(hasattr(obj, 'user_agent_detail'))
82+
7683

7784
class TestMetaMixins(unittest.TestCase):
7885
def test_retrieve_mixin(self):

gitlab/v4/objects.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ class LicenseManager(RetrieveMixin, RESTManager):
786786
_optional_get_attrs = ('project', 'fullname')
787787

788788

789-
class Snippet(SaveMixin, ObjectDeleteMixin, RESTObject):
789+
class Snippet(UserAgentDetailMixin, SaveMixin, ObjectDeleteMixin, RESTObject):
790790
_short_print_attr = 'title'
791791

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

13881388

1389-
class ProjectIssue(SubscribableMixin, TodoMixin, TimeTrackingMixin, SaveMixin,
1390-
ObjectDeleteMixin, RESTObject):
1389+
class ProjectIssue(UserAgentDetailMixin, SubscribableMixin, TodoMixin,
1390+
TimeTrackingMixin, SaveMixin, ObjectDeleteMixin,
1391+
RESTObject):
13911392
_short_print_attr = 'title'
13921393
_id_attr = 'iid'
13931394
_managers = (
@@ -1396,21 +1397,6 @@ class ProjectIssue(SubscribableMixin, TodoMixin, TimeTrackingMixin, SaveMixin,
13961397
('notes', 'ProjectIssueNoteManager'),
13971398
)
13981399

1399-
@cli.register_custom_action('ProjectIssue')
1400-
@exc.on_http_error(exc.GitlabUpdateError)
1401-
def user_agent_detail(self, **kwargs):
1402-
"""Get user agent detail.
1403-
1404-
Args:
1405-
**kwargs: Extra options to send to the server (e.g. sudo)
1406-
1407-
Raises:
1408-
GitlabAuthenticationError: If authentication is not correct
1409-
GitlabGetError: If the detail could not be retrieved
1410-
"""
1411-
path = '%s/%s/user_agent_detail' % (self.manager.path, self.get_id())
1412-
return self.manager.gitlab.http_get(path, **kwargs)
1413-
14141400
@cli.register_custom_action('ProjectIssue', ('to_project_id',))
14151401
@exc.on_http_error(exc.GitlabUpdateError)
14161402
def move(self, to_project_id, **kwargs):
@@ -2291,7 +2277,8 @@ class ProjectSnippetDiscussionManager(RetrieveMixin, CreateMixin, RESTManager):
22912277
_create_attrs = (('body',), ('created_at',))
22922278

22932279

2294-
class ProjectSnippet(SaveMixin, ObjectDeleteMixin, RESTObject):
2280+
class ProjectSnippet(UserAgentDetailMixin, SaveMixin, ObjectDeleteMixin,
2281+
RESTObject):
22952282
_url = '/projects/%(project_id)s/snippets'
22962283
_short_print_attr = 'title'
22972284
_managers = (

tools/python_test_v4.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@
506506
assert(len(issue1.notes.list()) == 0)
507507
assert(isinstance(issue1.user_agent_detail(), dict))
508508

509+
assert(issue1.user_agent_detail()['user_agent'])
510+
509511
discussion = issue1.discussions.create({'body': 'Discussion body'})
510512
assert(len(issue1.discussions.list()) == 1)
511513
d_note = discussion.notes.create({'body': 'first note'})
@@ -534,6 +536,8 @@
534536
'visibility': gitlab.v4.objects.VISIBILITY_PRIVATE}
535537
)
536538

539+
assert(snippet.user_agent_detail()['user_agent'])
540+
537541
discussion = snippet.discussions.create({'body': 'Discussion body'})
538542
assert(len(snippet.discussions.list()) == 1)
539543
d_note = discussion.notes.create({'body': 'first note'})
@@ -699,6 +703,8 @@
699703
content = snippet.content()
700704
assert(content == 'import gitlab')
701705

706+
assert(snippet.user_agent_detail()['user_agent'])
707+
702708
snippet.delete()
703709
snippets = gl.snippets.list(all=True)
704710
assert(len(snippets) == 0)

0 commit comments

Comments
 (0)