@@ -30,13 +30,33 @@ class GitlabCreateError(Exception):
30
30
class GitlabUpdateError (Exception ):
31
31
pass
32
32
33
- class GitlabSessionError (Exception ):
33
+ class GitlabAuthenticationError (Exception ):
34
34
pass
35
35
36
36
class Gitlab (object ):
37
- def __init__ (self , url , private_token ):
37
+ def __init__ (self , url , private_token = None , email = None , password = None ):
38
38
self .url = '%s/api/v3' % url
39
39
self .private_token = private_token
40
+ self .email = email
41
+ self .password = password
42
+
43
+ if not self .private_token :
44
+ self .authenticate
45
+
46
+ def authenticate (self , email = None , password = None ):
47
+ self .email = self .email or email
48
+ self .password = self .password or password
49
+
50
+ if not self .email or not self .password :
51
+ raise GitlabAuthenticationError ("Missing email/password" )
52
+
53
+ r = self .rawPost ('/session' , {'email' : email , 'password' : password })
54
+ if r .status_code == 201 :
55
+ self .user = User (self , r .json )
56
+ else :
57
+ raise GitlabAuthenticationError ()
58
+
59
+ self .private_token = self .user .private_token
40
60
41
61
def setUrl (self , url ):
42
62
self .url = '%s/api/v3' % url
@@ -234,13 +254,16 @@ def getObject(self, k, v):
234
254
235
255
def __init__ (self , gl , data ):
236
256
self .gitlab = gl
257
+
237
258
for k , v in data .items ():
238
259
if isinstance (v , list ):
239
260
self .__dict__ [k ] = []
240
261
for i in v :
241
262
self .__dict__ [k ].append (self .getObject (k ,i ))
242
- else :
263
+ elif v :
243
264
self .__dict__ [k ] = self .getObject (k ,v )
265
+ else : # None object
266
+ self .__dict__ [k ] = None
244
267
245
268
def __str__ (self ):
246
269
return '%s => %s' % (type (self ), str (self .__dict__ ))
@@ -275,13 +298,6 @@ class Issue(GitlabObject):
275
298
canUpdate = False
276
299
canCreate = False
277
300
278
- def Session (gl , email , password ):
279
- r = gl .rawPost ('/session' , {'email' : email , 'password' : password })
280
- if r .status_code == 201 :
281
- return User (gl , r .json )
282
- else :
283
- raise GitlabSessionError ()
284
-
285
301
class ProjectBranch (GitlabObject ):
286
302
url = '/projects/%(project_id)d/repository/branches'
287
303
canDelete = False
0 commit comments