2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-04-02 14:51:17 -04:00
|
|
|
module MergeRequests
|
|
|
|
class UpdateService < MergeRequests::BaseService
|
2020-06-29 11:08:56 -04:00
|
|
|
extend ::Gitlab::Utils::Override
|
|
|
|
|
2021-05-11 23:10:21 -04:00
|
|
|
def initialize(project:, current_user: nil, params: {})
|
2021-02-17 04:09:36 -05:00
|
|
|
super
|
|
|
|
|
|
|
|
@target_branch_was_deleted = @params.delete(:target_branch_was_deleted)
|
|
|
|
end
|
|
|
|
|
2014-04-02 14:51:17 -04:00
|
|
|
def execute(merge_request)
|
2022-05-12 17:09:08 -04:00
|
|
|
if Gitlab::Utils.to_boolean(params[:draft])
|
|
|
|
merge_request.title = merge_request.draft_title
|
|
|
|
end
|
|
|
|
|
2021-04-15 11:09:11 -04:00
|
|
|
update_merge_request_with_specialized_service(merge_request) || general_fallback(merge_request)
|
2015-11-12 18:06:42 -05:00
|
|
|
end
|
2014-06-10 05:33:17 -04:00
|
|
|
|
2017-05-04 08:11:15 -04:00
|
|
|
def handle_changes(merge_request, 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, [])
|
2019-04-07 14:35:16 -04:00
|
|
|
old_assignees = old_associations.fetch(:assignees, [])
|
2020-09-09 08:08:22 -04:00
|
|
|
old_reviewers = old_associations.fetch(:reviewers, [])
|
2021-03-03 10:10:53 -05:00
|
|
|
old_timelogs = old_associations.fetch(:timelogs, [])
|
2021-02-22 16:10:48 -05:00
|
|
|
changed_fields = merge_request.previous_changes.keys
|
2017-05-04 08:11:15 -04:00
|
|
|
|
2021-02-23 19:11:11 -05:00
|
|
|
resolve_todos(merge_request, old_labels, old_assignees, old_reviewers)
|
2016-02-18 16:12:52 -05:00
|
|
|
|
|
|
|
if merge_request.previous_changes.include?('title') ||
|
2016-12-15 17:14:20 -05:00
|
|
|
merge_request.previous_changes.include?('description')
|
2017-03-28 02:25:43 -04:00
|
|
|
todo_service.update_merge_request(merge_request, current_user, old_mentioned_users)
|
2016-02-16 18:18:38 -05:00
|
|
|
end
|
|
|
|
|
2021-02-23 19:11:11 -05:00
|
|
|
handle_target_branch_change(merge_request)
|
2018-11-02 12:29:32 -04:00
|
|
|
handle_milestone_change(merge_request)
|
2021-02-22 16:10:48 -05:00
|
|
|
handle_draft_status_change(merge_request, changed_fields)
|
2022-04-01 14:08:46 -04:00
|
|
|
handle_label_changes(merge_request, old_labels)
|
2021-02-22 16:10:48 -05:00
|
|
|
|
|
|
|
track_title_and_desc_edits(changed_fields)
|
2021-03-03 07:11:16 -05:00
|
|
|
track_discussion_lock_toggle(merge_request, changed_fields)
|
2021-03-03 10:10:53 -05:00
|
|
|
track_time_estimate_and_spend_edits(merge_request, old_timelogs, changed_fields)
|
2021-03-09 16:09:15 -05:00
|
|
|
track_labels_change(merge_request, old_labels)
|
2018-11-02 12:29:32 -04:00
|
|
|
|
2021-02-23 19:11:11 -05:00
|
|
|
notify_if_labels_added(merge_request, old_labels)
|
|
|
|
notify_if_mentions_added(merge_request, old_mentioned_users)
|
2021-02-22 16:10:48 -05:00
|
|
|
|
|
|
|
# Since #mark_as_unchecked triggers an update action through the MR's
|
|
|
|
# state machine, we want to push this as far down in the process so we
|
|
|
|
# avoid resetting #ActiveModel::Dirty
|
|
|
|
#
|
|
|
|
if merge_request.previous_changes.include?('target_branch') ||
|
|
|
|
merge_request.previous_changes.include?('source_branch')
|
2021-05-26 20:10:40 -04:00
|
|
|
merge_request.mark_as_unchecked unless merge_request.unchecked?
|
2021-02-22 16:10:48 -05:00
|
|
|
end
|
2014-04-02 14:51:17 -04:00
|
|
|
end
|
2015-11-17 06:42:43 -05:00
|
|
|
|
2019-01-29 19:31:31 -05:00
|
|
|
def handle_task_changes(merge_request)
|
2020-05-28 14:08:37 -04:00
|
|
|
todo_service.resolve_todos_for_target(merge_request, current_user)
|
2019-01-29 19:31:31 -05:00
|
|
|
todo_service.update_merge_request(merge_request, current_user)
|
|
|
|
end
|
|
|
|
|
2015-11-17 06:42:43 -05:00
|
|
|
def reopen_service
|
|
|
|
MergeRequests::ReopenService
|
|
|
|
end
|
|
|
|
|
|
|
|
def close_service
|
|
|
|
MergeRequests::CloseService
|
|
|
|
end
|
2016-09-20 15:52:20 -04:00
|
|
|
|
|
|
|
def after_update(issuable)
|
|
|
|
issuable.cache_merge_request_closes_issues!(current_user)
|
2021-02-22 16:10:48 -05:00
|
|
|
end
|
2021-02-05 19:09:54 -05:00
|
|
|
|
2021-02-22 16:10:48 -05:00
|
|
|
private
|
2021-02-05 19:09:54 -05:00
|
|
|
|
2021-02-22 16:10:48 -05:00
|
|
|
attr_reader :target_branch_was_deleted
|
|
|
|
|
2021-04-15 11:09:11 -04:00
|
|
|
def general_fallback(merge_request)
|
|
|
|
# We don't allow change of source/target projects and source branch
|
|
|
|
# after merge request was created
|
|
|
|
params.delete(:source_project_id)
|
|
|
|
params.delete(:target_project_id)
|
|
|
|
params.delete(:source_branch)
|
|
|
|
|
|
|
|
if merge_request.closed_or_merged_without_fork?
|
|
|
|
params.delete(:target_branch)
|
|
|
|
params.delete(:force_remove_source_branch)
|
|
|
|
end
|
|
|
|
|
|
|
|
update_task_event(merge_request) || update(merge_request)
|
|
|
|
end
|
|
|
|
|
2021-02-22 16:10:48 -05:00
|
|
|
def track_title_and_desc_edits(changed_fields)
|
|
|
|
tracked_fields = %w(title description)
|
|
|
|
|
|
|
|
return unless changed_fields.any? { |field| tracked_fields.include?(field) }
|
|
|
|
|
|
|
|
tracked_fields.each do |action|
|
|
|
|
next unless changed_fields.include?(action)
|
2021-02-05 19:09:54 -05:00
|
|
|
|
2021-02-11 07:08:52 -05:00
|
|
|
merge_request_activity_counter
|
2021-02-05 19:09:54 -05:00
|
|
|
.public_send("track_#{action}_edit_action".to_sym, user: current_user) # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
end
|
2016-09-20 15:52:20 -04:00
|
|
|
end
|
2016-09-08 05:18:41 -04:00
|
|
|
|
2021-03-03 07:11:16 -05:00
|
|
|
def track_discussion_lock_toggle(merge_request, changed_fields)
|
|
|
|
return unless changed_fields.include?('discussion_locked')
|
|
|
|
|
|
|
|
if merge_request.discussion_locked
|
|
|
|
merge_request_activity_counter.track_discussion_locked_action(user: current_user)
|
|
|
|
else
|
|
|
|
merge_request_activity_counter.track_discussion_unlocked_action(user: current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-03 10:10:53 -05:00
|
|
|
def track_time_estimate_and_spend_edits(merge_request, old_timelogs, changed_fields)
|
|
|
|
merge_request_activity_counter.track_time_estimate_changed_action(user: current_user) if changed_fields.include?('time_estimate')
|
|
|
|
merge_request_activity_counter.track_time_spent_changed_action(user: current_user) if old_timelogs != merge_request.timelogs
|
|
|
|
end
|
|
|
|
|
2021-03-09 16:09:15 -05:00
|
|
|
def track_labels_change(merge_request, old_labels)
|
|
|
|
return if Set.new(merge_request.labels) == Set.new(old_labels)
|
|
|
|
|
|
|
|
merge_request_activity_counter.track_labels_changed_action(user: current_user)
|
|
|
|
end
|
|
|
|
|
2021-02-23 19:11:11 -05:00
|
|
|
def notify_if_labels_added(merge_request, old_labels)
|
|
|
|
added_labels = merge_request.labels - old_labels
|
|
|
|
|
|
|
|
return unless added_labels.present?
|
|
|
|
|
|
|
|
notification_service.async.relabeled_merge_request(
|
|
|
|
merge_request,
|
|
|
|
added_labels,
|
|
|
|
current_user
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_if_mentions_added(merge_request, old_mentioned_users)
|
|
|
|
added_mentions = merge_request.mentioned_users(current_user) - old_mentioned_users
|
|
|
|
|
|
|
|
return unless added_mentions.present?
|
|
|
|
|
|
|
|
notification_service.async.new_mentions_in_merge_request(
|
|
|
|
merge_request,
|
|
|
|
added_mentions,
|
|
|
|
current_user
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def resolve_todos(merge_request, old_labels, old_assignees, old_reviewers)
|
|
|
|
return unless has_changes?(merge_request, old_labels: old_labels, old_assignees: old_assignees, old_reviewers: old_reviewers)
|
|
|
|
|
2022-08-15 05:11:06 -04:00
|
|
|
resolve_todos_for(merge_request)
|
2021-02-23 19:11:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def handle_target_branch_change(merge_request)
|
|
|
|
return unless merge_request.previous_changes.include?('target_branch')
|
|
|
|
|
|
|
|
create_branch_change_note(
|
|
|
|
merge_request,
|
|
|
|
'target',
|
|
|
|
target_branch_was_deleted ? 'delete' : 'update',
|
|
|
|
merge_request.previous_changes['target_branch'].first,
|
|
|
|
merge_request.target_branch
|
|
|
|
)
|
|
|
|
|
|
|
|
abort_auto_merge(merge_request, 'target branch was changed')
|
|
|
|
end
|
|
|
|
|
2021-02-22 16:10:48 -05:00
|
|
|
def handle_draft_status_change(merge_request, changed_fields)
|
|
|
|
return unless changed_fields.include?("title")
|
2016-09-08 05:18:41 -04:00
|
|
|
|
2021-02-22 16:10:48 -05:00
|
|
|
old_title, new_title = merge_request.previous_changes["title"]
|
2022-05-24 11:08:28 -04:00
|
|
|
old_title_draft = MergeRequest.draft?(old_title)
|
|
|
|
new_title_draft = MergeRequest.draft?(new_title)
|
2021-02-22 16:10:48 -05:00
|
|
|
|
2022-05-24 11:08:28 -04:00
|
|
|
if !old_title_draft && new_title_draft
|
|
|
|
# Marked as Draft
|
2021-02-22 16:10:48 -05:00
|
|
|
#
|
|
|
|
merge_request_activity_counter
|
|
|
|
.track_marked_as_draft_action(user: current_user)
|
2022-05-24 11:08:28 -04:00
|
|
|
elsif old_title_draft && !new_title_draft
|
|
|
|
# Unmarked as Draft
|
2021-02-22 16:10:48 -05:00
|
|
|
#
|
2021-03-02 19:10:50 -05:00
|
|
|
notify_draft_status_changed(merge_request)
|
|
|
|
|
2021-02-22 16:10:48 -05:00
|
|
|
merge_request_activity_counter
|
|
|
|
.track_unmarked_as_draft_action(user: current_user)
|
|
|
|
end
|
|
|
|
end
|
2021-02-17 04:09:36 -05:00
|
|
|
|
2021-03-02 19:10:50 -05:00
|
|
|
def notify_draft_status_changed(merge_request)
|
|
|
|
notification_service.async.change_in_merge_request_draft_status(
|
|
|
|
merge_request,
|
|
|
|
current_user
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-11-02 12:29:32 -04:00
|
|
|
def handle_milestone_change(merge_request)
|
|
|
|
return if skip_milestone_email
|
|
|
|
|
|
|
|
return unless merge_request.previous_changes.include?('milestone_id')
|
|
|
|
|
2021-03-09 16:09:15 -05:00
|
|
|
merge_request_activity_counter.track_milestone_changed_action(user: current_user)
|
|
|
|
|
2021-04-08 14:09:32 -04:00
|
|
|
previous_milestone = Milestone.find_by_id(merge_request.previous_changes['milestone_id'].first)
|
|
|
|
delete_milestone_total_merge_requests_counter_cache(previous_milestone)
|
|
|
|
|
2018-11-02 12:29:32 -04:00
|
|
|
if merge_request.milestone.nil?
|
|
|
|
notification_service.async.removed_milestone_merge_request(merge_request, current_user)
|
|
|
|
else
|
|
|
|
notification_service.async.changed_milestone_merge_request(merge_request, merge_request.milestone, current_user)
|
2021-04-08 14:09:32 -04:00
|
|
|
|
|
|
|
delete_milestone_total_merge_requests_counter_cache(merge_request.milestone)
|
2018-11-02 12:29:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-17 04:09:36 -05:00
|
|
|
def create_branch_change_note(issuable, branch_type, event_type, old_branch, new_branch)
|
2017-11-01 13:35:14 -04:00
|
|
|
SystemNoteService.change_branch(
|
2021-02-17 04:09:36 -05:00
|
|
|
issuable, issuable.project, current_user, branch_type, event_type,
|
2017-11-01 13:35:14 -04:00
|
|
|
old_branch, new_branch)
|
|
|
|
end
|
2020-06-29 11:08:56 -04:00
|
|
|
|
|
|
|
override :handle_quick_actions
|
|
|
|
def handle_quick_actions(merge_request)
|
|
|
|
super
|
2020-12-29 07:10:19 -05:00
|
|
|
|
|
|
|
# Ensure this parameter does not get used as an attribute
|
|
|
|
rebase = params.delete(:rebase)
|
|
|
|
|
|
|
|
if rebase
|
|
|
|
rebase_from_quick_action(merge_request)
|
|
|
|
# Ignore "/merge" if "/rebase" is used to avoid an unexpected race
|
|
|
|
params.delete(:merge)
|
|
|
|
end
|
|
|
|
|
2020-06-29 11:08:56 -04:00
|
|
|
merge_from_quick_action(merge_request) if params[:merge]
|
|
|
|
end
|
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
def rebase_from_quick_action(merge_request)
|
|
|
|
merge_request.rebase_async(current_user.id)
|
|
|
|
end
|
|
|
|
|
2020-06-29 11:08:56 -04:00
|
|
|
def merge_from_quick_action(merge_request)
|
|
|
|
last_diff_sha = params.delete(:merge)
|
|
|
|
|
2021-10-11 02:13:09 -04:00
|
|
|
::MergeRequests::MergeOrchestrationService
|
2020-12-17 13:10:14 -05:00
|
|
|
.new(project, current_user, { sha: last_diff_sha })
|
|
|
|
.execute(merge_request)
|
2020-06-29 11:08:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
override :quick_action_options
|
|
|
|
def quick_action_options
|
|
|
|
{ merge_request_diff_head_sha: params.delete(:merge_request_diff_head_sha) }
|
|
|
|
end
|
2021-04-15 11:09:11 -04:00
|
|
|
|
|
|
|
def update_merge_request_with_specialized_service(merge_request)
|
|
|
|
return unless params.delete(:use_specialized_service)
|
|
|
|
|
|
|
|
# If we're attempting to modify only a single attribute, look up whether
|
|
|
|
# we have a specialized, targeted service we should use instead. We may
|
|
|
|
# in the future extend this to include specialized services that operate
|
|
|
|
# on multiple attributes, but for now limit to only single attribute
|
|
|
|
# updates.
|
|
|
|
#
|
|
|
|
return unless params.one?
|
|
|
|
|
|
|
|
attempt_specialized_update_services(merge_request, params.each_key.first.to_sym)
|
|
|
|
end
|
|
|
|
|
|
|
|
def attempt_specialized_update_services(merge_request, attribute)
|
|
|
|
case attribute
|
2021-05-20 11:10:13 -04:00
|
|
|
when :assignee_ids, :assignee_id
|
2021-04-15 11:09:11 -04:00
|
|
|
assignees_service.execute(merge_request)
|
2021-04-28 17:10:01 -04:00
|
|
|
when :spend_time
|
|
|
|
add_time_spent_service.execute(merge_request)
|
2021-04-15 11:09:11 -04:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def assignees_service
|
|
|
|
@assignees_service ||= ::MergeRequests::UpdateAssigneesService
|
2021-05-11 23:10:21 -04:00
|
|
|
.new(project: project, current_user: current_user, params: params)
|
2021-04-15 11:09:11 -04:00
|
|
|
end
|
2021-04-28 17:10:01 -04:00
|
|
|
|
|
|
|
def add_time_spent_service
|
2021-05-11 23:10:21 -04:00
|
|
|
@add_time_spent_service ||= ::MergeRequests::AddSpentTimeService.new(project: project, current_user: current_user, params: params)
|
2021-04-28 17:10:01 -04:00
|
|
|
end
|
2022-08-15 05:11:06 -04:00
|
|
|
|
|
|
|
def new_user_ids(merge_request, user_ids, attribute)
|
|
|
|
# prime the cache - prevent N+1 lookup during authorization loop.
|
|
|
|
return [] if user_ids.empty?
|
|
|
|
|
|
|
|
merge_request.project.team.max_member_access_for_user_ids(user_ids)
|
|
|
|
User.id_in(user_ids).map do |user|
|
|
|
|
if user.can?(:read_merge_request, merge_request)
|
|
|
|
user.id
|
|
|
|
else
|
|
|
|
merge_request.errors.add(
|
|
|
|
attribute,
|
|
|
|
"Cannot assign #{user.to_reference} to #{merge_request.to_reference}"
|
|
|
|
)
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
def resolve_todos_for(merge_request)
|
|
|
|
service_user = current_user
|
|
|
|
|
|
|
|
merge_request.run_after_commit_or_now do
|
|
|
|
::MergeRequests::ResolveTodosService.new(merge_request, service_user).async_execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_sentinel_values(param)
|
|
|
|
param.reject { _1 == 0 }
|
|
|
|
end
|
2014-04-02 14:51:17 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
MergeRequests::UpdateService.prepend_mod_with('MergeRequests::UpdateService')
|