Skip to content

Commit f71e391

Browse files
committed
cleanup debug code.
1 parent 7ad436e commit f71e391

File tree

3 files changed

+57
-60
lines changed

3 files changed

+57
-60
lines changed

gitlab/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,6 @@ def http_get(self, path, query_data=None, streamed=False, raw=False, **kwargs):
588588
GitlabParsingError: If the json data could not be parsed
589589
"""
590590
query_data = query_data or {}
591-
print(f"http_get with path {path}, {query_data}, {streamed}")
592591
result = self.http_request(
593592
"get", path, query_data=query_data, streamed=streamed, **kwargs
594593
)
@@ -710,8 +709,6 @@ def http_put(self, path, query_data=None, post_data=None, files=None, **kwargs):
710709
query_data = query_data or {}
711710
post_data = post_data or {}
712711

713-
print(f"put with {path} data {query_data}, {post_data}")
714-
715712
result = self.http_request(
716713
"put",
717714
path,

gitlab/mixins.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,14 +355,11 @@ class SaveMixin(object):
355355
def _get_updated_data(self):
356356
updated_data = {}
357357
required, optional = self.manager.get_update_attrs()
358-
print(f"required, optional : {required}, {optional}, {self.manager}")
359358
for attr in required:
360359
# Get everything required, no matter if it's been updated
361-
print(f"getattr {attr}")
362360
updated_data[attr] = getattr(self, attr)
363361
# Add the updated attributes
364362
updated_data.update(self._updated_attrs)
365-
print(f"updated_data is {updated_data}")
366363

367364
return updated_data
368365

@@ -387,7 +384,6 @@ def save(self, **kwargs):
387384
obj_id = self.get_id()
388385
server_data = self.manager.update(obj_id, updated_data, **kwargs)
389386
if server_data is not None:
390-
print(f"result is {server_data}")
391387
self._update_attrs(server_data)
392388

393389

gitlab/v4/objects.py

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3008,84 +3008,88 @@ def set_approvers(
30083008
approver_ids = approver_ids or []
30093009
approver_group_ids = approver_group_ids or []
30103010

3011-
path = "%s/%s/approval_rules" % (
3012-
self._parent.manager.path,
3013-
self._parent.get_id(),
3014-
)
30153011
data = {
30163012
"name": approval_rule_name,
30173013
"approvals_required": approvals_required,
30183014
"rule_type": "regular",
30193015
"user_ids": approver_ids,
30203016
"group_ids": approver_group_ids,
30213017
}
3022-
print(f" data for set approvals: {data}, path {path}")
3023-
3024-
approval_rules = self.gitlab.http_get(path)
3025-
print(f"got some rules! {approval_rules}")
3026-
# TODO: this list is working, but the filtering does not appear to be working
3027-
approval_rules = self._parent.approval_rules.list(name=approval_rule_name)
3028-
print(f"approval_rules list {approval_rules}")
3029-
for ar in approval_rules:
3030-
print(f"id {ar.id} name {ar.name}, dict {ar.__dict__}")
3018+
approval_rules = self._parent.approval_rules
3019+
""" update any existing approval rule matching the name"""
3020+
existing_approval_rules = approval_rules.list()
3021+
for ar in existing_approval_rules:
30313022
if ar.name == approval_rule_name:
3032-
print("FOUND IT")
3033-
ar.user_ids = approver_ids
3023+
ar.user_ids = data["user_ids"]
3024+
ar.approvals_required = data["approvals_required"]
3025+
ar.group_ids = data["group_ids"]
30343026
ar.save()
3035-
exit(0)
3036-
try:
3037-
self.gitlab.http_post(path, post_data=data, **kwargs)
3038-
except exc.GitlabHttpError as e:
3039-
name_exists = False
3040-
try:
3041-
message = e.response_body.decode()
3042-
name_exists = '{"message":{"name":["has already been taken"]}}' in message
3043-
if (name_exists):
3044-
""" in this case, we determine id of name and update that approval rule."""
3045-
print(self.__dict__)
3046-
approval_rule = self.get(self.list(name=approval_rule_name)[0])
3047-
try:
3048-
approval_rule.update(data)
3049-
except Exception as update_error:
3050-
print("Update failed")
3051-
raise update_error
3052-
except:
3053-
raise e
3054-
except Exception as e:
3055-
print("in generic handler")
3056-
print(e)
3057-
print(e.__dict__)
3058-
print(type(e))
3059-
raise e
3060-
print("done with post")
3027+
return
3028+
""" if there was no rule matching the rule name, create a new one"""
3029+
approval_rules.create(data=data)
30613030

30623031

30633032
class ProjectMergeRequestApprovalRule(SaveMixin, RESTObject):
30643033
_id_attr = "approval_rule_id"
30653034
_short_print_attr = "approval_rule"
30663035

3036+
@exc.on_http_error(exc.GitlabUpdateError)
30673037
def save(self, **kwargs):
3068-
""" There is a mismatch between the name of our attributes and the put REST API expected names,
3069-
we map them over here.
3070-
TODO: Am I missing something more elegant here?
3038+
"""Save the changes made to the object to the server.
3039+
3040+
The object is updated to match what the server returns.
3041+
3042+
Args:
3043+
**kwargs: Extra options to send to the server (e.g. sudo)
3044+
3045+
Raise:
3046+
GitlabAuthenticationError: If authentication is not correct
3047+
GitlabUpdateError: If the server cannot perform the request
30713048
"""
3072-
orig_id = self.id
3073-
self.approval_rule_id = self.id
3074-
self.merge_request_iid = self._parent_attrs["mr_iid"]
3075-
self.id = self._parent_attrs["project_id"]
3076-
super().save(**kwargs)
3077-
""" save will update self.id with the result from the server, so no need to overwrite with
3078-
what it was before we hacked it."""
3049+
#There is a mismatch between the name of our id attribute and the put REST API name for the
3050+
#project_id, so we override it here.
3051+
self.approval_rule_id = self.id
3052+
self.merge_request_iid = self._parent_attrs["mr_iid"]
3053+
self.id = self._parent_attrs["project_id"]
3054+
#save will update self.id with the result from the server, so no need to overwrite with
3055+
#what it was before we overwrote it."""
3056+
SaveMixin.save(self, **kwargs)
30793057

3080-
30813058

3082-
class ProjectMergeRequestApprovalRuleManger(ListMixin, UpdateMixin, RESTManager):
3059+
class ProjectMergeRequestApprovalRuleManger(ListMixin, UpdateMixin, CreateMixin, RESTManager):
30833060
_path = "/projects/%(project_id)s/merge_requests/%(mr_iid)s/approval_rules"
30843061
_obj_cls = ProjectMergeRequestApprovalRule
30853062
_from_parent_attrs = {"project_id": "project_id", "mr_iid": "iid"}
30863063
_list_filters = ("name","rule_type")
30873064
_update_attrs = (("id","merge_request_iid","approval_rule_id","name","approvals_required"),
30883065
("user_ids","group_ids"))
3066+
#Important: When approval_project_rule_id is set, the name, users and groups of
3067+
#project-level rule will be copied. The approvals_required specified will be used. """
3068+
_create_attrs = (("id","merge_request_iid","name","approvals_required"),
3069+
("approval_project_rule_id", "user_ids","group_ids"))
3070+
3071+
def create(self, data, **kwargs):
3072+
"""Create a new object.
3073+
3074+
Args:
3075+
data (dict): Parameters to send to the server to create the
3076+
resource
3077+
**kwargs: Extra options to send to the server (e.g. sudo or
3078+
'ref_name', 'stage', 'name', 'all')
3079+
3080+
Raises:
3081+
GitlabAuthenticationError: If authentication is not correct
3082+
GitlabCreateError: If the server cannot perform the request
3083+
3084+
Returns:
3085+
RESTObject: A new instance of the manage object class build with
3086+
the data sent by the server
3087+
"""
3088+
new_data = data.copy()
3089+
new_data["id"] = self._from_parent_attrs["project_id"]
3090+
new_data["merge_request_iid"] = self._from_parent_attrs["mr_iid"]
3091+
return CreateMixin.create(self, new_data, **kwargs)
3092+
30893093

30903094
class ProjectMergeRequestAwardEmoji(ObjectDeleteMixin, RESTObject):
30913095
pass

0 commit comments

Comments
 (0)