2020-07-07 08:09:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module MergeRequests
|
|
|
|
class RemoveApprovalService < MergeRequests::BaseService
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def execute(merge_request)
|
2020-07-16 11:09:38 -04:00
|
|
|
return unless merge_request.approved_by?(current_user)
|
2020-07-10 14:09:45 -04:00
|
|
|
|
2020-07-07 08:09:16 -04:00
|
|
|
# paranoid protection against running wrong deletes
|
|
|
|
return unless merge_request.id && current_user.id
|
|
|
|
|
|
|
|
approval = merge_request.approvals.where(user: current_user)
|
|
|
|
|
|
|
|
trigger_approval_hooks(merge_request) do
|
|
|
|
next unless approval.destroy_all # rubocop: disable Cop/DestroyAll
|
|
|
|
|
|
|
|
reset_approvals_cache(merge_request)
|
|
|
|
create_note(merge_request)
|
2021-02-08 16:09:25 -05:00
|
|
|
merge_request_activity_counter.track_unapprove_mr_action(user: current_user)
|
2020-07-07 08:09:16 -04:00
|
|
|
end
|
2020-07-10 14:09:45 -04:00
|
|
|
|
|
|
|
success
|
2020-07-07 08:09:16 -04:00
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def reset_approvals_cache(merge_request)
|
|
|
|
merge_request.approvals.reset
|
|
|
|
end
|
|
|
|
|
|
|
|
def trigger_approval_hooks(merge_request)
|
|
|
|
yield
|
|
|
|
|
|
|
|
execute_hooks(merge_request, 'unapproved')
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_note(merge_request)
|
|
|
|
SystemNoteService.unapprove_mr(merge_request, current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
MergeRequests::RemoveApprovalService.prepend_mod_with('MergeRequests::RemoveApprovalService')
|