diff options
author | Nejc Habjan <hab.nejc@gmail.com> | 2021-02-07 00:29:59 +0100 |
---|---|---|
committer | Nejc Habjan <hab.nejc@gmail.com> | 2021-02-07 16:05:05 +0100 |
commit | a5a48ad08577be70c6ca511d3b4803624e5c2043 (patch) | |
tree | c1b80daad6dbb6ea3b1a3498d70016ef77abf6b3 /gitlab/v4/objects/branches.py | |
parent | 9d6c1882d567116e16484f3e0a1036da4967c537 (diff) | |
download | gitlab-a5a48ad08577be70c6ca511d3b4803624e5c2043.tar.gz |
refactor(v4): split objects and managers per API resource
Diffstat (limited to 'gitlab/v4/objects/branches.py')
-rw-r--r-- | gitlab/v4/objects/branches.py | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/gitlab/v4/objects/branches.py b/gitlab/v4/objects/branches.py new file mode 100644 index 0000000..c6ff1e8 --- /dev/null +++ b/gitlab/v4/objects/branches.py @@ -0,0 +1,80 @@ +from gitlab import cli +from gitlab import exceptions as exc +from gitlab.base import * # noqa +from gitlab.mixins import * # noqa + + +class ProjectBranch(ObjectDeleteMixin, RESTObject): + _id_attr = "name" + + @cli.register_custom_action( + "ProjectBranch", tuple(), ("developers_can_push", "developers_can_merge") + ) + @exc.on_http_error(exc.GitlabProtectError) + def protect(self, developers_can_push=False, developers_can_merge=False, **kwargs): + """Protect the branch. + + Args: + developers_can_push (bool): Set to True if developers are allowed + to push to the branch + developers_can_merge (bool): Set to True if developers are allowed + to merge to the branch + **kwargs: Extra options to send to the server (e.g. sudo) + + Raises: + GitlabAuthenticationError: If authentication is not correct + GitlabProtectError: If the branch could not be protected + """ + id = self.get_id().replace("/", "%2F") + path = "%s/%s/protect" % (self.manager.path, id) + post_data = { + "developers_can_push": developers_can_push, + "developers_can_merge": developers_can_merge, + } + self.manager.gitlab.http_put(path, post_data=post_data, **kwargs) + self._attrs["protected"] = True + + @cli.register_custom_action("ProjectBranch") + @exc.on_http_error(exc.GitlabProtectError) + def unprotect(self, **kwargs): + """Unprotect the branch. + + Args: + **kwargs: Extra options to send to the server (e.g. sudo) + + Raises: + GitlabAuthenticationError: If authentication is not correct + GitlabProtectError: If the branch could not be unprotected + """ + id = self.get_id().replace("/", "%2F") + path = "%s/%s/unprotect" % (self.manager.path, id) + self.manager.gitlab.http_put(path, **kwargs) + self._attrs["protected"] = False + + +class ProjectBranchManager(NoUpdateMixin, RESTManager): + _path = "/projects/%(project_id)s/repository/branches" + _obj_cls = ProjectBranch + _from_parent_attrs = {"project_id": "id"} + _create_attrs = (("branch", "ref"), tuple()) + + +class ProjectProtectedBranch(ObjectDeleteMixin, RESTObject): + _id_attr = "name" + + +class ProjectProtectedBranchManager(NoUpdateMixin, RESTManager): + _path = "/projects/%(project_id)s/protected_branches" + _obj_cls = ProjectProtectedBranch + _from_parent_attrs = {"project_id": "id"} + _create_attrs = ( + ("name",), + ( + "push_access_level", + "merge_access_level", + "unprotect_access_level", + "allowed_to_push", + "allowed_to_merge", + "allowed_to_unprotect", + ), + ) |