Skip to content

feat(api): allow updating protected branches #2771

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 3 commits into from
Feb 10, 2024
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
5 changes: 5 additions & 0 deletions docs/gl_objects/protected_branches.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ Get a single protected branch::

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

Update a protected branch:

p_branch.allow_force_push = True
p_branch.save()

Create a protected branch::

p_branch = project.protectedbranches.create({
Expand Down
13 changes: 10 additions & 3 deletions gitlab/v4/objects/branches.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from typing import Any, cast, Union

from gitlab.base import RESTManager, RESTObject
from gitlab.mixins import NoUpdateMixin, ObjectDeleteMixin
from gitlab.mixins import (
CRUDMixin,
NoUpdateMixin,
ObjectDeleteMixin,
SaveMixin,
UpdateMethod,
)
from gitlab.types import RequiredOptional

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


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


class ProjectProtectedBranchManager(NoUpdateMixin, RESTManager):
class ProjectProtectedBranchManager(CRUDMixin, RESTManager):
_path = "/projects/{project_id}/protected_branches"
_obj_cls = ProjectProtectedBranch
_from_parent_attrs = {"project_id": "id"}
Expand All @@ -49,6 +55,7 @@ class ProjectProtectedBranchManager(NoUpdateMixin, RESTManager):
"code_owner_approval_required",
),
)
_update_method = UpdateMethod.PATCH

def get(
self, id: Union[str, int], lazy: bool = False, **kwargs: Any
Expand Down
23 changes: 21 additions & 2 deletions tests/functional/api/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,31 @@ def test_project_pages_domains(gl, project):
assert domain not in project.pagesdomains.list()


def test_project_protected_branches(project):
p_b = project.protectedbranches.create({"name": "*-stable"})
def test_project_protected_branches(project, wait_for_sidekiq, gitlab_version):
# Updating a protected branch is possible from Gitlab 15.6
# https://docs.gitlab.com/ee/api/protected_branches.html#update-a-protected-branch
can_update_prot_branch = gitlab_version.major > 15 or (
gitlab_version.major == 15 and gitlab_version.minor >= 6
)

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

if can_update_prot_branch:
p_b.allow_force_push = True
p_b.save()
wait_for_sidekiq(timeout=60)

p_b = project.protectedbranches.get("*-stable")
if can_update_prot_branch:
assert p_b.allow_force_push
p_b.delete()
assert p_b not in project.protectedbranches.list()

Expand Down