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
|
2020-07-28 23:09:34 +05:30
|
|
|
extend ::Gitlab::Utils::Override
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
def initialize(project, user = nil, params = {})
|
|
|
|
super
|
|
|
|
|
|
|
|
@target_branch_was_deleted = @params.delete(:target_branch_was_deleted)
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
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
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
if merge_request.closed_or_merged_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
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
update_task_event(merge_request) || update(merge_request)
|
2015-11-26 14:37:03 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
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, [])
|
2019-07-31 22:56:46 +05:30
|
|
|
old_assignees = old_associations.fetch(:assignees, [])
|
2020-11-24 15:15:51 +05:30
|
|
|
old_reviewers = old_associations.fetch(:reviewers, [])
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
if has_changes?(merge_request, old_labels: old_labels, old_assignees: old_assignees, old_reviewers: old_reviewers)
|
2020-06-23 00:09:42 +05:30
|
|
|
todo_service.resolve_todos_for_target(merge_request, current_user)
|
2016-04-02 18:10:28 +05:30
|
|
|
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')
|
2021-03-11 19:13:27 +05:30
|
|
|
create_branch_change_note(merge_request,
|
|
|
|
'target',
|
|
|
|
target_branch_was_deleted ? 'delete' : 'update',
|
2015-11-26 14:37:03 +05:30
|
|
|
merge_request.previous_changes['target_branch'].first,
|
|
|
|
merge_request.target_branch)
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
abort_auto_merge(merge_request, 'target branch was changed')
|
2015-11-26 14:37:03 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
handle_assignees_change(merge_request, old_assignees) if merge_request.assignees != old_assignees
|
2015-09-11 14:41:01 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
handle_reviewers_change(merge_request, old_reviewers) if merge_request.reviewers != old_reviewers
|
|
|
|
|
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
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
added_mentions = merge_request.mentioned_users(current_user) - old_mentioned_users
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
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
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
def handle_task_changes(merge_request)
|
2020-06-23 00:09:42 +05:30
|
|
|
todo_service.resolve_todos_for_target(merge_request, current_user)
|
2019-03-02 22:35:43 +05:30
|
|
|
todo_service.update_merge_request(merge_request, current_user)
|
|
|
|
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
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
def before_update(issuable, skip_spam_check: false)
|
|
|
|
return unless issuable.changed?
|
|
|
|
|
|
|
|
@issuable_changes = issuable.changes
|
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
def after_update(issuable)
|
|
|
|
issuable.cache_merge_request_closes_issues!(current_user)
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
return unless @issuable_changes
|
|
|
|
|
|
|
|
%w(title description).each do |action|
|
|
|
|
next unless @issuable_changes.key?(action)
|
|
|
|
|
|
|
|
# Track edits to title or description
|
|
|
|
#
|
|
|
|
merge_request_activity_counter
|
|
|
|
.public_send("track_#{action}_edit_action".to_sym, user: current_user) # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
|
|
|
|
# Track changes to Draft/WIP status
|
|
|
|
#
|
|
|
|
if action == "title"
|
|
|
|
old_title, new_title = @issuable_changes["title"]
|
|
|
|
old_title_wip = MergeRequest.work_in_progress?(old_title)
|
|
|
|
new_title_wip = MergeRequest.work_in_progress?(new_title)
|
|
|
|
|
|
|
|
if !old_title_wip && new_title_wip
|
|
|
|
# Marked as Draft/WIP
|
|
|
|
#
|
|
|
|
merge_request_activity_counter
|
|
|
|
.track_marked_as_draft_action(user: current_user)
|
|
|
|
elsif old_title_wip && !new_title_wip
|
|
|
|
# Unmarked as Draft/WIP
|
|
|
|
#
|
|
|
|
merge_request_activity_counter
|
|
|
|
.track_unmarked_as_draft_action(user: current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
attr_reader :target_branch_was_deleted
|
|
|
|
|
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
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
def handle_assignees_change(merge_request, old_assignees)
|
|
|
|
create_assignee_note(merge_request, old_assignees)
|
|
|
|
notification_service.async.reassigned_merge_request(merge_request, current_user, old_assignees)
|
2020-10-24 23:57:45 +05:30
|
|
|
todo_service.reassigned_assignable(merge_request, current_user, old_assignees)
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
new_assignees = merge_request.assignees - old_assignees
|
|
|
|
merge_request_activity_counter.track_users_assigned_to_mr(users: new_assignees)
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
def handle_reviewers_change(merge_request, old_reviewers)
|
2021-03-08 18:12:59 +05:30
|
|
|
affected_reviewers = (old_reviewers + merge_request.reviewers) - (old_reviewers & merge_request.reviewers)
|
2021-01-03 14:25:43 +05:30
|
|
|
create_reviewer_note(merge_request, old_reviewers)
|
|
|
|
notification_service.async.changed_reviewer_of_merge_request(merge_request, current_user, old_reviewers)
|
2020-11-24 15:15:51 +05:30
|
|
|
todo_service.reassigned_reviewable(merge_request, current_user, old_reviewers)
|
2021-03-08 18:12:59 +05:30
|
|
|
invalidate_cache_counts(merge_request, users: affected_reviewers.compact)
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
new_reviewers = merge_request.reviewers - old_reviewers
|
|
|
|
merge_request_activity_counter.track_users_review_requested(users: new_reviewers)
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
def create_branch_change_note(issuable, branch_type, event_type, old_branch, new_branch)
|
2018-03-17 18:26:18 +05:30
|
|
|
SystemNoteService.change_branch(
|
2021-03-11 19:13:27 +05:30
|
|
|
issuable, issuable.project, current_user, branch_type, event_type,
|
2018-03-17 18:26:18 +05:30
|
|
|
old_branch, new_branch)
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
override :handle_quick_actions
|
|
|
|
def handle_quick_actions(merge_request)
|
|
|
|
super
|
2021-03-08 18:12:59 +05:30
|
|
|
|
|
|
|
# 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-07-28 23:09:34 +05:30
|
|
|
merge_from_quick_action(merge_request) if params[:merge]
|
|
|
|
end
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
def rebase_from_quick_action(merge_request)
|
|
|
|
merge_request.rebase_async(current_user.id)
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def merge_from_quick_action(merge_request)
|
|
|
|
last_diff_sha = params.delete(:merge)
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
MergeRequests::MergeOrchestrationService
|
|
|
|
.new(project, current_user, { sha: last_diff_sha })
|
|
|
|
.execute(merge_request)
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
override :quick_action_options
|
|
|
|
def quick_action_options
|
|
|
|
{ merge_request_diff_head_sha: params.delete(:merge_request_diff_head_sha) }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
MergeRequests::UpdateService.prepend_if_ee('EE::MergeRequests::UpdateService')
|