Commit 7607e1b9 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'kerrizor/refactor-tracking-of-mr-title-desc-and-draft-status' into 'master'

Refactor tracking of MR title, desc, and draft status

See merge request gitlab-org/gitlab!54740
parents 3df4636c 14794190
...@@ -25,12 +25,14 @@ module MergeRequests ...@@ -25,12 +25,14 @@ module MergeRequests
update_task_event(merge_request) || update(merge_request) update_task_event(merge_request) || update(merge_request)
end end
# rubocop:disable Metrics/AbcSize
def handle_changes(merge_request, options) def handle_changes(merge_request, options)
old_associations = options.fetch(:old_associations, {}) old_associations = options.fetch(:old_associations, {})
old_labels = old_associations.fetch(:labels, []) old_labels = old_associations.fetch(:labels, [])
old_mentioned_users = old_associations.fetch(:mentioned_users, []) old_mentioned_users = old_associations.fetch(:mentioned_users, [])
old_assignees = old_associations.fetch(:assignees, []) old_assignees = old_associations.fetch(:assignees, [])
old_reviewers = old_associations.fetch(:reviewers, []) old_reviewers = old_associations.fetch(:reviewers, [])
changed_fields = merge_request.previous_changes.keys
if has_changes?(merge_request, old_labels: old_labels, old_assignees: old_assignees, old_reviewers: old_reviewers) if has_changes?(merge_request, old_labels: old_labels, old_assignees: old_assignees, old_reviewers: old_reviewers)
todo_service.resolve_todos_for_target(merge_request, current_user) todo_service.resolve_todos_for_target(merge_request, current_user)
...@@ -52,15 +54,11 @@ module MergeRequests ...@@ -52,15 +54,11 @@ module MergeRequests
end end
handle_assignees_change(merge_request, old_assignees) if merge_request.assignees != old_assignees handle_assignees_change(merge_request, old_assignees) if merge_request.assignees != old_assignees
handle_reviewers_change(merge_request, old_reviewers) if merge_request.reviewers != old_reviewers handle_reviewers_change(merge_request, old_reviewers) if merge_request.reviewers != old_reviewers
if merge_request.previous_changes.include?('target_branch') ||
merge_request.previous_changes.include?('source_branch')
merge_request.mark_as_unchecked
end
handle_milestone_change(merge_request) handle_milestone_change(merge_request)
handle_draft_status_change(merge_request, changed_fields)
track_title_and_desc_edits(changed_fields)
added_labels = merge_request.labels - old_labels added_labels = merge_request.labels - old_labels
if added_labels.present? if added_labels.present?
...@@ -80,7 +78,17 @@ module MergeRequests ...@@ -80,7 +78,17 @@ module MergeRequests
current_user current_user
) )
end end
# 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')
merge_request.mark_as_unchecked
end end
end
# rubocop:enable Metrics/AbcSize
def handle_task_changes(merge_request) def handle_task_changes(merge_request)
todo_service.resolve_todos_for_target(merge_request, current_user) todo_service.resolve_todos_for_target(merge_request, current_user)
...@@ -95,29 +103,31 @@ module MergeRequests ...@@ -95,29 +103,31 @@ module MergeRequests
MergeRequests::CloseService MergeRequests::CloseService
end end
def before_update(issuable, skip_spam_check: false)
return unless issuable.changed?
@issuable_changes = issuable.changes
end
def after_update(issuable) def after_update(issuable)
issuable.cache_merge_request_closes_issues!(current_user) issuable.cache_merge_request_closes_issues!(current_user)
end
return unless @issuable_changes private
%w(title description).each do |action| attr_reader :target_branch_was_deleted
next unless @issuable_changes.key?(action)
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)
# Track edits to title or description
#
merge_request_activity_counter merge_request_activity_counter
.public_send("track_#{action}_edit_action".to_sym, user: current_user) # rubocop:disable GitlabSecurity/PublicSend .public_send("track_#{action}_edit_action".to_sym, user: current_user) # rubocop:disable GitlabSecurity/PublicSend
end
end
# Track changes to Draft/WIP status def handle_draft_status_change(merge_request, changed_fields)
# return unless changed_fields.include?("title")
if action == "title"
old_title, new_title = @issuable_changes["title"] old_title, new_title = merge_request.previous_changes["title"]
old_title_wip = MergeRequest.work_in_progress?(old_title) old_title_wip = MergeRequest.work_in_progress?(old_title)
new_title_wip = MergeRequest.work_in_progress?(new_title) new_title_wip = MergeRequest.work_in_progress?(new_title)
...@@ -133,12 +143,6 @@ module MergeRequests ...@@ -133,12 +143,6 @@ module MergeRequests
.track_unmarked_as_draft_action(user: current_user) .track_unmarked_as_draft_action(user: current_user)
end end
end end
end
end
private
attr_reader :target_branch_was_deleted
def handle_milestone_change(merge_request) def handle_milestone_change(merge_request)
return if skip_milestone_email return if skip_milestone_email
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment