import { BaseResource } from '@gitbeaker/requester-utils' ; import { BaseRequestOptions , endpoint , RequestHelper , Sudo } from '../infrastructure' ; import { UserSchema } from './Users' ; import { GroupSchema } from './Groups' ; import { ProtectedBranchSchema } from './ProtectedBranches' ; export interface ProjectLevelMergeRequestApprovalSchema extends Record < string , unknown > { approvals_before_merge : number ; reset_approvals_on_push : boolean ; disable_overriding_approvers_per_merge_request : boolean ; merge_requests_author_approval : boolean ; merge_requests_disable_committers_approval : boolean ; require_password_to_approve : boolean ; } export interface ApprovedByEntity { user : Pick < UserSchema , 'name' | 'username' | 'id' | 'state' | 'avatar_url' | 'web_url' > ; } export interface MergeRequestLevelMergeRequestApprovalSchema extends Record < string , unknown > { id : number ; iid : number ; project_id : number ; title : string ; description : string ; state : string ; created_at : string ; updated_at : string ; merge_status : string ; approvals_required : number ; approvals_left : number ; approved_by ? : ApprovedByEntity [ ] ; } export type ApprovalRulesRequestOptions = { userIds ? : number [ ] ; groupIds ? : number [ ] ; protectedBranchIds ? : number [ ] ; } ; export interface ApprovalRuleSchema extends Record < string , unknown > { id : number ; name : string ; rule_type : string ; eligible_approvers ? : Pick < UserSchema , 'name' | 'username' | 'id' | 'state' | 'avatar_url' | 'web_url' > [ ] ; approvals_required : number ; users ? : Pick < UserSchema , 'name' | 'username' | 'id' | 'state' | 'avatar_url' | 'web_url' > [ ] ; groups ? : GroupSchema [ ] ; contains_hidden_groups : boolean ; overridden : boolean ; } export interface ProjectLevelApprovalRuleSchema extends ApprovalRuleSchema { protected_branches ? : ProtectedBranchSchema [ ] ; } export interface MergeRequestLevelApprovalRuleSchema extends ApprovalRuleSchema { source_rule ? : string ; } export class MergeRequestApprovals < C extends boolean = false > extends BaseResource < C > { configuration ( projectId , options ? ) ; configuration ( projectId , options ) ; configuration ( projectId , { mergerequestIid , ... options } = { } ) { let url ; if ( mergerequestIid ) { url = endpoint `projects/${ projectId }/merge_requests/${ mergerequestIid }/approvals` ; } else { url = endpoint `projects/${ projectId }/approvals` ; } return RequestHelper . get ( ) ( this , url , options ) ; } editConfiguration ( projectId , options ? ) ; editConfiguration ( projectId , options ) ; editConfiguration ( projectId , { mergerequestIid , ... options } = { } ) { let url ; if ( mergerequestIid ) { url = endpoint `projects/${ projectId }/merge_requests/${ mergerequestIid }/approvals` ; } else { url = endpoint `projects/${ projectId }/approvals` ; } return RequestHelper . post ( ) ( this , url , options ) ; } approvalRule ( projectId , approvalRuleId , options ) { return RequestHelper . get ( ) ( this , endpoint `projects/${ projectId }/approval_rules/${ approvalRuleId }` , options ) ; } approvalRules ( projectId , options ? ) ; approvalRules ( projectId , options ) ; approvalRules ( projectId , { mergerequestIid , ... options } = { } ) { let url ; if ( mergerequestIid ) { url = endpoint `projects/${ projectId }/merge_requests/${ mergerequestIid }/approval_rules` ; } else { url = endpoint `projects/${ projectId }/approval_rules` ; } return RequestHelper . get ( ) ( this , url , options ) ; } addApprovalRule ( projectId , name , approvalsRequired , options ? ) ; addApprovalRule ( projectId , name , approvalsRequired , options ) ; addApprovalRule ( projectId , name , approvalsRequired , { mergerequestIid , ... options } = { } ) { let url ; if ( mergerequestIid ) { url = endpoint `projects/${ projectId }/merge_requests/${ mergerequestIid }/approval_rules` ; } else { url = endpoint `projects/${ projectId }/approval_rules` ; } return RequestHelper . post ( ) ( this , url , { name , approvalsRequired , ... options } ) ; } approvalState ( projectId , mergerequestIid , options ? ) { return RequestHelper . get ( ) ( this , endpoint `projects/${ projectId }/merge_requests/${ mergerequestIid }/approval_state` , options ) ; } editApprovalRule ( projectId , approvalRuleId , name , approvalsRequired , options ? ) ; editApprovalRule ( projectId , approvalRuleId , name , approvalsRequired , options ) ; editApprovalRule ( projectId , approvalRuleId , name , approvalsRequired , { mergerequestIid , ... options } = { } ) { let url ; if ( mergerequestIid ) { url = endpoint `projects/${ projectId }/merge_requests/${ mergerequestIid }/approval_rules/${ approvalRuleId }` ; } else { url = endpoint `projects/${ projectId }/approval_rules/${ approvalRuleId }` ; } return RequestHelper . put ( ) ( this , url , { name , approvalsRequired , ... options } ) ; } removeApprovalRule ( projectId , approvalRuleId , options ? ) ; removeApprovalRule ( projectId , approvalRuleId , options ) ; removeApprovalRule ( projectId , approvalRuleId , { mergerequestIid } = { } ) { let url ; if ( mergerequestIid ) { url = endpoint `projects/${ projectId }/merge_requests/${ mergerequestIid }/approval_rules/${ approvalRuleId }` ; } else { url = endpoint `projects/${ projectId }/approval_rules/${ approvalRuleId }` ; } return RequestHelper . del ( ) ( this , url ) ; } approve ( projectId , mergerequestIid , options ? ) { return RequestHelper . post < MergeRequestLevelMergeRequestApprovalSchema > ( ) ( this , endpoint `projects/${ projectId }/merge_requests/${ mergerequestIid }/approve` , options ) ; } unapprove ( projectId , mergerequestIid , options ? ) { return RequestHelper . post < void > ( ) ( this , endpoint `projects/${ projectId }/merge_requests/${ mergerequestIid }/unapprove` , options ) ; } }