Welcome to mirror list, hosted at ThFree Co, Russian Federation.

change_milestone_service.rb « resource_events « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bad7d002c65c05b01d0e25c7bf90b9222fa0774c (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
# frozen_string_literal: true

module ResourceEvents
  class ChangeMilestoneService
    attr_reader :resource, :user, :event_created_at, :resource_args

    def initialize(resource:, user:, created_at: Time.now)
      @resource = resource
      @user = user
      @event_created_at = created_at

      @resource_args = {
        user_id: user.id,
        created_at: event_created_at
      }
    end

    def execute
      args = build_resource_args

      action = if milestone.nil?
                 :remove
               else
                 :add
               end

      record = args.merge(milestone_id: milestone&.id, action: ResourceMilestoneEvent.actions[action])

      create_event(record)
    end

    private

    def milestone
      resource&.milestone
    end

    def create_event(record)
      ResourceMilestoneEvent.create(record)

      resource.expire_note_etag_cache
    end

    def build_resource_args
      key = resource.class.name.underscore.foreign_key

      resource_args.merge(key => resource.id, state: ResourceMilestoneEvent.states[resource.state])
    end
  end
end