@@ -107,15 +107,20 @@ class GitlabTransferProjectError(GitlabOperationError):
107
107
pass
108
108
109
109
110
- def _raise_error_from_response (response , error ):
110
+ def _raise_error_from_response (response , error , expected_code = 200 ):
111
111
"""Tries to parse gitlab error message from response and raises error.
112
112
113
+ Do nothing if the response status is the expected one.
114
+
113
115
If response status code is 401, raises instead GitlabAuthenticationError.
114
116
115
117
response: requests response object
116
118
error: Error-class to raise. Should be inherited from GitLabError
117
119
"""
118
120
121
+ if expected_code == response .status_code :
122
+ return
123
+
119
124
try :
120
125
message = response .json ()['message' ]
121
126
except (KeyError , ValueError ):
@@ -183,12 +188,8 @@ def credentials_auth(self):
183
188
184
189
data = json .dumps ({'email' : self .email , 'password' : self .password })
185
190
r = self ._raw_post ('/session' , data , content_type = 'application/json' )
186
-
187
- if r .status_code == 201 :
188
- self .user = CurrentUser (self , r .json ())
189
- else :
190
- _raise_error_from_response (r , GitlabAuthenticationError )
191
-
191
+ _raise_error_from_response (r , GitlabAuthenticationError , 201 )
192
+ self .user = CurrentUser (self , r .json ())
192
193
self .set_token (self .user .private_token )
193
194
194
195
def token_auth (self ):
@@ -342,34 +343,33 @@ def list(self, obj_class, **kwargs):
342
343
raise GitlabConnectionError (
343
344
"Can't connect to GitLab server (%s)" % self ._url )
344
345
345
- if r .status_code == 200 :
346
- cls = obj_class
347
- if obj_class ._returnClass :
348
- cls = obj_class ._returnClass
346
+ _raise_error_from_response (r , GitlabListError )
349
347
350
- cls_kwargs = kwargs .copy ()
348
+ cls = obj_class
349
+ if obj_class ._returnClass :
350
+ cls = obj_class ._returnClass
351
351
352
- # Add _created manually, because we are not creating objects
353
- # through normal path
354
- cls_kwargs ['_created' ] = True
352
+ cls_kwargs = kwargs .copy ()
355
353
356
- get_all_results = params .get ('all' , False )
354
+ # Add _created manually, because we are not creating objects
355
+ # through normal path
356
+ cls_kwargs ['_created' ] = True
357
357
358
- # Remove parameters from kwargs before passing it to constructor
359
- for key in ['all' , 'page' , 'per_page' , 'sudo' ]:
360
- if key in cls_kwargs :
361
- del cls_kwargs [key ]
358
+ get_all_results = params .get ('all' , False )
362
359
363
- results = [cls (self , item , ** cls_kwargs ) for item in r .json ()
364
- if item is not None ]
365
- if ('next' in r .links and 'url' in r .links ['next' ]
366
- and get_all_results is True ):
367
- args = kwargs .copy ()
368
- args ['next_url' ] = r .links ['next' ]['url' ]
369
- results .extend (self .list (obj_class , ** args ))
370
- return results
371
- else :
372
- _raise_error_from_response (r , GitlabListError )
360
+ # Remove parameters from kwargs before passing it to constructor
361
+ for key in ['all' , 'page' , 'per_page' , 'sudo' ]:
362
+ if key in cls_kwargs :
363
+ del cls_kwargs [key ]
364
+
365
+ results = [cls (self , item , ** cls_kwargs ) for item in r .json ()
366
+ if item is not None ]
367
+ if ('next' in r .links and 'url' in r .links ['next' ]
368
+ and get_all_results is True ):
369
+ args = kwargs .copy ()
370
+ args ['next_url' ] = r .links ['next' ]['url' ]
371
+ results .extend (self .list (obj_class , ** args ))
372
+ return results
373
373
374
374
def get (self , obj_class , id = None , ** kwargs ):
375
375
missing = []
@@ -399,10 +399,8 @@ def get(self, obj_class, id=None, **kwargs):
399
399
raise GitlabConnectionError (
400
400
"Can't connect to GitLab server (%s)" % self ._url )
401
401
402
- if r .status_code == 200 :
403
- return r .json ()
404
- else :
405
- _raise_error_from_response (r , GitlabGetError )
402
+ _raise_error_from_response (r , GitlabGetError )
403
+ return r .json ()
406
404
407
405
def delete (self , obj , ** kwargs ):
408
406
params = obj .__dict__ .copy ()
@@ -435,10 +433,8 @@ def delete(self, obj, **kwargs):
435
433
raise GitlabConnectionError (
436
434
"Can't connect to GitLab server (%s)" % self ._url )
437
435
438
- if r .status_code == 200 :
439
- return True
440
- else :
441
- _raise_error_from_response (r , GitlabDeleteError )
436
+ _raise_error_from_response (r , GitlabDeleteError )
437
+ return True
442
438
443
439
def create (self , obj , ** kwargs ):
444
440
params = obj .__dict__ .copy ()
@@ -467,10 +463,8 @@ def create(self, obj, **kwargs):
467
463
raise GitlabConnectionError (
468
464
"Can't connect to GitLab server (%s)" % self ._url )
469
465
470
- if r .status_code == 201 :
471
- return r .json ()
472
- else :
473
- _raise_error_from_response (r , GitlabCreateError )
466
+ _raise_error_from_response (r , GitlabCreateError , 201 )
467
+ return r .json ()
474
468
475
469
def update (self , obj , ** kwargs ):
476
470
params = obj .__dict__ .copy ()
@@ -498,10 +492,8 @@ def update(self, obj, **kwargs):
498
492
raise GitlabConnectionError (
499
493
"Can't connect to GitLab server (%s)" % self ._url )
500
494
501
- if r .status_code == 200 :
502
- return r .json ()
503
- else :
504
- _raise_error_from_response (r , GitlabUpdateError )
495
+ _raise_error_from_response (r , GitlabUpdateError )
496
+ return r .json ()
505
497
506
498
def Hook (self , id = None , ** kwargs ):
507
499
"""Creates/tests/lists system hook(s) known by the GitLab server.
@@ -539,8 +531,7 @@ def UserProject(self, id=None, **kwargs):
539
531
540
532
def _list_projects (self , url , ** kwargs ):
541
533
r = self ._raw_get (url , ** kwargs )
542
- if r .status_code != 200 :
543
- _raise_error_from_response (r , GitlabListError )
534
+ _raise_error_from_response (r , GitlabListError )
544
535
545
536
l = []
546
537
for o in r .json ():
@@ -923,8 +914,7 @@ def Member(self, id=None, **kwargs):
923
914
def transfer_project (self , id , ** kwargs ):
924
915
url = '/groups/%d/projects/%d' % (self .id , id )
925
916
r = self .gitlab ._raw_post (url , None , ** kwargs )
926
- if r .status_code != 201 :
927
- _raise_error_from_response (r , GitlabTransferProjectError )
917
+ _raise_error_from_response (r , GitlabTransferProjectError , 201 )
928
918
929
919
930
920
class Hook (GitlabObject ):
@@ -960,14 +950,12 @@ def protect(self, protect=True, **kwargs):
960
950
action = 'protect' if protect else 'unprotect'
961
951
url = "%s/%s/%s" % (url , self .name , action )
962
952
r = self .gitlab ._raw_put (url , data = None , content_type = None , ** kwargs )
953
+ _raise_error_from_response (r , GitlabProtectError )
963
954
964
- if r .status_code == 200 :
965
- if protect :
966
- self .protected = protect
967
- else :
968
- del self .protected
955
+ if protect :
956
+ self .protected = protect
969
957
else :
970
- _raise_error_from_response ( r , GitlabProtectError )
958
+ del self . protected
971
959
972
960
def unprotect (self , ** kwargs ):
973
961
self .protect (False , ** kwargs )
@@ -985,20 +973,19 @@ def diff(self, **kwargs):
985
973
url = ('/projects/%(project_id)s/repository/commits/%(commit_id)s/diff'
986
974
% {'project_id' : self .project_id , 'commit_id' : self .id })
987
975
r = self .gitlab ._raw_get (url , ** kwargs )
988
- if r .status_code == 200 :
989
- return r .json ()
990
- else :
991
- _raise_error_from_response (r , GitlabGetError )
976
+ _raise_error_from_response (r , GitlabGetError )
977
+
978
+ return r .json ()
992
979
993
980
def blob (self , filepath , ** kwargs ):
994
981
url = ('/projects/%(project_id)s/repository/blobs/%(commit_id)s' %
995
982
{'project_id' : self .project_id , 'commit_id' : self .id })
996
983
url += '?filepath=%s' % filepath
997
984
r = self .gitlab ._raw_get (url , ** kwargs )
998
- if r . status_code == 200 :
999
- return r . content
1000
- else :
1001
- _raise_error_from_response ( r , GitlabGetError )
985
+
986
+ _raise_error_from_response ( r , GitlabGetError )
987
+
988
+ return r . content
1002
989
1003
990
1004
991
class ProjectKey (GitlabObject ):
@@ -1173,11 +1160,8 @@ def Content(self, **kwargs):
1173
1160
url = ("/projects/%(project_id)s/snippets/%(snippet_id)s/raw" %
1174
1161
{'project_id' : self .project_id , 'snippet_id' : self .id })
1175
1162
r = self .gitlab ._raw_get (url , ** kwargs )
1176
-
1177
- if r .status_code == 200 :
1178
- return r .content
1179
- else :
1180
- _raise_error_from_response (r , GitlabGetError )
1163
+ _raise_error_from_response (r , GitlabGetError )
1164
+ return r .content
1181
1165
1182
1166
def Note (self , id = None , ** kwargs ):
1183
1167
return ProjectSnippetNote ._get_list_or_object (
@@ -1288,29 +1272,23 @@ def tree(self, path='', ref_name='', **kwargs):
1288
1272
url = "%s/%s/repository/tree" % (self ._url , self .id )
1289
1273
url += '?path=%s&ref_name=%s' % (path , ref_name )
1290
1274
r = self .gitlab ._raw_get (url , ** kwargs )
1291
- if r .status_code == 200 :
1292
- return r .json ()
1293
- else :
1294
- _raise_error_from_response (r , GitlabGetError )
1275
+ _raise_error_from_response (r , GitlabGetError )
1276
+ return r .json ()
1295
1277
1296
1278
def blob (self , sha , filepath , ** kwargs ):
1297
1279
url = "%s/%s/repository/blobs/%s" % (self ._url , self .id , sha )
1298
1280
url += '?filepath=%s' % (filepath )
1299
1281
r = self .gitlab ._raw_get (url , ** kwargs )
1300
- if r .status_code == 200 :
1301
- return r .content
1302
- else :
1303
- _raise_error_from_response (r , GitlabGetError )
1282
+ _raise_error_from_response (r , GitlabGetError )
1283
+ return r .content
1304
1284
1305
1285
def archive (self , sha = None , ** kwargs ):
1306
1286
url = '/projects/%s/repository/archive' % self .id
1307
1287
if sha :
1308
1288
url += '?sha=%s' % sha
1309
1289
r = self .gitlab ._raw_get (url , ** kwargs )
1310
- if r .status_code == 200 :
1311
- return r .content
1312
- else :
1313
- _raise_error_from_response (r , GitlabGetError )
1290
+ _raise_error_from_response (r , GitlabGetError )
1291
+ return r .content
1314
1292
1315
1293
def create_file (self , path , branch , content , message , ** kwargs ):
1316
1294
"""Creates file in project repository
@@ -1330,24 +1308,21 @@ def create_file(self, path, branch, content, message, **kwargs):
1330
1308
url += ("?file_path=%s&branch_name=%s&content=%s&commit_message=%s" %
1331
1309
(path , branch , content , message ))
1332
1310
r = self .gitlab ._raw_post (url , data = None , content_type = None , ** kwargs )
1333
- if r .status_code != 201 :
1334
- _raise_error_from_response (r , GitlabCreateError )
1311
+ _raise_error_from_response (r , GitlabCreateError , 201 )
1335
1312
1336
1313
def update_file (self , path , branch , content , message , ** kwargs ):
1337
1314
url = "/projects/%s/repository/files" % self .id
1338
1315
url += ("?file_path=%s&branch_name=%s&content=%s&commit_message=%s" %
1339
1316
(path , branch , content , message ))
1340
1317
r = self .gitlab ._raw_put (url , data = None , content_type = None , ** kwargs )
1341
- if r .status_code != 200 :
1342
- _raise_error_from_response (r , GitlabUpdateError )
1318
+ _raise_error_from_response (r , GitlabUpdateError )
1343
1319
1344
1320
def delete_file (self , path , branch , message , ** kwargs ):
1345
1321
url = "/projects/%s/repository/files" % self .id
1346
1322
url += ("?file_path=%s&branch_name=%s&commit_message=%s" %
1347
1323
(path , branch , message ))
1348
1324
r = self .gitlab ._raw_delete (url , ** kwargs )
1349
- if r .status_code != 200 :
1350
- _raise_error_from_response (r , GitlabDeleteError )
1325
+ _raise_error_from_response (r , GitlabDeleteError )
1351
1326
1352
1327
1353
1328
class TeamMember (GitlabObject ):
0 commit comments