2021-12-11 22:18:48 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module MergeRequests
|
|
|
|
class ToggleAttentionRequestedService < MergeRequests::BaseService
|
|
|
|
attr_accessor :merge_request, :user
|
|
|
|
|
|
|
|
def initialize(project:, current_user:, merge_request:, user:)
|
|
|
|
super(project: project, current_user: current_user)
|
|
|
|
|
|
|
|
@merge_request = merge_request
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
return error("Invalid permissions") unless can?(current_user, :update_merge_request, merge_request)
|
|
|
|
|
|
|
|
if reviewer || assignee
|
|
|
|
update_state(reviewer)
|
|
|
|
update_state(assignee)
|
|
|
|
|
|
|
|
if reviewer&.attention_requested? || assignee&.attention_requested?
|
2022-01-26 12:08:38 +05:30
|
|
|
create_attention_request_note
|
2021-12-11 22:18:48 +05:30
|
|
|
notity_user
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
if current_user.id != user.id
|
|
|
|
remove_attention_requested(merge_request, current_user)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
create_remove_attention_request_note
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
success
|
|
|
|
else
|
|
|
|
error("User is not a reviewer or assignee of the merge request")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def notity_user
|
2022-01-26 12:08:38 +05:30
|
|
|
notification_service.async.attention_requested_of_merge_request(merge_request, current_user, user)
|
2021-12-11 22:18:48 +05:30
|
|
|
todo_service.create_attention_requested_todo(merge_request, current_user, user)
|
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
def create_attention_request_note
|
|
|
|
SystemNoteService.request_attention(merge_request, merge_request.project, current_user, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_remove_attention_request_note
|
|
|
|
SystemNoteService.remove_attention_request(merge_request, merge_request.project, current_user, user)
|
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
def assignee
|
|
|
|
merge_request.find_assignee(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reviewer
|
|
|
|
merge_request.find_reviewer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_state(reviewer_or_assignee)
|
|
|
|
reviewer_or_assignee&.update(state: reviewer_or_assignee&.attention_requested? ? :reviewed : :attention_requested)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|