Skip to content

Commit 7073a2d

Browse files
JohnVillalovosnejch
authored andcommitted
chore: change _update_uses to _update_method and use an Enum
Change the name of the `_update_uses` attribute to `_update_method` and store an Enum in the attribute to indicate which type of HTTP method to use. At the moment it supports `POST` and `PUT`. But can in the future support `PATCH`.
1 parent b22d662 commit 7073a2d

File tree

5 files changed

+30
-14
lines changed

5 files changed

+30
-14
lines changed

gitlab/mixins.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import enum
12
from types import ModuleType
23
from typing import (
34
Any,
@@ -304,14 +305,20 @@ def create(
304305
return self._obj_cls(self, server_data)
305306

306307

308+
@enum.unique
309+
class UpdateMethod(enum.IntEnum):
310+
PUT = 1
311+
POST = 2
312+
313+
307314
class UpdateMixin(_RestManagerBase):
308315
_computed_path: Optional[str]
309316
_from_parent_attrs: Dict[str, Any]
310317
_obj_cls: Optional[Type[base.RESTObject]]
311318
_parent: Optional[base.RESTObject]
312319
_parent_attrs: Dict[str, Any]
313320
_path: Optional[str]
314-
_update_uses_post: bool = False
321+
_update_method: UpdateMethod = UpdateMethod.PUT
315322
gitlab: gitlab.Gitlab
316323

317324
def _get_update_method(
@@ -322,7 +329,7 @@ def _get_update_method(
322329
Returns:
323330
http_put (default) or http_post
324331
"""
325-
if self._update_uses_post:
332+
if self._update_method is UpdateMethod.POST:
326333
http_method = self.gitlab.http_post
327334
else:
328335
http_method = self.gitlab.http_put
@@ -894,7 +901,7 @@ class PromoteMixin(_RestObjectBase):
894901
_module: ModuleType
895902
_parent_attrs: Dict[str, Any]
896903
_updated_attrs: Dict[str, Any]
897-
_update_uses_post: bool = False
904+
_update_method: UpdateMethod = UpdateMethod.PUT
898905
manager: base.RESTManager
899906

900907
def _get_update_method(
@@ -905,7 +912,7 @@ def _get_update_method(
905912
Returns:
906913
http_put (default) or http_post
907914
"""
908-
if self._update_uses_post:
915+
if self._update_method is UpdateMethod.POST:
909916
http_method = self.manager.gitlab.http_post
910917
else:
911918
http_method = self.manager.gitlab.http_put

gitlab/v4/objects/merge_request_approvals.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
ListMixin,
1111
ObjectDeleteMixin,
1212
SaveMixin,
13+
UpdateMethod,
1314
UpdateMixin,
1415
)
1516
from gitlab.types import RequiredOptional
@@ -45,7 +46,7 @@ class ProjectApprovalManager(GetWithoutIdMixin, UpdateMixin, RESTManager):
4546
"merge_requests_disable_committers_approval",
4647
),
4748
)
48-
_update_uses_post = True
49+
_update_method = UpdateMethod.POST
4950

5051
def get(self, **kwargs: Any) -> ProjectApproval:
5152
return cast(ProjectApproval, super().get(**kwargs))
@@ -76,7 +77,7 @@ class ProjectMergeRequestApprovalManager(GetWithoutIdMixin, UpdateMixin, RESTMan
7677
_obj_cls = ProjectMergeRequestApproval
7778
_from_parent_attrs = {"project_id": "project_id", "mr_iid": "iid"}
7879
_update_attrs = RequiredOptional(required=("approvals_required",))
79-
_update_uses_post = True
80+
_update_method = UpdateMethod.POST
8081

8182
def get(self, **kwargs: Any) -> ProjectMergeRequestApproval:
8283
return cast(ProjectMergeRequestApproval, super().get(**kwargs))

gitlab/v4/objects/milestones.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
from gitlab import exceptions as exc
55
from gitlab import types
66
from gitlab.base import RESTManager, RESTObject, RESTObjectList
7-
from gitlab.mixins import CRUDMixin, ObjectDeleteMixin, PromoteMixin, SaveMixin
7+
from gitlab.mixins import (
8+
CRUDMixin,
9+
ObjectDeleteMixin,
10+
PromoteMixin,
11+
SaveMixin,
12+
UpdateMethod,
13+
)
814
from gitlab.types import RequiredOptional
915

1016
from .issues import GroupIssue, GroupIssueManager, ProjectIssue, ProjectIssueManager
@@ -100,7 +106,7 @@ def get(
100106

101107
class ProjectMilestone(PromoteMixin, SaveMixin, ObjectDeleteMixin, RESTObject):
102108
_repr_attr = "title"
103-
_update_uses_post = True
109+
_update_method = UpdateMethod.POST
104110

105111
@cli.register_custom_action("ProjectMilestone")
106112
@exc.on_http_error(exc.GitlabListError)

tests/unit/mixins/test_mixin_methods.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
RefreshMixin,
1414
SaveMixin,
1515
SetMixin,
16+
UpdateMethod,
1617
UpdateMixin,
1718
)
1819

@@ -377,7 +378,7 @@ class M(UpdateMixin, FakeManager):
377378
@responses.activate
378379
def test_update_mixin_uses_post(gl):
379380
class M(UpdateMixin, FakeManager):
380-
_update_uses_post = True
381+
_update_method = UpdateMethod.POST
381382

382383
url = "http://localhost/api/v4/tests/1"
383384
responses.add(

tests/unit/objects/test_project_merge_request_approvals.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import responses
99

1010
import gitlab
11+
from gitlab.mixins import UpdateMethod
1112

1213
approval_rule_id = 1
1314
approval_rule_name = "security"
@@ -157,15 +158,15 @@ def resp_delete_mr_approval_rule():
157158
yield rsps
158159

159160

160-
def test_project_approval_manager_update_uses_post(project):
161+
def test_project_approval_manager_update_method_post(project):
161162
"""Ensure the
162163
gitlab.v4.objects.merge_request_approvals.ProjectApprovalManager object has
163-
_update_uses_post set to True"""
164+
_update_method set to UpdateMethod.POST"""
164165
approvals = project.approvals
165166
assert isinstance(
166167
approvals, gitlab.v4.objects.merge_request_approvals.ProjectApprovalManager
167168
)
168-
assert approvals._update_uses_post is True
169+
assert approvals._update_method is UpdateMethod.POST
169170

170171

171172
def test_list_merge_request_approval_rules(project, resp_mr_approval_rules):
@@ -187,7 +188,7 @@ def test_update_merge_request_approvals_set_approvers(project, resp_mr_approval_
187188
approvals,
188189
gitlab.v4.objects.merge_request_approvals.ProjectMergeRequestApprovalManager,
189190
)
190-
assert approvals._update_uses_post is True
191+
assert approvals._update_method is UpdateMethod.POST
191192
response = approvals.set_approvers(
192193
updated_approval_rule_approvals_required,
193194
approver_ids=updated_approval_rule_user_ids,
@@ -207,7 +208,7 @@ def test_create_merge_request_approvals_set_approvers(project, resp_mr_approval_
207208
approvals,
208209
gitlab.v4.objects.merge_request_approvals.ProjectMergeRequestApprovalManager,
209210
)
210-
assert approvals._update_uses_post is True
211+
assert approvals._update_method is UpdateMethod.POST
211212
response = approvals.set_approvers(
212213
new_approval_rule_approvals_required,
213214
approver_ids=new_approval_rule_user_ids,

0 commit comments

Comments
 (0)