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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

314 lines
11 KiB
Ruby
Raw Permalink 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
2020-07-28 23:09:34 +05:30
extend ::Gitlab::Utils::Override
2021-06-08 01:23:25 +05:30
def initialize(project:, current_user: nil, params: {})
2021-03-11 19:13:27 +05:30
super
@target_branch_was_deleted = @params.delete(:target_branch_was_deleted)
end
2014-09-02 18:07:02 +05:30
def execute(merge_request)
2022-07-16 23:28:13 +05:30
if Gitlab::Utils.to_boolean(params[:draft])
merge_request.title = merge_request.draft_title
end
2021-04-29 21:17:54 +05:30
update_merge_request_with_specialized_service(merge_request) || general_fallback(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)
2021-09-04 01:27:46 +05:30
super
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, [])
2021-04-17 20:07:23 +05:30
old_timelogs = old_associations.fetch(:timelogs, [])
changed_fields = merge_request.previous_changes.keys
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
resolve_todos(merge_request, old_labels, old_assignees, old_reviewers)
2016-04-02 18:10:28 +05:30
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
2021-04-17 20:07:23 +05:30
handle_target_branch_change(merge_request)
handle_draft_status_change(merge_request, changed_fields)
2018-12-13 13:39:08 +05:30
2021-04-17 20:07:23 +05:30
track_title_and_desc_edits(changed_fields)
track_discussion_lock_toggle(merge_request, changed_fields)
track_time_estimate_and_spend_edits(merge_request, old_timelogs, changed_fields)
track_labels_change(merge_request, old_labels)
2016-09-13 17:45:13 +05:30
2021-04-17 20:07:23 +05:30
notify_if_labels_added(merge_request, old_labels)
notify_if_mentions_added(merge_request, old_mentioned_users)
2019-12-21 20:55:43 +05:30
2021-04-17 20:07:23 +05:30
# 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-09-04 01:27:46 +05:30
merge_request.mark_as_unchecked unless merge_request.unchecked?
2016-09-13 17:45:13 +05:30
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
2022-11-25 23:54:43 +05:30
def after_update(issuable, old_associations)
super
2016-09-29 09:46:39 +05:30
issuable.cache_merge_request_closes_issues!(current_user)
2021-04-17 20:07:23 +05:30
end
2021-03-11 19:13:27 +05:30
2021-04-17 20:07:23 +05:30
private
2021-03-11 19:13:27 +05:30
2021-04-17 20:07:23 +05:30
attr_reader :target_branch_was_deleted
2021-04-29 21:17:54 +05:30
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-04-17 20:07:23 +05:30
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-03-11 19:13:27 +05:30
merge_request_activity_counter
.public_send("track_#{action}_edit_action".to_sym, user: current_user) # rubocop:disable GitlabSecurity/PublicSend
2021-04-17 20:07:23 +05:30
end
end
2021-03-11 19:13:27 +05:30
2021-04-17 20:07:23 +05:30
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)
2021-03-11 19:13:27 +05:30
end
2016-09-29 09:46:39 +05:30
end
2016-11-03 12:29:30 +05:30
2021-04-17 20:07:23 +05:30
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
2016-11-03 12:29:30 +05:30
2021-04-17 20:07:23 +05:30
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
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-27 11:52:29 +05:30
resolve_todos_for(merge_request)
2021-04-17 20:07:23 +05:30
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
)
2023-03-17 16:20:25 +05:30
refresh_pipelines_on_merge_requests(merge_request, allow_duplicate: true)
2021-04-17 20:07:23 +05:30
abort_auto_merge(merge_request, 'target branch was changed')
end
def handle_draft_status_change(merge_request, changed_fields)
return unless changed_fields.include?("title")
old_title, new_title = merge_request.previous_changes["title"]
2022-07-23 23:45:48 +05:30
old_title_draft = MergeRequest.draft?(old_title)
new_title_draft = MergeRequest.draft?(new_title)
2021-04-17 20:07:23 +05:30
2022-11-25 23:54:43 +05:30
if old_title_draft || new_title_draft
# notify the draft status changed. Added/removed message is handled in the
# email template itself, see `change_in_merge_request_draft_status_email` template.
notify_draft_status_changed(merge_request)
trigger_merge_request_status_updated(merge_request)
end
2022-10-11 01:57:18 +05:30
2022-07-23 23:45:48 +05:30
if !old_title_draft && new_title_draft
# Marked as Draft
2022-10-11 01:57:18 +05:30
merge_request_activity_counter.track_marked_as_draft_action(user: current_user)
2022-07-23 23:45:48 +05:30
elsif old_title_draft && !new_title_draft
# Unmarked as Draft
2022-10-11 01:57:18 +05:30
merge_request_activity_counter.track_unmarked_as_draft_action(user: current_user)
2021-04-17 20:07:23 +05:30
end
end
def notify_draft_status_changed(merge_request)
notification_service.async.change_in_merge_request_draft_status(
merge_request,
current_user
)
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-11-18 22:05:49 +05:30
::MergeRequests::MergeOrchestrationService
2021-03-08 18:12:59 +05:30
.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
2021-04-29 21:17:54 +05:30
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-09-04 01:27:46 +05:30
when :assignee_ids, :assignee_id
2021-04-29 21:17:54 +05:30
assignees_service.execute(merge_request)
2021-06-08 01:23:25 +05:30
when :spend_time
add_time_spent_service.execute(merge_request)
2021-04-29 21:17:54 +05:30
end
end
def assignees_service
@assignees_service ||= ::MergeRequests::UpdateAssigneesService
2021-06-08 01:23:25 +05:30
.new(project: project, current_user: current_user, params: params)
end
def add_time_spent_service
@add_time_spent_service ||= ::MergeRequests::AddSpentTimeService.new(project: project, current_user: current_user, params: params)
2021-04-29 21:17:54 +05:30
end
2022-08-27 11:52:29 +05:30
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
2022-11-25 23:54:43 +05:30
def trigger_merge_request_status_updated(merge_request)
GraphqlTriggers.merge_request_merge_status_updated(merge_request)
end
2014-09-02 18:07:02 +05:30
end
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
MergeRequests::UpdateService.prepend_mod_with('MergeRequests::UpdateService')