@@ -75,7 +75,8 @@ class GitlabAuthenticationError(Exception):
75
75
76
76
class Gitlab (object ):
77
77
"""Represents a GitLab server connection"""
78
- def __init__ (self , url , private_token = None , email = None , password = None , ssl_verify = True ):
78
+ def __init__ (self , url , private_token = None ,
79
+ email = None , password = None , ssl_verify = True ):
79
80
"""Stores informations about the server
80
81
81
82
url: the URL of the Gitlab server
@@ -122,12 +123,8 @@ def setUrl(self, url):
122
123
123
124
def setToken (self , token ):
124
125
"""Sets the private token for authentication"""
125
- if token :
126
- self .private_token = token
127
- self .headers = {"PRIVATE-TOKEN" : token }
128
- else :
129
- self .private_token = None
130
- self .headers = {}
126
+ self .private_token = token if token else None
127
+ self .headers = {"PRIVATE-TOKEN" : token } if token else None
131
128
132
129
def setCredentials (self , email , password ):
133
130
"""Sets the email/login and password for authentication"""
@@ -137,52 +134,48 @@ def setCredentials(self, email, password):
137
134
def rawGet (self , path ):
138
135
url = '%s%s' % (self ._url , path )
139
136
try :
140
- r = requests .get (url , headers = self .headers , verify = self .ssl_verify )
137
+ return requests .get (url ,
138
+ headers = self .headers ,
139
+ verify = self .ssl_verify )
141
140
except :
142
141
raise GitlabConnectionError (
143
142
"Can't connect to GitLab server (%s)" % self ._url )
144
143
145
- return r
146
-
147
144
def rawPost (self , path , data ):
148
145
url = '%s%s' % (self ._url , path )
149
146
try :
150
- r = requests .post (url , data ,
151
- headers = self .headers ,
152
- verify = self .ssl_verify )
147
+ return requests .post (url , data ,
148
+ headers = self .headers ,
149
+ verify = self .ssl_verify )
153
150
except :
154
151
raise GitlabConnectionError (
155
152
"Can't connect to GitLab server (%s)" % self ._url )
156
153
157
- return r
158
-
159
154
def rawPut (self , path ):
160
155
url = '%s%s' % (self ._url , path )
161
156
162
157
try :
163
- r = requests .put (url , headers = self .headers , verify = self .ssl_verify )
158
+ return requests .put (url ,
159
+ headers = self .headers ,
160
+ verify = self .ssl_verify )
164
161
except :
165
162
raise GitlabConnectionError (
166
163
"Can't connect to GitLab server (%s)" % self ._url )
167
164
168
- return r
169
-
170
165
def list (self , obj_class , ** kwargs ):
171
166
missing = []
172
167
for k in obj_class .requiredListAttrs :
173
168
if k not in kwargs :
174
- missing .append (k )
169
+ missing .append (k )
175
170
if missing :
176
- raise GitlabListError ('Missing attribute(s): %s' % \
177
- ", " .join (missing ))
171
+ raise GitlabListError ('Missing attribute(s): %s' %
172
+ ", " .join (missing ))
178
173
179
- url = obj_class ._url
180
- if kwargs :
181
- url = obj_class ._url % kwargs
174
+ url = obj_class ._url % kwargs
182
175
url = '%s%s' % (self ._url , url )
183
176
if kwargs :
184
177
url += "?%s" % ("&" .join (
185
- ["%s=%s" % (k , v ) for k , v in kwargs .items ()]))
178
+ ["%s=%s" % (k , v ) for k , v in kwargs .items ()]))
186
179
187
180
try :
188
181
r = requests .get (url , headers = self .headers , verify = self .ssl_verify )
@@ -211,24 +204,16 @@ def get(self, obj_class, id=None, **kwargs):
211
204
missing = []
212
205
for k in obj_class .requiredGetAttrs :
213
206
if k not in kwargs :
214
- missing .append (k )
207
+ missing .append (k )
215
208
if missing :
216
- raise GitlabListError ('Missing attribute(s): %s' % \
217
- ", " .join (missing ))
209
+ raise GitlabListError ('Missing attribute(s): %s' %
210
+ ", " .join (missing ))
218
211
219
- url = obj_class ._url
220
- if kwargs :
221
- url = obj_class ._url % kwargs
212
+ url = obj_class ._url % kwargs
222
213
if id is not None :
223
- try :
224
- url = '%s%s/%d' % \
225
- (self ._url , url , id )
226
- except TypeError : # id might be a str (ProjectBranch)
227
- url = '%s%s/%s' % \
228
- (self ._url , url , id )
214
+ url = '%s%s/%s' % (self ._url , url , str (id ))
229
215
else :
230
- url = '%s%s' % \
231
- (self ._url , url )
216
+ url = '%s%s' % (self ._url , url )
232
217
233
218
try :
234
219
r = requests .get (url , headers = self .headers , verify = self .ssl_verify )
@@ -247,11 +232,12 @@ def get(self, obj_class, id=None, **kwargs):
247
232
248
233
def delete (self , obj ):
249
234
url = obj ._url % obj .__dict__
250
- url = '%s%s/%d' % \
251
- (self ._url , url , obj .id )
235
+ url = '%s%s/%s' % (self ._url , url , str (obj .id ))
252
236
253
237
try :
254
- r = requests .delete (url , headers = self .headers , verify = self .ssl_verify )
238
+ r = requests .delete (url ,
239
+ headers = self .headers ,
240
+ verify = self .ssl_verify )
255
241
except :
256
242
raise GitlabConnectionError (
257
243
"Can't connect to GitLab server (%s)" % self ._url )
@@ -268,18 +254,18 @@ def create(self, obj):
268
254
missing = []
269
255
for k in obj .requiredCreateAttrs :
270
256
if k not in obj .__dict__ :
271
- missing .append (k )
257
+ missing .append (k )
272
258
if missing :
273
- raise GitlabCreateError ('Missing attribute(s): %s' % \
274
- ", " .join (missing ))
259
+ raise GitlabCreateError ('Missing attribute(s): %s' %
260
+ ", " .join (missing ))
275
261
276
262
url = obj ._url % obj .__dict__
277
263
url = '%s%s' % (self ._url , url )
278
264
279
265
try :
280
- # TODO: avoid too much work on the server side by filtering the
281
- # __dict__ keys
282
- r = requests . post ( url , obj . __dict__ , headers = self . headers , verify = self .ssl_verify )
266
+ r = requests . post ( url , obj . __dict__ ,
267
+ headers = self . headers ,
268
+ verify = self .ssl_verify )
283
269
except :
284
270
raise GitlabConnectionError (
285
271
"Can't connect to GitLab server (%s)" % self ._url )
@@ -293,8 +279,7 @@ def create(self, obj):
293
279
294
280
def update (self , obj ):
295
281
url = obj ._url % obj .__dict__
296
- url = '%s%s/%d' % \
297
- (self ._url , url , obj .id )
282
+ url = '%s%s/%s' % (self ._url , url , str (obj .id ))
298
283
299
284
# build a dict of data that can really be sent to server
300
285
d = {}
@@ -305,7 +290,9 @@ def update(self, obj):
305
290
d [k ] = str (v .encode (sys .stdout .encoding , "replace" ))
306
291
307
292
try :
308
- r = requests .put (url , d , headers = self .headers , verify = self .ssl_verify )
293
+ r = requests .put (url , d ,
294
+ headers = self .headers ,
295
+ verify = self .ssl_verify )
309
296
except :
310
297
raise GitlabConnectionError (
311
298
"Can't connect to GitLab server (%s)" % self ._url )
@@ -427,17 +414,16 @@ def _getListOrObject(self, cls, id, **kwargs):
427
414
if id is None :
428
415
if not cls .canList :
429
416
raise GitlabGetError
430
-
431
417
return cls .list (self .gitlab , ** kwargs )
418
+
432
419
elif isinstance (id , dict ):
433
420
if not cls .canCreate :
434
421
raise GitlabCreateError
435
-
436
422
return cls (self .gitlab , id , ** kwargs )
423
+
437
424
else :
438
425
if not cls .canGet :
439
426
raise GitlabGetError
440
-
441
427
return cls (self .gitlab , id , ** kwargs )
442
428
443
429
def _getObject (self , k , v ):
@@ -511,18 +497,20 @@ def short_print(self, depth=0):
511
497
id = self .__dict__ [self .idAttr ]
512
498
print ("%s%s: %s" % (" " * depth * 2 , self .idAttr , id ))
513
499
if self .shortPrintAttr :
514
- print ("%s%s: %s" % (" " * depth * 2 ,
515
- self .shortPrintAttr .replace ('_' , '-' ),
516
- self .__dict__ [self .shortPrintAttr ]))
500
+ print ("%s%s: %s" % (" " * depth * 2 ,
501
+ self .shortPrintAttr .replace ('_' , '-' ),
502
+ self .__dict__ [self .shortPrintAttr ]))
517
503
518
504
@staticmethod
519
505
def _obj_to_str (obj ):
520
506
if isinstance (obj , dict ):
521
- s = ", " .join (["%s: %s" % (x , GitlabObject ._obj_to_str (y )) for (x , y ) in obj .items ()])
507
+ s = ", " .join (["%s: %s" %
508
+ (x , GitlabObject ._obj_to_str (y ))
509
+ for (x , y ) in obj .items ()])
522
510
return "{ %s }" % s
523
511
elif isinstance (obj , list ):
524
512
s = ", " .join ([GitlabObject ._obj_to_str (x ) for x in obj ])
525
- return "[ %s ]" % s
513
+ return "[ %s ]" % s
526
514
elif isinstance (obj , unicode ):
527
515
return obj .encode (sys .stdout .encoding , "replace" )
528
516
else :
@@ -535,7 +523,8 @@ def pretty_print(self, depth=0):
535
523
if k == self .idAttr :
536
524
continue
537
525
v = self .__dict__ [k ]
538
- pretty_k = k .replace ('_' , '-' ).encode (sys .stdout .encoding , "replace" )
526
+ pretty_k = k .replace ('_' , '-' ).encode (sys .stdout .encoding ,
527
+ "replace" )
539
528
if isinstance (v , GitlabObject ):
540
529
if depth == 0 :
541
530
print ("%s:" % pretty_k )
@@ -589,8 +578,7 @@ class Group(GitlabObject):
589
578
shortPrintAttr = 'name'
590
579
591
580
def transfer_project (self , id ):
592
- url = '/groups/%d/projects/%d' % \
593
- (self .id , id )
581
+ url = '/groups/%d/projects/%d' % (self .id , id )
594
582
r = self .gitlab .rawPost (url , None )
595
583
if r .status_code != 201 :
596
584
raise GitlabTransferProjectError ()
@@ -625,10 +613,8 @@ class ProjectBranch(GitlabObject):
625
613
626
614
def protect (self , protect = True ):
627
615
url = self ._url % {'project_id' : self .project_id }
628
- if protect :
629
- url = "%s/%s/protect" % (url , self .name )
630
- else :
631
- url = "%s/%s/unprotect" % (url , self .name )
616
+ action = 'protect' if protect else 'unprotect'
617
+ url = "%s/%s/%s" % (url , self .name , action )
632
618
r = self .gitlab .rawPut (url )
633
619
634
620
if r .status_code == 200 :
@@ -653,22 +639,22 @@ class ProjectCommit(GitlabObject):
653
639
654
640
def diff (self ):
655
641
url = '/projects/%(project_id)s/repository/commits/%(commit_id)s/diff' % \
656
- {'project_id' : self .project_id , 'commit_id' : self .id }
642
+ {'project_id' : self .project_id , 'commit_id' : self .id }
657
643
r = self .gitlab .rawGet (url )
658
644
if r .status_code == 200 :
659
645
return r .json ()
660
646
661
- raise GitlabGetError ()
647
+ raise GitlabGetError
662
648
663
649
def blob (self , filepath ):
664
650
url = '/projects/%(project_id)s/repository/blobs/%(commit_id)s' % \
665
- {'project_id' : self .project_id , 'commit_id' : self .id }
651
+ {'project_id' : self .project_id , 'commit_id' : self .id }
666
652
url += '?filepath=%s' % filepath
667
653
r = self .gitlab .rawGet (url )
668
654
if r .status_code == 200 :
669
655
return r .content
670
656
671
- raise GitlabGetError ()
657
+ raise GitlabGetError
672
658
673
659
674
660
class ProjectKey (GitlabObject ):
@@ -762,7 +748,8 @@ class ProjectMergeRequest(GitlabObject):
762
748
canDelete = False
763
749
requiredListAttrs = ['project_id' ]
764
750
requiredGetAttrs = ['project_id' ]
765
- requiredCreateAttrs = ['project_id' , 'source_branch' , 'target_branch' , 'title' ]
751
+ requiredCreateAttrs = ['project_id' , 'source_branch' ,
752
+ 'target_branch' , 'title' ]
766
753
optionalCreateAttrs = ['assignee_id' ]
767
754
768
755
def Note (self , id = None , ** kwargs ):
@@ -891,7 +878,7 @@ def tree(self, path='', ref_name=''):
891
878
if r .status_code == 200 :
892
879
return r .json ()
893
880
894
- raise GitlabGetError ()
881
+ raise GitlabGetError
895
882
896
883
def blob (self , sha , filepath ):
897
884
url = "%s/%s/repository/blobs/%s" % (self ._url , self .id , sha )
@@ -900,7 +887,7 @@ def blob(self, sha, filepath):
900
887
if r .status_code == 200 :
901
888
return r .content
902
889
903
- raise GitlabGetError ()
890
+ raise GitlabGetError
904
891
905
892
906
893
class TeamMember (GitlabObject ):
0 commit comments