@@ -2992,13 +2992,13 @@ class ProjectMergeRequestApprovalManager(GetWithoutIdMixin, UpdateMixin, RESTMan
2992
2992
2993
2993
@exc .on_http_error (exc .GitlabUpdateError )
2994
2994
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
2996
2996
):
2997
2997
"""Change MR-level allowed approvers and approver groups.
2998
2998
2999
2999
Args:
3000
3000
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
3002
3002
approver_group_ids (list): Group IDs whose members can approve MRs
3003
3003
3004
3004
Raises:
@@ -3013,14 +3013,79 @@ def set_approvers(
3013
3013
self ._parent .get_id (),
3014
3014
)
3015
3015
data = {
3016
- "name" : "name" ,
3016
+ "name" : approval_rule_name ,
3017
3017
"approvals_required" : approvals_required ,
3018
3018
"rule_type" : "regular" ,
3019
3019
"user_ids" : approver_ids ,
3020
3020
"group_ids" : approver_group_ids ,
3021
3021
}
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"
3023
3066
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" ))
3024
3089
3025
3090
class ProjectMergeRequestAwardEmoji (ObjectDeleteMixin , RESTObject ):
3026
3091
pass
@@ -3145,6 +3210,7 @@ class ProjectMergeRequest(
3145
3210
3146
3211
_managers = (
3147
3212
("approvals" , "ProjectMergeRequestApprovalManager" ),
3213
+ ("approval_rules" , "ProjectMergeRequestApprovalRuleManger" ),
3148
3214
("awardemojis" , "ProjectMergeRequestAwardEmojiManager" ),
3149
3215
("diffs" , "ProjectMergeRequestDiffManager" ),
3150
3216
("discussions" , "ProjectMergeRequestDiscussionManager" ),
0 commit comments