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
|
|
|
|
def execute(merge_request)
|
2015-05-29 12:40:35 -04:00
|
|
|
# We don't allow change of source/target projects and source branch
|
2014-04-02 14:51:17 -04:00
|
|
|
# after merge request was created
|
2014-06-26 16:24:17 -04:00
|
|
|
params.except!(:source_project_id)
|
|
|
|
params.except!(:target_project_id)
|
2015-05-29 12:40:35 -04:00
|
|
|
params.except!(:source_branch)
|
2014-04-02 14:51:17 -04:00
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
merge_from_quick_action(merge_request) if params[:merge]
|
2016-11-24 09:05:15 -05:00
|
|
|
|
2016-07-26 07:57:43 -04:00
|
|
|
if merge_request.closed_without_fork?
|
|
|
|
params.except!(:target_branch, :force_remove_source_branch)
|
|
|
|
end
|
|
|
|
|
2016-10-11 11:52:49 -04:00
|
|
|
if params[:force_remove_source_branch].present?
|
|
|
|
merge_request.merge_params['force_remove_source_branch'] = params.delete(:force_remove_source_branch)
|
|
|
|
end
|
|
|
|
|
2016-09-08 05:18:41 -04:00
|
|
|
handle_wip_event(merge_request)
|
2015-11-17 06:42:43 -05:00
|
|
|
update(merge_request)
|
2015-11-12 18:06:42 -05:00
|
|
|
end
|
2014-06-10 05:33:17 -04:00
|
|
|
|
2018-04-20 13:37:38 -04:00
|
|
|
# rubocop:disable Metrics/AbcSize
|
2017-05-04 08:11:15 -04:00
|
|
|
def handle_changes(merge_request, options)
|
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, [])
|
2017-05-04 08:11:15 -04:00
|
|
|
|
2016-03-01 11:33:13 -05:00
|
|
|
if has_changes?(merge_request, old_labels: old_labels)
|
2016-02-20 08:59:59 -05:00
|
|
|
todo_service.mark_pending_todos_as_done(merge_request, current_user)
|
2016-02-18 16:12:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-11-12 18:06:42 -05:00
|
|
|
if merge_request.previous_changes.include?('target_branch')
|
|
|
|
create_branch_change_note(merge_request, 'target',
|
|
|
|
merge_request.previous_changes['target_branch'].first,
|
|
|
|
merge_request.target_branch)
|
|
|
|
end
|
2014-04-02 14:51:17 -04:00
|
|
|
|
2015-11-12 18:06:42 -05:00
|
|
|
if merge_request.previous_changes.include?('assignee_id')
|
2018-04-20 13:37:38 -04:00
|
|
|
old_assignee_id = merge_request.previous_changes['assignee_id'].first
|
|
|
|
old_assignee = User.find(old_assignee_id) if old_assignee_id
|
|
|
|
|
2015-11-12 18:06:42 -05:00
|
|
|
create_assignee_note(merge_request)
|
2018-04-20 13:37:38 -04:00
|
|
|
notification_service.async.reassigned_merge_request(merge_request, current_user, old_assignee)
|
2016-02-20 08:59:59 -05:00
|
|
|
todo_service.reassigned_merge_request(merge_request, current_user)
|
2015-11-12 18:06:42 -05:00
|
|
|
end
|
2015-05-28 21:00:37 -04:00
|
|
|
|
2015-11-12 18:06:42 -05:00
|
|
|
if merge_request.previous_changes.include?('target_branch') ||
|
|
|
|
merge_request.previous_changes.include?('source_branch')
|
|
|
|
merge_request.mark_as_unchecked
|
|
|
|
end
|
2016-02-12 09:58:39 -05:00
|
|
|
|
2016-03-01 11:33:13 -05:00
|
|
|
added_labels = merge_request.labels - old_labels
|
|
|
|
if added_labels.present?
|
2018-04-20 13:37:38 -04:00
|
|
|
notification_service.async.relabeled_merge_request(
|
2016-03-01 11:33:13 -05:00
|
|
|
merge_request,
|
|
|
|
added_labels,
|
|
|
|
current_user
|
|
|
|
)
|
2016-02-12 09:58:39 -05:00
|
|
|
end
|
2016-08-15 07:05:08 -04:00
|
|
|
|
2016-08-15 09:47:16 -04:00
|
|
|
added_mentions = merge_request.mentioned_users - old_mentioned_users
|
|
|
|
if added_mentions.present?
|
2018-04-20 13:37:38 -04:00
|
|
|
notification_service.async.new_mentions_in_merge_request(
|
2016-08-15 09:47:16 -04:00
|
|
|
merge_request,
|
|
|
|
added_mentions,
|
|
|
|
current_user
|
|
|
|
)
|
|
|
|
end
|
2014-04-02 14:51:17 -04:00
|
|
|
end
|
2018-04-20 13:37:38 -04:00
|
|
|
# rubocop:enable Metrics/AbcSize
|
2015-11-17 06:42:43 -05:00
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
def merge_from_quick_action(merge_request)
|
2016-11-24 09:05:15 -05:00
|
|
|
last_diff_sha = params.delete(:merge)
|
2017-05-31 01:50:53 -04:00
|
|
|
return unless merge_request.mergeable_with_quick_action?(current_user, last_diff_sha: last_diff_sha)
|
2016-11-24 09:05:15 -05:00
|
|
|
|
|
|
|
merge_request.update(merge_error: nil)
|
|
|
|
|
|
|
|
if merge_request.head_pipeline && merge_request.head_pipeline.active?
|
|
|
|
MergeRequests::MergeWhenPipelineSucceedsService.new(project, current_user).execute(merge_request)
|
|
|
|
else
|
2017-08-28 14:55:25 -04:00
|
|
|
merge_request.merge_async(current_user.id, {})
|
2016-11-24 09:05:15 -05:00
|
|
|
end
|
|
|
|
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)
|
|
|
|
end
|
2016-09-08 05:18:41 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-11-01 13:35:14 -04:00
|
|
|
def create_branch_change_note(issuable, branch_type, old_branch, new_branch)
|
|
|
|
SystemNoteService.change_branch(
|
|
|
|
issuable, issuable.project, current_user, branch_type,
|
|
|
|
old_branch, new_branch)
|
|
|
|
end
|
2014-04-02 14:51:17 -04:00
|
|
|
end
|
|
|
|
end
|