@@ -46,6 +46,10 @@ class GitlabDeleteError(Exception):
46
46
pass
47
47
48
48
49
+ class GitlabProtectError (Exception ):
50
+ pass
51
+
52
+
49
53
class GitlabAuthenticationError (Exception ):
50
54
pass
51
55
@@ -121,6 +125,19 @@ def rawPost(self, path, data):
121
125
122
126
return r
123
127
128
+ def rawPut (self , path , with_token = False ):
129
+ url = '%s%s' % (self ._url , path )
130
+ if with_token :
131
+ url += "?private_token=%s" % self .private_token
132
+
133
+ try :
134
+ r = requests .put (url )
135
+ except :
136
+ raise GitlabConnectionError (
137
+ "Can't connect to GitLab server (%s)" % self ._url )
138
+
139
+ return r
140
+
124
141
def list (self , obj_class , ** kwargs ):
125
142
url = obj_class ._url
126
143
if kwargs :
@@ -432,6 +449,24 @@ class ProjectBranch(GitlabObject):
432
449
canUpdate = False
433
450
canCreate = False
434
451
452
+ def protect (self , protect = True ):
453
+ url = self ._url % {'project_id' : self .project_id }
454
+ if protect :
455
+ url = "%s/%s/protect" % (url , self .name )
456
+ else :
457
+ url = "%s/%s/unprotect" % (url , self .name )
458
+ r = self .gitlab .rawPut (url , True )
459
+
460
+ if r .status_code == 200 :
461
+ if protect :
462
+ self .protected = protect
463
+ else :
464
+ del self .protected
465
+ else :
466
+ raise GitlabProtectError
467
+
468
+ def unprotect (self ):
469
+ self .protect (False )
435
470
436
471
class ProjectCommit (GitlabObject ):
437
472
_url = '/projects/%(project_id)d/repository/commits'
0 commit comments