Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0a4d40e

Browse files
author
Gauvain Pocentek
committed
Merge branch 'guyzmo-features/personal_snippets'
2 parentsb05c0b6 +26c8a0f commit0a4d40e

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-1
lines changed

‎gitlab/__init__.py

Lines changed: 3 additions & 1 deletion
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
returnTrue
474476

475477
defcreate(self,obj,**kwargs):

‎gitlab/objects.py

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

10201020

1021+
classSnippet(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+
defcontent(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+
returnutils.response_content(r,streamed,action,chunk_size)
1052+
1053+
1054+
classSnippetManager(BaseManager):
1055+
obj_cls=Snippet
1056+
1057+
defall(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+
returnself.gitlab._raw_list("/snippets/public",Snippet,**kwargs)
1068+
1069+
defowned(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+
returnself.gitlab._raw_list("/snippets",Snippet,**kwargs)
1080+
1081+
10211082
classNamespace(GitlabObject):
10221083
_url='/namespaces'
10231084
canGet='from_list'

‎gitlab/tests/test_gitlabobject.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,3 +455,37 @@ def test_content(self):
455455
deftest_blob_fail(self):
456456
withHTTMock(self.resp_content_fail):
457457
self.assertRaises(GitlabGetError,self.obj.content)
458+
459+
460+
classTestSnippet(unittest.TestCase):
461+
defsetUp(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+
defresp_content(self,url,request):
471+
headers= {'content-type':'application/json'}
472+
content='content'.encode("utf-8")
473+
returnresponse(200,content,headers,None,5,request)
474+
475+
@urlmatch(scheme="http",netloc="localhost",
476+
path="/api/v3/snippets/3/raw",
477+
method="get")
478+
defresp_content_fail(self,url,request):
479+
headers= {'content-type':'application/json'}
480+
content='{"message": "messagecontent" }'.encode("utf-8")
481+
returnresponse(400,content,headers,None,5,request)
482+
483+
deftest_content(self):
484+
withHTTMock(self.resp_content):
485+
data=b'content'
486+
content=self.obj.content()
487+
self.assertEqual(content,data)
488+
489+
deftest_blob_fail(self):
490+
withHTTMock(self.resp_content_fail):
491+
self.assertRaises(GitlabGetError,self.obj.content)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp