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

standard_context.rb « tracking « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 62c45368410b09ab56519465df45368844aa8d8e (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
# frozen_string_literal: true

module Gitlab
  module Tracking
    class StandardContext
      GITLAB_STANDARD_SCHEMA_URL = 'iglu:com.gitlab/gitlab_standard/jsonschema/1-0-8'
      GITLAB_RAILS_SOURCE = 'gitlab-rails'

      def initialize(namespace_id: nil, plan_name: nil, project_id: nil, user_id: nil, **extra)
        check_argument_type(:namespace_id, namespace_id, [Integer])
        check_argument_type(:plan_name, plan_name, [String])
        check_argument_type(:project_id, project_id, [Integer])
        check_argument_type(:user_id, user_id, [Integer])

        @namespace_id = namespace_id
        @plan_name = plan_name
        @project_id = project_id
        @user_id = user_id
        @extra = extra
      end

      def to_context
        SnowplowTracker::SelfDescribingJson.new(GITLAB_STANDARD_SCHEMA_URL, to_h)
      end

      def environment
        return 'staging' if Gitlab.staging?

        return 'production' if Gitlab.com?

        return 'org' if Gitlab.org?

        return 'self-managed' if Rails.env.production?

        'development'
      end

      def source
        GITLAB_RAILS_SOURCE
      end

      private

      attr_accessor :namespace_id, :project_id, :extra, :plan_name, :user_id

      def to_h
        {
          environment: environment,
          source: source,
          plan: plan_name,
          extra: extra,
          user_id: user_id,
          namespace_id: namespace_id,
          project_id: project_id,
          context_generated_at: Time.current
        }
      end

      def check_argument_type(argument_name, argument_value, allowed_classes)
        return if argument_value.nil? || allowed_classes.any? { |allowed_class| argument_value.is_a?(allowed_class) }

        exception = "Invalid argument type passed for #{argument_name}." \
          " Should be one of #{allowed_classes.map(&:to_s)}"
        Gitlab::ErrorTracking.track_and_raise_for_dev_exception(ArgumentError.new(exception))
      end
    end
  end
end