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
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def initialize(namespace: nil, project: nil, user: nil, **extra)
|
2021-06-08 01:23:25 +05:30
|
|
|
@namespace = namespace
|
2021-11-11 11:23:49 +05:30
|
|
|
@plan = namespace&.actual_plan_name
|
|
|
|
@project = project
|
2021-11-18 22:05:49 +05:30
|
|
|
@user = user
|
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
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
attr_accessor :namespace, :project, :extra, :plan, :user
|
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,
|
2021-11-11 11:23:49 +05:30
|
|
|
plan: plan,
|
2021-12-11 22:18:48 +05:30
|
|
|
extra: extra,
|
|
|
|
user_id: user&.id,
|
2021-11-11 11:23:49 +05:30
|
|
|
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
|
|
|
|
|
|
|
def project_id
|
|
|
|
project.is_a?(Integer) ? project : project&.id
|
|
|
|
end
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|