@@ -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
30633032class 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
30903094class ProjectMergeRequestAwardEmoji (ObjectDeleteMixin , RESTObject ):
30913095 pass
0 commit comments