Skip to content

Commit a02180d

Browse files
author
Gauvain Pocentek
committed
implement protect/unprotect for branches
1 parent dd210be commit a02180d

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

gitlab.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class GitlabDeleteError(Exception):
4646
pass
4747

4848

49+
class GitlabProtectError(Exception):
50+
pass
51+
52+
4953
class GitlabAuthenticationError(Exception):
5054
pass
5155

@@ -121,6 +125,19 @@ def rawPost(self, path, data):
121125

122126
return r
123127

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+
124141
def list(self, obj_class, **kwargs):
125142
url = obj_class._url
126143
if kwargs:
@@ -432,6 +449,24 @@ class ProjectBranch(GitlabObject):
432449
canUpdate = False
433450
canCreate = False
434451

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)
435470

436471
class ProjectCommit(GitlabObject):
437472
_url = '/projects/%(project_id)d/repository/commits'

0 commit comments

Comments
 (0)