debian-mirror-gitlab/app/services/merge_requests/approval_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

50 lines
1.5 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
module MergeRequests
class ApprovalService < MergeRequests::BaseService
def execute(merge_request)
2022-11-25 23:54:43 +05:30
return unless eligible_for_approval?(merge_request)
2020-07-28 23:09:34 +05:30
approval = merge_request.approvals.new(user: current_user)
return success unless save_approval(approval)
reset_approvals_cache(merge_request)
2022-08-13 15:12:31 +05:30
merge_request_activity_counter.track_approve_mr_action(user: current_user, merge_request: merge_request)
2023-01-13 00:05:48 +05:30
trigger_merge_request_merge_status_updated(merge_request)
trigger_merge_request_reviewers_updated(merge_request)
2020-07-28 23:09:34 +05:30
2022-08-27 11:52:29 +05:30
# Approval side effects (things not required to be done immediately but
# should happen after a successful approval) should be done asynchronously
# utilizing the `Gitlab::EventStore`.
#
# Workers can subscribe to the `MergeRequests::ApprovedEvent`.
2022-10-11 01:57:18 +05:30
Gitlab::EventStore.publish(
MergeRequests::ApprovedEvent.new(
data: { current_user_id: current_user.id, merge_request_id: merge_request.id }
2022-08-27 11:52:29 +05:30
)
2022-10-11 01:57:18 +05:30
)
2022-08-27 11:52:29 +05:30
2020-07-28 23:09:34 +05:30
success
end
private
2022-11-25 23:54:43 +05:30
def eligible_for_approval?(merge_request)
merge_request.eligible_for_approval_by?(current_user)
2020-07-28 23:09:34 +05:30
end
2022-08-27 11:52:29 +05:30
def save_approval(approval)
Approval.safe_ensure_unique do
approval.save
end
end
2020-07-28 23:09:34 +05:30
def reset_approvals_cache(merge_request)
merge_request.approvals.reset
end
end
end
2021-06-08 01:23:25 +05:30
MergeRequests::ApprovalService.prepend_mod_with('MergeRequests::ApprovalService')