summaryrefslogtreecommitdiff
path: root/app/services/milestones/transfer_service.rb
blob: 18d7e41adc72df8b3fce5138ec6a4bf93c90aec2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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)

          update_issues_milestone(milestone, new_milestone)
          update_merge_requests_milestone(milestone.id, new_milestone&.id)
        end
      end
    end

    private

    # rubocop: disable CodeReuse/ActiveRecord
    def milestones_to_transfer
      Milestone.from_union([
          group_milestones_applied_to_issues,
          group_milestones_applied_to_merge_requests
        ])
        .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 },
          group_id: old_group.self_and_ancestors
        )
    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 },
          group_id: old_group.self_and_ancestors
        )
    end
    # rubocop: enable CodeReuse/ActiveRecord

    def find_or_create_milestone(milestone)
      params = milestone.attributes.slice('title', 'description', 'start_date', 'due_date')

      FindOrCreateService.new(project, current_user, params).execute
    end

    # rubocop: disable CodeReuse/ActiveRecord
    def update_issues_milestone(old_milestone, new_milestone)
      Issue.where(project: project, milestone_id: old_milestone.id)
        .update_all(milestone_id: new_milestone&.id)

      delete_milestone_issues_caches(old_milestone)
      delete_milestone_issues_caches(new_milestone)
    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

    def delete_milestone_issues_caches(milestone)
      return unless milestone

      Milestones::IssuesCountService.new(milestone).delete_cache
      Milestones::ClosedIssuesCountService.new(milestone).delete_cache
    end
  end
end