Skip to content

Commit

Permalink
fix(api): fix saving merge request approval rules
Browse files Browse the repository at this point in the history
Closes #2548
  • Loading branch information
Sjord authored and JohnVillalovos committed May 13, 2024
1 parent 4f338ae commit b8b3849
Showing 1 changed file with 4 additions and 56 deletions.
60 changes: 4 additions & 56 deletions gitlab/v4/objects/merge_request_approvals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, cast, Dict, List, Optional, TYPE_CHECKING, Union
from typing import Any, cast, List, Optional, TYPE_CHECKING, Union

from gitlab import exceptions as exc
from gitlab.base import RESTManager, RESTObject
Expand Down Expand Up @@ -132,42 +132,16 @@ def set_approvers(

class ProjectMergeRequestApprovalRule(SaveMixin, ObjectDeleteMixin, RESTObject):
_repr_attr = "name"
id: int
approval_rule_id: int
merge_request_iid: int

@exc.on_http_error(exc.GitlabUpdateError)
def save(self, **kwargs: Any) -> None:
"""Save the changes made to the object to the server.
The object is updated to match what the server returns.
Args:
**kwargs: Extra options to send to the server (e.g. sudo)
Raise:
GitlabAuthenticationError: If authentication is not correct
GitlabUpdateError: If the server cannot perform the request
"""
# There is a mismatch between the name of our id attribute and the put
# REST API name for the project_id, so we override it here.
self.approval_rule_id = self.id
self.merge_request_iid = self._parent_attrs["mr_iid"]
self.id = self._parent_attrs["project_id"]
# save will update self.id with the result from the server, so no need
# to overwrite with what it was before we overwrote it.
SaveMixin.save(self, **kwargs)


class ProjectMergeRequestApprovalRuleManager(CRUDMixin, RESTManager):
_path = "/projects/{project_id}/merge_requests/{mr_iid}/approval_rules"
_path = "/projects/{project_id}/merge_requests/{merge_request_iid}/approval_rules"
_obj_cls = ProjectMergeRequestApprovalRule
_from_parent_attrs = {"project_id": "project_id", "mr_iid": "iid"}
_from_parent_attrs = {"project_id": "project_id", "merge_request_iid": "iid"}
_update_attrs = RequiredOptional(
required=(
"id",
"merge_request_iid",
"approval_rule_id",
"name",
"approvals_required",
),
Expand All @@ -177,7 +151,7 @@ class ProjectMergeRequestApprovalRuleManager(CRUDMixin, RESTManager):
# groups of project-level rule will be copied. The approvals_required
# specified will be used.
_create_attrs = RequiredOptional(
required=("id", "merge_request_iid", "name", "approvals_required"),
required=("name", "approvals_required"),
optional=("approval_project_rule_id", "user_ids", "group_ids"),
)

Expand All @@ -188,32 +162,6 @@ def get(
ProjectMergeRequestApprovalRule, super().get(id=id, lazy=lazy, **kwargs)
)

def create(
self, data: Optional[Dict[str, Any]] = None, **kwargs: Any
) -> RESTObject:
"""Create a new object.
Args:
data: Parameters to send to the server to create the
resource
**kwargs: Extra options to send to the server (e.g. sudo or
'ref_name', 'stage', 'name', 'all')
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabCreateError: If the server cannot perform the request
Returns:
A new instance of the manage object class build with
the data sent by the server
"""
if TYPE_CHECKING:
assert data is not None
new_data = data.copy()
new_data["id"] = self._from_parent_attrs["project_id"]
new_data["merge_request_iid"] = self._from_parent_attrs["mr_iid"]
return CreateMixin.create(self, new_data, **kwargs)


class ProjectMergeRequestApprovalState(RESTObject):
pass
Expand Down

0 comments on commit b8b3849

Please sign in to comment.