| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,8 +2,8 @@ | |||
| 2 | 2 | Protected branches | |
| 3 | 3 | ################## | |
| 4 | 4 | ||
| 5 | - You can define a list of protected branch names on a repository. Names can use | ||
| 6 | - wildcards (``*``). | ||
| 5 | + You can define a list of protected branch names on a repository or group. | ||
| 6 | + Names can use wildcards (``*``). | ||
| 7 | 7 | ||
| 8 | 8 | References | |
| 9 | 9 | ---------- | |
@@ -13,19 +13,24 @@ References | |||
| 13 | 13 | + :class:`gitlab.v4.objects.ProjectProtectedBranch` | |
| 14 | 14 | + :class:`gitlab.v4.objects.ProjectProtectedBranchManager` | |
| 15 | 15 | + :attr:`gitlab.v4.objects.Project.protectedbranches` | |
| 16 | + + :class:`gitlab.v4.objects.GroupProtectedBranch` | ||
| 17 | + + :class:`gitlab.v4.objects.GroupProtectedBranchManager` | ||
| 18 | + + :attr:`gitlab.v4.objects.Group.protectedbranches` | ||
| 16 | 19 | ||
| 17 | 20 | * GitLab API: https://docs.gitlab.com/api/protected_branches#protected-branches-api | |
| 18 | 21 | ||
| 19 | 22 | Examples | |
| 20 | 23 | -------- | |
| 21 | 24 | ||
| 22 | - Get the list of protected branches for a project:: | ||
| 25 | + Get the list of protected branches for a project or group:: | ||
| 23 | 26 | ||
| 24 | - p_branches = project.protectedbranches.list(get_all=True) | ||
| 27 | + p_branches = project.protectedbranches.list() | ||
| 28 | + p_branches = group.protectedbranches.list() | ||
| 25 | 29 | ||
| 26 | 30 | Get a single protected branch:: | |
| 27 | 31 | ||
| 28 | 32 | p_branch = project.protectedbranches.get('main') | |
| 33 | + p_branch = group.protectedbranches.get('main') | ||
| 29 | 34 | ||
| 30 | 35 | Update a protected branch:: | |
| 31 | 36 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -49,3 +49,27 @@ class ProjectProtectedBranchManager(CRUDMixin[ProjectProtectedBranch]): | |||
| 49 | 49 | ), | |
| 50 | 50 | ) | |
| 51 | 51 | _update_method = UpdateMethod.PATCH | |
| 52 | + | ||
| 53 | + | ||
| 54 | + class GroupProtectedBranch(SaveMixin, ObjectDeleteMixin, RESTObject): | ||
| 55 | + _id_attr = "name" | ||
| 56 | + | ||
| 57 | + | ||
| 58 | + class GroupProtectedBranchManager(CRUDMixin[GroupProtectedBranch]): | ||
| 59 | + _path = "/groups/{group_id}/protected_branches" | ||
| 60 | + _obj_cls = GroupProtectedBranch | ||
| 61 | + _from_parent_attrs = {"group_id": "id"} | ||
| 62 | + _create_attrs = RequiredOptional( | ||
| 63 | + required=("name",), | ||
| 64 | + optional=( | ||
| 65 | + "push_access_level", | ||
| 66 | + "merge_access_level", | ||
| 67 | + "unprotect_access_level", | ||
| 68 | + "allow_force_push", | ||
| 69 | + "allowed_to_push", | ||
| 70 | + "allowed_to_merge", | ||
| 71 | + "allowed_to_unprotect", | ||
| 72 | + "code_owner_approval_required", | ||
| 73 | + ), | ||
| 74 | + ) | ||
| 75 | + _update_method = UpdateMethod.PATCH | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,6 +24,7 @@ | |||
| 24 | 24 | from .audit_events import GroupAuditEventManager # noqa: F401 | |
| 25 | 25 | from .badges import GroupBadgeManager # noqa: F401 | |
| 26 | 26 | from .boards import GroupBoardManager # noqa: F401 | |
| 27 | + from .branches import GroupProtectedBranchManager # noqa: F401 | ||
| 27 | 28 | from .clusters import GroupClusterManager # noqa: F401 | |
| 28 | 29 | from .container_registry import GroupRegistryRepositoryManager # noqa: F401 | |
| 29 | 30 | from .custom_attributes import GroupCustomAttributeManager # noqa: F401 | |
@@ -102,6 +103,7 @@ class Group(SaveMixin, ObjectDeleteMixin, RESTObject): | |||
| 102 | 103 | packages: GroupPackageManager | |
| 103 | 104 | projects: GroupProjectManager | |
| 104 | 105 | shared_projects: SharedProjectManager | |
| 106 | + protectedbranches: GroupProtectedBranchManager | ||
| 105 | 107 | pushrules: GroupPushRulesManager | |
| 106 | 108 | registry_repositories: GroupRegistryRepositoryManager | |
| 107 | 109 | runners: GroupRunnerManager | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -312,6 +312,31 @@ def test_group_hooks(group): | |||
| 312 | 312 | hook.delete() | |
| 313 | 313 | ||
| 314 | 314 | ||
| 315 | + def test_group_protected_branches(group, gitlab_version): | ||
| 316 | + # Updating a protected branch at the group level is possible from Gitlab 15.9 | ||
| 317 | + # https://docs.gitlab.com/api/group_protected_branches/ | ||
| 318 | + can_update_prot_branch = gitlab_version.major > 15 or ( | ||
| 319 | + gitlab_version.major == 15 and gitlab_version.minor >= 9 | ||
| 320 | + ) | ||
| 321 | + | ||
| 322 | + p_b = group.protectedbranches.create( | ||
| 323 | + {"name": "*-stable", "allow_force_push": False} | ||
| 324 | + ) | ||
| 325 | + assert p_b.name == "*-stable" | ||
| 326 | + assert not p_b.allow_force_push | ||
| 327 | + assert p_b in group.protectedbranches.list() | ||
| 328 | + | ||
| 329 | + if can_update_prot_branch: | ||
| 330 | + p_b.allow_force_push = True | ||
| 331 | + p_b.save() | ||
| 332 | + | ||
| 333 | + p_b = group.protectedbranches.get("*-stable") | ||
| 334 | + if can_update_prot_branch: | ||
| 335 | + assert p_b.allow_force_push | ||
| 336 | + | ||
| 337 | + p_b.delete() | ||
| 338 | + | ||
| 339 | + | ||
| 315 | 340 | def test_group_transfer(gl, group): | |
| 316 | 341 | transfer_group = gl.groups.create( | |
| 317 | 342 | {"name": "transfer-test-group", "path": "transfer-test-group"} | |
| Back | FazBrowse Home | New Git URL |
0 commit comments