From 66430ea2ac96433abf6d03943338fa523659823b Mon Sep 17 00:00:00 2001 From: xakepnz Date: Fri, 11 Apr 2025 23:29:28 +1200 Subject: [PATCH] feat(groups): add protectedbranches to group class (#3164) --- docs/gl_objects/protected_branches.rst | 13 +++++++++---- gitlab/v4/objects/branches.py | 24 ++++++++++++++++++++++++ gitlab/v4/objects/groups.py | 2 ++ tests/functional/api/test_groups.py | 25 +++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 4 deletions(-) diff --git a/docs/gl_objects/protected_branches.rst b/docs/gl_objects/protected_branches.rst index ce5e300db..a1b1ef5c5 100644 --- a/docs/gl_objects/protected_branches.rst +++ b/docs/gl_objects/protected_branches.rst @@ -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 ---------- @@ -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:: diff --git a/gitlab/v4/objects/branches.py b/gitlab/v4/objects/branches.py index 0724476a6..12dbf8848 100644 --- a/gitlab/v4/objects/branches.py +++ b/gitlab/v4/objects/branches.py @@ -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 diff --git a/gitlab/v4/objects/groups.py b/gitlab/v4/objects/groups.py index 473b40391..7a1767817 100644 --- a/gitlab/v4/objects/groups.py +++ b/gitlab/v4/objects/groups.py @@ -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 @@ -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 diff --git a/tests/functional/api/test_groups.py b/tests/functional/api/test_groups.py index 2485ac660..301fea6a2 100644 --- a/tests/functional/api/test_groups.py +++ b/tests/functional/api/test_groups.py @@ -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"}