summaryrefslogtreecommitdiff
path: root/app/services/resource_events/change_state_service.rb
blob: c5120ba82e1aaf52354844fe00f41d9c64009231 (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
# frozen_string_literal: true

module ResourceEvents
  class ChangeStateService
    attr_reader :resource, :user

    def initialize(user:, resource:)
      @user, @resource = user, resource
    end

    def execute(params)
      @params = params

      ResourceStateEvent.create(
        user: user,
        resource.class.underscore => resource,
        source_commit: commit_id_of(mentionable_source),
        source_merge_request_id: merge_request_id_of(mentionable_source),
        state: ResourceStateEvent.states[state],
        close_after_error_tracking_resolve: close_after_error_tracking_resolve,
        close_auto_resolve_prometheus_alert: close_auto_resolve_prometheus_alert,
        created_at: resource.system_note_timestamp
      )

      resource.expire_note_etag_cache
    end

    private

    attr_reader :params

    def close_auto_resolve_prometheus_alert
      params[:close_auto_resolve_prometheus_alert] || false
    end

    def close_after_error_tracking_resolve
      params[:close_after_error_tracking_resolve] || false
    end

    def state
      params[:status]
    end

    def mentionable_source
      params[:mentionable_source]
    end

    def commit_id_of(mentionable_source)
      return unless mentionable_source.is_a?(Commit)

      mentionable_source.id[0...40]
    end

    def merge_request_id_of(mentionable_source)
      return unless mentionable_source.is_a?(MergeRequest)

      mentionable_source.id
    end
  end
end