2018-07-16 12:31:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-04-02 06:54:41 -04:00
|
|
|
module Issues
|
2014-04-02 12:55:23 -04:00
|
|
|
class UpdateService < Issues::BaseService
|
2021-06-21 08:07:45 -04:00
|
|
|
# NOTE: For Issues::UpdateService, we default the spam_params to nil, because spam_checking is not
|
|
|
|
# necessary in many cases, and we don't want to require every caller to explicitly pass it as nil
|
|
|
|
# to disable spam checking.
|
|
|
|
def initialize(project:, current_user: nil, params: {}, spam_params: nil)
|
|
|
|
super(project: project, current_user: current_user, params: params)
|
|
|
|
@spam_params = spam_params
|
|
|
|
end
|
|
|
|
|
2014-04-02 06:54:41 -04:00
|
|
|
def execute(issue)
|
2017-08-28 17:56:49 -04:00
|
|
|
handle_move_between_ids(issue)
|
2021-01-27 04:09:01 -05:00
|
|
|
|
2017-07-20 10:42:33 -04:00
|
|
|
change_issue_duplicate(issue)
|
2020-12-03 16:09:35 -05:00
|
|
|
move_issue_to_new_project(issue) || clone_issue(issue) || update_task_event(issue) || update(issue)
|
2014-04-02 06:54:41 -04:00
|
|
|
end
|
2015-11-12 18:13:45 -05:00
|
|
|
|
2018-10-19 03:16:58 -04:00
|
|
|
def update(issue)
|
|
|
|
create_merge_request_from_quick_action
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2019-07-09 11:21:10 -04:00
|
|
|
def before_update(issue, skip_spam_check: false)
|
2021-09-10 11:11:12 -04:00
|
|
|
change_work_item_type(issue)
|
|
|
|
|
2021-01-27 04:09:01 -05:00
|
|
|
return if skip_spam_check
|
|
|
|
|
|
|
|
Spam::SpamActionService.new(
|
|
|
|
spammable: issue,
|
2021-06-21 08:07:45 -04:00
|
|
|
spam_params: spam_params,
|
2021-01-27 04:09:01 -05:00
|
|
|
user: current_user,
|
|
|
|
action: :update
|
2021-06-21 08:07:45 -04:00
|
|
|
).execute
|
2017-02-14 14:07:11 -05:00
|
|
|
end
|
|
|
|
|
2021-09-10 11:11:12 -04:00
|
|
|
def change_work_item_type(issue)
|
|
|
|
return unless issue.changed_attributes['issue_type']
|
|
|
|
|
|
|
|
type_id = find_work_item_type_id(issue.issue_type)
|
|
|
|
|
|
|
|
issue.work_item_type_id = type_id
|
|
|
|
end
|
|
|
|
|
2017-05-04 08:11:15 -04:00
|
|
|
def handle_changes(issue, options)
|
2021-05-25 05:10:54 -04:00
|
|
|
super
|
2017-11-21 12:13:07 -05:00
|
|
|
old_associations = options.fetch(:old_associations, {})
|
|
|
|
old_labels = old_associations.fetch(:labels, [])
|
|
|
|
old_mentioned_users = old_associations.fetch(:mentioned_users, [])
|
|
|
|
old_assignees = old_associations.fetch(:assignees, [])
|
2021-07-01 14:07:29 -04:00
|
|
|
old_severity = old_associations[:severity]
|
2017-05-04 08:11:15 -04:00
|
|
|
|
|
|
|
if has_changes?(issue, old_labels: old_labels, old_assignees: old_assignees)
|
2020-05-28 14:08:37 -04:00
|
|
|
todo_service.resolve_todos_for_target(issue, current_user)
|
2016-02-18 16:12:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if issue.previous_changes.include?('title') ||
|
2016-12-15 17:14:20 -05:00
|
|
|
issue.previous_changes.include?('description')
|
2017-03-28 02:25:43 -04:00
|
|
|
todo_service.update_issue(issue, current_user, old_mentioned_users)
|
2016-02-15 21:26:33 -05:00
|
|
|
end
|
|
|
|
|
2021-04-23 05:10:03 -04:00
|
|
|
handle_assignee_changes(issue, old_assignees)
|
2021-08-23 05:10:23 -04:00
|
|
|
handle_confidential_change(issue)
|
2022-04-01 14:08:46 -04:00
|
|
|
handle_label_changes(issue, old_labels)
|
2021-08-23 05:10:23 -04:00
|
|
|
handle_added_labels(issue, old_labels)
|
2018-11-02 12:29:32 -04:00
|
|
|
handle_milestone_change(issue)
|
2021-08-23 05:10:23 -04:00
|
|
|
handle_added_mentions(issue, old_mentioned_users)
|
2021-07-01 14:07:29 -04:00
|
|
|
handle_severity_change(issue, old_severity)
|
2022-02-22 22:17:13 -05:00
|
|
|
handle_escalation_status_change(issue)
|
2021-08-19 14:10:32 -04:00
|
|
|
handle_issue_type_change(issue)
|
2015-11-12 18:13:45 -05:00
|
|
|
end
|
2015-11-17 06:42:43 -05:00
|
|
|
|
2021-04-23 05:10:03 -04:00
|
|
|
def handle_assignee_changes(issue, old_assignees)
|
|
|
|
return if issue.assignees == old_assignees
|
|
|
|
|
|
|
|
create_assignee_note(issue, old_assignees)
|
|
|
|
notification_service.async.reassigned_issue(issue, current_user, old_assignees)
|
|
|
|
todo_service.reassigned_assignable(issue, current_user, old_assignees)
|
|
|
|
track_incident_action(current_user, issue, :incident_assigned)
|
|
|
|
|
2022-03-09 01:07:38 -05:00
|
|
|
GraphqlTriggers.issuable_assignees_updated(issue)
|
2021-04-23 05:10:03 -04:00
|
|
|
end
|
|
|
|
|
2019-01-22 19:05:38 -05:00
|
|
|
def handle_task_changes(issuable)
|
2020-05-28 14:08:37 -04:00
|
|
|
todo_service.resolve_todos_for_target(issuable, current_user)
|
2019-01-22 19:05:38 -05:00
|
|
|
todo_service.update_issue(issuable, current_user)
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-07-20 10:42:33 -04:00
|
|
|
def change_issue_duplicate(issue)
|
|
|
|
canonical_issue_id = params.delete(:canonical_issue_id)
|
2019-01-22 19:05:38 -05:00
|
|
|
return unless canonical_issue_id
|
|
|
|
|
2017-07-20 10:42:33 -04:00
|
|
|
canonical_issue = IssuesFinder.new(current_user).find_by(id: canonical_issue_id)
|
|
|
|
|
|
|
|
if canonical_issue
|
2021-05-11 23:10:21 -04:00
|
|
|
Issues::DuplicateService.new(project: project, current_user: current_user).execute(issue, canonical_issue)
|
2017-07-20 10:42:33 -04:00
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-07-20 10:42:33 -04:00
|
|
|
|
2017-08-09 11:37:06 -04:00
|
|
|
def move_issue_to_new_project(issue)
|
|
|
|
target_project = params.delete(:target_project)
|
|
|
|
|
|
|
|
return unless target_project &&
|
|
|
|
issue.can_move?(current_user, target_project) &&
|
|
|
|
target_project != issue.project
|
|
|
|
|
|
|
|
update(issue)
|
2021-05-11 23:10:21 -04:00
|
|
|
Issues::MoveService.new(project: project, current_user: current_user).execute(issue, target_project)
|
2017-08-09 11:37:06 -04:00
|
|
|
end
|
|
|
|
|
2016-05-18 13:56:13 -04:00
|
|
|
private
|
|
|
|
|
2021-06-21 08:07:45 -04:00
|
|
|
attr_reader :spam_params
|
2021-01-27 04:09:01 -05:00
|
|
|
|
2020-12-03 16:09:35 -05:00
|
|
|
def clone_issue(issue)
|
|
|
|
target_project = params.delete(:target_clone_project)
|
2020-12-07 19:09:45 -05:00
|
|
|
with_notes = params.delete(:clone_with_notes)
|
2020-12-03 16:09:35 -05:00
|
|
|
|
|
|
|
return unless target_project &&
|
|
|
|
issue.can_clone?(current_user, target_project)
|
|
|
|
|
|
|
|
# we've pre-empted this from running in #execute, so let's go ahead and update the Issue now.
|
|
|
|
update(issue)
|
2021-05-11 23:10:21 -04:00
|
|
|
Issues::CloneService.new(project: project, current_user: current_user).execute(issue, target_project, with_notes: with_notes)
|
2020-12-03 16:09:35 -05:00
|
|
|
end
|
|
|
|
|
2018-10-19 03:16:58 -04:00
|
|
|
def create_merge_request_from_quick_action
|
|
|
|
create_merge_request_params = params.delete(:create_merge_request)
|
|
|
|
return unless create_merge_request_params
|
|
|
|
|
2021-05-11 23:10:21 -04:00
|
|
|
MergeRequests::CreateFromIssueService.new(project: project, current_user: current_user, mr_params: create_merge_request_params).execute
|
2018-10-19 03:16:58 -04:00
|
|
|
end
|
|
|
|
|
2021-08-23 05:10:23 -04:00
|
|
|
def handle_confidential_change(issue)
|
|
|
|
if issue.previous_changes.include?('confidential')
|
|
|
|
# don't enqueue immediately to prevent todos removal in case of a mistake
|
|
|
|
TodosDestroyer::ConfidentialIssueWorker.perform_in(Todo::WAIT_FOR_DELETE, issue.id) if issue.confidential?
|
|
|
|
create_confidentiality_note(issue)
|
|
|
|
track_usage_event(:incident_management_incident_change_confidential, current_user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_added_labels(issue, old_labels)
|
|
|
|
added_labels = issue.labels - old_labels
|
|
|
|
|
|
|
|
if added_labels.present?
|
|
|
|
notification_service.async.relabeled_issue(issue, added_labels, current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-02 12:29:32 -04:00
|
|
|
def handle_milestone_change(issue)
|
|
|
|
return unless issue.previous_changes.include?('milestone_id')
|
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
invalidate_milestone_issue_counters(issue)
|
|
|
|
send_milestone_change_notification(issue)
|
|
|
|
end
|
|
|
|
|
|
|
|
def invalidate_milestone_issue_counters(issue)
|
|
|
|
issue.previous_changes['milestone_id'].each do |milestone_id|
|
|
|
|
next unless milestone_id
|
|
|
|
|
|
|
|
milestone = Milestone.find_by_id(milestone_id)
|
|
|
|
|
|
|
|
delete_milestone_closed_issue_counter_cache(milestone)
|
|
|
|
delete_milestone_total_issue_counter_cache(milestone)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_milestone_change_notification(issue)
|
|
|
|
return if skip_milestone_email
|
|
|
|
|
2018-11-02 12:29:32 -04:00
|
|
|
if issue.milestone.nil?
|
|
|
|
notification_service.async.removed_milestone_issue(issue, current_user)
|
|
|
|
else
|
|
|
|
notification_service.async.changed_milestone_issue(issue, issue.milestone, current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-23 05:10:23 -04:00
|
|
|
def handle_added_mentions(issue, old_mentioned_users)
|
|
|
|
added_mentions = issue.mentioned_users(current_user) - old_mentioned_users
|
|
|
|
|
|
|
|
if added_mentions.present?
|
|
|
|
notification_service.async.new_mentions_in_issue(issue, added_mentions, current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-01 14:07:29 -04:00
|
|
|
def handle_severity_change(issue, old_severity)
|
|
|
|
return unless old_severity && issue.severity != old_severity
|
|
|
|
|
|
|
|
::IncidentManagement::AddSeveritySystemNoteWorker.perform_async(issue.id, current_user.id)
|
|
|
|
end
|
|
|
|
|
2022-02-22 22:17:13 -05:00
|
|
|
def handle_escalation_status_change(issue)
|
|
|
|
return unless issue.supports_escalation? && issue.escalation_status
|
2021-12-20 13:13:27 -05:00
|
|
|
|
2022-01-21 13:12:45 -05:00
|
|
|
::IncidentManagement::IssuableEscalationStatuses::AfterUpdateService.new(
|
|
|
|
issue,
|
2022-07-13 14:09:35 -04:00
|
|
|
current_user
|
2022-01-21 13:12:45 -05:00
|
|
|
).execute
|
2021-12-20 13:13:27 -05:00
|
|
|
end
|
|
|
|
|
2016-05-18 13:56:13 -04:00
|
|
|
def create_confidentiality_note(issue)
|
|
|
|
SystemNoteService.change_issue_confidentiality(issue, issue.project, current_user)
|
|
|
|
end
|
2021-05-19 17:12:42 -04:00
|
|
|
|
2021-08-19 14:10:32 -04:00
|
|
|
def handle_issue_type_change(issue)
|
|
|
|
return unless issue.previous_changes.include?('issue_type')
|
|
|
|
|
|
|
|
do_handle_issue_type_change(issue)
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_handle_issue_type_change(issue)
|
|
|
|
SystemNoteService.change_issue_type(issue, current_user)
|
2021-12-21 19:13:54 -05:00
|
|
|
|
|
|
|
::IncidentManagement::IssuableEscalationStatuses::CreateService.new(issue).execute if issue.supports_escalation?
|
2021-08-19 14:10:32 -04:00
|
|
|
end
|
2014-04-02 06:54:41 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Issues::UpdateService.prepend_mod_with('Issues::UpdateService')
|