Skip to content

Commit

Permalink
implement protect/unprotect for branches
Browse files Browse the repository at this point in the history
  • Loading branch information
Gauvain Pocentek committed Feb 16, 2013
1 parent dd210be commit a02180d
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions gitlab.py
Expand Up @@ -46,6 +46,10 @@ class GitlabDeleteError(Exception):
pass


class GitlabProtectError(Exception):
pass


class GitlabAuthenticationError(Exception):
pass

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

return r

def rawPut(self, path, with_token=False):
url = '%s%s' % (self._url, path)
if with_token:
url += "?private_token=%s" % self.private_token

try:
r = requests.put(url)
except:
raise GitlabConnectionError(
"Can't connect to GitLab server (%s)" % self._url)

return r

def list(self, obj_class, **kwargs):
url = obj_class._url
if kwargs:
Expand Down Expand Up @@ -432,6 +449,24 @@ class ProjectBranch(GitlabObject):
canUpdate = False
canCreate = False

def protect(self, protect=True):
url = self._url % {'project_id': self.project_id}
if protect:
url = "%s/%s/protect" % (url, self.name)
else:
url = "%s/%s/unprotect" % (url, self.name)
r = self.gitlab.rawPut(url, True)

if r.status_code == 200:
if protect:
self.protected = protect
else:
del self.protected
else:
raise GitlabProtectError

def unprotect(self):
self.protect(False)

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

0 comments on commit a02180d

Please sign in to comment.