2021-01-14 22:10:30 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Tracking
|
|
|
|
class StandardContext
|
2021-12-22 10:14:10 -05:00
|
|
|
GITLAB_STANDARD_SCHEMA_URL = 'iglu:com.gitlab/gitlab_standard/jsonschema/1-0-7'
|
2021-03-23 11:09:28 -04:00
|
|
|
GITLAB_RAILS_SOURCE = 'gitlab-rails'
|
2021-01-14 22:10:30 -05:00
|
|
|
|
2021-04-01 08:08:56 -04:00
|
|
|
def initialize(namespace: nil, project: nil, user: nil, **extra)
|
2021-04-26 17:10:25 -04:00
|
|
|
@namespace = namespace
|
2021-08-20 11:10:24 -04:00
|
|
|
@plan = namespace&.actual_plan_name
|
|
|
|
@project = project
|
2021-10-06 11:11:48 -04:00
|
|
|
@user = user
|
2021-04-01 08:08:56 -04:00
|
|
|
@extra = extra
|
2021-01-14 22:10:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_context
|
|
|
|
SnowplowTracker::SelfDescribingJson.new(GITLAB_STANDARD_SCHEMA_URL, to_h)
|
|
|
|
end
|
|
|
|
|
2021-01-25 10:09:00 -05:00
|
|
|
def environment
|
|
|
|
return 'staging' if Gitlab.staging?
|
|
|
|
|
2021-02-19 19:10:55 -05:00
|
|
|
return 'production' if Gitlab.com?
|
|
|
|
|
|
|
|
return 'org' if Gitlab.org?
|
|
|
|
|
|
|
|
return 'self-managed' if Rails.env.production?
|
|
|
|
|
2021-01-25 10:09:00 -05:00
|
|
|
'development'
|
|
|
|
end
|
|
|
|
|
2021-01-28 16:09:04 -05:00
|
|
|
def source
|
|
|
|
GITLAB_RAILS_SOURCE
|
|
|
|
end
|
|
|
|
|
2021-01-14 22:10:30 -05:00
|
|
|
private
|
|
|
|
|
2021-10-06 11:11:48 -04:00
|
|
|
attr_accessor :namespace, :project, :extra, :plan, :user
|
2021-08-20 11:10:24 -04:00
|
|
|
|
2021-01-14 22:10:30 -05:00
|
|
|
def to_h
|
2021-02-11 16:09:00 -05:00
|
|
|
{
|
|
|
|
environment: environment,
|
2021-04-01 08:08:56 -04:00
|
|
|
source: source,
|
2021-08-20 11:10:24 -04:00
|
|
|
plan: plan,
|
2021-11-05 05:09:47 -04:00
|
|
|
extra: extra,
|
|
|
|
user_id: user&.id,
|
2021-08-20 11:10:24 -04:00
|
|
|
namespace_id: namespace&.id,
|
2021-12-22 10:14:10 -05:00
|
|
|
project_id: project_id
|
2021-04-01 08:08:56 -04:00
|
|
|
}
|
2021-01-14 22:10:30 -05:00
|
|
|
end
|
2021-08-20 11:10:24 -04:00
|
|
|
|
|
|
|
def project_id
|
|
|
|
project.is_a?(Integer) ? project : project&.id
|
|
|
|
end
|
2021-01-14 22:10:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|