debian-mirror-gitlab/lib/gitlab/tracking/standard_context.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

69 lines
1.9 KiB
Ruby
Raw Normal View History

2021-03-08 18:12:59 +05:30
# frozen_string_literal: true
module Gitlab
module Tracking
class StandardContext
2022-03-02 08:16:31 +05:30
GITLAB_STANDARD_SCHEMA_URL = 'iglu:com.gitlab/gitlab_standard/jsonschema/1-0-8'
2021-04-29 21:17:54 +05:30
GITLAB_RAILS_SOURCE = 'gitlab-rails'
2021-03-08 18:12:59 +05:30
2023-07-09 08:55:56 +05:30
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
2021-04-29 21:17:54 +05:30
@extra = extra
2021-03-08 18:12:59 +05:30
end
2021-03-11 19:13:27 +05:30
def to_context
SnowplowTracker::SelfDescribingJson.new(GITLAB_STANDARD_SCHEMA_URL, to_h)
2021-03-08 18:12:59 +05:30
end
2021-03-11 19:13:27 +05:30
def environment
return 'staging' if Gitlab.staging?
2021-04-17 20:07:23 +05:30
return 'production' if Gitlab.com?
return 'org' if Gitlab.org?
return 'self-managed' if Rails.env.production?
2021-03-11 19:13:27 +05:30
'development'
2021-03-08 18:12:59 +05:30
end
2021-03-11 19:13:27 +05:30
def source
GITLAB_RAILS_SOURCE
2021-03-08 18:12:59 +05:30
end
private
2023-07-09 08:55:56 +05:30
attr_accessor :namespace_id, :project_id, :extra, :plan_name, :user_id
2021-11-11 11:23:49 +05:30
2021-03-08 18:12:59 +05:30
def to_h
2021-03-11 19:13:27 +05:30
{
environment: environment,
2021-04-29 21:17:54 +05:30
source: source,
2023-07-09 08:55:56 +05:30
plan: plan_name,
2021-12-11 22:18:48 +05:30
extra: extra,
2023-07-09 08:55:56 +05:30
user_id: user_id,
namespace_id: namespace_id,
2022-03-02 08:16:31 +05:30
project_id: project_id,
context_generated_at: Time.current
2021-04-29 21:17:54 +05:30
}
2021-03-08 18:12:59 +05:30
end
2021-11-11 11:23:49 +05:30
2022-07-23 23:45:48 +05:30
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
2021-03-08 18:12:59 +05:30
end
end
end