Skip to content

Commit 85163f7

Browse files
committed
feat(api): allow updating protected branches
Closes #2390
1 parent 48726fd commit 85163f7

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

docs/gl_objects/protected_branches.rst

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ Get a single protected branch::
2727

2828
p_branch = project.protectedbranches.get('main')
2929

30+
Update a protected branch:
31+
32+
p_branch.allow_force_push = True
33+
p_branch.save()
34+
3035
Create a protected branch::
3136

3237
p_branch = project.protectedbranches.create({

gitlab/v4/objects/branches.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
from typing import Any, cast, Union
22

33
from gitlab.base import RESTManager, RESTObject
4-
from gitlab.mixins import NoUpdateMixin, ObjectDeleteMixin
4+
from gitlab.mixins import (
5+
CRUDMixin,
6+
NoUpdateMixin,
7+
ObjectDeleteMixin,
8+
SaveMixin,
9+
UpdateMethod,
10+
)
511
from gitlab.types import RequiredOptional
612

713
__all__ = [
@@ -28,11 +34,11 @@ def get(
2834
return cast(ProjectBranch, super().get(id=id, lazy=lazy, **kwargs))
2935

3036

31-
class ProjectProtectedBranch(ObjectDeleteMixin, RESTObject):
37+
class ProjectProtectedBranch(SaveMixin, ObjectDeleteMixin, RESTObject):
3238
_id_attr = "name"
3339

3440

35-
class ProjectProtectedBranchManager(NoUpdateMixin, RESTManager):
41+
class ProjectProtectedBranchManager(CRUDMixin, RESTManager):
3642
_path = "/projects/{project_id}/protected_branches"
3743
_obj_cls = ProjectProtectedBranch
3844
_from_parent_attrs = {"project_id": "id"}
@@ -49,6 +55,7 @@ class ProjectProtectedBranchManager(NoUpdateMixin, RESTManager):
4955
"code_owner_approval_required",
5056
),
5157
)
58+
_update_method = UpdateMethod.PATCH
5259

5360
def get(
5461
self, id: Union[str, int], lazy: bool = False, **kwargs: Any

tests/functional/api/test_projects.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,21 @@ def test_project_pages_domains(gl, project):
259259

260260

261261
def test_project_protected_branches(project):
262-
p_b = project.protectedbranches.create({"name": "*-stable"})
262+
p_b = project.protectedbranches.create(
263+
{
264+
"name": "*-stable",
265+
"allow_force_push": False,
266+
}
267+
)
263268
assert p_b.name == "*-stable"
269+
assert not p_b.allow_force_push
264270
assert p_b in project.protectedbranches.list()
265271

272+
p_b.allow_force_push = True
273+
p_b.save()
274+
266275
p_b = project.protectedbranches.get("*-stable")
276+
assert p_b.allow_force_push
267277
p_b.delete()
268278
assert p_b not in project.protectedbranches.list()
269279

0 commit comments

Comments
 (0)