debian-mirror-gitlab/app/services/milestones/transfer_service.rb

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

93 lines
2.9 KiB
Ruby
Raw Permalink Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
# Milestones::TransferService class
#
# Used for recreating the missing group milestones at project level when
# transferring a project to a new namespace
#
module Milestones
class TransferService
attr_reader :current_user, :old_group, :project
def initialize(current_user, old_group, project)
@current_user = current_user
@old_group = old_group
@project = project
end
def execute
return unless old_group.present?
Milestone.transaction do
milestones_to_transfer.find_each do |milestone|
new_milestone = find_or_create_milestone(milestone)
2020-04-08 14:13:33 +05:30
update_issues_milestone(milestone, new_milestone)
2019-12-04 20:38:33 +05:30
update_merge_requests_milestone(milestone.id, new_milestone&.id)
2021-04-29 21:17:54 +05:30
delete_milestone_counts_caches(milestone)
delete_milestone_counts_caches(new_milestone)
2019-12-04 20:38:33 +05:30
end
end
end
private
# rubocop: disable CodeReuse/ActiveRecord
def milestones_to_transfer
2022-10-11 01:57:18 +05:30
Milestone.from_union([group_milestones_applied_to_issues, group_milestones_applied_to_merge_requests])
2019-12-04 20:38:33 +05:30
.reorder(nil)
.distinct
end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def group_milestones_applied_to_issues
Milestone.joins(:issues)
.where(
issues: { project_id: project.id },
2020-04-22 19:07:51 +05:30
group_id: old_group.self_and_ancestors
2019-12-04 20:38:33 +05:30
)
end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def group_milestones_applied_to_merge_requests
Milestone.joins(:merge_requests)
.where(
merge_requests: { target_project_id: project.id },
2020-04-22 19:07:51 +05:30
group_id: old_group.self_and_ancestors
2019-12-04 20:38:33 +05:30
)
end
# rubocop: enable CodeReuse/ActiveRecord
def find_or_create_milestone(milestone)
2023-01-13 00:05:48 +05:30
params = milestone.attributes.slice('title', 'description', 'start_date', 'due_date', 'state')
2019-12-04 20:38:33 +05:30
FindOrCreateService.new(project, current_user, params).execute
end
# rubocop: disable CodeReuse/ActiveRecord
2020-04-08 14:13:33 +05:30
def update_issues_milestone(old_milestone, new_milestone)
Issue.where(project: project, milestone_id: old_milestone.id)
.update_all(milestone_id: new_milestone&.id)
2019-12-04 20:38:33 +05:30
end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def update_merge_requests_milestone(old_milestone_id, new_milestone_id)
MergeRequest.where(project: project, milestone_id: old_milestone_id)
.update_all(milestone_id: new_milestone_id)
end
# rubocop: enable CodeReuse/ActiveRecord
2020-04-08 14:13:33 +05:30
2021-04-29 21:17:54 +05:30
def delete_milestone_counts_caches(milestone)
2020-04-08 14:13:33 +05:30
return unless milestone
Milestones::IssuesCountService.new(milestone).delete_cache
Milestones::ClosedIssuesCountService.new(milestone).delete_cache
2021-04-29 21:17:54 +05:30
Milestones::MergeRequestsCountService.new(milestone).delete_cache
2020-04-08 14:13:33 +05:30
end
2019-12-04 20:38:33 +05:30
end
end