Skip to content

feat: add protectedbranches to group class (#3164) #3170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions docs/gl_objects/protected_branches.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Protected branches
##################

You can define a list of protected branch names on a repository. Names can use
wildcards (``*``).
You can define a list of protected branch names on a repository or group.
Names can use wildcards (``*``).

References
----------
Expand All @@ -13,19 +13,24 @@ References
+ :class:`gitlab.v4.objects.ProjectProtectedBranch`
+ :class:`gitlab.v4.objects.ProjectProtectedBranchManager`
+ :attr:`gitlab.v4.objects.Project.protectedbranches`
+ :class:`gitlab.v4.objects.GroupProtectedBranch`
+ :class:`gitlab.v4.objects.GroupProtectedBranchManager`
+ :attr:`gitlab.v4.objects.Group.protectedbranches`

* GitLab API: https://docs.gitlab.com/api/protected_branches#protected-branches-api

Examples
--------

Get the list of protected branches for a project::
Get the list of protected branches for a project or group::

p_branches = project.protectedbranches.list(get_all=True)
p_branches = project.protectedbranches.list()
p_branches = group.protectedbranches.list()

Get a single protected branch::

p_branch = project.protectedbranches.get('main')
p_branch = group.protectedbranches.get('main')

Update a protected branch::

Expand Down
24 changes: 24 additions & 0 deletions gitlab/v4/objects/branches.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,27 @@ class ProjectProtectedBranchManager(CRUDMixin[ProjectProtectedBranch]):
),
)
_update_method = UpdateMethod.PATCH


class GroupProtectedBranch(SaveMixin, ObjectDeleteMixin, RESTObject):
_id_attr = "name"


class GroupProtectedBranchManager(CRUDMixin[GroupProtectedBranch]):
_path = "/groups/{group_id}/protected_branches"
_obj_cls = GroupProtectedBranch
_from_parent_attrs = {"group_id": "id"}
_create_attrs = RequiredOptional(
required=("name",),
optional=(
"push_access_level",
"merge_access_level",
"unprotect_access_level",
"allow_force_push",
"allowed_to_push",
"allowed_to_merge",
"allowed_to_unprotect",
"code_owner_approval_required",
),
)
_update_method = UpdateMethod.PATCH
2 changes: 2 additions & 0 deletions gitlab/v4/objects/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from .audit_events import GroupAuditEventManager # noqa: F401
from .badges import GroupBadgeManager # noqa: F401
from .boards import GroupBoardManager # noqa: F401
from .branches import GroupProtectedBranchManager # noqa: F401
from .clusters import GroupClusterManager # noqa: F401
from .container_registry import GroupRegistryRepositoryManager # noqa: F401
from .custom_attributes import GroupCustomAttributeManager # noqa: F401
Expand Down Expand Up @@ -102,6 +103,7 @@ class Group(SaveMixin, ObjectDeleteMixin, RESTObject):
packages: GroupPackageManager
projects: GroupProjectManager
shared_projects: SharedProjectManager
protectedbranches: GroupProtectedBranchManager
pushrules: GroupPushRulesManager
registry_repositories: GroupRegistryRepositoryManager
runners: GroupRunnerManager
Expand Down
25 changes: 25 additions & 0 deletions tests/functional/api/test_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,31 @@ def test_group_hooks(group):
hook.delete()


def test_group_protected_branches(group, gitlab_version):
# Updating a protected branch at the group level is possible from Gitlab 15.9
# https://docs.gitlab.com/api/group_protected_branches/
can_update_prot_branch = gitlab_version.major > 15 or (
gitlab_version.major == 15 and gitlab_version.minor >= 9
)

p_b = group.protectedbranches.create(
{"name": "*-stable", "allow_force_push": False}
)
assert p_b.name == "*-stable"
assert not p_b.allow_force_push
assert p_b in group.protectedbranches.list()

if can_update_prot_branch:
p_b.allow_force_push = True
p_b.save()

p_b = group.protectedbranches.get("*-stable")
if can_update_prot_branch:
assert p_b.allow_force_push

p_b.delete()


def test_group_transfer(gl, group):
transfer_group = gl.groups.create(
{"name": "transfer-test-group", "path": "transfer-test-group"}
Expand Down