summaryrefslogtreecommitdiff
path: root/app/services/milestones/promote_service.rb
blob: 39071b5dc145b03f54a86ae50efe6af1f4c8b639 (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
95
# frozen_string_literal: true

module Milestones
  class PromoteService < Milestones::BaseService
    PromoteMilestoneError = Class.new(StandardError)

    def execute(milestone)
      check_project_milestone!(milestone)

      Milestone.transaction do
        group_milestone = clone_project_milestone(milestone)

        move_children_to_group_milestone(group_milestone)

        # Destroy all milestones with same title across projects
        destroy_old_milestones(milestone)

        # Rollback if milestone is not valid
        unless group_milestone.valid?
          raise_error(group_milestone.errors.full_messages.to_sentence)
        end

        group_milestone
      end
    end

    private

    # rubocop: disable CodeReuse/ActiveRecord
    def milestone_ids_for_merge(group_milestone)
      # Pluck need to be used here instead of select so the array of ids
      # is persistent after old milestones gets deleted.
      @milestone_ids_for_merge ||= begin
        search_params = { title: group_milestone.title, project_ids: group_project_ids, state: 'all' }
        milestones = MilestonesFinder.new(search_params).execute
        milestones.pluck(:id)
      end
    end
    # rubocop: enable CodeReuse/ActiveRecord

    def move_children_to_group_milestone(group_milestone)
      milestone_ids_for_merge(group_milestone).in_groups_of(100, false) do |milestone_ids|
        update_children(group_milestone, milestone_ids)
      end
    end

    def check_project_milestone!(milestone)
      raise_error('Only project milestones can be promoted.') unless milestone.project_milestone?
    end

    def clone_project_milestone(milestone)
      params = milestone.slice(:title, :description, :start_date, :due_date, :state_event)

      create_service = CreateService.new(group, current_user, params)

      milestone = create_service.execute

      # milestone won't be valid here because of duplicated title
      milestone.save(validate: false)

      milestone
    end

    # rubocop: disable CodeReuse/ActiveRecord
    def update_children(group_milestone, milestone_ids)
      issues = Issue.where(project_id: group_project_ids, milestone_id: milestone_ids)
      merge_requests = MergeRequest.where(source_project_id: group_project_ids, milestone_id: milestone_ids)

      [issues, merge_requests].each do |issuable_collection|
        issuable_collection.update_all(milestone_id: group_milestone.id)
      end
    end
    # rubocop: enable CodeReuse/ActiveRecord

    def group
      @group ||= parent.group || raise_error('Project does not belong to a group.')
    end

    # rubocop: disable CodeReuse/ActiveRecord
    def destroy_old_milestones(milestone)
      Milestone.where(id: milestone_ids_for_merge(milestone)).destroy_all # rubocop: disable DestroyAll
    end
    # rubocop: enable CodeReuse/ActiveRecord

    # rubocop: disable CodeReuse/ActiveRecord
    def group_project_ids
      @group_project_ids ||= group.projects.pluck(:id)
    end
    # rubocop: enable CodeReuse/ActiveRecord

    def raise_error(message)
      raise PromoteMilestoneError, "Promotion failed - #{message}"
    end
  end
end