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

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

module AuditEvents
  class BuildService
    # Handle missing attributes
    MissingAttributeError = Class.new(StandardError)

    # @raise [MissingAttributeError] when required attributes are blank
    #
    # @return [BuildService]
    def initialize(
      author:, scope:, target:, message:,
      created_at: DateTime.current, additional_details: {}, ip_address: nil, target_details: nil)
      raise MissingAttributeError, "author" if author.blank?
      raise MissingAttributeError, "scope" if scope.blank?
      raise MissingAttributeError, "target" if target.blank?
      raise MissingAttributeError, "message" if message.blank?

      @author = build_author(author)
      @scope = scope
      @target = build_target(target)
      @ip_address = ip_address || build_ip_address
      @message = build_message(message)
      @created_at = created_at
      @additional_details = additional_details
      @target_details = target_details
    end

    # Create an instance of AuditEvent
    #
    # @return [AuditEvent]
    def execute
      AuditEvent.new(payload)
    end

    private

    def payload
      base_payload.merge(details: base_details_payload)
    end

    def base_payload
      {
        author_id: @author.id,
        author_name: @author.name,
        entity_id: @scope.id,
        entity_type: @scope.class.name,
        created_at: @created_at
      }
    end

    def base_details_payload
      @additional_details.merge({
                                  author_name: @author.name,
                                  author_class: @author.class.name,
                                  target_id: @target.id,
                                  target_type: @target.type,
                                  target_details: @target_details || @target.details,
                                  custom_message: @message
                                })
    end

    def build_author(author)
      author.id = -2 if author.instance_of? DeployToken
      author.id = -3 if author.instance_of? DeployKey

      author
    end

    def build_target(target)
      return target if target.is_a? ::Gitlab::Audit::NullTarget

      ::Gitlab::Audit::Target.new(target)
    end

    def build_message(message)
      message
    end

    def build_ip_address
      Gitlab::RequestContext.instance.client_ip || @author.current_sign_in_ip
    end
  end
end

AuditEvents::BuildService.prepend_mod_with('AuditEvents::BuildService')