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

132 lines
4.4 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module MergeRequests
class UpdateService < MergeRequests::BaseService
def execute(merge_request)
2015-09-11 14:41:01 +05:30
# We don't allow change of source/target projects and source branch
2014-09-02 18:07:02 +05:30
# after merge request was created
2019-02-15 15:39:39 +05:30
params.delete(:source_project_id)
params.delete(:target_project_id)
params.delete(:source_branch)
2014-09-02 18:07:02 +05:30
2017-09-10 17:25:29 +05:30
merge_from_quick_action(merge_request) if params[:merge]
2017-08-17 22:00:37 +05:30
2016-09-29 09:46:39 +05:30
if merge_request.closed_without_fork?
2019-02-15 15:39:39 +05:30
params.delete(:target_branch)
params.delete(:force_remove_source_branch)
2016-09-29 09:46:39 +05:30
end
2016-11-03 12:29:30 +05:30
if params[:force_remove_source_branch].present?
merge_request.merge_params['force_remove_source_branch'] = params.delete(:force_remove_source_branch)
end
2016-06-02 11:05:42 +05:30
2016-11-03 12:29:30 +05:30
handle_wip_event(merge_request)
2015-12-23 02:04:40 +05:30
update(merge_request)
2015-11-26 14:37:03 +05:30
end
2014-09-02 18:07:02 +05:30
2018-10-15 14:42:47 +05:30
# rubocop:disable Metrics/AbcSize
2017-08-17 22:00:37 +05:30
def handle_changes(merge_request, options)
2018-03-17 18:26:18 +05:30
old_associations = options.fetch(:old_associations, {})
old_labels = old_associations.fetch(:labels, [])
old_mentioned_users = old_associations.fetch(:mentioned_users, [])
2017-08-17 22:00:37 +05:30
2016-06-02 11:05:42 +05:30
if has_changes?(merge_request, old_labels: old_labels)
2016-04-02 18:10:28 +05:30
todo_service.mark_pending_todos_as_done(merge_request, current_user)
end
if merge_request.previous_changes.include?('title') ||
2017-08-17 22:00:37 +05:30
merge_request.previous_changes.include?('description')
todo_service.update_merge_request(merge_request, current_user, old_mentioned_users)
2016-04-02 18:10:28 +05:30
end
2015-11-26 14:37:03 +05:30
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-09-02 18:07:02 +05:30
2015-11-26 14:37:03 +05:30
if merge_request.previous_changes.include?('assignee_id')
2019-02-15 15:39:39 +05:30
reassigned_merge_request_args = [merge_request, current_user]
2018-10-15 14:42:47 +05:30
old_assignee_id = merge_request.previous_changes['assignee_id'].first
2019-02-15 15:39:39 +05:30
reassigned_merge_request_args << User.find(old_assignee_id) if old_assignee_id
2018-10-15 14:42:47 +05:30
2015-11-26 14:37:03 +05:30
create_assignee_note(merge_request)
2019-02-15 15:39:39 +05:30
notification_service.async.reassigned_merge_request(*reassigned_merge_request_args)
2016-04-02 18:10:28 +05:30
todo_service.reassigned_merge_request(merge_request, current_user)
2015-11-26 14:37:03 +05:30
end
2015-09-11 14:41:01 +05:30
2015-11-26 14:37:03 +05:30
if merge_request.previous_changes.include?('target_branch') ||
merge_request.previous_changes.include?('source_branch')
merge_request.mark_as_unchecked
end
2016-06-02 11:05:42 +05:30
2018-12-13 13:39:08 +05:30
handle_milestone_change(merge_request)
2016-06-02 11:05:42 +05:30
added_labels = merge_request.labels - old_labels
if added_labels.present?
2018-10-15 14:42:47 +05:30
notification_service.async.relabeled_merge_request(
2016-06-02 11:05:42 +05:30
merge_request,
added_labels,
current_user
)
end
2016-09-13 17:45:13 +05:30
added_mentions = merge_request.mentioned_users - old_mentioned_users
if added_mentions.present?
2018-10-15 14:42:47 +05:30
notification_service.async.new_mentions_in_merge_request(
2016-09-13 17:45:13 +05:30
merge_request,
added_mentions,
current_user
)
end
2014-09-02 18:07:02 +05:30
end
2018-10-15 14:42:47 +05:30
# rubocop:enable Metrics/AbcSize
2015-12-23 02:04:40 +05:30
2017-09-10 17:25:29 +05:30
def merge_from_quick_action(merge_request)
2017-08-17 22:00:37 +05:30
last_diff_sha = params.delete(:merge)
2017-09-10 17:25:29 +05:30
return unless merge_request.mergeable_with_quick_action?(current_user, last_diff_sha: last_diff_sha)
2017-08-17 22:00:37 +05:30
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
2018-03-17 18:26:18 +05:30
merge_request.merge_async(current_user.id, {})
2017-08-17 22:00:37 +05:30
end
end
2015-12-23 02:04:40 +05:30
def reopen_service
MergeRequests::ReopenService
end
def close_service
MergeRequests::CloseService
end
2016-09-29 09:46:39 +05:30
def after_update(issuable)
issuable.cache_merge_request_closes_issues!(current_user)
end
2016-11-03 12:29:30 +05:30
private
2018-12-13 13:39:08 +05:30
def handle_milestone_change(merge_request)
return if skip_milestone_email
return unless merge_request.previous_changes.include?('milestone_id')
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)
end
end
2018-03-17 18:26:18 +05:30
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-09-02 18:07:02 +05:30
end
end