Commit d1aef052 authored by Nikola Milojevic's avatar Nikola Milojevic

Merge branch '356620-be-dispatch-snowplow-events-from-their-event-definitions' into 'master'

BE - Dispatch Snowplow events from their event definitions

See merge request gitlab-org/gitlab!83480
parents fa48d107 3fc1d220
# frozen_string_literal: true
module EE
module Gitlab
module Tracking
extend ActiveSupport::Concern
class_methods do
extend ::Gitlab::Utils::Override
override :definition
def definition(basename, category: nil, action: nil, label: nil, property: nil, value: nil, context: [], project: nil, user: nil, namespace: nil, **extra) # rubocop:disable Metrics/ParameterLists
ee_prefix = 'ee_'
events_path = 'config/events'
if basename.starts_with?(ee_prefix)
events_path = 'ee/config/events'
basename.slice! ee_prefix
end
definition = YAML.load_file(Rails.root.join(*events_path, "#{basename}.yml"))
self.dispatch_from_definition(definition, label: label, property: property, value: value, context: context,
project: project, user: user, namespace: namespace, **extra)
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ::Gitlab::Tracking do
describe '.definition' do
let_it_be(:test_definition) {{ 'category': 'category', 'action': 'action' }}
let_it_be(:filepath) { Rails.root.join('ee/config/events/filename.yml') }
before do
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:event)
end
allow_next_instance_of(Gitlab::Tracking::Destinations::Snowplow) do |instance|
allow(instance).to receive(:event)
end
allow(YAML).to receive(:load_file).with(filepath).and_return(test_definition)
end
it 'fetch EE definitions when prefixed with ee_' do
expect(YAML).to receive(:load_file).with(filepath)
described_class.definition(+'ee_filename')
end
end
end
......@@ -15,6 +15,21 @@ module Gitlab
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(error, snowplow_category: category, snowplow_action: action)
end
def definition(basename, category: nil, action: nil, label: nil, property: nil, value: nil, context: [], project: nil, user: nil, namespace: nil, **extra) # rubocop:disable Metrics/ParameterLists
definition = YAML.load_file(Rails.root.join("config/events/#{basename}.yml"))
dispatch_from_definition(definition, label: label, property: property, value: value, context: context, project: project, user: user, namespace: namespace, **extra)
end
def dispatch_from_definition(definition, **event_data)
definition = definition.with_indifferent_access
category ||= definition[:category]
action ||= definition[:action]
event(category, action, **event_data)
end
def options(group)
snowplow.options(group)
end
......@@ -39,3 +54,5 @@ module Gitlab
end
end
end
Gitlab::Tracking.prepend_mod_with('Gitlab::Tracking')
......@@ -149,4 +149,42 @@ RSpec.describe Gitlab::Tracking do
described_class.event(nil, 'some_action')
end
end
describe '.definition' do
let(:namespace) { create(:namespace) }
let_it_be(:definition_action) { 'definition_action' }
let_it_be(:definition_category) { 'definition_category' }
let_it_be(:label_description) { 'definition label description' }
let_it_be(:test_definition) {{ 'category': definition_category, 'action': definition_action }}
before do
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:event)
end
allow_next_instance_of(Gitlab::Tracking::Destinations::Snowplow) do |instance|
allow(instance).to receive(:event)
end
allow(YAML).to receive(:load_file).with(Rails.root.join('config/events/filename.yml')).and_return(test_definition)
end
it 'dispatchs the data to .event' do
project = build_stubbed(:project)
user = build_stubbed(:user)
expect(described_class).to receive(:event) do |category, action, args|
expect(category).to eq(definition_category)
expect(action).to eq(definition_action)
expect(args[:label]).to eq('label')
expect(args[:property]).to eq('...')
expect(args[:project]).to eq(project)
expect(args[:user]).to eq(user)
expect(args[:namespace]).to eq(namespace)
expect(args[:extra_key_1]).to eq('extra value 1')
end
described_class.definition('filename', category: nil, action: nil, label: 'label', property: '...',
project: project, user: user, namespace: namespace, extra_key_1: 'extra value 1')
end
end
end
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