Skip to content

Commit a867c48

Browse files
authored
feat(api): allow updating protected branches (#2771)
* feat(api): allow updating protected branches Closes #2390
1 parent 9440a32 commit a867c48

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
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

+21-2
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,31 @@ def test_project_pages_domains(gl, project):
258258
assert domain not in project.pagesdomains.list()
259259

260260

261-
def test_project_protected_branches(project):
262-
p_b = project.protectedbranches.create({"name": "*-stable"})
261+
def test_project_protected_branches(project, wait_for_sidekiq, gitlab_version):
262+
# Updating a protected branch is possible from Gitlab 15.6
263+
# https://docs.gitlab.com/ee/api/protected_branches.html#update-a-protected-branch
264+
can_update_prot_branch = gitlab_version.major > 15 or (
265+
gitlab_version.major == 15 and gitlab_version.minor >= 6
266+
)
267+
268+
p_b = project.protectedbranches.create(
269+
{
270+
"name": "*-stable",
271+
"allow_force_push": False,
272+
}
273+
)
263274
assert p_b.name == "*-stable"
275+
assert not p_b.allow_force_push
264276
assert p_b in project.protectedbranches.list()
265277

278+
if can_update_prot_branch:
279+
p_b.allow_force_push = True
280+
p_b.save()
281+
wait_for_sidekiq(timeout=60)
282+
266283
p_b = project.protectedbranches.get("*-stable")
284+
if can_update_prot_branch:
285+
assert p_b.allow_force_push
267286
p_b.delete()
268287
assert p_b not in project.protectedbranches.list()
269288

0 commit comments

Comments
 (0)