Skip to content

Commit 26c8a0f

Browse files
author
Gauvain Pocentek
committed
Merge branch 'features/personal_snippets' of https://github.com/guyzmo/python-gitlab into guyzmo-features/personal_snippets
2 parents b05c0b6 + 6022dfe commit 26c8a0f

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-1
lines changed

gitlab/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def __init__(self, url, private_token=None, email=None, password=None,
103103
self.runners = RunnerManager(self)
104104
self.settings = ApplicationSettingsManager(self)
105105
self.sidekiq = SidekiqManager(self)
106+
self.snippets = SnippetManager(self)
106107
self.users = UserManager(self)
107108
self.teams = TeamManager(self)
108109
self.todos = TodoManager(self)
@@ -469,7 +470,8 @@ def delete(self, obj, id=None, **kwargs):
469470
params.pop(obj.idAttr)
470471

471472
r = self._raw_delete(url, **params)
472-
raise_error_from_response(r, GitlabDeleteError)
473+
raise_error_from_response(r, GitlabDeleteError,
474+
expected_code=[200, 204])
473475
return True
474476

475477
def create(self, obj, **kwargs):

gitlab/objects.py

+61
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,67 @@ class LicenseManager(BaseManager):
10181018
obj_cls = License
10191019

10201020

1021+
class Snippet(GitlabObject):
1022+
_url = '/snippets'
1023+
_constructorTypes = {'author': 'User'}
1024+
requiredCreateAttrs = ['title', 'file_name', 'content']
1025+
optionalCreateAttrs = ['lifetime', 'visibility_level']
1026+
optionalUpdateAttrs = ['title', 'file_name', 'content', 'visibility_level']
1027+
shortPrintAttr = 'title'
1028+
1029+
def content(self, streamed=False, action=None, chunk_size=1024, **kwargs):
1030+
"""Return the raw content of a snippet.
1031+
1032+
Args:
1033+
streamed (bool): If True the data will be processed by chunks of
1034+
`chunk_size` and each chunk is passed to `action` for
1035+
treatment.
1036+
action (callable): Callable responsible of dealing with chunk of
1037+
data.
1038+
chunk_size (int): Size of each chunk.
1039+
1040+
Returns:
1041+
str: The snippet content
1042+
1043+
Raises:
1044+
GitlabConnectionError: If the server cannot be reached.
1045+
GitlabGetError: If the server fails to perform the request.
1046+
"""
1047+
url = ("/snippets/%(snippet_id)s/raw" %
1048+
{'snippet_id': self.id})
1049+
r = self.gitlab._raw_get(url, **kwargs)
1050+
raise_error_from_response(r, GitlabGetError)
1051+
return utils.response_content(r, streamed, action, chunk_size)
1052+
1053+
1054+
class SnippetManager(BaseManager):
1055+
obj_cls = Snippet
1056+
1057+
def all(self, **kwargs):
1058+
"""List all the snippets
1059+
1060+
Args:
1061+
all (bool): If True, return all the items, without pagination
1062+
**kwargs: Additional arguments to send to GitLab.
1063+
1064+
Returns:
1065+
list(gitlab.Gitlab.Snippet): The list of snippets.
1066+
"""
1067+
return self.gitlab._raw_list("/snippets/public", Snippet, **kwargs)
1068+
1069+
def owned(self, **kwargs):
1070+
"""List owned snippets.
1071+
1072+
Args:
1073+
all (bool): If True, return all the items, without pagination
1074+
**kwargs: Additional arguments to send to GitLab.
1075+
1076+
Returns:
1077+
list(gitlab.Gitlab.Snippet): The list of owned snippets.
1078+
"""
1079+
return self.gitlab._raw_list("/snippets", Snippet, **kwargs)
1080+
1081+
10211082
class Namespace(GitlabObject):
10221083
_url = '/namespaces'
10231084
canGet = 'from_list'

gitlab/tests/test_gitlabobject.py

+34
Original file line numberDiff line numberDiff line change
@@ -455,3 +455,37 @@ def test_content(self):
455455
def test_blob_fail(self):
456456
with HTTMock(self.resp_content_fail):
457457
self.assertRaises(GitlabGetError, self.obj.content)
458+
459+
460+
class TestSnippet(unittest.TestCase):
461+
def setUp(self):
462+
self.gl = Gitlab("http://localhost", private_token="private_token",
463+
email="testuser@test.com", password="testpassword",
464+
ssl_verify=True)
465+
self.obj = Snippet(self.gl, data={"id": 3})
466+
467+
@urlmatch(scheme="http", netloc="localhost",
468+
path="/api/v3/snippets/3/raw",
469+
method="get")
470+
def resp_content(self, url, request):
471+
headers = {'content-type': 'application/json'}
472+
content = 'content'.encode("utf-8")
473+
return response(200, content, headers, None, 5, request)
474+
475+
@urlmatch(scheme="http", netloc="localhost",
476+
path="/api/v3/snippets/3/raw",
477+
method="get")
478+
def resp_content_fail(self, url, request):
479+
headers = {'content-type': 'application/json'}
480+
content = '{"message": "messagecontent" }'.encode("utf-8")
481+
return response(400, content, headers, None, 5, request)
482+
483+
def test_content(self):
484+
with HTTMock(self.resp_content):
485+
data = b'content'
486+
content = self.obj.content()
487+
self.assertEqual(content, data)
488+
489+
def test_blob_fail(self):
490+
with HTTMock(self.resp_content_fail):
491+
self.assertRaises(GitlabGetError, self.obj.content)

0 commit comments

Comments
 (0)