@@ -2992,13 +2992,13 @@ class ProjectMergeRequestApprovalManager(GetWithoutIdMixin, UpdateMixin, RESTMan
29922992
29932993 @exc .on_http_error (exc .GitlabUpdateError )
29942994 def set_approvers (
2995- self , approvals_required , approver_ids = None , approver_group_ids = None , ** kwargs
2995+ self , approvals_required , approver_ids = None , approver_group_ids = None , approval_rule_name = "name" , ** kwargs
29962996 ):
29972997 """Change MR-level allowed approvers and approver groups.
29982998
29992999 Args:
30003000 approvals_required (integer): The number of required approvals for this rule
3001- approver_ids (list): User IDs that can approve MRs
3001+ approver_ids (list of integers ): User IDs that can approve MRs
30023002 approver_group_ids (list): Group IDs whose members can approve MRs
30033003
30043004 Raises:
@@ -3013,14 +3013,79 @@ def set_approvers(
30133013 self ._parent .get_id (),
30143014 )
30153015 data = {
3016- "name" : "name" ,
3016+ "name" : approval_rule_name ,
30173017 "approvals_required" : approvals_required ,
30183018 "rule_type" : "regular" ,
30193019 "user_ids" : approver_ids ,
30203020 "group_ids" : approver_group_ids ,
30213021 }
3022- self .gitlab .http_post (path , post_data = data , ** kwargs )
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__ } " )
3031+ if ar .name == approval_rule_name :
3032+ print ("FOUND IT" )
3033+ ar .user_ids = approver_ids
3034+ 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" )
3061+
3062+
3063+ class ProjectMergeRequestApprovalRule (SaveMixin , RESTObject ):
3064+ _id_attr = "approval_rule_id"
3065+ _short_print_attr = "approval_rule"
30233066
3067+ 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?
3071+ """
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."""
3079+
3080+
3081+
3082+ class ProjectMergeRequestApprovalRuleManger (ListMixin , UpdateMixin , RESTManager ):
3083+ _path = "/projects/%(project_id)s/merge_requests/%(mr_iid)s/approval_rules"
3084+ _obj_cls = ProjectMergeRequestApprovalRule
3085+ _from_parent_attrs = {"project_id" : "project_id" , "mr_iid" : "iid" }
3086+ _list_filters = ("name" ,"rule_type" )
3087+ _update_attrs = (("id" ,"merge_request_iid" ,"approval_rule_id" ,"name" ,"approvals_required" ),
3088+ ("user_ids" ,"group_ids" ))
30243089
30253090class ProjectMergeRequestAwardEmoji (ObjectDeleteMixin , RESTObject ):
30263091 pass
@@ -3145,6 +3210,7 @@ class ProjectMergeRequest(
31453210
31463211 _managers = (
31473212 ("approvals" , "ProjectMergeRequestApprovalManager" ),
3213+ ("approval_rules" , "ProjectMergeRequestApprovalRuleManger" ),
31483214 ("awardemojis" , "ProjectMergeRequestAwardEmojiManager" ),
31493215 ("diffs" , "ProjectMergeRequestDiffManager" ),
31503216 ("discussions" , "ProjectMergeRequestDiscussionManager" ),
0 commit comments