Commit 9c8bca3e authored by Michael Kozono's avatar Michael Kozono

Merge branch 'ali/use-gitlab-standard-schema' into 'master'

Send gitlab_standard context with issue promotion event

See merge request gitlab-org/gitlab!49873
parents 4584ed8a 3ce22f05
......@@ -32,9 +32,8 @@ module Epics
end
def track_event
::Gitlab::Tracking.event(
'epics', 'promote', property: 'issue_id', value: original_entity.id
)
::Gitlab::Tracking.event('epics', 'promote', property: 'issue_id', value: original_entity.id,
standard_context: ::Gitlab::Tracking::StandardContext.new(namespace: @parent_group, project: issue.project))
end
def create_new_entity
......
......@@ -64,7 +64,8 @@ RSpec.describe Epics::IssuePromoteService, :aggregate_failures do
before do
subject.execute(issue)
expect_snowplow_event(category: 'epics', action: 'promote', property: 'issue_id', value: issue.id)
expect_snowplow_event(category: 'epics', action: 'promote', property: 'issue_id', value: issue.id,
standard_context: kind_of(Gitlab::Tracking::StandardContext))
end
it 'creates a new epic with correct attributes' do
......@@ -199,7 +200,8 @@ RSpec.describe Epics::IssuePromoteService, :aggregate_failures do
expect(epic.notes.count).to eq(issue.notes.count)
expect(epic.notes.where(discussion_id: discussion.discussion_id).count).to eq(0)
expect(issue.notes.where(discussion_id: discussion.discussion_id).count).to eq(1)
expect_snowplow_event(category: 'epics', action: 'promote', property: 'issue_id', value: issue.id)
expect_snowplow_event(category: 'epics', action: 'promote', property: 'issue_id', value: issue.id,
standard_context: kind_of(Gitlab::Tracking::StandardContext))
end
it 'copies note attachments' do
......@@ -208,7 +210,8 @@ RSpec.describe Epics::IssuePromoteService, :aggregate_failures do
epic = subject.execute(issue)
expect(epic.notes.user.first.attachment).to be_kind_of(AttachmentUploader)
expect_snowplow_event(category: 'epics', action: 'promote', property: 'issue_id', value: issue.id)
expect_snowplow_event(category: 'epics', action: 'promote', property: 'issue_id', value: issue.id,
standard_context: kind_of(Gitlab::Tracking::StandardContext))
end
end
......
......@@ -24,7 +24,9 @@ module Gitlab
Gitlab::CurrentSettings.snowplow_enabled?
end
def event(category, action, label: nil, property: nil, value: nil, context: nil)
def event(category, action, label: nil, property: nil, value: nil, context: [], standard_context: nil)
context.push(standard_context.to_context) if standard_context
snowplow.event(category, action, label: label, property: property, value: value, context: context)
product_analytics.event(category, action, label: label, property: property, value: value, context: context)
end
......
# frozen_string_literal: true
module Gitlab
module Tracking
class StandardContext
GITLAB_STANDARD_SCHEMA_URL = 'iglu:com.gitlab/gitlab_standard/jsonschema/1-0-1'.freeze
def initialize(namespace: nil, project: nil, **data)
@namespace = namespace
@project = project
@data = data
end
def namespace_id
namespace&.id
end
def project_id
@project&.id
end
def to_context
SnowplowTracker::SelfDescribingJson.new(GITLAB_STANDARD_SCHEMA_URL, to_h)
end
private
def namespace
@namespace || @project&.namespace
end
def to_h
public_methods(false).each_with_object({}) do |method, hash|
next if method == :to_context
hash[method] = public_send(method) # rubocop:disable GitlabSecurity/PublicSend
end.merge(@data)
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Tracking::StandardContext do
let_it_be(:project) { create(:project) }
let_it_be(:namespace) { create(:namespace) }
let(:snowplow_context) { subject.to_context }
describe '#to_context' do
context 'with no arguments' do
it 'creates a Snowplow context with no data' do
snowplow_context.to_json[:data].each do |_, v|
expect(v).to be_nil
end
end
end
context 'with extra data' do
subject { described_class.new(foo: 'bar') }
it 'creates a Snowplow context with the given data' do
expect(snowplow_context.to_json.dig(:data, :foo)).to eq('bar')
end
end
context 'with namespace' do
subject { described_class.new(namespace: namespace) }
it 'creates a Snowplow context using the given data' do
expect(snowplow_context.to_json.dig(:data, :namespace_id)).to eq(namespace.id)
expect(snowplow_context.to_json.dig(:data, :project_id)).to be_nil
end
end
context 'with project' do
subject { described_class.new(project: project) }
it 'creates a Snowplow context using the given data' do
expect(snowplow_context.to_json.dig(:data, :namespace_id)).to eq(project.namespace.id)
expect(snowplow_context.to_json.dig(:data, :project_id)).to eq(project.id)
end
end
context 'with project and namespace' do
subject { described_class.new(namespace: namespace, project: project) }
it 'creates a Snowplow context using the given data' do
expect(snowplow_context.to_json.dig(:data, :namespace_id)).to eq(namespace.id)
expect(snowplow_context.to_json.dig(:data, :project_id)).to eq(project.id)
end
end
end
end
......@@ -41,21 +41,42 @@ RSpec.describe Gitlab::Tracking do
allow_any_instance_of(Gitlab::Tracking::Destinations::ProductAnalytics).to receive(:event)
end
it 'delegates to snowplow destination' do
expect_any_instance_of(Gitlab::Tracking::Destinations::Snowplow)
.to receive(:event)
.with('category', 'action', label: 'label', property: 'property', value: 1.5, context: nil)
shared_examples 'delegates to destination' do |klass|
context 'with standard context' do
it "delegates to #{klass} destination" do
expect_any_instance_of(klass).to receive(:event) do |_, category, action, args|
expect(category).to eq('category')
expect(action).to eq('action')
expect(args[:label]).to eq('label')
expect(args[:property]).to eq('property')
expect(args[:value]).to eq(1.5)
expect(args[:context].length).to eq(1)
expect(args[:context].first.to_json[:schema]).to eq(Gitlab::Tracking::StandardContext::GITLAB_STANDARD_SCHEMA_URL)
expect(args[:context].first.to_json[:data]).to include(foo: 'bar')
end
described_class.event('category', 'action', label: 'label', property: 'property', value: 1.5)
end
described_class.event('category', 'action', label: 'label', property: 'property', value: 1.5,
standard_context: Gitlab::Tracking::StandardContext.new(foo: 'bar'))
end
end
it 'delegates to ProductAnalytics destination' do
expect_any_instance_of(Gitlab::Tracking::Destinations::ProductAnalytics)
.to receive(:event)
.with('category', 'action', label: 'label', property: 'property', value: 1.5, context: nil)
context 'without standard context' do
it "delegates to #{klass} destination" do
expect_any_instance_of(klass).to receive(:event) do |_, category, action, args|
expect(category).to eq('category')
expect(action).to eq('action')
expect(args[:label]).to eq('label')
expect(args[:property]).to eq('property')
expect(args[:value]).to eq(1.5)
end
described_class.event('category', 'action', label: 'label', property: 'property', value: 1.5)
described_class.event('category', 'action', label: 'label', property: 'property', value: 1.5)
end
end
end
include_examples 'delegates to destination', Gitlab::Tracking::Destinations::Snowplow
include_examples 'delegates to destination', Gitlab::Tracking::Destinations::ProductAnalytics
end
describe '.self_describing_event' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment